Search
lxdream.org :: lxdream/src/pvr2/rendbkg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendbkg.c
changeset 239:e5cd6b2d4586
prev223:f6c28fa9076b
next331:a6048d3a9a79
author nkeynes
date Wed Jan 17 09:21:09 2007 +0000 (17 years ago)
permissions -rw-r--r--
last change Change tex mode 1 to MODULATE
file annotate diff log raw
nkeynes@219
     1
/**
nkeynes@239
     2
 * $Id: rendbkg.c,v 1.4 2006-12-15 10:17:30 nkeynes Exp $
nkeynes@219
     3
 *
nkeynes@219
     4
 * PVR2 background renderer. 
nkeynes@219
     5
 *
nkeynes@219
     6
 * Yes, it uses the same basic data structure. Yes, it needs to be handled
nkeynes@219
     7
 * completely differently.
nkeynes@219
     8
 *
nkeynes@239
     9
 * PVR2 backgrounds are defined as a set of three fully specified vertexes,
nkeynes@239
    10
 * stored in compiled-vertex format. The vertexes form a triangle which is
nkeynes@239
    11
 * rendered in the normal fashion. Points outside the triangle are rendered
nkeynes@239
    12
 * by extrapolating from the gradients established by the triangle, giving
nkeynes@239
    13
 * an overall smooth gradient across the background. Points are colour-clamped
nkeynes@239
    14
 * prior to output to the buffer.
nkeynes@239
    15
 *
nkeynes@239
    16
 * As a special case, if all three points lie on the same line (or are the same
nkeynes@239
    17
 * point, the third point is used by itself to define the entire buffer (ie
nkeynes@239
    18
 * effectively a solid colour).
nkeynes@239
    19
 *
nkeynes@221
    20
 * Note: this would be really simple if GL did unclamped colour interpolation
nkeynes@221
    21
 * but it doesn't (portably), which makes this roughly 2 orders of magnitude
nkeynes@221
    22
 * more complicated than it otherwise would be.
nkeynes@221
    23
 *
nkeynes@219
    24
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@219
    25
 *
nkeynes@219
    26
 * This program is free software; you can redistribute it and/or modify
nkeynes@219
    27
 * it under the terms of the GNU General Public License as published by
nkeynes@219
    28
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@219
    29
 * (at your option) any later version.
nkeynes@219
    30
 *
nkeynes@219
    31
 * This program is distributed in the hope that it will be useful,
nkeynes@219
    32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@219
    33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@219
    34
 * GNU General Public License for more details.
nkeynes@219
    35
 */
nkeynes@219
    36
nkeynes@219
    37
#include <sys/time.h>
nkeynes@219
    38
#include "pvr2/pvr2.h"
nkeynes@221
    39
#include <GL/glext.h>
nkeynes@221
    40
#include <math.h>
nkeynes@219
    41
nkeynes@221
    42
#define MAX_CLAMP_LINES 8
nkeynes@221
    43
#define MAX_VERTEXES 256
nkeynes@221
    44
#define MAX_REGIONS  256
nkeynes@219
    45
nkeynes@221
    46
/**
nkeynes@221
    47
 * Structure to hold an unpacked vertex
nkeynes@221
    48
 */
nkeynes@219
    49
struct vertex_all {
nkeynes@219
    50
    float x,y,z;
nkeynes@219
    51
    float u,v;
nkeynes@219
    52
    float rgba[4];      /* Note - RGBA order, as preferred by GL */
nkeynes@219
    53
    float spec_rgba[4];
nkeynes@219
    54
};
nkeynes@219
    55
nkeynes@219
    56
#define FARGB_A(x) (((float)(((x)>>24)+1))/256.0)
nkeynes@219
    57
#define FARGB_R(x) (((float)((((x)>>16)&0xFF)+1))/256.0)
nkeynes@219
    58
#define FARGB_G(x) (((float)((((x)>>8)&0xFF)+1))/256.0)
nkeynes@219
    59
#define FARGB_B(x) (((float)(((x)&0xFF)+1))/256.0)
nkeynes@219
    60
nkeynes@219
    61
/**
nkeynes@221
    62
 * Compute the line where k = target_k, (where k is normally one of
nkeynes@221
    63
 * r,g,b,a, or z) and determines the points at which the line intersects
nkeynes@221
    64
 * the viewport (0,0,width,height).
nkeynes@221
    65
 *
nkeynes@221
    66
 * @param center_x the x value for the center position
nkeynes@221
    67
 * @param center_y the y value for the center position
nkeynes@221
    68
 * @param center_k the k value for the center position
nkeynes@221
    69
 * @param width Width of the viewport (ie 640)
nkeynes@221
    70
 * @param height Height of the viewport (ie 480)
nkeynes@221
    71
 * @param target_k determine the line where k = this value, ie 1.0
nkeynes@221
    72
 * @param detxy
nkeynes@221
    73
 * @param target Array to write the resultant x,y pairs to (note this
nkeynes@221
    74
 * function only sets x and y values).
nkeynes@221
    75
 * @return number of vertexes written to the target.
nkeynes@221
    76
 */
nkeynes@221
    77
