Search
lxdream.org :: lxdream/src/drivers/video_gl.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_gl.c
changeset 854:130928a3cdcb
prev736:a02d1475ccfd
next1076:18c164e8aec4
author nkeynes
date Thu Oct 16 12:06:24 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix bound check on number of framebuffers allocated
Fix repurposing framebuffers to clear out previous attachments to prevent
dimension erros.
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Common GL code that doesn't depend on a specific implementation
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <sys/time.h>
    21 #include "display.h"
    22 #include "pvr2/pvr2.h"
    23 #include "drivers/video_gl.h"
    25 extern uint32_t video_width, video_height;
    27 /**
    28  * Reset the gl state to simple orthographic projection with 
    29  * texturing, alpha/depth/scissor/cull tests disabled.
    30  */
    31 void gl_reset_state()
    32 {
    33     glViewport( 0, 0, video_width, video_height );
    34     glMatrixMode(GL_PROJECTION);
    35     glLoadIdentity();
    36     glOrtho( 0, video_width, video_height, 0, 0, 65535 );
    37     glMatrixMode(GL_MODELVIEW);
    38     glLoadIdentity();
    39     glEnable( GL_BLEND );
    40     glDisable( GL_TEXTURE_2D );
    41     glDisable( GL_TEXTURE_RECTANGLE_ARB );
    42     glDisable( GL_ALPHA_TEST );
    43     glDisable( GL_DEPTH_TEST );
    44     glDisable( GL_SCISSOR_TEST );
    45     glDisable( GL_CULL_FACE );
    46     glDrawBuffer( GL_FRONT );
    47 }
    49 void gl_display_render_buffer( render_buffer_t buffer )
    50 {
    51     gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
    52 }
    54 /**
    55  * Convert window coordinates to dreamcast device coords (640x480) using the 
    56  * same viewable area as gl_texture_window.
    57  * If the coordinates are outside the viewable area, the result is -1,-1.
    58  */ 
    59 void gl_window_to_system_coords( int *x, int *y )
    60 {
    61     int x1=0,y1=0,x2=video_width,y2=video_height;
    63     int ah = video_width * 0.75;
    65     if( ah > video_height ) {
    66         int w = (video_height/0.75);
    67         x1 = (video_width - w)/2;
    68         x2 -= x1;
    69     } else if( ah < video_height ) {
    70         y1 = (video_height - ah)/2;
    71         y2 -= y1;
    72     }
    73     if( *x < x1 || *x >= x2 || *y < y1 || *y >= y2 ) {
    74         *x = -1;
    75         *y = -1;
    76     } else {
    77         *x = (*x - x1) * DISPLAY_WIDTH / (x2-x1);
    78         *y = (*y - y1) * DISPLAY_HEIGHT / (y2-y1);
    79     }
    80 }
    82 void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
    83 {
    84     float top, bottom;
    85     if( inverted ) {
    86         top = ((float)height);
    87         bottom = 0;
    88     } else {
    89         top = 0;
    90         bottom = ((float)height);
    91     }
    93     /* Reset display parameters */
    94     gl_reset_state();
    95     glColor3f( 0,0,0 );    
    97     int x1=0,y1=0,x2=video_width,y2=video_height;
    99     int ah = video_width * 0.75;
   101     if( ah > video_height ) {
   102         int w = (video_height/0.75);
   103         x1 = (video_width - w)/2;
   104         x2 -= x1;
   106         glBegin( GL_QUADS );
   107         glVertex2f( 0, 0 );
   108         glVertex2f( x1, 0 );
   109         glVertex2f( x1, video_height );
   110         glVertex2f( 0, video_height);
   111         glVertex2f( x2, 0 );
   112         glVertex2f( video_width, 0 );
   113         glVertex2f( video_width, video_height );
   114         glVertex2f( x2, video_height);
   115         glEnd();
   116     } else if( ah < video_height ) {
   117         y1 = (video_height - ah)/2;
   118         y2 -= y1;
   119         glBegin( GL_QUADS );
   120         glVertex2f( 0, 0 );
   121         glVertex2f( video_width, 0 );
   122         glVertex2f( video_width, y1 );
   123         glVertex2f( 0, y1 );
   124         glVertex2f( 0, y2 );
   125         glVertex2f( video_width, y2 );
   126         glVertex2f( video_width, video_height );
   127         glVertex2f( 0, video_height );
   128         glEnd();
   129     }
   131     /* Render the textured rectangle */
   132     glEnable( GL_TEXTURE_RECTANGLE_ARB );
   133     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   134     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   135     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   136     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   137     glEnable( GL_BLEND );
   138     glBlendFunc( GL_ONE, GL_ZERO );
   139     glBegin( GL_QUADS );
   140     glTexCoord2f( 0, top );
   141     glVertex2f( x1, y1 );
   142     glTexCoord2f( ((float)width), top );
   143     glVertex2f( x2, y1 );
   144     glTexCoord2f( ((float)width), bottom );
   145     glVertex2f( x2, y2 );
   146     glTexCoord2f( 0, bottom );
   147     glVertex2f( x1, y2 );
   148     glEnd();
   149     glDisable( GL_TEXTURE_RECTANGLE_ARB );
   150     glFlush();
   151 }
   153 gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
   154 {
   155     GLenum type = colour_formats[frame->colour_format].type;
   156     GLenum format = colour_formats[frame->colour_format].format;
   157     int bpp = colour_formats[frame->colour_format].bpp;
   158     int rowstride = (frame->rowstride / bpp) - frame->width;
   160     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   161     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   162     glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0,0,
   163                      frame->width, frame->height, format, type, frame->data );
   164     return TRUE;
   165 }
   167 void gl_display_blank( uint32_t colour )
   168 {
   169     gl_reset_state();
   170     glColor3ub( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
   171     glRecti(0,0, video_width, video_height );
   172     glFlush();
   173 }
   175 /**
   176  * Generic GL read_render_buffer. This function assumes that the caller
   177  * has already set the appropriate glReadBuffer(); in other words, unless
   178  * there's only one buffer this needs to be wrapped.
   179  */
   180 gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   181                                 int rowstride, int colour_format ) 
   182 {
   183     glFinish();
   184     GLenum type = colour_formats[colour_format].type;
   185     GLenum format = colour_formats[colour_format].format;
   186     // int line_size = buffer->width * colour_formats[colour_format].bpp;
   187     // int size = line_size * buffer->height;
   188     int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
   189     glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
   191     glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
   192     return TRUE;
   193 }
.