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 561:533f6b478071
prev558:428cf8528699
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@352
     1
/**
nkeynes@561
     2
 * $Id$
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@443
    19
#include <sys/time.h>
nkeynes@443
    20
nkeynes@540
    21
#include "display.h"
nkeynes@481
    22
#include "pvr2/pvr2.h"
nkeynes@352
    23
#include "drivers/gl_common.h"
nkeynes@352
    24
nkeynes@352
    25
extern uint32_t video_width, video_height;
nkeynes@352
    26
nkeynes@352
    27
char *required_extensions[] = { "GL_EXT_framebuffer_object", NULL };
nkeynes@352
    28
nkeynes@352
    29
/**
nkeynes@352
    30
 * Test if a specific extension is supported. From opengl.org
nkeynes@352
    31
 * @param extension extension name to check for
nkeynes@352
    32
 * @return TRUE if supported, otherwise FALSE.
nkeynes@352
    33
 */
nkeynes@352
    34
gboolean isGLExtensionSupported( const char *extension )
nkeynes@352
    35
{
nkeynes@352
    36
    const GLubyte *extensions = NULL;
nkeynes@352
    37
    const GLubyte *start;
nkeynes@352
    38
    GLubyte *where, *terminator;
nkeynes@352
    39
nkeynes@352
    40
    /* Extension names should not have spaces. */
nkeynes@352
    41
    where = (GLubyte *) strchr(extension, ' ');
nkeynes@352
    42
    if (where || *extension == '\0')
nkeynes@352
    43
	return 0;
nkeynes@352
    44
    extensions = glGetString(GL_EXTENSIONS);
nkeynes@352
    45
    /* It takes a bit of care to be fool-proof about parsing the
nkeynes@352
    46
       OpenGL extensions string. Don't be fooled by sub-strings,
nkeynes@352
    47
       etc. */
nkeynes@352
    48
    start = extensions;
nkeynes@352
    49
    for (;;) {
nkeynes@352
    50
	where = (GLubyte *) strstr((const char *) start, extension);
nkeynes@352
    51
	if (!where)
nkeynes@352
    52
	    break;
nkeynes@352
    53
	terminator = where + strlen(extension);
nkeynes@352
    54
	if (where == start || *(where - 1) == ' ')
nkeynes@352
    55
	    if (*terminator == ' ' || *terminator == '\0')
nkeynes@352
    56
		return TRUE;
nkeynes@352
    57
	start = terminator;
nkeynes@352
    58
    }
nkeynes@352
    59
    return FALSE;
nkeynes@352
    60
}
nkeynes@352
    61
nkeynes@352
    62
gboolean hasRequiredGLExtensions( ) 
nkeynes@352
    63
{
nkeynes@352
    64
    int i;
nkeynes@352
    65
    gboolean isOK = TRUE;
nkeynes@352
    66
nkeynes@352
    67
    for( i=0; required_extensions[i] != NULL; i++ ) {
nkeynes@352
    68
	if( !isGLExtensionSupported(required_extensions[i]) ) {
nkeynes@352
    69
	    ERROR( "Required OpenGL extension not supported: %s", required_extensions[i] );
nkeynes@352
    70
	    isOK = FALSE;
nkeynes@352
    71
	}
nkeynes@352
    72
    }
nkeynes@352
    73
    return isOK;
nkeynes@352
    74
}
nkeynes@352
    75
nkeynes@545
    76
/**
nkeynes@545
    77
 * Reset the gl state to simple orthographic projection with 
nkeynes@545
    78
 * texturing, alpha/depth/scissor/cull tests disabled.
nkeynes@545
    79
 */
nkeynes@545
    80
void gl_reset_state()
nkeynes@443
    81
{
nkeynes@352
    82
    glViewport( 0, 0, video_width, video_height );
nkeynes@352
    83
    glMatrixMode(GL_PROJECTION);
nkeynes@352
    84
    glLoadIdentity();
nkeynes@352
    85
    glOrtho( 0, video_width, video_height, 0, 0, -65535 );
nkeynes@352
    86
    glMatrixMode(GL_MODELVIEW);
nkeynes@352
    87
    glLoadIdentity();
nkeynes@352
    88
    glDisable( GL_TEXTURE_2D );
nkeynes@352
    89
    glDisable( GL_ALPHA_TEST );
nkeynes@352
    90
    glDisable( GL_DEPTH_TEST );
nkeynes@352
    91
    glDisable( GL_SCISSOR_TEST );
nkeynes@352
    92
    glDisable( GL_CULL_FACE );
nkeynes@545
    93
    glDrawBuffer( GL_FRONT );
nkeynes@545
    94
}
nkeynes@545
    95
nkeynes@545
    96
void gl_display_render_buffer( render_buffer_t buffer )
nkeynes@545
    97
{
nkeynes@545
    98
    gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
nkeynes@545
    99
}
nkeynes@545
   100
nkeynes@545
   101
