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