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 352:f0df7a6d4703
next424:421d68e78c46
author nkeynes
date Sun Feb 11 10:09:32 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Bug 27: Implement opengl framebuffer objects
Rewrite much of the final video output stage. Now uses generic "render
buffers", implemented on GL using framebuffer objects + textures.
view annotate diff log raw
     1 /**
     2  * $Id: gl_common.c,v 1.1 2007-02-11 10:09:32 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;
    80     GLint texid;
    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();
    89     /* Disable everything */
    90     glDisable( GL_TEXTURE_2D );
    91     glDisable( GL_ALPHA_TEST );
    92     glDisable( GL_DEPTH_TEST );
    93     glDisable( GL_SCISSOR_TEST );
    94     glDisable( GL_CULL_FACE );
    96     float scale = ((float)video_height) / frame->height;
    97     int rowstride = (frame->rowstride / bpp) - frame->width;
   100     /*
   101     glGenTextures( 1, &texid );
   102     glBindTexture( GL_TEXTURE_2D, texid );
   103     glTexImage2D( GL_TEXTURE_2D, 0, colour_formats[frame->colour_format].int_format,
   104 		  frame->width, frame->height, 0, format, type, frame->data );
   105     glBegin( GL_QUADS );
   106     glTexCoord2i( 0, 1.0 );
   107     glVertex2f( 0.0, 0.0 );
   108     glTexCoord2i( 1.0, 1.0 );
   109     glVertex2f( frame->width, 0.0 );
   110     glTexCoord2i( 1.0, 0.0 );
   111     glVertex2f( frame->width, frame->height );
   112     glTexCoord2i( 0, 0.0 );
   113     glVertex2f( 0.0, frame->height );
   114     glEnd();
   115     glDeleteTextures( 1, &texid );
   116     */
   117     glRasterPos2i( 0, 0 );
   118     glPixelZoom( 1.0f, -scale );
   119     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   120     glDrawPixels( frame->width, frame->height, format, type,
   121 		  frame->data );
   123     glFlush();
   124     return TRUE;
   125 }
   127 gboolean gl_display_blank( uint32_t colour )
   128 {
   129     glViewport( 0, 0, video_width, video_height );
   130     glMatrixMode( GL_PROJECTION );
   131     glLoadIdentity();
   132     glOrtho( 0, video_width, video_height, 0, 0, -65535 );
   133     glMatrixMode(GL_MODELVIEW);
   134     glLoadIdentity();
   135     glColor3b( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
   136     glRecti(0,0, video_width, video_height );
   137     glFlush();
   138     return TRUE;
   139 }
   141 /**
   142  * Generic GL read_render_buffer. This function assumes that the caller
   143  * has already set the appropriate glReadBuffer(); in other words, unless
   144  * there's only one buffer this needs to be wrapped.
   145  */
   146 gboolean gl_read_render_buffer( render_buffer_t buffer, char *target ) 
   147 {
   148     if( buffer->address == -1 )
   149 	return FALSE;
   150     glFinish();
   151     GLenum type = colour_formats[buffer->colour_format].type;
   152     GLenum format = colour_formats[buffer->colour_format].format;
   153     int line_size = buffer->width * colour_formats[buffer->colour_format].bpp;
   154     int size = line_size * buffer->height;
   155     int rowstride = (buffer->rowstride / colour_formats[buffer->colour_format].bpp) - buffer->width;
   156     // glPixelStorei( GL_PACK_ROW_LENGTH, rowstride );
   158     glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
   159     return TRUE;
   160 }
.