static int compute_colour_line( float center_x, float center_y, float center_k, 
nkeynes@221
    78
		  int width, int height, float target_k,
nkeynes@221
    79
		  float detxy, float detxk, float detyk,
nkeynes@221
    80
		  struct vertex_all *target ) {
nkeynes@221
    81
    int num_points = 0;
nkeynes@221
    82
    float tmpk = (target_k - center_k) * detxy;
nkeynes@221
    83
    float x0 = -1;
nkeynes@221
    84
    float x1 = -1;
nkeynes@221
    85
    
nkeynes@221
    86
    if( detyk != 0 ) {
nkeynes@221
    87
	x0 = (tmpk - ((0-center_y)*detxk))/detyk + center_x; /* x where y=0 */
nkeynes@221
    88
	if( x0 >= 0.0 && x0 <= width ) {
nkeynes@221
    89
	    target[num_points].x = x0;
nkeynes@221
    90
	    target[num_points].y = 0.0;
nkeynes@221
    91
	    num_points++;
nkeynes@221
    92
	}
nkeynes@221
    93
	
nkeynes@221
    94
	x1 = (tmpk - ((height-center_y)*detxk))/detyk + center_x; /* x where y=height */
nkeynes@221
    95
	if( x1 >= 0.0 && x1 <= width ) {
nkeynes@221
    96
	    target[num_points].x = x1;
nkeynes@221
    97
	    target[num_points].y = height;
nkeynes@221
    98
	    num_points++;
nkeynes@221
    99
	}
nkeynes@221
   100
    }
nkeynes@221
   101
    
nkeynes@221
   102
    if( detxk != 0 ) {
nkeynes@221
   103
	if( x0 != 0.0 && x1 != 0.0 ) { /* If x0 == 0 or x1 == 0, then we already have this one */
nkeynes@221
   104
	    float y0 = (tmpk - ((0-center_x)*detyk))/detxk + center_y; /* y where x=0 */
nkeynes@221
   105
	    if( y0 >= 0.0 && y0 <= height ) {
nkeynes@221
   106
		target[num_points].x = 0.0;
nkeynes@221
   107
		target[num_points].y = y0;
nkeynes@221
   108
		num_points++;
nkeynes@221
   109
	    }
nkeynes@221
   110
	}
nkeynes@221
   111
	
nkeynes@221
   112
	if( x0 != width && x1 != width ) {
nkeynes@221
   113
	    float y1 = (tmpk - ((width-center_x)*detyk))/detxk + center_y; /* y where x=width */
nkeynes@221
   114
	    if( y1 >= 0.0 && y1 <= height ) {
nkeynes@221
   115
		target[num_points].x = width;
nkeynes@221
   116
		target[num_points].y = y1;
nkeynes@221
   117
		num_points++;
nkeynes@221
   118
	    }
nkeynes@221
   119
	}
nkeynes@221
   120
    }
nkeynes@221
   121
nkeynes@221
   122
    if( num_points == 0 || num_points == 2 ) {
nkeynes@221
   123
	/* 0 = no points - line doesn't pass through the viewport */
nkeynes@221
   124
	/* 2 = normal case - got 2 endpoints */
nkeynes@221
   125
	return num_points;
nkeynes@221
   126
    } else {
nkeynes@221
   127
	ERROR( "compute_colour_line got bad number of points: %d", num_points );
nkeynes@221
   128
	return 0;
nkeynes@221
   129
    }
nkeynes@221
   130
}
nkeynes@221
   131
nkeynes@221
   132
/**
nkeynes@221
   133
 * A region describes a portion of the screen, possibly subdivided by a line.
nkeynes@221
   134
 * if region_left and region_right are -1, this is a terminal region that can
nkeynes@221
   135
 * be rendered directly. Otherwise region_left and region_right refer two 
nkeynes@221
   136
 * sub-regions that are separated by the line segment vertex1-vertex2.
nkeynes@221
   137
 */
nkeynes@221
   138
struct bkg_region {
nkeynes@221
   139
    /* Vertexes marking the line segment that splits this region */
nkeynes@221
   140
    int vertex1;
nkeynes@221
   141
    int vertex2;
nkeynes@221
   142
    /* Index of the left sub-region */
nkeynes@221
   143
    int region_left;
nkeynes@221
   144
    /* Index of the right sub-region */
nkeynes@221
   145
    int region_right;
nkeynes@221
   146
};
nkeynes@221
   147
nkeynes@221
   148
/**
nkeynes@221
   149
 * Convenience structure to bundle together the vertex and region data.
nkeynes@221
   150
 */
nkeynes@221
   151
struct bkg_scene {
nkeynes@221
   152
    int num_vertexes;
nkeynes@221
   153
    int num_regions;
nkeynes@221
   154
    struct vertex_all vertexes[MAX_VERTEXES];
nkeynes@221
   155
    struct bkg_region regions[MAX_REGIONS];
nkeynes@221
   156
};
nkeynes@221
   157
nkeynes@221
   158
void parse_vertexes( uint32_t *polygon, int num_vertexes, struct vertex_all *result )
nkeynes@221
   159
{
nkeynes@221
   160
    uint32_t *vertexes = polygon + 3; 
nkeynes@221
   161
    int i,m = 0;
nkeynes@221
   162
    for( i=0; i<num_vertexes; i++ ) {
nkeynes@221
   163
	float *vertexf = (float *)vertexes;
nkeynes@221
   164
	result[i].x = vertexf[0];
nkeynes@221
   165
	result[i].y = vertexf[1];
nkeynes@221
   166
	result[i].z = vertexf[2];
nkeynes@221
   167
	uint32_t argb;
nkeynes@221
   168
	if( POLY1_TEXTURED(*polygon) ) {
nkeynes@221
   169
	    if( POLY1_UV16(*polygon) ) {
nkeynes@221
   170
		result[i].u = halftofloat(vertexes[m+3]>>16);
nkeynes@221
   171
		result[i].v = halftofloat(vertexes[m+3]);
nkeynes@221
   172
		argb = vertexes[m+4];
nkeynes@221
   173
		vertexes += 5;
nkeynes@221
   174
	    } else {
nkeynes@221
   175
		result[i].u = vertexf[m+3];
nkeynes@221
   176
		result[i].v = vertexf[m+4];
nkeynes@221
   177
		argb = vertexes[m+5];
nkeynes@221
   178
		vertexes += 6;
nkeynes@221
   179
	    }
nkeynes@221
   180
	} else {
nkeynes@221
   181
	    argb = vertexes[m+3];
nkeynes@221
   182
	    vertexes += 4;
nkeynes@221
   183
	}
nkeynes@221
   184
	result[i].rgba[0] = FARGB_R(argb);
nkeynes@221
   185
	result[i].rgba[1] = FARGB_G(argb);
nkeynes@221
   186
        result[i].rgba[2] = FARGB_B(argb);
nkeynes@221
   187
	result[i].rgba[3] = FARGB_A(argb);
nkeynes@221
   188
    }
nkeynes@221
   189
nkeynes@221
   190
}
nkeynes@221
   191
