例2-8 sdl和opengl
/*
* sdl opengl tutorial.
* (c) michael vance, 2000
* briareos@lokigames.com
*
* distributed under terms of the lgpl.
*/
#include <sdl/sdl.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <stdio.h>
#include <stdlib.h>
static glboolean should_rotate = gl_true;
static void quit_tutorial( int code )
{
/*
* quit sdl so we can release the fullscreen
* mode and restore the previous video settings,
* etc.
*/
sdl_quit( );
/* exit program. */
exit( code );
}
static void handle_key_down( sdl_keysym* keysym )
{
/*
* we're only interested if 'esc' has
* been presssed.
*
* exercise:
* handle the arrow keys and have that change the
* viewing position/angle.
*/
switch( keysym->sym ) {
case sdlk_escape:
quit_tutorial( 0 );
break;
case sdlk_space:
should_rotate = !should_rotate;
break;
default:
break;
}
}
static void process_events( void )
{
/* our sdl event placeholder. */
sdl_event event;
/* grab all the events off the queue. */
while( sdl_pollevent( &event ) ) {
switch( event.type ) {
case sdl_keydown:
/* handle key presses. */
handle_key_down( &event.key.keysym );
break;
case sdl_quit:
/* handle quit requests (like ctrl-c). */
quit_tutorial( 0 );
break;
}
}
}
static void draw_screen( void )
{
/* our angle of rotation. */
static float angle = 0.0f;
/*
* exercise:
* replace this awful mess with vertex
* arrays and a call to gldrawelements.
*
* exercise:
* after completing the above, change
* it to use compiled vertex arrays.
*
* exercise:
* verify my windings are correct here ;).
*/
static glfloat v0[] = { -1.0f, -1.0f, 1.0f };
static glfloat v1[] = { 1.0f, -1.0f, 1.0f };
static glfloat v2[] = { 1.0f, 1.0f, 1.0f };
static glfloat v3[] = { -1.0f, 1.0f, 1.0f };
static glfloat v4[] = { -1.0f, -1.0f, -1.0f };
static glfloat v5[] = { 1.0f, -1.0f, -1.0f };
static glfloat v6[] = { 1.0f, 1.0f, -1.0f };
static glfloat v7[] = { -1.0f, 1.0f, -1.0f };
static glubyte red[] = { 255, 0, 0, 255 };
static glubyte green[] = { 0, 255, 0, 255 };
static glubyte blue[] = { 0, 0, 255, 255 };
static glubyte white[] = { 255, 255, 255, 255 };
static glubyte yellow[] = { 0, 255, 255, 255 };
static glubyte black[] = { 0, 0, 0, 255 };
static glubyte orange[] = { 255, 255, 0, 255 };
static glubyte purple[] = { 255, 0, 255, 0 };
/* clear the color and depth buffers. */
glclear( gl_color_buffer_bit | gl_depth_buffer_bit );
/* we don't want to modify the projection matrix. */
glmatrixmode( gl_modelview );
glloadidentity( );
/* move down the z-axis. */
gltranslatef( 0.0, 0.0, -5.0 );
/* rotate. */
glrotatef( angle, 0.0, 1.0, 0.0 );
if( should_rotate ) {
if( ++angle > 360.0f ) {
angle = 0.0f;
}
}
/* send our triangle data to the pipeline. */
glbegin( gl_triangles );
glcolor4ubv( red );
glvertex3fv( v0 );
glcolor4ubv( green );
glvertex3fv( v1 );
glcolor4ubv( blue );
glvertex3fv( v2 );
glcolor4ubv( red );
glvertex3fv( v0 );
glcolor4ubv( blue );
glvertex3fv( v2 );
glcolor4ubv( white );
glvertex3fv( v3 );
glcolor4ubv( green );
glvertex3fv( v1 );
glcolor4ubv( black );
glvertex3fv( v5 );
glcolor4ubv( orange );
glvertex3fv( v6 );
glcolor4ubv( green );
glvertex3fv( v1 );
glcolor4ubv( orange );
glvertex3fv( v6 );
glcolor4ubv( blue );
glvertex3fv( v2 );
glcolor4ubv( black );
glvertex3fv( v5 );
glcolor4ubv( yellow );
glvertex3fv( v4 );
glcolor4ubv( purple );
glvertex3fv( v7 );
glcolor4ubv( black );
glvertex3fv( v5 );
glcolor4ubv( purple );
glvertex3fv( v7 );
glcolor4ubv( orange );
glvertex3fv( v6 );
glcolor4ubv( yellow );
glvertex3fv( v4 );
glcolor4ubv( red );
glvertex3fv( v0 );
glcolor4ubv( white );
glvertex3fv( v3 );
glcolor4ubv( yellow );
glvertex3fv( v4 );
glcolor4ubv( white );
glvertex3fv( v3 );
glcolor4ubv( purple );
glvertex3fv( v7 );
glcolor4ubv( white );
glvertex3fv( v3 );
glcolor4ubv( blue );
glvertex3fv( v2 );
glcolor4ubv( orange );
glvertex3fv( v6 );
glcolor4ubv( white );
glvertex3fv( v3 );
glcolor4ubv( orange );
glvertex3fv( v6 );
glcolor4ubv( purple );
glvertex3fv( v7 );
glcolor4ubv( green