filename | src/drivers/gl_common.c |
changeset | 429:e581b90c3fb3 |
prev | 424:421d68e78c46 |
next | 443:1163eb5c0590 |
author | nkeynes |
date | Thu Oct 11 08:22:03 2007 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Add speed meter to main window Add sunken border around video area Add debugger menu item Move logging out to util.c |
view | annotate | diff | log | raw |
1 /**
2 * $Id: gl_common.c,v 1.3 2007-10-08 11:49:35 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 <GL/gl.h>
20 #include "dream.h"
21 #include "drivers/gl_common.h"
23 extern uint32_t video_width, video_height;
25 char *required_extensions[] = { "GL_EXT_framebuffer_object", NULL };
27 /**
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.
31 */
32 gboolean isGLExtensionSupported( const char *extension )
33 {
34 const GLubyte *extensions = NULL;
35 const GLubyte *start;
36 GLubyte *where, *terminator;
38 /* Extension names should not have spaces. */
39 where = (GLubyte *) strchr(extension, ' ');
40 if (where || *extension == '\0')
41 return 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,
45 etc. */
46 start = extensions;
47 for (;;) {
48 where = (GLubyte *) strstr((const char *) start, extension);
49 if (!where)
50 break;
51 terminator = where + strlen(extension);
52 if (where == start || *(where - 1) == ' ')
53 if (*terminator == ' ' || *terminator == '\0')
54 return TRUE;
55 start = terminator;
56 }
57 return FALSE;
58 }
60 gboolean hasRequiredGLExtensions( )
61 {
62 int i;
63 gboolean isOK = TRUE;
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] );
68 isOK = FALSE;
69 }
70 }
71 return isOK;
72 }
75 gboolean gl_display_frame_buffer( frame_buffer_t frame )
76 {
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);
83 glLoadIdentity();
84 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
85 glMatrixMode(GL_MODELVIEW);
86 glLoadIdentity();
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;
99 /*
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 );
104 glBegin( GL_QUADS );
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 );
113 glEnd();
114 glDeleteTextures( 1, &texid );
115 */
116 glRasterPos2i( 0, 0 );
117 glPixelZoom( 1.0f, -scale );
118 glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
119 glDrawPixels( frame->width, frame->height, format, type,
120 frame->data );
122 glFlush();
123 return TRUE;
124 }
126 gboolean gl_display_blank( uint32_t colour )
127 {
128 glViewport( 0, 0, video_width, video_height );
129 glMatrixMode( GL_PROJECTION );
130 glLoadIdentity();
131 glOrtho( 0, video_width, video_height, 0, 0, -65535 );
132 glMatrixMode(GL_MODELVIEW);
133 glLoadIdentity();
134 glColor3b( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
135 glRecti(0,0, video_width, video_height );
136 glFlush();
137 return TRUE;
138 }
140 /**
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.
144 */
145 gboolean gl_read_render_buffer( render_buffer_t buffer, unsigned char *target )
146 {
147 if( buffer->address == -1 )
148 return FALSE;
149 glFinish();
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 );
158 return TRUE;
159 }
.