2 * $Id: gl_common.c,v 1.3 2007-10-08 11:49:35 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.
21 #include "drivers/gl_common.h"
23 extern uint32_t video_width, video_height;
25 char *required_extensions[] = { "GL_EXT_framebuffer_object", NULL };
28 * Test if a specific extension is supported. From opengl.org
29 * @param extension extension name to check for
30 * @return TRUE if supported, otherwise FALSE.
32 gboolean isGLExtensionSupported( const char *extension )
34 const GLubyte *extensions = NULL;
36 GLubyte *where, *terminator;
38 /* Extension names should not have spaces. */
39 where = (GLubyte *) strchr(extension, ' ');
40 if (where || *extension == '\0')
42 extensions = glGetString(GL_EXTENSIONS);
43 /* It takes a bit of care to be fool-proof about parsing the
44 OpenGL extensions string. Don't be fooled by sub-strings,
48 where = (GLubyte *) strstr((const char *) start, extension);
51 terminator = where + strlen(extension);
52 if (where == start || *(where - 1) == ' ')
53 if (*terminator == ' ' || *terminator == '\0')
60 gboolean hasRequiredGLExtensions( )
65 for( i=0; required_extensions[i] != NULL; i++ ) {
66 if( !isGLExtensionSupported(required_extensions[i]) ) {
67 ERROR( "Required OpenGL extension not supported: %s", required_extensions[i] );
75 gboolean gl_display_frame_buffer( frame_buffer_t frame )
77 GLenum type = colour_formats[frame->colour_format].type;
78 GLenum format = colour_formats[frame->colour_format].format;
79 int bpp = colour_formats[frame->colour_format].bpp;
81 glViewport( 0, 0, video_width, video_height );
82 glMatrixMode(GL_PROJECTION);
84 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
85 glMatrixMode(GL_MODELVIEW);
88 /* Disable everything */
89 glDisable( GL_TEXTURE_2D );
90 glDisable( GL_ALPHA_TEST );
91 glDisable( GL_DEPTH_TEST );
92 glDisable( GL_SCISSOR_TEST );
93 glDisable( GL_CULL_FACE );
95 float scale = ((float)video_height) / frame->height;
96 int rowstride = (frame->rowstride / bpp) - frame->width;
100 glGenTextures( 1, &texid );
101 glBindTexture( GL_TEXTURE_2D, texid );
102 glTexImage2D( GL_TEXTURE_2D, 0, colour_formats[frame->colour_format].int_format,
103 frame->width, frame->height, 0, format, type, frame->data );
105 glTexCoord2i( 0, 1.0 );
106 glVertex2f( 0.0, 0.0 );
107 glTexCoord2i( 1.0, 1.0 );
108 glVertex2f( frame->width, 0.0 );
109 glTexCoord2i( 1.0, 0.0 );
110 glVertex2f( frame->width, frame->height );
111 glTexCoord2i( 0, 0.0 );
112 glVertex2f( 0.0, frame->height );
114 glDeleteTextures( 1, &texid );
116 glRasterPos2i( 0, 0 );
117 glPixelZoom( 1.0f, -scale );
118 glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
119 glDrawPixels( frame->width, frame->height, format, type,
126 gboolean gl_display_blank( uint32_t colour )
128 glViewport( 0, 0, video_width, video_height );
129 glMatrixMode( GL_PROJECTION );
131 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
132 glMatrixMode(GL_MODELVIEW);
134 glColor3b( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
135 glRecti(0,0, video_width, video_height );
141 * Generic GL read_render_buffer. This function assumes that the caller
142 * has already set the appropriate glReadBuffer(); in other words, unless
143 * there's only one buffer this needs to be wrapped.
145 gboolean gl_read_render_buffer( render_buffer_t buffer, unsigned char *target )
147 if( buffer->address == -1 )
150 GLenum type = colour_formats[buffer->colour_format].type;
151 GLenum format = colour_formats[buffer->colour_format].format;
152 // int line_size = buffer->width * colour_formats[buffer->colour_format].bpp;
153 // int size = line_size * buffer->height;
154 // int rowstride = (buffer->rowstride / colour_formats[buffer->colour_format].bpp) - buffer->width;
155 // glPixelStorei( GL_PACK_ROW_LENGTH, rowstride );
157 glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
.