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 (7 years ago)
permissions -rw-r--r--
last change Android support WIP
file annotate diff log raw
nkeynes@635
     1
/**
nkeynes@635
     2
 * $Id$
nkeynes@635
     3
 *
nkeynes@635
     4
 * Common GL code that doesn't depend on a specific implementation
nkeynes@635
     5
 *
nkeynes@635
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@635
     7
 *
nkeynes@635
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@635
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@635
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@635
    11
 * (at your option) any later version.
nkeynes@635
    12
 *
nkeynes@635
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@635
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@635
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@635
    16
 * GNU General Public License for more details.
nkeynes@635
    17
 */
nkeynes@635
    18
nkeynes@635
    19
#include <sys/time.h>
nkeynes@635
    20
nkeynes@635
    21
#include "display.h"
nkeynes@635
    22
#include "pvr2/pvr2.h"
nkeynes@1236
    23
#include "pvr2/glutil.h"
nkeynes@635
    24
#include "drivers/video_gl.h"
nkeynes@635
    25
nkeynes@1239
    26
/* FIXME: Need to actually handle this case */
nkeynes@1239
    27
#ifndef GL_PACK_ROW_LENGTH
nkeynes@1239
    28
#define glPixelStorei(key,val)
nkeynes@1239
    29
#endif
nkeynes@1239
    30
nkeynes@1236
    31
uint32_t video_width, video_height;
nkeynes@1236
    32
struct video_vertex {
nkeynes@1236
    33
    float x,y;
nkeynes@1236
    34
    float u,v;
nkeynes@1236
    35
    float r,g,b;
nkeynes@1236
    36
};
nkeynes@1236
    37
nkeynes@1236
    38
static struct video_box_t {
nkeynes@1236
    39
    float viewMatrix[16];
nkeynes@1236
    40
    struct video_vertex gap1[4];
nkeynes@1236
    41
    struct video_vertex gap2[4];
nkeynes@1236
    42
    struct video_vertex video_view[4];
nkeynes@1236
    43
    struct video_vertex invert_view[4];
nkeynes@1236
    44
} video_box;
nkeynes@1236
    45
nkeynes@1236
    46
void gl_set_video_size( uint32_t width, uint32_t height )
nkeynes@1236
    47
{
nkeynes@1236
    48
    video_width = width;
nkeynes@1236
    49
    video_height = height;
nkeynes@1236
    50
nkeynes@1236
    51
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@1236
    52
nkeynes@1236
    53
    int ah = video_width * 0.75;
nkeynes@1236
    54
nkeynes@1236
    55
    if( ah > video_height ) {
nkeynes@1236
    56
        int w = (video_height/0.75);
nkeynes@1236
    57
        x1 = (video_width - w)/2;
nkeynes@1236
    58
        x2 -= x1;
nkeynes@1236
    59
        video_box.gap1[0].x = 0; video_box.gap1[0].y = 0;
nkeynes@1236
    60
        video_box.gap1[1].x = x1; video_box.gap1[1].y = 0;
nkeynes@1236
    61
        video_box.gap1[2].x = 0; video_box.gap1[2].y = video_height;
nkeynes@1236
    62
        video_box.gap1[3].x = x2; video_box.gap1[3].y = video_height;
nkeynes@1236
    63
        video_box.gap2[0].x = x2; video_box.gap2[0].y = 0;
nkeynes@1236
    64
        video_box.gap2[1].x = video_width; video_box.gap2[1].y = 0;
nkeynes@1236
    65
        video_box.gap2[2].x = x2; video_box.gap2[2].y = video_height;
nkeynes@1236
    66
        video_box.gap2[3].x = video_width; video_box.gap2[3].y = video_height;
nkeynes@1236
    67
    } else if( ah < video_height ) {
nkeynes@1236
    68
        y1 = (video_height - ah)/2;
nkeynes@1236
    69
        y2 -= y1;
nkeynes@1236
    70
nkeynes@1236
    71
        video_box.gap1[0].x = 0; video_box.gap1[0].y = 0;
nkeynes@1236
    72
        video_box.gap1[1].x = video_width; video_box.gap1[1].y = 0;
nkeynes@1236
    73
        video_box.gap1[2].x = 0; video_box.gap1[2].y = y1;
nkeynes@1236
    74
        video_box.gap1[3].x = video_width; video_box.gap1[3].y = y1;
nkeynes@1236
    75
        video_box.gap2[0].x = 0; video_box.gap2[0].y = y2;
nkeynes@1236
    76
        video_box.gap2[1].x = video_width; video_box.gap2[1].y = y2;
nkeynes@1236
    77
        video_box.gap2[2].x = 0; video_box.gap2[2].y = video_height;
nkeynes@1236
    78
        video_box.gap2[3].x = video_width; video_box.gap2[3].y = video_height;
nkeynes@1236
    79
    }
nkeynes@1236
    80
nkeynes@1236
    81
    video_box.video_view[0].x = x1; video_box.video_view[0].y = y1;
nkeynes@1236
    82
    video_box.video_view[0].u = 0; video_box.video_view[0].v = 0;
nkeynes@1236
    83
    video_box.video_view[1].x = x2; video_box.video_view[1].y = y1;
nkeynes@1236
    84
    video_box.video_view[1].u = 1; video_box.video_view[1].v = 0;
nkeynes@1236
    85
    video_box.video_view[2].x = x1; video_box.video_view[2].y = y2;
nkeynes@1236
    86
    video_box.video_view[2].u = 0; video_box.video_view[2].v = 1;
nkeynes@1236
    87
    video_box.video_view[3].x = x2; video_box.video_view[3].y = y2;
nkeynes@1236
    88
    video_box.video_view[3].u = 1; video_box.video_view[3].v = 1;
nkeynes@1236
    89
nkeynes@1236
    90
    memcpy( &video_box.invert_view, &video_box.video_view, sizeof(video_box.video_view) );
nkeynes@1236
    91
    video_box.invert_view[0].v = 1; video_box.invert_view[1].v = 1;
nkeynes@1236
    92
    video_box.invert_view[2].v = 0; video_box.invert_view[3].v = 0;
nkeynes@1236
    93
nkeynes@1236
    94
    defineOrthoMatrix(video_box.viewMatrix, video_width, video_height, 0, 65535);
nkeynes@1236
    95
}
nkeynes@635
    96
