Search
lxdream.org :: lxdream/src/drivers/video_gl.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_gl.c
changeset 635:76c63aac3590
next669:ab344e42bca9
author nkeynes
date Wed Apr 02 01:46:58 2008 +0000 (16 years ago)
permissions -rw-r--r--
last change Add configure-time checks for fbo and shader functions in libGL (so linking
doesn't fail on really old libGL implementations)
view annotate diff log raw
     1 /**
     2  * $Id$
     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 <sys/time.h>
    21 #include "display.h"
    22 #include "pvr2/pvr2.h"
    23 #include "drivers/video_gl.h"
    25 extern uint32_t video_width, video_height;
    27 /**
    28  * Reset the gl state to simple orthographic projection with 
    29  * texturing, alpha/depth/scissor/cull tests disabled.
    30  */
    31 void gl_reset_state()
    32 {
    33     glViewport( 0, 0, video_width, video_height );
    34     glMatrixMode(GL_PROJECTION);
    35     glLoadIdentity();
    36     glOrtho( 0, video_width, video_height, 0, 0, 65535 );
    37     glMatrixMode(GL_MODELVIEW);
    38     glLoadIdentity();
    39     glDisable( GL_TEXTURE_2D );
    40     glDisable( GL_ALPHA_TEST );
    41     glDisable( GL_DEPTH_TEST );
    42     glDisable( GL_SCISSOR_TEST );
    43     glDisable( GL_CULL_FACE );
    44     glDrawBuffer( GL_FRONT );
    45 }
    47 void gl_display_render_buffer( render_buffer_t buffer )
    48 {
    49     gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
    50 }
    52 void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
    53 {
    54     float top, bottom;
    55     if( inverted ) {
    56 	top = ((float)height);
    57 	bottom = 0;
    58     } else {
    59 	top = 0;
    60 	bottom = ((float)height);
    61     }
    63     /* Reset display parameters */
    64     gl_reset_state();
    65     glColor3f( 0,0,0 );    
    67     int x1=0,y1=0,x2=video_width,y2=video_height;
    69     int ah = video_width * 0.75;
    71     if( ah > video_height ) {
    72 	int w = (video_height/0.75);
    73 	x1 = (video_width - w)/2;
    74 	x2 -= x1;
    76 	glBegin( GL_QUADS );
    77 	glVertex2f( 0, 0 );
    78 	glVertex2f( x1, 0 );
    79 	glVertex2f( x1, video_height );
    80 	glVertex2f( 0, video_height);
    81 	glVertex2f( x2, 0 );
    82 	glVertex2f( video_width, 0 );
    83 	glVertex2f( video_width, video_height );
    84 	glVertex2f( x2, video_height);
    85 	glEnd();
    86     } else if( ah < video_height ) {
    87 	y1 = (video_height - ah)/2;
    88 	y2 -= y1;
    89 	glBegin( GL_QUADS );
    90 	glVertex2f( 0, 0 );
    91 	glVertex2f( video_width, 0 );
    92 	glVertex2f( video_width, y1 );
    93 	glVertex2f( 0, y1 );
    94 	glVertex2f( 0, y2 );
    95 	glVertex2f( video_width, y2 );
    96 	glVertex2f( video_width, video_height );
    97 	glVertex2f( 0, video_height );
    98 	glEnd();
    99     }
   101     /* Render the textured rectangle */
   102     glEnable( GL_TEXTURE_RECTANGLE_ARB );
   103     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   104     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   105     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   106     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   107     glEnable( GL_BLEND );
   108     glBlendFunc( GL_ONE, GL_ZERO );
   109     glBegin( GL_QUADS );
   110     glTexCoord2f( 0, top );
   111     glVertex2f( x1, y1 );
   112     glTexCoord2f( ((float)width), top );
   113     glVertex2f( x2, y1 );
   114     glTexCoord2f( ((float)width), bottom );
   115     glVertex2f( x2, y2 );
   116     glTexCoord2f( 0, bottom );
   117     glVertex2f( x1, y2 );
   118     glEnd();
   119     glDisable( GL_TEXTURE_RECTANGLE_ARB );
   120     glFlush();
   121 }
   123 gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
   124 {
   125     GLenum type = colour_formats[frame->colour_format].type;
   126     GLenum format = colour_formats[frame->colour_format].format;
   127     int bpp = colour_formats[frame->colour_format].bpp;
   128     int rowstride = (frame->rowstride / bpp) - frame->width;
   130     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   131     glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
   132     glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0,0,
   133 		     frame->width, frame->height, format, type, frame->data );
   134     return TRUE;
   135 }
   137 gboolean gl_display_blank( uint32_t colour )
   138 {
   139     gl_reset_state();
   140     glColor3ub( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
   141     glRecti(0,0, video_width, video_height );
   142     glFlush();
   143     return TRUE;
   144 }
   146 /**
   147  * Generic GL read_render_buffer. This function assumes that the caller
   148  * has already set the appropriate glReadBuffer(); in other words, unless
   149  * there's only one buffer this needs to be wrapped.
   150  */
   151 gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   152 				int rowstride, int colour_format ) 
   153 {
   154     glFinish();
   155     GLenum type = colour_formats[colour_format].type;
   156     GLenum format = colour_formats[colour_format].format;
   157     // int line_size = buffer->width * colour_formats[colour_format].bpp;
   158     // int size = line_size * buffer->height;
   159     int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
   160     glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
   162     glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
   163     return TRUE;
   164 }
.