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 1222:a4545699a82b
prev1076:18c164e8aec4
next1236:d93175c36387
author nkeynes
date Wed Feb 15 17:54:51 2012 +1000 (7 years ago)
permissions -rw-r--r--
last change Use GL_TEXTURE_2D instead of GL_TEXTURE_RECTANGLE_ARB for frame buffers, for
systems that don't provide the latter (and there's not really much
difference anyway).
Add macro wrangling for GL_DEPTH24_STENCIL8 format
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@635
    23
#include "drivers/video_gl.h"
nkeynes@635
    24
nkeynes@635
    25
extern uint32_t video_width, video_height;
nkeynes@635
    26
nkeynes@635
    27
/**
nkeynes@635
    28
 * Reset the gl state to simple orthographic projection with 
nkeynes@635
    29
 * texturing, alpha/depth/scissor/cull tests disabled.
nkeynes@635
    30
 */
nkeynes@635
    31
void gl_reset_state()
nkeynes@635
    32
{
nkeynes@635
    33
    glViewport( 0, 0, video_width, video_height );
nkeynes@635
    34
    glMatrixMode(GL_PROJECTION);
nkeynes@635
    35
    glLoadIdentity();
nkeynes@635
    36
    glOrtho( 0, video_width, video_height, 0, 0, 65535 );
nkeynes@635
    37
    glMatrixMode(GL_MODELVIEW);
nkeynes@635
    38
    glLoadIdentity();
nkeynes@681
    39
    glEnable( GL_BLEND );
nkeynes@635
    40
    glDisable( GL_TEXTURE_2D );
nkeynes@635
    41
    glDisable( GL_ALPHA_TEST );
nkeynes@635
    42
    glDisable( GL_DEPTH_TEST );
nkeynes@635
    43
    glDisable( GL_SCISSOR_TEST );
nkeynes@635
    44
    glDisable( GL_CULL_FACE );
nkeynes@635
    45
}
nkeynes@635
    46
nkeynes@635
    47
void gl_display_render_buffer( render_buffer_t buffer )
nkeynes@635
    48
{
nkeynes@635
    49
    gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
nkeynes@635
    50
}
nkeynes@635
    51
nkeynes@854
    52
/**
nkeynes@854
    53
 * Convert window coordinates to dreamcast device coords (640x480) using the 
nkeynes@854
    54
 * same viewable area as gl_texture_window.
nkeynes@854
    55
 * If the coordinates are outside the viewable area, the result is -1,-1.
nkeynes@854
    56
 */ 
nkeynes@854
    57
void gl_window_to_system_coords( int *x, int *y )
nkeynes@854
    58
{
nkeynes@854
    59
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@854
    60
nkeynes@854
    61
    int ah = video_width * 0.75;
nkeynes@854
    62
nkeynes@854
    63
    if( ah > video_height ) {
nkeynes@854
    64
        int w = (video_height/0.75);
nkeynes@854
    65
        x1 = (video_width - w)/2;
nkeynes@854
    66
        x2 -= x1;
nkeynes@854
    67
    } else if( ah < video_height ) {
nkeynes@854
    68
        y1 = (video_height - ah)/2;
nkeynes@854
    69
        y2 -= y1;
nkeynes@854
    70
    }
nkeynes@854
    71
    if( *x < x1 || *x >= x2 || *y < y1 || *y >= y2 ) {
nkeynes@854
    72
        *x = -1;
nkeynes@854
    73
        *y = -1;
nkeynes@854
    74
    } else {
nkeynes@854
    75
        *x = (*x - x1) * DISPLAY_WIDTH / (x2-x1);
nkeynes@854
    76
        *y = (*y - y1) * DISPLAY_HEIGHT / (y2-y1);
nkeynes@854
    77
    }
nkeynes@854
    78
}
nkeynes@854
    79
nkeynes@635
    80