void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
nkeynes@545
   102
{
nkeynes@545
   103
    float top, bottom;
nkeynes@545
   104
    if( inverted ) {
nkeynes@558
   105
	top = ((float)height);
nkeynes@558
   106
	bottom = 0;
nkeynes@545
   107
    } else {
nkeynes@558
   108
	top = 0;
nkeynes@558
   109
	bottom = ((float)height);
nkeynes@545
   110
    }
nkeynes@545
   111
nkeynes@545
   112
    /* Reset display parameters */
nkeynes@545
   113
    gl_reset_state();
nkeynes@545
   114
    glColor3f( 0,0,0 );    
nkeynes@444
   115
nkeynes@444
   116
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@444
   117
nkeynes@444
   118
    int ah = video_width * 0.75;
nkeynes@444
   119
nkeynes@444
   120
    if( ah > video_height ) {
nkeynes@444
   121
	int w = (video_height/0.75);
nkeynes@444
   122
	x1 = (video_width - w)/2;
nkeynes@444
   123
	x2 -= x1;
nkeynes@444
   124
nkeynes@444
   125
	glBegin( GL_QUADS );
nkeynes@444
   126
	glVertex2f( 0, 0 );
nkeynes@444
   127
	glVertex2f( x1, 0 );
nkeynes@444
   128
	glVertex2f( x1, video_height );
nkeynes@444
   129
	glVertex2f( 0, video_height);
nkeynes@444
   130
	glVertex2f( x2, 0 );
nkeynes@444
   131
	glVertex2f( video_width, 0 );
nkeynes@444
   132
	glVertex2f( video_width, video_height );
nkeynes@444
   133
	glVertex2f( x2, video_height);
nkeynes@444
   134
	glEnd();
nkeynes@444
   135
    } else if( ah < video_height ) {
nkeynes@444
   136
	y1 = (video_height - ah)/2;
nkeynes@444
   137
	y2 -= y1;
nkeynes@444
   138
	glBegin( GL_QUADS );
nkeynes@444
   139
	glVertex2f( 0, 0 );
nkeynes@444
   140
	glVertex2f( video_width, 0 );
nkeynes@444
   141
	glVertex2f( video_width, y1 );
nkeynes@444
   142
	glVertex2f( 0, y1 );
nkeynes@444
   143
	glVertex2f( 0, y2 );
nkeynes@444
   144
	glVertex2f( video_width, y2 );
nkeynes@444
   145
	glVertex2f( video_width, video_height );
nkeynes@444
   146
	glVertex2f( 0, video_height );
nkeynes@444
   147
	glEnd();
nkeynes@444
   148
    }
nkeynes@352
   149
nkeynes@443
   150
    /* Render the textured rectangle */
nkeynes@443
   151
    glEnable( GL_TEXTURE_RECTANGLE_ARB );
nkeynes@545
   152
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
nkeynes@443
   153
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
nkeynes@443
   154
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nkeynes@443
   155
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@443
   156
    glEnable( GL_BLEND );
nkeynes@443
   157
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@352
   158
    glBegin( GL_QUADS );
nkeynes@558
   159
    glTexCoord2f( 0, top );
nkeynes@444
   160
    glVertex2f( x1, y1 );
nkeynes@558
   161
    glTexCoord2f( ((float)width), top );
nkeynes@444
   162
    glVertex2f( x2, y1 );
nkeynes@558
   163
    glTexCoord2f( ((float)width), bottom );
nkeynes@444
   164
    glVertex2f( x2, y2 );
nkeynes@558
   165
    glTexCoord2f( 0, bottom );
nkeynes@444
   166
    glVertex2f( x1, y2 );
nkeynes@352
   167
    glEnd();
nkeynes@443
   168
    glDisable( GL_TEXTURE_RECTANGLE_ARB );
nkeynes@352
   169
    glFlush();
nkeynes@443
   170
}
nkeynes@443
   171
nkeynes@545
   172
gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
nkeynes@443
   173
{
nkeynes@477
   174
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@477
   175
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@477
   176
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@477
   177
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@477
   178
    
nkeynes@477
   179
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@545
   180
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
nkeynes@477
   181
    glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0,0,
nkeynes@481
   182
		     frame->width, frame->height, format, type, frame->data );
nkeynes@481
   183
    return TRUE;
nkeynes@352
   184
}
nkeynes@352
   185
nkeynes@352
   186
gboolean gl_display_blank( uint32_t colour )
nkeynes@352
   187
{
nkeynes@545
   188
    gl_reset_state();
nkeynes@545
   189
    glColor3ub( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
nkeynes@352
   190
    glRecti(0,0, video_width, video_height );
nkeynes@352
   191
    glFlush();
nkeynes@352
   192
    return TRUE;
nkeynes@352
   193
}
nkeynes@352
   194
nkeynes@352
   195
/**
nkeynes@352
   196
 * Generic GL read_render_buffer. This function assumes that the caller
nkeynes@352
   197
 * has already set the appropriate glReadBuffer(); in other words, unless
nkeynes@352
   198
 * there's only one buffer this needs to be wrapped.
nkeynes@352
   199
 */
nkeynes@477
   200
gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@477
   201
				int rowstride, int colour_format ) 
nkeynes@352
   202
{
nkeynes@352
   203
    glFinish();
nkeynes@477
   204
    GLenum type = colour_formats[colour_format].type;
nkeynes@477
   205
    GLenum format = colour_formats[colour_format].format;
nkeynes@477
   206
    // int line_size = buffer->width * colour_formats[colour_format].bpp;
nkeynes@424
   207
    // int size = line_size * buffer->height;
nkeynes@477
   208
    int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
nkeynes@477
   209
    glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
nkeynes@352
   210
    
nkeynes@352
   211
    glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@352
   212
    return TRUE;
nkeynes@352
   213
}
.