nkeynes@221
   192
/**
nkeynes@221
   193
 * Constants returned by compute_line_intersection. Note that for these purposes,
nkeynes@221
   194
 * "Left" means the point(s) result in a negative value in the line equation, while
nkeynes@221
   195
 * "Right" means the points(s) result in a positive value in the line equation. The
nkeynes@221
   196
 * exact meaning isn't particularly important though, as long as we're consistent
nkeynes@221
   197
 * throughout this process
nkeynes@221
   198
 */
nkeynes@221
   199
#define LINE_COLLINEAR 0   /* The line segments are part of the same line */
nkeynes@221
   200
#define LINE_SIDE_LEFT 1   /* The second line is entirely to the "left" of the first line */
nkeynes@221
   201
#define LINE_SIDE_RIGHT 2  /* The second line is entirely to the "right" of the first line */
nkeynes@221
   202
#define LINE_INTERSECT_FROM_LEFT 3 /* The lines intersect, and (x3,y3) is to the "left" of the first line */
nkeynes@221
   203
#define LINE_INTERSECT_FROM_RIGHT 4 /* The lines intersect, and (x3,y3) is to the "right" of the first line */
nkeynes@221
   204
#define LINE_SKEW 5        /* The line segments neither intersect nor do any of the above apply (should never happen here) */
nkeynes@221
   205
nkeynes@221
   206
/**
nkeynes@221
   207
 * Compute the intersection of two line segments, where 
nkeynes@221
   208
 * (x1,y1)-(x2,y2) defines the target segment, and
nkeynes@221
   209
 * (x3,y3)-(x4,y4) defines the line intersecting it.
nkeynes@221
   210
 *
nkeynes@221
   211
 * Based off work by Mukesh Prasad (http://www.acm.org/pubs/tog/GraphicsGems/index.html)
nkeynes@221
   212
 *
nkeynes@221
   213
 * @return one of the above LINE_* constants
nkeynes@221
   214
 */
nkeynes@221
   215
static int compute_line_intersection( float x1, float y1,   /* First line segment */
nkeynes@221
   216
				      float x2, float y2,
nkeynes@221
   217
				      float x3, float y3,   /* Second line segment */
nkeynes@221
   218
				      float x4, float y4,
nkeynes@221
   219
				      float *x, float *y  )  /* Output value: */
nkeynes@221
   220
{
nkeynes@221
   221
    float a1, a2, b1, b2, c1, c2; /* Coefficients of line eqns. */
nkeynes@221
   222
    float r1, r2, r3, r4;         /* test values */
nkeynes@221
   223
    float denom;     /* Intermediate values */
nkeynes@221
   224
nkeynes@221
   225
    /* Compute a1, b1, c1, where line joining points 1 and 2
nkeynes@221
   226
     * is "a1 x  +  b1 y  +  c1  =  0".
nkeynes@221
   227
     */
nkeynes@221
   228
nkeynes@221
   229
    a1 = y2 - y1;
nkeynes@221
   230
    b1 = x1 - x2;
nkeynes@221
   231
    c1 = x2 * y1 - x1 * y2;
nkeynes@221
   232
nkeynes@221
   233
    /* Compute r3 and r4. */
nkeynes@221
   234
nkeynes@221
   235
    r3 = a1 * x3 + b1 * y3 + c1;
nkeynes@221
   236
    r4 = a1 * x4 + b1 * y4 + c1;
nkeynes@221
   237
nkeynes@221
   238
    /* Check signs of r3 and r4.  If both point 3 and point 4 lie on
nkeynes@221
   239
     * same side of line 1, the line segments do not intersect.
nkeynes@221
   240
     */
nkeynes@221
   241
nkeynes@221
   242
    if( r3 == 0 && r4 == 0 ) {
nkeynes@221
   243
	return LINE_COLLINEAR;
nkeynes@221
   244
    } else if( r3 <= 0 && r4 <= 0 ) {
nkeynes@221
   245
	return LINE_SIDE_LEFT;
nkeynes@221
   246
    } else if( r3 >= 0 && r4 >= 0 ) {
nkeynes@221
   247
	return LINE_SIDE_RIGHT;
nkeynes@221
   248
    }
nkeynes@221
   249
nkeynes@221
   250
    /* Compute a2, b2, c2 */
nkeynes@221
   251
nkeynes@221
   252
    a2 = y4 - y3;
nkeynes@221
   253
    b2 = x3 - x4;
nkeynes@221
   254
    c2 = x4 * y3 - x3 * y4;
nkeynes@221
   255
nkeynes@221
   256
    /* Compute r1 and r2 */
nkeynes@221
   257
nkeynes@221
   258
    r1 = a2 * x1 + b2 * y1 + c2;
nkeynes@221
   259
    r2 = a2 * x2 + b2 * y2 + c2;
nkeynes@221
   260
nkeynes@221
   261
    /* Check signs of r1 and r2.  If both point 1 and point 2 lie
nkeynes@221
   262
     * on same side of second line segment, the line segments do
nkeynes@221
   263
     * not intersect.
nkeynes@221
   264
     */
nkeynes@221
   265
nkeynes@221
   266
    if ( r1 != 0 && r2 != 0 &&
nkeynes@221
   267
         signbit(r1) == signbit(r2) ) {
nkeynes@221
   268
        return LINE_SKEW; /* Should never happen */
nkeynes@221
   269
    }
nkeynes@221
   270
nkeynes@221
   271
    /* Cmpute intersection point. 
nkeynes@221
   272
     */
nkeynes@221
   273
    denom = a1 * b2 - a2 * b1;
nkeynes@221
   274
    if ( denom == 0 )
nkeynes@221
   275
        return LINE_COLLINEAR; /* Should never get to this point either */
nkeynes@221
   276
nkeynes@221
   277
    *x = (b1 * c2 - b2 * c1) / denom;
nkeynes@221
   278
    *y = (a2 * c1 - a1 * c2) / denom;
nkeynes@221
   279
nkeynes@221
   280
    if( r3 <= 0 && r4 >= 0 ) {
nkeynes@221
   281
	return LINE_INTERSECT_FROM_LEFT;
nkeynes@221
   282
    } else {
nkeynes@221
   283
	return LINE_INTERSECT_FROM_RIGHT;
nkeynes@221
   284
    }
nkeynes@221
   285
}
nkeynes@221
   286