void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
nkeynes@635
    81
{
nkeynes@635
    82
    float top, bottom;
nkeynes@635
    83
    if( inverted ) {
nkeynes@1222
    84
        top = 1;
nkeynes@736
    85
        bottom = 0;
nkeynes@635
    86
    } else {
nkeynes@736
    87
        top = 0;
nkeynes@1222
    88
        bottom = 1;
nkeynes@635
    89
    }
nkeynes@635
    90
nkeynes@635
    91
    /* Reset display parameters */
nkeynes@635
    92
    gl_reset_state();
nkeynes@635
    93
    glColor3f( 0,0,0 );    
nkeynes@635
    94
nkeynes@635
    95
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@635
    96
nkeynes@635
    97
    int ah = video_width * 0.75;
nkeynes@635
    98
nkeynes@635
    99
    if( ah > video_height ) {
nkeynes@736
   100
        int w = (video_height/0.75);
nkeynes@736
   101
        x1 = (video_width - w)/2;
nkeynes@736
   102
        x2 -= x1;
nkeynes@635
   103
nkeynes@736
   104
        glBegin( GL_QUADS );
nkeynes@736
   105
        glVertex2f( 0, 0 );
nkeynes@736
   106
        glVertex2f( x1, 0 );
nkeynes@736
   107
        glVertex2f( x1, video_height );
nkeynes@736
   108
        glVertex2f( 0, video_height);
nkeynes@736
   109
        glVertex2f( x2, 0 );
nkeynes@736
   110
        glVertex2f( video_width, 0 );
nkeynes@736
   111
        glVertex2f( video_width, video_height );
nkeynes@736
   112
        glVertex2f( x2, video_height);
nkeynes@736
   113
        glEnd();
nkeynes@635
   114
    } else if( ah < video_height ) {
nkeynes@736
   115
        y1 = (video_height - ah)/2;
nkeynes@736
   116
        y2 -= y1;
nkeynes@736
   117
        glBegin( GL_QUADS );
nkeynes@736
   118
        glVertex2f( 0, 0 );
nkeynes@736
   119
        glVertex2f( video_width, 0 );
nkeynes@736
   120
        glVertex2f( video_width, y1 );
nkeynes@736
   121
        glVertex2f( 0, y1 );
nkeynes@736
   122
        glVertex2f( 0, y2 );
nkeynes@736
   123
        glVertex2f( video_width, y2 );
nkeynes@736
   124
        glVertex2f( video_width, video_height );
nkeynes@736
   125
        glVertex2f( 0, video_height );
nkeynes@736
   126
        glEnd();
nkeynes@635
   127
    }
nkeynes@635
   128
nkeynes@635
   129
    /* Render the textured rectangle */
nkeynes@1222
   130
    glEnable( GL_TEXTURE_2D );
nkeynes@1222
   131
    glBindTexture( GL_TEXTURE_2D, tex_id );
nkeynes@635
   132
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
nkeynes@1222
   133
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nkeynes@1222
   134
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@635
   135
    glEnable( GL_BLEND );
nkeynes@635
   136
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@635
   137
    glBegin( GL_QUADS );
nkeynes@635
   138
    glTexCoord2f( 0, top );
nkeynes@635
   139
    glVertex2f( x1, y1 );
nkeynes@1222
   140
    glTexCoord2f( 1, top );
nkeynes@635
   141
    glVertex2f( x2, y1 );
nkeynes@1222
   142
    glTexCoord2f( 1, bottom );
nkeynes@635
   143
    glVertex2f( x2, y2 );
nkeynes@635
   144
    glTexCoord2f( 0, bottom );
nkeynes@635
   145
    glVertex2f( x1, y2 );
nkeynes@635
   146
    glEnd();
nkeynes@1222
   147
    glDisable( GL_TEXTURE_2D );
nkeynes@635
   148
    glFlush();
nkeynes@635
   149
}
nkeynes@635
   150
nkeynes@635
   151
gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
nkeynes@635
   152
{
nkeynes@635
   153
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@635
   154
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@635
   155
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@635
   156
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@736
   157
nkeynes@635
   158
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@1222
   159
    glBindTexture( GL_TEXTURE_2D, tex_id );
nkeynes@1222
   160
    glTexSubImage2D( GL_TEXTURE_2D, 0, 0,0,
nkeynes@736
   161
                     frame->width, frame->height, format, type, frame->data );
nkeynes@635
   162
    return TRUE;
nkeynes@635
   163
}
nkeynes@635
   164
nkeynes@669
   165
void gl_display_blank( uint32_t colour )
nkeynes@635
   166
{
nkeynes@635
   167
    gl_reset_state();
nkeynes@635
   168
    glColor3ub( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
nkeynes@635
   169
    glRecti(0,0, video_width, video_height );
nkeynes@635
   170
    glFlush();
nkeynes@635
   171
}
nkeynes@635
   172
nkeynes@635
   173
/**
nkeynes@635
   174
 * Generic GL read_render_buffer. This function assumes that the caller
nkeynes@635
   175
 * has already set the appropriate glReadBuffer(); in other words, unless
nkeynes@635
   176
 * there's only one buffer this needs to be wrapped.
nkeynes@635
   177
 */
nkeynes@635
   178
gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@736
   179
                                int rowstride, int colour_format ) 
nkeynes@635
   180
{
nkeynes@635
   181
    glFinish();
nkeynes@635
   182
    GLenum type = colour_formats[colour_format].type;
nkeynes@635
   183
    GLenum format = colour_formats[colour_format].format;
nkeynes@635
   184
    // int line_size = buffer->width * colour_formats[colour_format].bpp;
nkeynes@635
   185
    // int size = line_size * buffer->height;
nkeynes@635
   186
    int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
nkeynes@635
   187
    glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
nkeynes@736
   188
nkeynes@635
   189
    glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@635
   190
    return TRUE;
nkeynes@635
   191
}
.