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 424:421d68e78c46
prev352:f0df7a6d4703
next429:e581b90c3fb3
author nkeynes
date Sun Oct 07 05:42:25 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
file annotate diff log raw
nkeynes@352
     1
/**
nkeynes@424
     2
 * $Id: gl_common.c,v 1.2 2007-10-07 05:42:25 nkeynes Exp $
nkeynes@352
     3
 *
nkeynes@352
     4
 * Common GL code that doesn't depend on a specific implementation
nkeynes@352
     5
 *
nkeynes@352
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@352
     7
 *
nkeynes@352
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@352
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@352
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@352
    11
 * (at your option) any later version.
nkeynes@352
    12
 *
nkeynes@352
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@352
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@352
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@352
    16
 * GNU General Public License for more details.
nkeynes@352
    17
 */
nkeynes@352
    18
nkeynes@352
    19
#include <GL/gl.h>
nkeynes@352
    20
#include "dream.h"
nkeynes@352
    21
#include "drivers/gl_common.h"
nkeynes@352
    22
nkeynes@352
    23
extern uint32_t video_width, video_height;
nkeynes@352
    24
nkeynes@352
    25
char *required_extensions[] = { "GL_EXT_framebuffer_object", NULL };
nkeynes@352
    26
nkeynes@352
    27
/**
nkeynes@352
    28
 * Test if a specific extension is supported. From opengl.org
nkeynes@352
    29
 * @param extension extension name to check for
nkeynes@352
    30
 * @return TRUE if supported, otherwise FALSE.
nkeynes@352
    31
 */
nkeynes@352
    32
gboolean isGLExtensionSupported( const char *extension )
nkeynes@352
    33
{
nkeynes@352
    34
    const GLubyte *extensions = NULL;
nkeynes@352
    35
    const GLubyte *start;
nkeynes@352
    36
    GLubyte *where, *terminator;
nkeynes@352
    37
nkeynes@352
    38
    /* Extension names should not have spaces. */
nkeynes@352
    39
    where = (GLubyte *) strchr(extension, ' ');
nkeynes@352
    40
    if (where || *extension == '\0')
nkeynes@352
    41
	return 0;
nkeynes@352
    42
    extensions = glGetString(GL_EXTENSIONS);
nkeynes@352
    43
    /* It takes a bit of care to be fool-proof about parsing the
nkeynes@352
    44
       OpenGL extensions string. Don't be fooled by sub-strings,
nkeynes@352
    45
       etc. */
nkeynes@352
    46
    start = extensions;
nkeynes@352
    47
    for (;;) {
nkeynes@352
    48
	where = (GLubyte *) strstr((const char *) start, extension);
nkeynes@352
    49
	if (!where)
nkeynes@352
    50
	    break;
nkeynes@352
    51
	terminator = where + strlen(extension);
nkeynes@352
    52
	if (where == start || *(where - 1) == ' ')
nkeynes@352
    53
	    if (*terminator == ' ' || *terminator == '\0')
nkeynes@352
    54
		return TRUE;
nkeynes@352
    55
	start = terminator;
nkeynes@352
    56
    }
nkeynes@352
    57
    return FALSE;
nkeynes@352
    58
}
nkeynes@352
    59
nkeynes@352
    60
gboolean hasRequiredGLExtensions( ) 
nkeynes@352
    61
{
nkeynes@352
    62
    int i;
nkeynes@352
    63
    gboolean isOK = TRUE;
nkeynes@352
    64
nkeynes@352
    65
    for( i=0; required_extensions[i] != NULL; i++ ) {
nkeynes@352
    66
	if( !isGLExtensionSupported(required_extensions[i]) ) {
nkeynes@352
    67
	    ERROR( "Required OpenGL extension not supported: %s", required_extensions[i] );
nkeynes@352
    68
	    isOK = FALSE;
nkeynes@352
    69
	}
nkeynes@352
    70
    }
nkeynes@352
    71
    return isOK;
nkeynes@352
    72
}
nkeynes@352
    73
nkeynes@352
    74
nkeynes@352
    75
