Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 639:162ee7614b60
next645:a7392098299c
author nkeynes
date Mon Feb 18 09:21:43 2008 +0000 (12 years ago)
branchlxdream-render
permissions -rw-r--r--
last change More render WIP - initial glrender.c
file annotate diff log raw
nkeynes@639
     1
/**
nkeynes@639
     2
 * $Id$
nkeynes@639
     3
 *
nkeynes@639
     4
 * Standard OpenGL rendering engine. 
nkeynes@639
     5
 *
nkeynes@639
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@639
     7
 *
nkeynes@639
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@639
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@639
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@639
    11
 * (at your option) any later version.
nkeynes@639
    12
 *
nkeynes@639
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@639
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@639
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@639
    16
 * GNU General Public License for more details.
nkeynes@639
    17
 */
nkeynes@639
    18
nkeynes@639
    19
#include "display.h"
nkeynes@639
    20
#include "pvr2/pvr2.h"
nkeynes@639
    21
#include "pvr2/scene.h"
nkeynes@639
    22
nkeynes@639
    23
/**
nkeynes@639
    24
 * Clip the tile bounds to the clipping plane. 
nkeynes@639
    25
 * @return TRUE if the tile was not clipped completely.
nkeynes@639
    26
 */
nkeynes@639
    27
static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
nkeynes@639
    28
{
nkeynes@639
    29
    if( tile[0] < clip[0] ) tile[0] = clip[0];
nkeynes@639
    30
    if( tile[1] > clip[1] ) tile[1] = clip[1];
nkeynes@639
    31
    if( tile[2] < clip[2] ) tile[2] = clip[2];
nkeynes@639
    32
    if( tile[3] > clip[3] ) tile[3] = clip[3];
nkeynes@639
    33
    return tile[0] < tile[1] && tile[2] < tile[3];
nkeynes@639
    34
}
nkeynes@639
    35
nkeynes@639
    36
/**
nkeynes@639
    37
 * Once-off call to setup the OpenGL context.
nkeynes@639
    38
 */
nkeynes@639
    39
void pvr2_setup_gl_context()
nkeynes@639
    40
{
nkeynes@639
    41
    texcache_gl_init(); // Allocate texture IDs
nkeynes@639
    42
    glShadeModel(GL_SMOOTH);
nkeynes@639
    43
    glCullFace( GL_BACK );
nkeynes@639
    44
    glEnable( GL_BLEND );
nkeynes@639
    45
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@639
    46
    glMatrixMode(GL_MODELVIEW);
nkeynes@639
    47
    glLoadIdentity();
nkeynes@639
    48
nkeynes@639
    49
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@639
    50
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@639
    51
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@639
    52
    glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
nkeynes@639
    53
nkeynes@639
    54
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@639
    55
    glClearDepth(0);
nkeynes@639
    56
    glClearStencil(0);
nkeynes@639
    57
}
nkeynes@639
    58
nkeynes@639
    59
static void gl_render_poly( struct polygon_struct *poly )
nkeynes@639
    60
{
nkeynes@639
    61
    render_set_context( poly->context, RENDER_NORMAL );
nkeynes@639
    62
    glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@639
    63
}
nkeynes@639
    64
nkeynes@639
    65
static void gl_render_tilelist( pvraddr_t tile_entry )
nkeynes@639
    66
{
nkeynes@639
    67
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@639
    68
    int strip_count;
nkeynes@639
    69
    struct polygon_struct *poly;
nkeynes@639
    70
nkeynes@639
    71
    while(1) {
nkeynes@639
    72
	uint32_t entry = *tile_list++;
nkeynes@639
    73
	switch( entry >> 28 ) {
nkeynes@639
    74
	case 0x0F:
nkeynes@639
    75
	    return; // End-of-list
nkeynes@639
    76
	case 0x0E:
nkeynes@639
    77
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@639
    78
	    break;
nkeynes@639
    79
	case 0x08:
nkeynes@639
    80
	case 0x09:
nkeynes@639
    81
	case 0x0A:
nkeynes@639
    82
	case 0x0B:
nkeynes@639
    83
	    strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@639
    84
	    poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@639
    85
	    while( strip_count > 0 ) {
nkeynes@639
    86
		gl_render_poly( poly );
nkeynes@639
    87
		poly = poly->next;
nkeynes@639
    88
		strip_count--;
nkeynes@639
    89
	    }
nkeynes@639
    90
	    break;
nkeynes@639
    91
	default:
nkeynes@639
    92
	    poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@639
    93
	    gl_render_poly( poly );
nkeynes@639
    94
	}
nkeynes@639
    95
    }	    
nkeynes@639
    96
}
nkeynes@639
    97
