2 * $Id: gl_common.c,v 1.6 2007-10-31 09:10:23 nkeynes Exp $
4 * Common GL code that doesn't depend on a specific implementation
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
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 };
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.
35 gboolean isGLExtensionSupported( const char *extension )
37 const GLubyte *extensions = NULL;
39 GLubyte *where, *terminator;
41 /* Extension names should not have spaces. */
42 where = (GLubyte *) strchr(extension, ' ');
43 if (where || *extension == '\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,
51 where = (GLubyte *) strstr((const char *) start, extension);
54 terminator = where + strlen(extension);
55 if (where == start || *(where - 1) == ' ')
56 if (*terminator == ' ' || *terminator == '\0')
63 gboolean hasRequiredGLExtensions( )
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] );
77 void gl_display_render_buffer( render_buffer_t buffer )
80 if( buffer->inverted ) {
81 top = ((float)buffer->height) - 0.5;
85 bottom = ((float)buffer->height) - 0.5;
88 /* Reset display parameters */
89 glViewport( 0, 0, video_width, video_height );
90 glMatrixMode(GL_PROJECTION);
92 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
93 glMatrixMode(GL_MODELVIEW);
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 );
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;
115 glVertex2f( x1, video_height );
116 glVertex2f( 0, video_height);
118 glVertex2f( video_width, 0 );
119 glVertex2f( video_width, video_height );
120 glVertex2f( x2, video_height);
122 } else if( ah < video_height ) {
123 y1 = (video_height - ah)/2;
127 glVertex2f( video_width, 0 );
128 glVertex2f( video_width, y1 );
131 glVertex2f( video_width, y2 );
132 glVertex2f( video_width, video_height );
133 glVertex2f( 0, video_height );
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 );
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 );
155 glDisable( GL_TEXTURE_RECTANGLE_ARB );
159 gboolean gl_load_frame_buffer( frame_buffer_t frame, render_buffer_t render )
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 );
172 gboolean gl_display_blank( uint32_t colour )
174 glViewport( 0, 0, video_width, video_height );
175 glMatrixMode( GL_PROJECTION );
177 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
178 glMatrixMode(GL_MODELVIEW);
180 glColor3b( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
181 glRecti(0,0, video_width, video_height );
183 frame_colour = colour;
187 void gl_redisplay_last()
189 render_buffer_t buffer = pvr2_get_front_buffer();
190 if( buffer == NULL ) {
191 gl_display_blank( frame_colour );
193 gl_display_render_buffer( buffer );
198 * Generic GL read_render_buffer. This function assumes that the caller
199 * has already set the appropriate glReadBuffer(); in other words, unless
200 * there's only one buffer this needs to be wrapped.
202 gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer,
203 int rowstride, int colour_format )
206 GLenum type = colour_formats[colour_format].type;
207 GLenum format = colour_formats[colour_format].format;
208 // int line_size = buffer->width * colour_formats[colour_format].bpp;
209 // int size = line_size * buffer->height;
210 int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
211 glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
213 glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
.