gboolean gl_display_frame_buffer( frame_buffer_t frame )
nkeynes@352
    76
{
nkeynes@352
    77
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@352
    78
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@352
    79
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@352
    80
nkeynes@352
    81
    glViewport( 0, 0, video_width, video_height );
nkeynes@352
    82
    glMatrixMode(GL_PROJECTION);
nkeynes@352
    83
    glLoadIdentity();
nkeynes@352
    84
    glOrtho( 0, video_width, video_height, 0, 0, -65535 );
nkeynes@352
    85
    glMatrixMode(GL_MODELVIEW);
nkeynes@352
    86
    glLoadIdentity();
nkeynes@352
    87
    
nkeynes@352
    88
    /* Disable everything */
nkeynes@352
    89
    glDisable( GL_TEXTURE_2D );
nkeynes@352
    90
    glDisable( GL_ALPHA_TEST );
nkeynes@352
    91
    glDisable( GL_DEPTH_TEST );
nkeynes@352
    92
    glDisable( GL_SCISSOR_TEST );
nkeynes@352
    93
    glDisable( GL_CULL_FACE );
nkeynes@352
    94
nkeynes@352
    95
    float scale = ((float)video_height) / frame->height;
nkeynes@352
    96
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@352
    97
    
nkeynes@352
    98
    
nkeynes@352
    99
    /*
nkeynes@352
   100
    glGenTextures( 1, &texid );
nkeynes@352
   101
    glBindTexture( GL_TEXTURE_2D, texid );
nkeynes@352
   102
    glTexImage2D( GL_TEXTURE_2D, 0, colour_formats[frame->colour_format].int_format,
nkeynes@352
   103
		  frame->width, frame->height, 0, format, type, frame->data );
nkeynes@352
   104
    glBegin( GL_QUADS );
nkeynes@352
   105
    glTexCoord2i( 0, 1.0 );
nkeynes@352
   106
    glVertex2f( 0.0, 0.0 );
nkeynes@352
   107
    glTexCoord2i( 1.0, 1.0 );
nkeynes@352
   108
    glVertex2f( frame->width, 0.0 );
nkeynes@352
   109
    glTexCoord2i( 1.0, 0.0 );
nkeynes@352
   110
    glVertex2f( frame->width, frame->height );
nkeynes@352
   111
    glTexCoord2i( 0, 0.0 );
nkeynes@352
   112
    glVertex2f( 0.0, frame->height );
nkeynes@352
   113
    glEnd();
nkeynes@352
   114
    glDeleteTextures( 1, &texid );
nkeynes@352
   115
    */
nkeynes@352
   116
    glRasterPos2i( 0, 0 );
nkeynes@352
   117
    glPixelZoom( 1.0f, -scale );
nkeynes@352
   118
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@352
   119
    glDrawPixels( frame->width, frame->height, format, type,
nkeynes@352
   120
		  frame->data );
nkeynes@352
   121
    
nkeynes@352
   122
    glFlush();
nkeynes@352
   123
    return TRUE;
nkeynes@352
   124
}
nkeynes@352
   125
nkeynes@352
   126
gboolean gl_display_blank( uint32_t colour )
nkeynes@352
   127
{
nkeynes@352
   128
    glViewport( 0, 0, video_width, video_height );
nkeynes@352
   129
    glMatrixMode( GL_PROJECTION );
nkeynes@352
   130
    glLoadIdentity();
nkeynes@352
   131
    glOrtho( 0, video_width, video_height, 0, 0, -65535 );
nkeynes@352
   132
    glMatrixMode(GL_MODELVIEW);
nkeynes@352
   133
    glLoadIdentity();
nkeynes@352
   134
    glColor3b( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
nkeynes@352
   135
    glRecti(0,0, video_width, video_height );
nkeynes@352
   136
    glFlush();
nkeynes@352
   137
    return TRUE;
nkeynes@352
   138
}
nkeynes@352
   139
nkeynes@352
   140
/**
nkeynes@352
   141
 * Generic GL read_render_buffer. This function assumes that the caller
nkeynes@352
   142
 * has already set the appropriate glReadBuffer(); in other words, unless
nkeynes@352
   143
 * there's only one buffer this needs to be wrapped.
nkeynes@352
   144
 */
nkeynes@352
   145
gboolean gl_read_render_buffer( render_buffer_t buffer, char *target ) 
nkeynes@352
   146
{
nkeynes@352
   147
    if( buffer->address == -1 )
nkeynes@352
   148
	return FALSE;
nkeynes@352
   149
    glFinish();
nkeynes@352
   150
    GLenum type = colour_formats[buffer->colour_format].type;
nkeynes@352
   151
    GLenum format = colour_formats[buffer->colour_format].format;
nkeynes@424
   152
    // int line_size = buffer->width * colour_formats[buffer->colour_format].bpp;
nkeynes@424
   153
    // int size = line_size * buffer->height;
nkeynes@424
   154
    // int rowstride = (buffer->rowstride / colour_formats[buffer->colour_format].bpp) - buffer->width;
nkeynes@352
   155
    // glPixelStorei( GL_PACK_ROW_LENGTH, rowstride );
nkeynes@352
   156
    
nkeynes@352
   157
    glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@352
   158
    return TRUE;
nkeynes@352
   159
}
.