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 1239:be3121267597
prev1236:d93175c36387
next1240:190df8a791ca
author nkeynes
date Sat Feb 25 21:30:49 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Android support WIP
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 "pvr2/glutil.h"
    24 #include "drivers/video_gl.h"
    26 /* FIXME: Need to actually handle this case */
    27 #ifndef GL_PACK_ROW_LENGTH
    28 #define glPixelStorei(key,val)
    29 #endif
    31 uint32_t video_width, video_height;
    32 struct video_vertex {
    33     float x,y;
    34     float u,v;
    35     float r,g,b;
    36 };
    38 static struct video_box_t {
    39     float viewMatrix[16];
    40     struct video_vertex gap1[4];
    41     struct video_vertex gap2[4];
    42     struct video_vertex video_view[4];
    43     struct video_vertex invert_view[4];
    44 } video_box;
    46 void gl_set_video_size( uint32_t width, uint32_t height )
    47 {
    48     video_width = width;
    49     video_height = height;
    51     int x1=0,y1=0,x2=video_width,y2=video_height;
    53     int ah = video_width * 0.75;
    55     if( ah > video_height ) {
    56         int w = (video_height/0.75);
    57         x1 = (video_width - w)/2;
    58         x2 -= x1;
    59         video_box.gap1[0].x = 0; video_box.gap1[0].y = 0;
    60         video_box.gap1[1].x = x1; video_box.gap1[1].y = 0;
    61         video_box.gap1[2].x = 0; video_box.gap1[2].y = video_height;
    62         video_box.gap1[3].x = x2; video_box.gap1[3].y = video_height;
    63         video_box.gap2[0].x = x2; video_box.gap2[0].y = 0;
    64         video_box.gap2[1].x = video_width; video_box.gap2[1].y = 0;
    65         video_box.gap2[2].x = x2; video_box.gap2[2].y = video_height;
    66         video_box.gap2[3].x = video_width; video_box.gap2[3].y = video_height;
    67     } else if( ah < video_height ) {
    68         y1 = (video_height - ah)/2;
    69         y2 -= y1;
    71         video_box.gap1[0].x = 0; video_box.gap1[0].y = 0;
    72         video_box.gap1[1].x = video_width; video_box.gap1[1].y = 0;
    73         video_box.gap1[2].x = 0; video_box.gap1[2].y = y1;
    74         video_box.gap1[3].x = video_width; video_box.gap1[3].y = y1;
    75         video_box.gap2[0].x = 0; video_box.gap2[0].y = y2;
    76         video_box.gap2[1].x = video_width; video_box.gap2[1].y = y2;
    77         video_box.gap2[2].x = 0; video_box.gap2[2].y = video_height;
    78         video_box.gap2[3].x = video_width; video_box.gap2[3].y = video_height;
    79     }
    81     video_box.video_view[0].x = x1; video_box.video_view[0].y = y1;
    82     video_box.video_view[0].u = 0; video_box.video_view[0].v = 0;
    83     video_box.video_view[1].x = x2; video_box.video_view[1].y = y1;
    84     video_box.video_view[1].u = 1; video_box.video_view[1].v = 0;
    85     video_box.video_view[2].x = x1; video_box.video_view[2].y = y2;
    86     video_box.video_view[2].u = 0; video_box.video_view[2].v = 1;
    87     video_box.video_view[3].x = x2; video_box.video_view[3].y = y2;
    88     video_box.video_view[3].u = 1; video_box.video_view[3].v = 1;
    90     memcpy( &video_box.invert_view, &video_box.video_view, sizeof(video_box.video_view) );
    91     video_box.invert_view[0].v = 1; video_box.invert_view[1].v = 1;
    92     video_box.invert_view[2].v = 0; video_box.invert_view[3].v = 0;
    94     defineOrthoMatrix(video_box.viewMatrix, video_width, video_height, 0, 65535);
    95 }
    97 #ifdef HAVE_OPENGL_FIXEDFUNC
    98 /**
    99  * Setup the gl context for writes to the display output.
   100  */
   101 void gl_framebuffer_setup()
   102 {
   103     glViewport( 0, 0, video_width, video_height );
   104     glLoadMatrixf(video_box.viewMatrix);
   105     glBlendFunc( GL_ONE, GL_ZERO );
   106     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   107     glVertexPointer(2, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].x);
   108     glColorPointer(3, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].r);
   109     glTexCoordPointer(2, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].u);
   110     glEnableClientState( GL_VERTEX_ARRAY );
   111     glEnableClientState( GL_COLOR_ARRAY );
   112     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   113 }
   115 #else
   116 void gl_framebuffer_setup()
   117 {
   118    /* TODO */
   119 }
   120 #endif
   122 void gl_display_render_buffer( render_buffer_t buffer )
   123 {
   124     gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
   125 }
   127 /**
   128  * Convert window coordinates to dreamcast device coords (640x480) using the 
   129  * same viewable area as gl_texture_window.
   130  * If the coordinates are outside the viewable area, the result is -1,-1.
   131  */ 
   132 void gl_window_to_system_coords( int *x, int *y )
   133 {
   134     int x1=0,y1=0,x2=video_width,y2=video_height;
   136     int ah = video_width * 0.75;
   138     if( ah > video_height ) {
   139         int w = (video_height/0.75);
   140         x1 = (video_width - w)/2;
   141         x2 -= x1;
   142     } else if( ah < video_height ) {
   143         y1 = (video_height - ah)/2;
   144         y2 -= y1;
   145     }
   146     if( *x < x1 || *x >= x2 || *y < y1 || *y >= y2 ) {
   147         *x = -1;
   148         *y = -1;
   149     } else {
   150         *x = (*x - x1) * DISPLAY_WIDTH / (x2-x1);
   151         *y = (*y - y1) * DISPLAY_HEIGHT / (y2-y1);
   152     }
   153 }
   155 void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
   156 {
   157     /* Reset display parameters */
   158     gl_framebuffer_setup();
   159     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   160     glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
   161     glEnable(GL_TEXTURE_2D);
   162     glBindTexture(GL_TEXTURE_2D,tex_id);
   163     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   164     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   165     glDrawArrays(GL_TRIANGLE_STRIP, inverted ? 12 : 8, 4);
   166     glDisable(GL_TEXTURE_2D);
   167     glFlush();
   168 }
   170 gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
   171 {
   172     GLenum type = colour_formats[frame->colour_format].type;
   173     GLenum format = colour_formats[frame->colour_format].format;
   174     int bpp = colour_formats[frame->colour_format].bpp;
   175     int rowstride = (frame->rowstride / bpp) - frame->width;
   177     glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
   178     glBindTexture( GL_TEXTURE_2D, tex_id );
   179     glTexSubImage2D( GL_TEXTURE_2D, 0, 0,0,
   180                      frame->width, frame->height, format, type, frame->data );
   181     glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
   182     return TRUE;
   183 }
   185 void gl_display_blank( uint32_t colour )
   186 {
   187     /* Set the video_box background colour */
   188     video_box.video_view[0].r = ((float)(((colour >> 16) & 0xFF) + 1)) / 256.0;
   189     video_box.video_view[0].g = ((float)(((colour >> 8) & 0xFF) + 1)) / 256.0;
   190     video_box.video_view[0].b = ((float)((colour & 0xFF) + 1)) / 256.0;
   191     memcpy( &video_box.video_view[1].r, &video_box.video_view[0].r, sizeof(float)*3 );
   192     memcpy( &video_box.video_view[2].r, &video_box.video_view[0].r, sizeof(float)*3 );
   193     memcpy( &video_box.video_view[3].r, &video_box.video_view[0].r, sizeof(float)*3 );
   195     /* And render */
   196     gl_framebuffer_setup();
   197     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   198     glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
   199     glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
   200     glFlush();
   201 }
   203 /**
   204  * Generic GL read_render_buffer. This function assumes that the caller
   205  * has already set the appropriate glReadBuffer(); in other words, unless
   206  * there's only one buffer this needs to be wrapped.
   207  */
   208 gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
   209                                 int rowstride, int colour_format ) 
   210 {
   211     glFinish();
   212     GLenum type = colour_formats[colour_format].type;
   213     GLenum format = colour_formats[colour_format].format;
   214     // int line_size = buffer->width * colour_formats[colour_format].bpp;
   215     // int size = line_size * buffer->height;
   216     int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
   217     glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
   218     glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
   219     glPixelStorei( GL_PACK_ROW_LENGTH, 0 );
   220     return TRUE;
   221 }
.