Search
lxdream.org :: lxdream/src/pvr2/rendbkg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendbkg.c
changeset 219:dfd3292143f2
next221:cf5c6d326162
author nkeynes
date Tue Aug 29 08:12:13 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Initial implementation of new background support
view annotate diff log raw
     1 /**
     2  * $Id: rendbkg.c,v 1.1 2006-08-29 08:12:13 nkeynes Exp $
     3  *
     4  * PVR2 background renderer. 
     5  *
     6  * Yes, it uses the same basic data structure. Yes, it needs to be handled
     7  * completely differently.
     8  *
     9  * Copyright (c) 2005 Nathan Keynes.
    10  *
    11  * This program is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU General Public License as published by
    13  * the Free Software Foundation; either version 2 of the License, or
    14  * (at your option) any later version.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  */
    22 #include <sys/time.h>
    23 #include "pvr2/pvr2.h"
    25 struct vertex_rgba {
    26     float x,y,z;
    27     uint32_t argb;
    28 };
    30 struct vertex_all {
    31     float x,y,z;
    32     float u,v;
    33     float rgba[4];      /* Note - RGBA order, as preferred by GL */
    34     float spec_rgba[4];
    35 };
    37 #define FARGB_A(x) (((float)(((x)>>24)+1))/256.0)
    38 #define FARGB_R(x) (((float)((((x)>>16)&0xFF)+1))/256.0)
    39 #define FARGB_G(x) (((float)((((x)>>8)&0xFF)+1))/256.0)
    40 #define FARGB_B(x) (((float)(((x)&0xFF)+1))/256.0)
    42 /**
    43  * Compute the values for an array of vertexes, given x,y for each
    44  * vertex and the base 3-vertex triple used to define the background
    45  * plane. Essentially the base vertexes are used to find the
    46  * plane equation for each of z,a,r,g,b,etc, which is then solved for
    47  * each of the required compute vertexes (normally the corner points).
    48  *
    49  * @param base The 3 vertexes supplied as the background definition
    50  * @param compute An array of vertexes to compute. x and y must be
    51  *   preset, other values are computed.
    52  * @param num_compute number of vertexes in the compute array.
    53  */
    54 void compute_vertexes( struct vertex_rgba *base, 
    55 		       struct vertex_all *compute,
    56 		       int num_compute )
    57 {
    58     struct vertex_all center;
    59     struct vertex_all diff0, diff1;
    60     int i;
    62     center.x = base[1].x;
    63     center.y = base[1].y;
    64     center.z = base[1].z;
    65     center.rgba[0] = FARGB_R(base[1].argb);
    66     center.rgba[1] = FARGB_G(base[1].argb);
    67     center.rgba[2] = FARGB_B(base[1].argb);
    68     center.rgba[3] = FARGB_A(base[1].argb);
    69     diff0.x = base[0].x - base[1].x;
    70     diff0.y = base[0].y - base[1].y;
    71     diff0.z = base[0].z - base[1].z;
    72     diff1.x = base[2].x - base[1].x;
    73     diff1.y = base[2].y - base[1].y;
    74     diff1.z = base[2].z - base[1].z;
    75     diff0.rgba[0] = FARGB_R(base[0].argb) - center.rgba[0];
    76     diff0.rgba[1] = FARGB_G(base[0].argb) - center.rgba[1];
    77     diff0.rgba[2] = FARGB_B(base[0].argb) - center.rgba[2];
    78     diff0.rgba[3] = FARGB_A(base[0].argb) - center.rgba[3];
    79     diff1.rgba[0] = FARGB_R(base[2].argb) - center.rgba[0];
    80     diff1.rgba[1] = FARGB_G(base[2].argb) - center.rgba[1];
    81     diff1.rgba[2] = FARGB_B(base[2].argb) - center.rgba[2];
    82     diff1.rgba[3] = FARGB_A(base[2].argb) - center.rgba[3];
    84     float divisor = ((diff1.y) * (diff0.x)) - ((diff0.y) * (diff1.x));
    85     if( divisor == 0 ) {
    86 	/* The points lie on a single line - no plane for you. *shrugs* */
    87     } else {
    88 	for( i=0; i<num_compute; i++ ) {
    89 	    float t = ((compute[i].x - center.x) * diff1.y -
    90 		       (compute[i].y - center.y) * diff1.x) / divisor;
    91 	    float s = ((compute[i].y - center.y) * diff0.x -
    92 		       (compute[i].x - center.x) * diff0.y) / divisor;
    93 	    compute[i].z = center.z + (t*diff0.z) + (s*diff1.z);
    94 	    compute[i].rgba[0] = center.rgba[0] + (t*diff0.rgba[0]) + (s*diff1.rgba[0]);
    95 	    compute[i].rgba[1] = center.rgba[1] + (t*diff0.rgba[1]) + (s*diff1.rgba[1]);
    96 	    compute[i].rgba[2] = center.rgba[2] + (t*diff0.rgba[2]) + (s*diff1.rgba[2]);
    97 	    compute[i].rgba[3] = center.rgba[3] + (t*diff0.rgba[3]) + (s*diff1.rgba[3]);
    98 	}
    99     }
   100 }
   102 void render_backplane( uint32_t *polygon, uint32_t width, uint32_t height, uint32_t mode ) {
   103     struct vertex_rgba *vertex = (struct vertex_rgba *)(polygon + 3);
   104     struct vertex_all compute[4] = { {0.0,0.0}, {width,0.0}, {width, height}, {0.0,height} };
   105     int i;
   107     render_set_context(polygon, 0);
   108     compute_vertexes( vertex, compute, 4 );
   109     glBegin(GL_QUADS);
   110     for( i=0; i<4; i++ ) {
   111 	glColor4fv(compute[i].rgba);
   112 	glVertex3f(compute[i].x, compute[i].y, compute[i].z);
   113 	fprintf( stderr, "BG %d,%d: %f %f %f\n", (int)compute[i].x, (int)compute[i].y,
   114 		 compute[i].rgba[0], compute[i].rgba[1], compute[i].rgba[2] );
   115     }
   116     glEnd();
   117 }
.