nkeynes@221
   287
/**
nkeynes@221
   288
 * Given a set of vertexes and a line segment to use to split them, generates
nkeynes@221
   289
 * two sets of vertexes representing the polygon on either side of the line
nkeynes@221
   290
 * segment. This method preserves the winding direction of the input vertexes.
nkeynes@221
   291
 */
nkeynes@221
   292
static void compute_subregions( struct bkg_scene *scene,
nkeynes@221
   293
				int splitv1, int splitv2,
nkeynes@221
   294
				int *vertex_in, int num_vertex_in,
nkeynes@221
   295
				int *left_vertex_out, int *num_left_vertex_out,
nkeynes@221
   296
				int *right_vertex_out, int *num_right_vertex_out )
nkeynes@221
   297
{
nkeynes@221
   298
    float x1 = scene->vertexes[splitv1].x;
nkeynes@221
   299
    float y1 = scene->vertexes[splitv1].y;
nkeynes@221
   300
    float x2 = scene->vertexes[splitv2].x;
nkeynes@221
   301
    float y2 = scene->vertexes[splitv2].y;
nkeynes@221
   302
nkeynes@221
   303
    float a1 = y2 - y1;
nkeynes@221
   304
    float b1 = x1 - x2;
nkeynes@221
   305
    float c1 = x2 * y1 - x1 * y2;
nkeynes@221
   306
    int i;
nkeynes@221
   307
    
nkeynes@221
   308
    *num_left_vertex_out = 0;
nkeynes@221
   309
    *num_right_vertex_out = 0;
nkeynes@221
   310
    int last = 0;
nkeynes@221
   311
    for( i=0; i<num_vertex_in; i++ ) {
nkeynes@221
   312
	struct vertex_all *vertex = &scene->vertexes[vertex_in[i]];
nkeynes@221
   313
	float r = a1 * vertex->x + b1 * vertex->y + c1;
nkeynes@221
   314
	if( r <= 0 ) {
nkeynes@221
   315
	    if( last == 1 ) {
nkeynes@221
   316
		/* cross-point. add the split vertexes */
nkeynes@221
   317
		int v1 = vertex_in[i-1];
nkeynes@221
   318
		int v2 = vertex_in[i];
nkeynes@221
   319
		/* Determine which point is closer to the line. Strictly speaking
nkeynes@221
   320
		 * one of them must be ON the line, but this way allows for floating
nkeynes@221
   321
		 * point inaccuracies.
nkeynes@221
   322
		 */
nkeynes@221
   323
		float a2 = scene->vertexes[v2].y - scene->vertexes[v1].y;
nkeynes@221
   324
		float b2 = scene->vertexes[v1].x - scene->vertexes[v2].x;
nkeynes@221
   325
		float c2 = scene->vertexes[v2].x * scene->vertexes[v1].y - 
nkeynes@221
   326
		    scene->vertexes[v1].x * scene->vertexes[v2].y;
nkeynes@221
   327
		float r1 = a2 * x1 + b2 * y1 + c2;
nkeynes@221
   328
		float r2 = a2 * x2 + b2 * y2 + c2;
nkeynes@221
   329
		if( fabsf(r1) > fabs(r2) ) {
nkeynes@221
   330
		    int tmp = splitv1;
nkeynes@221
   331
		    splitv1 = splitv2;
nkeynes@221
   332
		    splitv2 = tmp;
nkeynes@221
   333
		}
nkeynes@221
   334
		right_vertex_out[(*num_right_vertex_out)++] = splitv1;
nkeynes@221
   335
		right_vertex_out[(*num_right_vertex_out)++] = splitv2;
nkeynes@221
   336
		left_vertex_out[(*num_left_vertex_out)++] = splitv2;
nkeynes@221
   337
		left_vertex_out[(*num_left_vertex_out)++] = splitv1;
nkeynes@221
   338
		last = 2;
nkeynes@221
   339
	    } else if( last != 2 ) {
nkeynes@221
   340
		last = -1;
nkeynes@221
   341
	    }
nkeynes@221
   342
	    left_vertex_out[(*num_left_vertex_out)++] = vertex_in[i];
nkeynes@221
   343
	} else {
nkeynes@221
   344
	    if( last == -1 ) {
nkeynes@221
   345
		/* cross-point. add the split vertexes */
nkeynes@221
   346
		int v1 = vertex_in[i-1];
nkeynes@221
   347
		int v2 = vertex_in[i];
nkeynes@221
   348
		/* Determine which point is closer to the line. Strictly speaking
nkeynes@221
   349
		 * one of them must be ON the line, but this way allows for floating
nkeynes@221
   350
		 * point inaccuracies.
nkeynes@221
   351
		 */
nkeynes@221
   352
		float a2 = scene->vertexes[v2].y - scene->vertexes[v1].y;
nkeynes@221
   353
		float b2 = scene->vertexes[v1].x - scene->vertexes[v2].x;
nkeynes@221
   354
		float c2 = scene->vertexes[v2].x * scene->vertexes[v1].y - 
nkeynes@221
   355
		    scene->vertexes[v1].x * scene->vertexes[v2].y;
nkeynes@221
   356
		float r1 = a2 * x1 + b2 * y1 + c2;
nkeynes@221
   357
		float r2 = a2 * x2 + b2 * y2 + c2;
nkeynes@221
   358
		if( fabsf(r1) > fabs(r2) ) {
nkeynes@221
   359
		    int tmp = splitv1;
nkeynes@221
   360
		    splitv1 = splitv2;
nkeynes@221
   361
		    splitv2 = tmp;
nkeynes@221
   362
		}
nkeynes@221
   363
		left_vertex_out[(*num_left_vertex_out)++] = splitv1;
nkeynes@221
   364
		left_vertex_out[(*num_left_vertex_out)++] = splitv2;
nkeynes@221
   365
		right_vertex_out[(*num_right_vertex_out)++] = splitv2;
nkeynes@221
   366
		right_vertex_out[(*num_right_vertex_out)++] = splitv1;
nkeynes@221
   367
		last = 2;
nkeynes@221
   368
	    } else if( last != 2 ) {
nkeynes@221
   369
		last = 1;
nkeynes@221
   370
	    }
nkeynes@221
   371
	    right_vertex_out[(*num_right_vertex_out)++] = vertex_in[i];
nkeynes@221
   372
	}
nkeynes@221
   373
    }
nkeynes@221
   374
}
nkeynes@221
   375