nkeynes@1239
    97
#ifdef HAVE_OPENGL_FIXEDFUNC
nkeynes@635
    98
/**
nkeynes@1236
    99
 * Setup the gl context for writes to the display output.
nkeynes@635
   100
 */
nkeynes@1236
   101
void gl_framebuffer_setup()
nkeynes@635
   102
{
nkeynes@1239
   103
    glViewport( 0, 0, video_width, video_height );
nkeynes@1236
   104
    glLoadMatrixf(video_box.viewMatrix);
nkeynes@1236
   105
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@1236
   106
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
nkeynes@1236
   107
    glVertexPointer(2, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].x);
nkeynes@1236
   108
    glColorPointer(3, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].r);
nkeynes@1236
   109
    glTexCoordPointer(2, GL_FLOAT, sizeof(struct video_vertex), &video_box.gap1[0].u);
nkeynes@1236
   110
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@1236
   111
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@1236
   112
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@635
   113
}
nkeynes@635
   114
nkeynes@1239
   115
#else
nkeynes@1239
   116
void gl_framebuffer_setup()
nkeynes@1239
   117
{
nkeynes@1239
   118
   /* TODO */
nkeynes@1239
   119
}
nkeynes@1239
   120
#endif
nkeynes@1239
   121
nkeynes@635
   122
void gl_display_render_buffer( render_buffer_t buffer )
nkeynes@635
   123
{
nkeynes@635
   124
    gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
nkeynes@635
   125
}
nkeynes@635
   126
nkeynes@854
   127
/**
nkeynes@854
   128
 * Convert window coordinates to dreamcast device coords (640x480) using the 
nkeynes@854
   129
 * same viewable area as gl_texture_window.
nkeynes@854
   130
 * If the coordinates are outside the viewable area, the result is -1,-1.
nkeynes@854
   131
 */ 
nkeynes@854
   132
void gl_window_to_system_coords( int *x, int *y )
nkeynes@854
   133
{
nkeynes@854
   134
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@854
   135
nkeynes@854
   136
    int ah = video_width * 0.75;
nkeynes@854
   137
nkeynes@854
   138
    if( ah > video_height ) {
nkeynes@854
   139
        int w = (video_height/0.75);
nkeynes@854
   140
        x1 = (video_width - w)/2;
nkeynes@854
   141
        x2 -= x1;
nkeynes@854
   142
    } else if( ah < video_height ) {
nkeynes@854
   143
        y1 = (video_height - ah)/2;
nkeynes@854
   144
        y2 -= y1;
nkeynes@854
   145
    }
nkeynes@854
   146
    if( *x < x1 || *x >= x2 || *y < y1 || *y >= y2 ) {
nkeynes@854
   147
        *x = -1;
nkeynes@854
   148
        *y = -1;
nkeynes@854
   149
    } else {
nkeynes@854
   150
        *x = (*x - x1) * DISPLAY_WIDTH / (x2-x1);
nkeynes@854
   151
        *y = (*y - y1) * DISPLAY_HEIGHT / (y2-y1);
nkeynes@854
   152
    }
nkeynes@854
   153
}
nkeynes@854
   154
