2 * $Id: gl_common.c,v 1.7 2007-10-31 12:05: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 "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 };
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.
36 gboolean isGLExtensionSupported( const char *extension )
38 const GLubyte *extensions = NULL;
40 GLubyte *where, *terminator;
42 /* Extension names should not have spaces. */
43 where = (GLubyte *) strchr(extension, ' ');
44 if (where || *extension == '\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,
52 where = (GLubyte *) strstr((const char *) start, extension);
55 terminator = where + strlen(extension);
56 if (where == start || *(where - 1) == ' ')
57 if (*terminator == ' ' || *terminator == '\0')
64 gboolean hasRequiredGLExtensions( )
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] );
78 void gl_display_render_buffer( render_buffer_t buffer )
81 if( buffer->inverted ) {
82 top = ((float)buffer->height) - 0.5;
86 bottom = ((float)buffer->height) - 0.5;
89 /* Reset display parameters */
90 glViewport( 0, 0, video_width, video_height );
91 glMatrixMode(GL_PROJECTION);
93 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
94 glMatrixMode(GL_MODELVIEW);
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 );
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;
116 glVertex2f( x1, video_height );
117 glVertex2f( 0, video_height);
119 glVertex2f( video_width, 0 );
120 glVertex2f( video_width, video_height );
121 glVertex2f( x2, video_height);
123 } else if( ah < video_height ) {
124 y1 = (video_height - ah)/2;
128 glVertex2f( video_width, 0 );
129 glVertex2f( video_width, y1 );
132 glVertex2f( video_width, y2 );
133 glVertex2f( video_width, video_height );
134 glVertex2f( 0, video_height );
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 );
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 );
156 glDisable( GL_TEXTURE_RECTANGLE_ARB );
160 gboolean gl_load_frame_buffer( frame_buffer_t frame, render_buffer_t render )
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 );
174 gboolean gl_display_blank( uint32_t colour )
176 glViewport( 0, 0, video_width, video_height );
177 glMatrixMode( GL_PROJECTION );
179 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
180 glMatrixMode(GL_MODELVIEW);
182 glColor3b( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
183 glRecti(0,0, video_width, video_height );
185 frame_colour = colour;
189 void gl_redisplay_last()
191 render_buffer_t buffer = pvr2_get_front_buffer();
192 if( buffer == NULL ) {
193 gl_display_blank( frame_colour );
195 gl_display_render_buffer( buffer );
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.
204 gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer,
205 int rowstride, int colour_format )
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 );
.