nkeynes@221
   376
/**
nkeynes@221
   377
 * Subdivide the region tree by splitting it along a given line.
nkeynes@221
   378
 * 
nkeynes@221
   379
 * @param scene  current bkg scene data
nkeynes@221
   380
 * @param region current region under examination
nkeynes@221
   381
 * @param vertex1 first vertex of the new line segment
nkeynes@221
   382
 * @param vertex2 second vertex of the new line segment
nkeynes@221
   383
 */
nkeynes@221
   384
static void bkg_region_subdivide( struct bkg_scene *scene, int region, int vertex1, int vertex2 ) {
nkeynes@221
   385
    struct bkg_region *this_region = &scene->regions[region];
nkeynes@221
   386
    
nkeynes@221
   387
    if( scene->regions[region].region_left == -1 || scene->regions[region].region_right == -1 ) {
nkeynes@221
   388
	/* Reached the end of the tree. Setup new left+right regions */
nkeynes@221
   389
	int i = scene->num_regions;
nkeynes@221
   390
	scene->regions[i].region_left = scene->regions[i].region_right = -1;
nkeynes@221
   391
	scene->regions[i+1].region_left = scene->regions[i+1].region_right = -1;
nkeynes@221
   392
	this_region->region_left = i;
nkeynes@221
   393
	this_region->region_right = i+1;
nkeynes@221
   394
	this_region->vertex1 = vertex1;
nkeynes@221
   395
	this_region->vertex2 = vertex2;
nkeynes@221
   396
	scene->num_regions += 2;
nkeynes@221
   397
    } else {
nkeynes@221
   398
	float x,y;
nkeynes@221
   399
	int thisv1 = this_region->vertex1;
nkeynes@221
   400
	int thisv2 = this_region->vertex2;
nkeynes@221
   401
	int vertex3;
nkeynes@221
   402
	int status = 
nkeynes@221
   403
	    compute_line_intersection( scene->vertexes[thisv1].x, scene->vertexes[thisv1].y,
nkeynes@221
   404
				       scene->vertexes[thisv2].x, scene->vertexes[thisv2].y,
nkeynes@221
   405
				       scene->vertexes[vertex1].x, scene->vertexes[vertex1].y,
nkeynes@221
   406
				       scene->vertexes[vertex2].x, scene->vertexes[vertex2].y,
nkeynes@221
   407
				       &x, &y );
nkeynes@221
   408
	switch( status ) {
nkeynes@221
   409
	case LINE_INTERSECT_FROM_LEFT:
nkeynes@221
   410
	    /* if new line segment intersects our current line segment,
nkeynes@221
   411
	     * subdivide the segment (add a new vertex) and recurse on both
nkeynes@221
   412
	     * sub trees 
nkeynes@221
   413
	     */
nkeynes@221
   414
	    /* Compute split-point vertex */
nkeynes@221
   415
	    vertex3 = scene->num_vertexes++;
nkeynes@221
   416
	    scene->vertexes[vertex3].x = x;
nkeynes@221
   417
	    scene->vertexes[vertex3].y = y;
nkeynes@221
   418
	    /* Recurse */
nkeynes@221
   419
	    bkg_region_subdivide( scene, scene->regions[region].region_left, vertex1,vertex3 );
nkeynes@221
   420
	    bkg_region_subdivide( scene, scene->regions[region].region_right, vertex3, vertex2 );
nkeynes@221
   421
	    break;
nkeynes@221
   422
	case LINE_INTERSECT_FROM_RIGHT:
nkeynes@221
   423
	    /* Same except line runs in the opposite direction */
nkeynes@221
   424
	    vertex3 = scene->num_vertexes++;
nkeynes@221
   425
	    scene->vertexes[vertex3].x = x;
nkeynes@221
   426
	    scene->vertexes[vertex3].y = y;
nkeynes@221
   427
	    /* Recurse */
nkeynes@221
   428
	    bkg_region_subdivide( scene, scene->regions[region].region_left, vertex2,vertex3 );
nkeynes@221
   429
	    bkg_region_subdivide( scene, scene->regions[region].region_right, vertex3, vertex1 );
nkeynes@221
   430
	    break;
nkeynes@221
   431
	case LINE_COLLINEAR:
nkeynes@221
   432
	case LINE_SKEW:
nkeynes@221
   433
	    /* Collinear - ignore */
nkeynes@221
   434
	    break;
nkeynes@221
   435
	case LINE_SIDE_LEFT:
nkeynes@221
   436
	    /* else if line segment passes through the left sub-region alone,
nkeynes@221
   437
	     * left-recurse only.
nkeynes@221
   438
	     */
nkeynes@221
   439
	    bkg_region_subdivide( scene, scene->regions[region].region_left, vertex1, vertex2 );
nkeynes@221
   440
	    break;
nkeynes@221
   441
	case LINE_SIDE_RIGHT:
nkeynes@221
   442
	    /* Otherwise line segment passes through the right sub-region alone,
nkeynes@221
   443
	     * so right-recurse.
nkeynes@221
   444
	     */
nkeynes@221
   445
	    bkg_region_subdivide( scene, scene->regions[region].region_right, vertex1, vertex2 );
nkeynes@221
   446
	    break;
nkeynes@221
   447
	}
nkeynes@221
   448
    }
nkeynes@221
   449
}
nkeynes@221
   450