nkeynes@635
   155
void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
nkeynes@635
   156
{
nkeynes@635
   157
    /* Reset display parameters */
nkeynes@1236
   158
    gl_framebuffer_setup();
nkeynes@1236
   159
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
nkeynes@1236
   160
    glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
nkeynes@1236
   161
    glEnable(GL_TEXTURE_2D);
nkeynes@1236
   162
    glBindTexture(GL_TEXTURE_2D,tex_id);
nkeynes@1222
   163
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nkeynes@1222
   164
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@1236
   165
    glDrawArrays(GL_TRIANGLE_STRIP, inverted ? 12 : 8, 4);
nkeynes@1236
   166
    glDisable(GL_TEXTURE_2D);
nkeynes@635
   167
    glFlush();
nkeynes@635
   168
}
nkeynes@635
   169
nkeynes@635
   170
gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
nkeynes@635
   171
{
nkeynes@635
   172
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@635
   173
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@635
   174
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@635
   175
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@736
   176
nkeynes@635
   177
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@1222
   178
    glBindTexture( GL_TEXTURE_2D, tex_id );
nkeynes@1222
   179
    glTexSubImage2D( GL_TEXTURE_2D, 0, 0,0,
nkeynes@736
   180
                     frame->width, frame->height, format, type, frame->data );
nkeynes@1236
   181
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
nkeynes@635
   182
    return TRUE;
nkeynes@635
   183
}
nkeynes@635
   184
nkeynes@669
   185
void gl_display_blank( uint32_t colour )
nkeynes@635
   186
{
nkeynes@1236
   187
    /* Set the video_box background colour */
nkeynes@1236
   188
    video_box.video_view[0].r = ((float)(((colour >> 16) & 0xFF) + 1)) / 256.0;
nkeynes@1236
   189
    video_box.video_view[0].g = ((float)(((colour >> 8) & 0xFF) + 1)) / 256.0;
nkeynes@1236
   190
    video_box.video_view[0].b = ((float)((colour & 0xFF) + 1)) / 256.0;
nkeynes@1236
   191
    memcpy( &video_box.video_view[1].r, &video_box.video_view[0].r, sizeof(float)*3 );
nkeynes@1236
   192
    memcpy( &video_box.video_view[2].r, &video_box.video_view[0].r, sizeof(float)*3 );
nkeynes@1236
   193
    memcpy( &video_box.video_view[3].r, &video_box.video_view[0].r, sizeof(float)*3 );
nkeynes@1236
   194
nkeynes@1236
   195
    /* And render */
nkeynes@1236
   196
    gl_framebuffer_setup();
nkeynes@1236
   197
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
nkeynes@1236
   198
    glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
nkeynes@1236
   199
    glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
nkeynes@635
   200
    glFlush();
nkeynes@635
   201
}
nkeynes@635
   202
nkeynes@635
   203
/**
nkeynes@635
   204
 * Generic GL read_render_buffer. This function assumes that the caller
nkeynes@635
   205
 * has already set the appropriate glReadBuffer(); in other words, unless
nkeynes@635
   206
 * there's only one buffer this needs to be wrapped.
nkeynes@635
   207
 */
nkeynes@635
   208
gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@736
   209
                                int rowstride, int colour_format ) 
nkeynes@635
   210
{
nkeynes@635
   211
    glFinish();
nkeynes@635
   212
    GLenum type = colour_formats[colour_format].type;
nkeynes@635
   213
    GLenum format = colour_formats[colour_format].format;
nkeynes@635
   214
    // int line_size = buffer->width * colour_formats[colour_format].bpp;
nkeynes@635
   215
    // int size = line_size * buffer->height;
nkeynes@635
   216
    int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
nkeynes@635
   217
    glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
nkeynes@635
   218
    glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@1236
   219
    glPixelStorei( GL_PACK_ROW_LENGTH, 0 );
nkeynes@635
   220
    return TRUE;
nkeynes@635
   221
}
.