|
OpenGL Tutorial
Index:
Installation
Overview
Ex.1: Square
Installation
Download the file GLexample.cpp and MSVC++ people also download GLexample.mak. Make
a permanent directory for these files. Open the makefile.
Overview
The OpenGL Aux library allows programmers to perform simple tasks such as opening a window, without having to learn platform-specific windowing commands. Aux creates a
window for OpenGL to draw into, then calls your display function to draw into the window.
Example 1: Square
// Windows builds differ from the OpenGL Programming Guide // in that they require the Windows header to be included // before the OpenGL header. Also, the display function
// callback must use the __stdcall convention in Windows.
#ifdef WIN32 # include <windows.h> # ifndef WINAPI
# define WINAPI __stdcall # endif #else # ifndef WINAPI
# define WINAPI # endif #endif // WIN32
// gl.h contains the declarations for functions starting with "gl".
// glaux.h contains "aux" functions.
#include <GL/gl.h> #include <GL/glaux.h>
// aux display callback as defined by OpenGL.
void WINAPI displayfunc( void );
// The main function does three things: // 1) Sets window properties. // 2) Opens a window. // 3) Calls an aux dispatch loop that displays your scene and // waits for user input.
int main( int argc, char *argv[] ) { ::auxInitDisplayMode( AUX_RGBA | AUX_SINGLE | AUX_DEPTH );
::auxInitPosition( 0, 0, 500, 500 ); ::auxInitWindow( argv[0] );
::auxMainLoop( &displayfunc ); return 0; }
// Your display function.
void WINAPI displayfunc( void ) { // Clear the screen to black ::glClearColor( 0.f, 0.f, 0.f, 0.f );
::glClear( GL_COLOR_BUFFER_BIT );
// Set the current color to white ::glColor3f( 1.f, 1.f, 1.f );
// Set the projection to ortho
::glMatrixMode( GL_PROJECTION ); ::glLoadIdentity(); ::glOrtho( -1.f, 1.f, -1.f, 1.f, -1.f, 1.f ); 0
// Draw a filled square.
::glBegin( GL_POLYGON ); ::glVertex2f( -0.5f, -0.5f ); ::glVertex2f( -0.5f, 0.5f );
::glVertex2f( 0.5f, 0.5f ); ::glVertex2f( 0.5f, -0.5f ); ::glEnd();
// Execute all queued OpenGL commands. ::glFlush(); }
Example 2: Pyramid
#ifdef WIN32 # include <windows.h> #endif // WIN32
#include <stdio.h>
#include <GL/gl.h> #include <GL/glu.h>
#include <GL/glaux.h>
// Our own little function to draw a polygon given a set of vertices void draw3d( const float [][3], int );
void main( void ) {
::auxInitDisplayMode( AUX_SINGLE | AUX_RGBA ); ::auxInitPosition( 0, 0, 500, 500 ); ::auxInitWindow( "simple" );
::glClearColor( 0.f, 0.f, 0.f, 0.f ); ::glClear( GL_COLOR_BUFFER_BIT ); ::glColor3f( 1.f, 1.f, 1.f );
::glMatrixMode( GL_PROJECTION ); ::glLoadIdentity(); // fov (y-direction), aspect, near, far
::gluPerspective( 90.0f, 1.0f, 0.1f, 10.0f );
::glMatrixMode( GL_MODELVIEW ); ::glLoadIdentity();
::glTranslatef( 0.f, 0.f, -5.f );
const float base[][3] = { {1.f, 1.f, 0.f},
{-1.f, 1.f, 0.f}, {-1.f, -1.f, 0.f}, {1.f, -1.f, 0.f}
}; const float side1[][3] = { {1.f, 1.f, 0.f}, {-1.f, 1.f, 0.f},
{0.f, 0.f, 1.f} }; const float side2[][3] = {
{-1.f, 1.f, 0.f}, {-1.f, -1.f, 0.f}, {0.f, 0.f, 1.f}
}; const float side3[][3] = { {-1.f, -1.f, 0.f},
{1.f, -1.f, 0.f}, {0.f, 0.f, 1.f} };
const float side4[][3] = { {1.f, -1.f, 0.f}, {1.f, 1.f, 0.f},
{0.f, 0.f, 1.f} };
draw3d( base, 4 ); draw3d( side1, 3 );
draw3d( side2, 3 ); draw3d( side3, 3 ); draw3d( side4, 3 );
::glFlush();
::auxMainLoop(NULL); }
void draw3d( const float verts[][3], int num ) { ::glBegin( GL_POLYGON );
for ( int i = 0; i
|