nkeynes@221
   451
	
nkeynes@221
   452
nkeynes@221
   453
/**
nkeynes@219
   454
 * Compute the values for an array of vertexes, given x,y for each
nkeynes@219
   455
 * vertex and the base 3-vertex triple used to define the background
nkeynes@219
   456
 * plane. Essentially the base vertexes are used to find the
nkeynes@219
   457
 * plane equation for each of z,a,r,g,b,etc, which is then solved for
nkeynes@219
   458
 * each of the required compute vertexes (normally the corner points).
nkeynes@219
   459
 *
nkeynes@219
   460
 * @param base The 3 vertexes supplied as the background definition
nkeynes@219
   461
 * @param compute An array of vertexes to compute. x and y must be
nkeynes@219
   462
 *   preset, other values are computed.
nkeynes@219
   463
 */
nkeynes@221
   464
static void bkg_compute_scene( struct vertex_all *base, int width, int height,
nkeynes@221
   465
				struct bkg_scene *scene )
nkeynes@219
   466
{
nkeynes@219
   467
    struct vertex_all center;
nkeynes@219
   468
    struct vertex_all diff0, diff1;
nkeynes@221
   469
    int i,k;
nkeynes@219
   470
nkeynes@219
   471
    center.x = base[1].x;
nkeynes@219
   472
    center.y = base[1].y;
nkeynes@219
   473
    center.z = base[1].z;
nkeynes@219
   474
    diff0.x = base[0].x - base[1].x;
nkeynes@219
   475
    diff0.y = base[0].y - base[1].y;
nkeynes@219
   476
    diff0.z = base[0].z - base[1].z;
nkeynes@219
   477
    diff1.x = base[2].x - base[1].x;
nkeynes@219
   478
    diff1.y = base[2].y - base[1].y;
nkeynes@219
   479
    diff1.z = base[2].z - base[1].z;
nkeynes@219
   480
nkeynes@221
   481
    float detxy = ((diff1.y) * (diff0.x)) - ((diff0.y) * (diff1.x));
nkeynes@221
   482
    
nkeynes@221
   483
    /* Corner points first */
nkeynes@221
   484
    scene->vertexes[0].x = 0.0;
nkeynes@221
   485
    scene->vertexes[0].y = 0.0;
nkeynes@221
   486
    scene->vertexes[1].x = width;
nkeynes@221
   487
    scene->vertexes[1].y = 0.0;
nkeynes@221
   488
    scene->vertexes[2].x = width;
nkeynes@221
   489
    scene->vertexes[2].y = height;
nkeynes@221
   490
    scene->vertexes[3].x = 0.0;
nkeynes@221
   491
    scene->vertexes[3].y = height;
nkeynes@221
   492
    scene->regions[0].region_left = -1;
nkeynes@221
   493
    scene->regions[0].region_right = -1;
nkeynes@221
   494
    scene->num_vertexes = 4;
nkeynes@221
   495
    scene->num_regions = 1;
nkeynes@221
   496
nkeynes@221
   497
    if( detxy == 0 ) {
nkeynes@221
   498
	/* The points lie on a single line - no plane for you. Use the values
nkeynes@221
   499
	 * from the 3rd point for the whole screen.
nkeynes@221
   500
	 */
nkeynes@221
   501
	for( i=0; i<4; i++ ) {
nkeynes@221
   502
	    scene->vertexes[i].rgba[0] = base[2].rgba[0];
nkeynes@221
   503
	    scene->vertexes[i].rgba[1] = base[2].rgba[1];
nkeynes@221
   504
	    scene->vertexes[i].rgba[2] = base[2].rgba[2];
nkeynes@221
   505
	    scene->vertexes[i].rgba[3] = base[2].rgba[3];
nkeynes@221
   506
	    scene->vertexes[i].u = base[2].u;
nkeynes@221
   507
	    scene->vertexes[i].v = base[2].v;
nkeynes@221
   508
	}
nkeynes@219
   509
    } else {
nkeynes@221
   510
	/* Compute the colour values at each corner */
nkeynes@221
   511
	center.rgba[0] = base[1].rgba[0];
nkeynes@221
   512
	center.rgba[1] = base[1].rgba[1];
nkeynes@221
   513
	center.rgba[2] = base[1].rgba[2];
nkeynes@221
   514
	center.rgba[3] = base[1].rgba[3];
nkeynes@221
   515
	diff0.rgba[0] = base[0].rgba[0] - center.rgba[0];
nkeynes@221
   516
	diff0.rgba[1] = base[0].rgba[1] - center.rgba[1];
nkeynes@221
   517
	diff0.rgba[2] = base[0].rgba[2] - center.rgba[2];
nkeynes@221
   518
	diff0.rgba[3] = base[0].rgba[3] - center.rgba[3];
nkeynes@221
   519
	diff0.u = base[0].u - center.u;
nkeynes@221
   520
	diff0.v = base[0].v - center.v;
nkeynes@221
   521
	diff1.rgba[0] = base[2].rgba[0] - center.rgba[0];
nkeynes@221
   522
	diff1.rgba[1] = base[2].rgba[1] - center.rgba[1];
nkeynes@221
   523
	diff1.rgba[2] = base[2].rgba[2] - center.rgba[2];
nkeynes@221
   524
	diff1.rgba[3] = base[2].rgba[3] - center.rgba[3];
nkeynes@221
   525
	diff1.u = base[2].u - center.u;
nkeynes@221
   526
	diff1.v = base[2].v - center.v;
nkeynes@221
   527
	for( i=0; i<4; i++ ) {
nkeynes@221
   528
	    float t = ((scene->vertexes[i].x - center.x) * diff1.y -
nkeynes@221
   529
		       (scene->vertexes[i].y - center.y) * diff1.x) / detxy;
nkeynes@221
   530
	    float s = ((scene->vertexes[i].y - center.y) * diff0.x -
nkeynes@221
   531
		       (scene->vertexes[i].x - center.x) * diff0.y) / detxy;
nkeynes@221
   532
	    scene->vertexes[i].z = center.z + (t*diff0.z) + (s*diff1.z);
nkeynes@221
   533
	    scene->vertexes[i].rgba[0] = center.rgba[0] + (t*diff0.rgba[0]) + (s*diff1.rgba[0]);
nkeynes@221
   534
	    scene->vertexes[i].rgba[1] = center.rgba[1] + (t*diff0.rgba[1]) + (s*diff1.rgba[1]);
nkeynes@221
   535
	    scene->vertexes[i].rgba[2] = center.rgba[2] + (t*diff0.rgba[2]) + (s*diff1.rgba[2]);
nkeynes@221
   536
	    scene->vertexes[i].rgba[3] = center.rgba[3] + (t*diff0.rgba[3]) + (s*diff1.rgba[3]);
nkeynes@221
   537
	    scene->vertexes[i].u = center.u + (t*diff0.u) + (s*diff1.u);
nkeynes@221
   538
	    scene->vertexes[i].v = center.v + (t*diff0.v) + (s*diff1.v);
nkeynes@221
   539
	}
nkeynes@221
   540
nkeynes@221
   541
	/* Check for values > 1.0 | < 0.0 */
nkeynes@221
   542
	for( k=0; k<4; k++ ) {
nkeynes@221
   543
	    float detyk = ((diff1.y) * (diff0.rgba[k])) - ((diff0.y)*(diff1.rgba[k]));
nkeynes@221
   544
	    float detxk = ((diff0.x) * (diff1.rgba[k])) - ((diff1.x)*(diff0.rgba[k]));
nkeynes@221
   545
	    if( scene->vertexes[0].rgba[k] > 1.0 || scene->vertexes[1].rgba[k] > 1.0 || 
nkeynes@221
   546
		scene->vertexes[2].rgba[k] > 1.0 || scene->vertexes[3].rgba[k] > 1.0 ) {
nkeynes@221
   547
		int v1 = scene->num_vertexes;
nkeynes@221
   548
		scene->num_vertexes += compute_colour_line(center.x, center.y, center.rgba[k],
nkeynes@221
   549
						    width, height, 1.0,
nkeynes@221
   550
						    detxy, detxk, detyk, 
nkeynes@221
   551
						    scene->vertexes+scene->num_vertexes );
nkeynes@221
   552
		if( scene->num_vertexes != v1 ) {
nkeynes@221
   553
		    bkg_region_subdivide( scene, 0, v1, v1+1 );
nkeynes@221
   554
		}
nkeynes@221
   555
	    }
nkeynes@221
   556
nkeynes@221
   557
	    if( scene->vertexes[0].rgba[k] < 0.0 || scene->vertexes[1].rgba[k] < 0.0 || 
nkeynes@221
   558
		scene->vertexes[2].rgba[k] < 0.0 || scene->vertexes[3].rgba[k] < 0.0 ) {
nkeynes@221
   559
		int v1 = scene->num_vertexes;
nkeynes@221
   560
		scene->num_vertexes += compute_colour_line(center.x, center.y, center.rgba[k],
nkeynes@221
   561
						    width, height, 0.0,
nkeynes@221
   562
						    detxy, detxk, detyk, 
nkeynes@221
   563
						    scene->vertexes+scene->num_vertexes );
nkeynes@221
   564
		if( scene->num_vertexes != v1 ) {
nkeynes@221
   565
		    bkg_region_subdivide( scene, 0, v1, v1+1 );
nkeynes@221
   566
		}
nkeynes@221
   567
nkeynes@221
   568
	    }
nkeynes@221
   569
	}
nkeynes@221
   570
nkeynes@221
   571
	/* Finally compute the colour values for all vertexes 
nkeynes@221
   572
	 * (excluding the 4 we did upfront) */
nkeynes@221
   573
	for( i=4; i<scene->num_vertexes; i++ ) {
nkeynes@221
   574
	    float t = ((scene->vertexes[i].x - center.x) * diff1.y -
nkeynes@221
   575
		       (scene->vertexes[i].y - center.y) * diff1.x) / detxy;
nkeynes@221
   576
	    float s = ((scene->vertexes[i].y - center.y) * diff0.x -
nkeynes@221
   577
		       (scene->vertexes[i].x - center.x) * diff0.y) / detxy;
nkeynes@221
   578
	    scene->vertexes[i].z = center.z + (t*diff0.z) + (s*diff1.z);
nkeynes@221
   579
	    scene->vertexes[i].rgba[0] = center.rgba[0] + (t*diff0.rgba[0]) + (s*diff1.rgba[0]);
nkeynes@221
   580
	    scene->vertexes[i].rgba[1] = center.rgba[1] + (t*diff0.rgba[1]) + (s*diff1.rgba[1]);
nkeynes@221
   581
	    scene->vertexes[i].rgba[2] = center.rgba[2] + (t*diff0.rgba[2]) + (s*diff1.rgba[2]);
nkeynes@221
   582
	    scene->vertexes[i].rgba[3] = center.rgba[3] + (t*diff0.rgba[3]) + (s*diff1.rgba[3]);
nkeynes@221
   583
	    scene->vertexes[i].u = center.u + (t*diff0.u) + (s*diff1.u);
nkeynes@221
   584
	    scene->vertexes[i].v = center.v + (t*diff0.v) + (s*diff1.v);
nkeynes@219
   585
	}
nkeynes@219
   586
    }
nkeynes@219
   587
}
nkeynes@219
   588
