Search
lxdream.org :: lxdream/src/drivers/video_gl.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_gl.c
changeset 635:76c63aac3590
next669:ab344e42bca9
author nkeynes
date Fri Mar 28 12:32:25 2008 +0000 (16 years ago)
permissions -rw-r--r--
last change Merge lxdream-render branch (643:670) to trunk
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/video_gl.c Fri Mar 28 12:32:25 2008 +0000
1.3 @@ -0,0 +1,164 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Common GL code that doesn't depend on a specific implementation
1.8 + *
1.9 + * Copyright (c) 2005 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <sys/time.h>
1.23 +
1.24 +#include "display.h"
1.25 +#include "pvr2/pvr2.h"
1.26 +#include "drivers/video_gl.h"
1.27 +
1.28 +extern uint32_t video_width, video_height;
1.29 +
1.30 +/**
1.31 + * Reset the gl state to simple orthographic projection with
1.32 + * texturing, alpha/depth/scissor/cull tests disabled.
1.33 + */
1.34 +void gl_reset_state()
1.35 +{
1.36 + glViewport( 0, 0, video_width, video_height );
1.37 + glMatrixMode(GL_PROJECTION);
1.38 + glLoadIdentity();
1.39 + glOrtho( 0, video_width, video_height, 0, 0, 65535 );
1.40 + glMatrixMode(GL_MODELVIEW);
1.41 + glLoadIdentity();
1.42 + glDisable( GL_TEXTURE_2D );
1.43 + glDisable( GL_ALPHA_TEST );
1.44 + glDisable( GL_DEPTH_TEST );
1.45 + glDisable( GL_SCISSOR_TEST );
1.46 + glDisable( GL_CULL_FACE );
1.47 + glDrawBuffer( GL_FRONT );
1.48 +}
1.49 +
1.50 +void gl_display_render_buffer( render_buffer_t buffer )
1.51 +{
1.52 + gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
1.53 +}
1.54 +
1.55 +void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
1.56 +{
1.57 + float top, bottom;
1.58 + if( inverted ) {
1.59 + top = ((float)height);
1.60 + bottom = 0;
1.61 + } else {
1.62 + top = 0;
1.63 + bottom = ((float)height);
1.64 + }
1.65 +
1.66 + /* Reset display parameters */
1.67 + gl_reset_state();
1.68 + glColor3f( 0,0,0 );
1.69 +
1.70 + int x1=0,y1=0,x2=video_width,y2=video_height;
1.71 +
1.72 + int ah = video_width * 0.75;
1.73 +
1.74 + if( ah > video_height ) {
1.75 + int w = (video_height/0.75);
1.76 + x1 = (video_width - w)/2;
1.77 + x2 -= x1;
1.78 +
1.79 + glBegin( GL_QUADS );
1.80 + glVertex2f( 0, 0 );
1.81 + glVertex2f( x1, 0 );
1.82 + glVertex2f( x1, video_height );
1.83 + glVertex2f( 0, video_height);
1.84 + glVertex2f( x2, 0 );
1.85 + glVertex2f( video_width, 0 );
1.86 + glVertex2f( video_width, video_height );
1.87 + glVertex2f( x2, video_height);
1.88 + glEnd();
1.89 + } else if( ah < video_height ) {
1.90 + y1 = (video_height - ah)/2;
1.91 + y2 -= y1;
1.92 + glBegin( GL_QUADS );
1.93 + glVertex2f( 0, 0 );
1.94 + glVertex2f( video_width, 0 );
1.95 + glVertex2f( video_width, y1 );
1.96 + glVertex2f( 0, y1 );
1.97 + glVertex2f( 0, y2 );
1.98 + glVertex2f( video_width, y2 );
1.99 + glVertex2f( video_width, video_height );
1.100 + glVertex2f( 0, video_height );
1.101 + glEnd();
1.102 + }
1.103 +
1.104 + /* Render the textured rectangle */
1.105 + glEnable( GL_TEXTURE_RECTANGLE_ARB );
1.106 + glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
1.107 + glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
1.108 + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1.109 + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1.110 + glEnable( GL_BLEND );
1.111 + glBlendFunc( GL_ONE, GL_ZERO );
1.112 + glBegin( GL_QUADS );
1.113 + glTexCoord2f( 0, top );
1.114 + glVertex2f( x1, y1 );
1.115 + glTexCoord2f( ((float)width), top );
1.116 + glVertex2f( x2, y1 );
1.117 + glTexCoord2f( ((float)width), bottom );
1.118 + glVertex2f( x2, y2 );
1.119 + glTexCoord2f( 0, bottom );
1.120 + glVertex2f( x1, y2 );
1.121 + glEnd();
1.122 + glDisable( GL_TEXTURE_RECTANGLE_ARB );
1.123 + glFlush();
1.124 +}
1.125 +
1.126 +gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
1.127 +{
1.128 + GLenum type = colour_formats[frame->colour_format].type;
1.129 + GLenum format = colour_formats[frame->colour_format].format;
1.130 + int bpp = colour_formats[frame->colour_format].bpp;
1.131 + int rowstride = (frame->rowstride / bpp) - frame->width;
1.132 +
1.133 + glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
1.134 + glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
1.135 + glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0,0,
1.136 + frame->width, frame->height, format, type, frame->data );
1.137 + return TRUE;
1.138 +}
1.139 +
1.140 +gboolean gl_display_blank( uint32_t colour )
1.141 +{
1.142 + gl_reset_state();
1.143 + glColor3ub( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
1.144 + glRecti(0,0, video_width, video_height );
1.145 + glFlush();
1.146 + return TRUE;
1.147 +}
1.148 +
1.149 +/**
1.150 + * Generic GL read_render_buffer. This function assumes that the caller
1.151 + * has already set the appropriate glReadBuffer(); in other words, unless
1.152 + * there's only one buffer this needs to be wrapped.
1.153 + */
1.154 +gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer,
1.155 + int rowstride, int colour_format )
1.156 +{
1.157 + glFinish();
1.158 + GLenum type = colour_formats[colour_format].type;
1.159 + GLenum format = colour_formats[colour_format].format;
1.160 + // int line_size = buffer->width * colour_formats[colour_format].bpp;
1.161 + // int size = line_size * buffer->height;
1.162 + int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
1.163 + glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
1.164 +
1.165 + glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
1.166 + return TRUE;
1.167 +}
.