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 561:533f6b478071
prev558:428cf8528699
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
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/gl_common.h"
    25 extern uint32_t video_width, video_height;
    27 char *required_extensions[] = { "GL_EXT_framebuffer_object", NULL };
    29 /**
    30  * Test if a specific extension is supported. From opengl.org
    31  * @param extension extension name to check for
    32  * @return TRUE if supported, otherwise FALSE.
    33  */
    34 gboolean isGLExtensionSupported( const char *extension )
    35 {
    36     const GLubyte *extensions = NULL;
    37     const GLubyte *start;
    38     GLubyte *where, *terminator;
    40     /* Extension names should not have spaces. */
    41     where = (GLubyte *) strchr(extension, ' ');
    42     if (where || *extension == '\0')
    43 	return 0;
    44     extensions = glGetString(GL_EXTENSIONS);
    45     /* It takes a bit of care to be fool-proof about parsing the
    46        OpenGL extensions string. Don't be fooled by sub-strings,
    47        etc. */
    48     start = extensions;
    49     for (;;) {
    50 	where = (GLubyte *) strstr((const char *) start, extension);
    51 	if (!where)
    52 	    break;
    53 	terminator = where + strlen(extension);
    54 	if (where == start || *(where - 1) == ' ')
    55 	    if (*terminator == ' ' || *terminator == '\0')
    56 		return TRUE;
    57 	start = terminator;
    58     }
    59     return FALSE;
    60 }
    62 gboolean hasRequiredGLExtensions( ) 
    63 {
    64     int i;
    65     gboolean isOK = TRUE;
    67     for( i=0; required_extensions[i] != NULL; i++ ) {
    68 	if( !isGLExtensionSupported(required_extensions[i]) ) {
    69 	    ERROR( "Required OpenGL extension not supported: %s", required_extensions[i] );
    70 	    isOK = FALSE;
    71 	}
    72     }
    73     return isOK;
    74 }
    76 /**
    77  * Reset the gl state to simple orthographic projection with 
    78  * texturing, alpha/depth/scissor/cull tests disabled.
    79  */
    80 void gl_reset_state()
    81 {
    82     glViewport( 0, 0, video_width, video_height );
    83     glMatrixMode(GL_PROJECTION);
    84     glLoadIdentity();
    85     glOrtho( 0, video_width, video_height, 0, 0, -65535 );
    86     glMatrixMode(GL_MODELVIEW);
    87     glLoadIdentity();
    88     glDisable( GL_TEXTURE_2D );
    89     glDisable( GL_ALPHA_TEST );
    90     glDisable( GL_DEPTH_TEST );
    91     glDisable( GL_SCISSOR_TEST );
    92     glDisable( GL_CULL_FACE );
    93     glDrawBuffer( GL_FRONT );
    94 }
    96 void gl_display_render_buffer( render_buffer_t buffer )
    97 {
    98     gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
    99 }
   101 void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
   102 {
   103     float top, bottom;
   104     if( inverted ) {
   105 	top = ((float)height);
   106 	bottom = 0;
   107     } else {
   108 	top = 0;
   109 	bottom = ((float)height);
   110     }
   112     /* Reset display parameters */
   113     gl_reset_state();
   114     glColor3f( 0,0,0 );    
   116     int x1=0,y1=0,x2=video_width,y2=video_height;
   118     int ah = video_width * 0.75;
   120     if( ah > video_height ) {
   121 	int w = (video_height/0.75);
   122 	x1 = (video_width - w)/2;
   123 	x2 -= x1;
   125 	glBegin( GL_QUADS );
   126 	glVertex2f( 0, 0 );
   127 	glVertex2f( x1, 0 );
   128 	glVertex2f( x1, video_height );
   129 	glVertex2f( 0, video_height);
   130 	glVertex2f( x2, 0 );
   131 	glVertex2f( video_width, 0 );
   132 	glVertex2f( video_width, video_height );
   133 	glVertex2f( x2, video_height);
   134 	glEnd();
   135     } else if( ah < video_height ) {
   136 	y1 = (video_height - ah)/2;
   137 	y2 -= y1;
   138 	glBegin( GL_QUADS );
   139 	glVertex2f( 0, 0 );
   140 	glVertex2f( video_width, 0 );
   141 	glVertex2f( video_width, y1 );
   142 	glVertex2f( 0, y1 );
   143 	glVertex2f( 0, y2 );
   144 	glVertex2f( video_width, y2 );
   145 	glVertex2f( video_width, video_height );
   146 	glVertex2f( 0, video_height );
   147 	glEnd();
   148     }
   150     /* Render the textured rectangle */
   151     glEnable( GL_TEXTURE_RECTANGLE_ARB );
   152     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   153     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   154     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   155     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   156     glEnable( GL_BLEND );
   157     glBlendFunc( GL_ONE, GL_ZERO );
   158     glBegin( GL_QUADS );
   159     glTexCoord2f( 0, top );
   160     glVertex2f( x1, y1 );
   161     glTexCoord2f( ((float)width), top );
   162     glVertex2f( x2, y1 );
   163     glTexCoord2f( ((float)width), bottom );
   164     glVertex2f( x2, y2 );
   165     glTexCoord2f( 0, bottom );
   166     glVertex2f( x1, y2 );
   167     glEnd();
   168     glDisable( GL_TEXTURE_RECTANGLE_ARB );
   169     glFlush();
   170 }
   172 gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
   173 {
   174     GLenum type = colour_formats[frame->colour_format].type;
   175     GLenum format = colour_formats[frame->colour_format].format;
   176     int bpp = colour_formats[frame->colour_format].bpp;
   177     int rowstride = (frame->rowstride / bpp) - frame->width;
   179     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   180     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   181     glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0,0,
   182 		     frame->width, frame->height, format, type, frame->data );
   183     return TRUE;
   184 }
   186 gboolean gl_display_blank( uint32_t colour )
   187 {
   188     gl_reset_state();
   189     glColor3ub( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
   190     glRecti(0,0, video_width, video_height );
   191     glFlush();
   192     return TRUE;
   193 }
   195 /**
   196  * Generic GL read_render_buffer. This function assumes that the caller
   197  * has already set the appropriate glReadBuffer(); in other words, unless
   198  * there's only one buffer this needs to be wrapped.
   199  */
   200 gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   201 				int rowstride, int colour_format ) 
   202 {
   203     glFinish();
   204     GLenum type = colour_formats[colour_format].type;
   205     GLenum format = colour_formats[colour_format].format;
   206     // int line_size = buffer->width * colour_formats[colour_format].bpp;
   207     // int size = line_size * buffer->height;
   208     int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
   209     glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
   211     glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
   212     return TRUE;
   213 }
.