nkeynes@221
   589
/**
nkeynes@221
   590
 * Render a bkg_region.
nkeynes@221
   591
 * @param scene the background scene data
nkeynes@221
   592
 * @param region the region to render
nkeynes@221
   593
 * @param vertexes the vertexes surrounding the region
nkeynes@221
   594
 * @param num_vertexes the number of vertexes in the vertex array
nkeynes@221
   595
 */
nkeynes@221
   596
void bkg_render_region( struct bkg_scene *scene, int region, int *vertexes, int num_vertexes,
nkeynes@221
   597
			uint32_t poly1 )
nkeynes@221
   598
{
nkeynes@221
   599
    if( scene->regions[region].region_left == -1 && scene->regions[region].region_right == -1 ) {
nkeynes@221
   600
	/* Leaf node - render the points as given */
nkeynes@221
   601
	int i,k;
nkeynes@221
   602
	glBegin(GL_POLYGON);
nkeynes@221
   603
	for( i=0; i<num_vertexes; i++ ) {
nkeynes@221
   604
	    k = vertexes[i];
nkeynes@221
   605
	    glColor4fv(scene->vertexes[k].rgba);
nkeynes@221
   606
	    if( POLY1_TEXTURED(poly1) ) {
nkeynes@221
   607
		glTexCoord2f(scene->vertexes[k].u, scene->vertexes[k].v);
nkeynes@221
   608
	    }
nkeynes@221
   609
	    glVertex3f(scene->vertexes[k].x, scene->vertexes[k].y, scene->vertexes[k].z);
nkeynes@221
   610
	}
nkeynes@221
   611
	glEnd();
nkeynes@221
   612
    } else {
nkeynes@221
   613
	/* split the region into left and right regions */
nkeynes@221
   614
	int left_vertexes[num_vertexes+1];
nkeynes@221
   615
	int right_vertexes[num_vertexes+1];
nkeynes@221
   616
	int num_left = 0;
nkeynes@221
   617
	int num_right = 0;
nkeynes@221
   618
	struct bkg_region *reg = &scene->regions[region];
nkeynes@221
   619
	compute_subregions( scene, reg->vertex1, reg->vertex2, vertexes, num_vertexes,
nkeynes@221
   620
			    left_vertexes, &num_left, right_vertexes, &num_right );
nkeynes@221
   621
	bkg_render_region( scene, reg->region_left, left_vertexes, num_left, poly1 );
nkeynes@221
   622
	bkg_render_region( scene, reg->region_right, right_vertexes, num_right, poly1 );
nkeynes@221
   623
    }
nkeynes@221
   624
    
nkeynes@221
   625
}
nkeynes@221
   626
nkeynes@221
   627
nkeynes@219
   628
void render_backplane( uint32_t *polygon, uint32_t width, uint32_t height, uint32_t mode ) {
nkeynes@221
   629
    struct vertex_all vertex[3];
nkeynes@221
   630
    int screen_vertexes[4] = {0,1,2,3};
nkeynes@221
   631
    struct bkg_scene scene;
nkeynes@221
   632
    int i,j,k, num_vertexes;
nkeynes@219
   633
nkeynes@221
   634
    parse_vertexes(polygon, 3, vertex);
nkeynes@219
   635
    render_set_context(polygon, 0);
nkeynes@221
   636
    glDisable(GL_CULL_FACE);
nkeynes@221
   637
    glDisable(GL_DEPTH_TEST);
nkeynes@221
   638
    glBlendFunc(GL_ONE, GL_ZERO); /* For now, just disable alpha blending on the bkg */
nkeynes@221
   639
    bkg_compute_scene(vertex, width, height, &scene);
nkeynes@221
   640
    bkg_render_region(&scene, 0, screen_vertexes, 4, *polygon);
nkeynes@219
   641
}
.