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 736:a02d1475ccfd
prev681:1755a126b109
next854:130928a3cdcb
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
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@681
    41
    glDisable( GL_TEXTURE_RECTANGLE_ARB );
nkeynes@635
    42
    glDisable( GL_ALPHA_TEST );
nkeynes@635
    43
    glDisable( GL_DEPTH_TEST );
nkeynes@635
    44
    glDisable( GL_SCISSOR_TEST );
nkeynes@635
    45
    glDisable( GL_CULL_FACE );
nkeynes@635
    46
    glDrawBuffer( GL_FRONT );
nkeynes@635
    47
}
nkeynes@635
    48
nkeynes@635
    49
void gl_display_render_buffer( render_buffer_t buffer )
nkeynes@635
    50
{
nkeynes@635
    51
    gl_texture_window( buffer->width, buffer->height, buffer->buf_id, buffer->inverted );
nkeynes@635
    52
}
nkeynes@635
    53
nkeynes@635
    54
void gl_texture_window( int width, int height, int tex_id, gboolean inverted )
nkeynes@635
    55
{
nkeynes@635
    56
    float top, bottom;
nkeynes@635
    57
    if( inverted ) {
nkeynes@736
    58
        top = ((float)height);
nkeynes@736
    59
        bottom = 0;
nkeynes@635
    60
    } else {
nkeynes@736
    61
        top = 0;
nkeynes@736
    62
        bottom = ((float)height);
nkeynes@635
    63
    }
nkeynes@635
    64
nkeynes@635
    65
    /* Reset display parameters */
nkeynes@635
    66
    gl_reset_state();
nkeynes@635
    67
    glColor3f( 0,0,0 );    
nkeynes@635
    68
nkeynes@635
    69
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@635
    70
nkeynes@635
    71
    int ah = video_width * 0.75;
nkeynes@635
    72
nkeynes@635
    73
    if( ah > video_height ) {
nkeynes@736
    74
        int w = (video_height/0.75);
nkeynes@736
    75
        x1 = (video_width - w)/2;
nkeynes@736
    76
        x2 -= x1;
nkeynes@635
    77
nkeynes@736
    78
        glBegin( GL_QUADS );
nkeynes@736
    79
        glVertex2f( 0, 0 );
nkeynes@736
    80
        glVertex2f( x1, 0 );
nkeynes@736
    81
        glVertex2f( x1, video_height );
nkeynes@736
    82
        glVertex2f( 0, video_height);
nkeynes@736
    83
        glVertex2f( x2, 0 );
nkeynes@736
    84
        glVertex2f( video_width, 0 );
nkeynes@736
    85
        glVertex2f( video_width, video_height );
nkeynes@736
    86
        glVertex2f( x2, video_height);
nkeynes@736
    87
        glEnd();
nkeynes@635
    88
    } else if( ah < video_height ) {
nkeynes@736
    89
        y1 = (video_height - ah)/2;
nkeynes@736
    90
        y2 -= y1;
nkeynes@736
    91
        glBegin( GL_QUADS );
nkeynes@736
    92
        glVertex2f( 0, 0 );
nkeynes@736
    93
        glVertex2f( video_width, 0 );
nkeynes@736
    94
        glVertex2f( video_width, y1 );
nkeynes@736
    95
        glVertex2f( 0, y1 );
nkeynes@736
    96
        glVertex2f( 0, y2 );
nkeynes@736
    97
        glVertex2f( video_width, y2 );
nkeynes@736
    98
        glVertex2f( video_width, video_height );
nkeynes@736
    99
        glVertex2f( 0, video_height );
nkeynes@736
   100
        glEnd();
nkeynes@635
   101
    }
nkeynes@635
   102
nkeynes@635
   103
    /* Render the textured rectangle */
nkeynes@635
   104
    glEnable( GL_TEXTURE_RECTANGLE_ARB );
nkeynes@635
   105
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
nkeynes@635
   106
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
nkeynes@635
   107
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nkeynes@635
   108
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@635
   109
    glEnable( GL_BLEND );
nkeynes@635
   110
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@635
   111
    glBegin( GL_QUADS );
nkeynes@635
   112
    glTexCoord2f( 0, top );
nkeynes@635
   113
    glVertex2f( x1, y1 );
nkeynes@635
   114
    glTexCoord2f( ((float)width), top );
nkeynes@635
   115
    glVertex2f( x2, y1 );
nkeynes@635
   116
    glTexCoord2f( ((float)width), bottom );
nkeynes@635
   117
    glVertex2f( x2, y2 );
nkeynes@635
   118
    glTexCoord2f( 0, bottom );
nkeynes@635
   119
    glVertex2f( x1, y2 );
nkeynes@635
   120
    glEnd();
nkeynes@635
   121
    glDisable( GL_TEXTURE_RECTANGLE_ARB );
nkeynes@635
   122
    glFlush();
nkeynes@635
   123
}
nkeynes@635
   124
nkeynes@635
   125
gboolean gl_load_frame_buffer( frame_buffer_t frame, int tex_id )
nkeynes@635
   126
{
nkeynes@635
   127
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@635
   128
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@635
   129
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@635
   130
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@736
   131
nkeynes@635
   132
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@635
   133
    glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex_id );
nkeynes@635
   134
    glTexSubImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, 0,0,
nkeynes@736
   135
                     frame->width, frame->height, format, type, frame->data );
nkeynes@635
   136
    return TRUE;
nkeynes@635
   137
}
nkeynes@635
   138
nkeynes@669
   139
void gl_display_blank( uint32_t colour )
nkeynes@635
   140
{
nkeynes@635
   141
    gl_reset_state();
nkeynes@635
   142
    glColor3ub( (colour >> 16) & 0xFF, (colour >> 8) & 0xFF, colour & 0xFF );
nkeynes@635
   143
    glRecti(0,0, video_width, video_height );
nkeynes@635
   144
    glFlush();
nkeynes@635
   145
}
nkeynes@635
   146
nkeynes@635
   147
/**
nkeynes@635
   148
 * Generic GL read_render_buffer. This function assumes that the caller
nkeynes@635
   149
 * has already set the appropriate glReadBuffer(); in other words, unless
nkeynes@635
   150
 * there's only one buffer this needs to be wrapped.
nkeynes@635
   151
 */
nkeynes@635
   152
gboolean gl_read_render_buffer( unsigned char *target, render_buffer_t buffer, 
nkeynes@736
   153
                                int rowstride, int colour_format ) 
nkeynes@635
   154
{
nkeynes@635
   155
    glFinish();
nkeynes@635
   156
    GLenum type = colour_formats[colour_format].type;
nkeynes@635
   157
    GLenum format = colour_formats[colour_format].format;
nkeynes@635
   158
    // int line_size = buffer->width * colour_formats[colour_format].bpp;
nkeynes@635
   159
    // int size = line_size * buffer->height;
nkeynes@635
   160
    int glrowstride = (rowstride / colour_formats[colour_format].bpp) - buffer->width;
nkeynes@635
   161
    glPixelStorei( GL_PACK_ROW_LENGTH, glrowstride );
nkeynes@736
   162
nkeynes@635
   163
    glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
nkeynes@635
   164
    return TRUE;
nkeynes@635
   165
}
.