nkeynes@639
    98
nkeynes@639
    99
/**
nkeynes@639
   100
 * Render the currently defined scene in pvr2_scene
nkeynes@639
   101
 */
nkeynes@639
   102
void pvr2_scene_render( render_buffer_t buffer )
nkeynes@639
   103
{
nkeynes@639
   104
    /* Scene setup */
nkeynes@639
   105
    display_driver->set_render_target(buffer);
nkeynes@639
   106
    pvr2_check_palette_changed();
nkeynes@639
   107
nkeynes@639
   108
    /* Setup view projection matrix */
nkeynes@639
   109
    glMatrixMode(GL_PROJECTION);
nkeynes@639
   110
    glLoadIdentity();
nkeynes@639
   111
    float nearz = pvr2_scene.bounds[4];
nkeynes@639
   112
    float farz = pvr2_scene.bounds[5];
nkeynes@639
   113
    if( nearz == farz ) {
nkeynes@639
   114
	farz*= 2.0;
nkeynes@639
   115
    }
nkeynes@639
   116
    glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
nkeynes@639
   117
	     -nearz, -farz );
nkeynes@639
   118
nkeynes@639
   119
    /* Clear the buffer (FIXME: May not want always want to do this) */
nkeynes@639
   120
    glDisable( GL_SCISSOR_TEST );
nkeynes@639
   121
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
nkeynes@639
   122
nkeynes@639
   123
    /* Setup vertex array pointers */
nkeynes@639
   124
    glInterleavedArrays(GL_T2F_C4UB_V3F, sizeof(struct vertex_struct), pvr2_scene.vertex_array);
nkeynes@639
   125
    glSecondaryColorPointerEXT(4, GL_UNSIGNED_BYTE, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba );
nkeynes@639
   126
nkeynes@639
   127
    uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
nkeynes@639
   128
    uint32_t *bgplane = pvr2_scene.pvr2_pbuf + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
nkeynes@639
   129
    render_backplane( bgplane, pvr2_scene.buffer_width, pvr2_scene.buffer_height, bgplane_mode );
nkeynes@639
   130
    
nkeynes@639
   131
    glEnable( GL_SCISSOR_TEST );
nkeynes@639
   132
nkeynes@639
   133
    /* Process the segment list */
nkeynes@639
   134
    struct tile_segment *segment = pvr2_scene.segment_list;
nkeynes@639
   135
    do {
nkeynes@639
   136
	int tilex = SEGMENT_X(segment->control);
nkeynes@639
   137
	int tiley = SEGMENT_Y(segment->control);
nkeynes@639
   138
	
nkeynes@639
   139
	int tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
nkeynes@639
   140
	if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
nkeynes@639
   141
	    continue; // fully clipped, skip tile
nkeynes@639
   142
	}
nkeynes@639
   143
nkeynes@639
   144
	/* Clip to the visible part of the tile */
nkeynes@639
   145
	glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
nkeynes@639
   146
		   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
nkeynes@639
   147
	if( IS_TILE_PTR(segment->opaque_ptr) ) {
nkeynes@639
   148
	    gl_render_tilelist(segment->opaque_ptr);
nkeynes@639
   149
	}
nkeynes@639
   150
	if( IS_TILE_PTR(segment->trans_ptr) ) {
nkeynes@639
   151
	    gl_render_tilelist(segment->trans_ptr);
nkeynes@639
   152
	}
nkeynes@639
   153
	if( IS_TILE_PTR(segment->punchout_ptr) ) {
nkeynes@639
   154
	    gl_render_tilelist(segment->punchout_ptr);
nkeynes@639
   155
	}
nkeynes@639
   156
    } while( !IS_LAST_SEGMENT(segment++) );
nkeynes@639
   157
    glDisable( GL_SCISSOR_TEST );
nkeynes@639
   158
nkeynes@639
   159
}
.