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