Search
lxdream.org :: lxdream/src/drivers/gl_common.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/gl_common.c
changeset 481:3b2d6c5a19ad
prev477:9a373f2ff009
next540:a3767018a96d
author nkeynes
date Wed Oct 31 12:05:23 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix miscellaneous warnings
view annotate diff log raw
     1 /**
     2  * $Id: gl_common.c,v 1.7 2007-10-31 12:05:23 nkeynes Exp $
     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 <GL/gl.h>
    22 #include "dream.h"
    23 #include "pvr2/pvr2.h"
    24 #include "drivers/gl_common.h"
    26 extern uint32_t video_width, video_height;
    27 static uint32_t frame_colour = 0;
    29 char *required_extensions[] = { "GL_EXT_framebuffer_object", NULL };
    31 /**
    32  * Test if a specific extension is supported. From opengl.org
    33  * @param extension extension name to check for
    34  * @return TRUE if supported, otherwise FALSE.
    35  */
    36 gboolean isGLExtensionSupported( const char *extension )
    37 {
    38     const GLubyte *extensions = NULL;
    39     const GLubyte *start;
    40     GLubyte *where, *terminator;
    42     /* Extension names should not have spaces. */
    43     where = (GLubyte *) strchr(extension, ' ');
    44     if (where || *extension == '\0')
    45 	return 0;
    46     extensions = glGetString(GL_EXTENSIONS);
    47     /* It takes a bit of care to be fool-proof about parsing the
    48        OpenGL extensions string. Don't be fooled by sub-strings,
    49        etc. */
    50     start = extensions;
    51     for (;;) {
    52 	where = (GLubyte *) strstr((const char *) start, extension);
    53 	if (!where)
    54 	    break;
    55 	terminator = where + strlen(extension);
    56 	if (where == start || *(where - 1) == ' ')
    57 	    if (*terminator == ' ' || *terminator == '\0')
    58 		return TRUE;
    59 	start = terminator;
    60     }
    61     return FALSE;
    62 }
    64 gboolean hasRequiredGLExtensions( ) 
    65 {
    66     int i;
    67     gboolean isOK = TRUE;
    69     for( i=0; required_extensions[i] != NULL; i++ ) {
    70 	if( !isGLExtensionSupported(required_extensions[i]) ) {
    71 	    ERROR( "Required OpenGL extension not supported: %s", required_extensions[i] );
    72 	    isOK = FALSE;
    73 	}
    74     }
    75     return isOK;
    76 }
    78 void gl_display_render_buffer( render_buffer_t buffer )
    79 {
    80     float top, bottom;
    81     if( buffer->inverted ) {
    82 	top = ((float)buffer->height) - 0.5;
    83 	bottom = 0.5;
    84     } else {
    85 	top = 0.5;
    86 	bottom = ((float)buffer->height) - 0.5;
    87     }
    89     /* Reset display parameters */
    90     glViewport( 0, 0, video_width, video_height );
    91     glMatrixMode(GL_PROJECTION);
    92     glLoadIdentity();
    93     glOrtho( 0, video_width, video_height, 0, 0, -65535 );
    94     glMatrixMode(GL_MODELVIEW);
    95     glLoadIdentity();
    96     glDisable( GL_TEXTURE_2D );
    97     glDisable( GL_ALPHA_TEST );
    98     glDisable( GL_DEPTH_TEST );
    99     glDisable( GL_SCISSOR_TEST );
   100     glDisable( GL_CULL_FACE );
   101     glColor3f( 0,0,0 );
   104     int x1=0,y1=0,x2=video_width,y2=video_height;
   106     int ah = video_width * 0.75;
   108     if( ah > video_height ) {
   109 	int w = (video_height/0.75);
   110 	x1 = (video_width - w)/2;
   111 	x2 -= x1;
   113 	glBegin( GL_QUADS );
   114 	glVertex2f( 0, 0 );
   115 	glVertex2f( x1, 0 );
   116 	glVertex2f( x1, video_height );
   117 	glVertex2f( 0, video_height);
   118 	glVertex2f( x2, 0 );
   119 	glVertex2f( video_width, 0 );
   120 	glVertex2f( video_width, video_height );
   121 	glVertex2f( x2, video_height);
   122 	glEnd();
   123     } else if( ah < video_height ) {
   124 	y1 = (video_height - ah)/2;
   125 	y2 -= y1;
   126 	glBegin( GL_QUADS );
   127 	glVertex2f( 0, 0 );
   128 	glVertex2f( video_width, 0 );
   129 	glVertex2f( video_width, y1 );
   130 	glVertex2f( 0, y1 );
   131 	glVertex2f( 0, y2 );
   132 	glVertex2f( video_width, y2 );
   133 	glVertex2f( video_width, video_height );
   134 	glVertex2f( 0, video_height );
   135 	glEnd();
   136     }
   138     /* Render the textured rectangle */
   139     glEnable( GL_TEXTURE_RECTANGLE_ARB );
   140     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, buffer->buf_id );
   141     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   142     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   143     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   144     glEnable( GL_BLEND );
   145     glBlendFunc( GL_ONE, GL_ZERO );
   146     glBegin( GL_QUADS );
   147     glTexCoord2f( 0.5, top );
   148     glVertex2f( x1, y1 );
   149     glTexCoord2f( ((float)buffer->width)-0.5, top );
   150     glVertex2f( x2, y1 );
   151     glTexCoord2f( ((float)buffer->width)-0.5, bottom );
   152     glVertex2f( x2, y2 );
   153     glTexCoord2f( 0.5, bottom );
   154     glVertex2f( x1, y2 );
   155     glEnd();
   156     glDisable( GL_TEXTURE_RECTANGLE_ARB );
   157     glFlush();
   158 }
   160 gboolean gl_load_frame_buffer( frame_buffer_t frame, render_buffer_t render )
   161 {
   162     GLenum type = colour_formats[frame->colour_format].type;
   163     GLenum format = colour_formats[frame->colour_format].format;
   164     int bpp = colour_formats[frame->colour_format].bpp;
   165     int rowstride = (frame->rowstride / bpp) - frame->width;
   167     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   168     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, render->buf_id );
   169     glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0,0,
   170 		     frame->width, frame->height, format, type, frame->data );
   171     return TRUE;
   172 }
   174 gboolean gl_display_blank( uint32_t colour )
   175 {
   176     glViewport( 0, 0, video_width, video_height );
   177     glMatrixMode( GL_PROJECTION );
   178     glLoadIdentity();
   179     glOrtho( 0, video_width, video_height, 0, 0, -65535 );
   180     glMatrixMode(GL_MODELVIEW);
   181     glLoadIdentity();
   182     glColor3b( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
   183     glRecti(0,0, video_width, video_height );
   184     glFlush();
   185     frame_colour = colour;
   186     return TRUE;
   187 }
   189 void gl_redisplay_last()
   190 {
   191     render_buffer_t buffer = pvr2_get_front_buffer();
   192     if( buffer == NULL ) {
   193 	gl_display_blank( frame_colour );
   194     } else {
   195 	gl_display_render_buffer( buffer );
   196     }
   197 }
   199 /**
   200  * Generic GL read_render_buffer. This function assumes that the caller
   201  * has already set the appropriate glReadBuffer(); in other words, unless
   202  * there's only one buffer this needs to be wrapped.
   203  */
   204 gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   205 				int rowstride, int colour_format ) 
   206 {
   207     glFinish();
   208     GLenum type = colour_formats[colour_format].type;
   209     GLenum format = colour_formats[colour_format].format;
   210     // int line_size = buffer->width * colour_formats[colour_format].bpp;
   211     // int size = line_size * buffer->height;
   212     int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
   213     glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
   215     glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
   216     return TRUE;
   217 }
.