Aghh! did’nt fit.
</font><blockquote><font size=“1” face=“Verdana, Arial”>code:</font><hr /><pre style=“font-size:x-small; font-family: monospace;”>/***************************************************************************
-
Application Identification
-
OPEN GL C++ First Person 3D ENGINE
-
VER 0.1
- A Remake of a Classic original OpenGL Blaine Hodges Example
- Made by a Beginner for Beginners!
- Extremely well commented for novices
- Only 4 functions ! Incredible!
- Only one Small .cpp file to compile
- Easy to Understand. No Fan-Dangled OOP code to unscramble!
- A 3D World with Sun, Starfield, Ground and other 3D Objects
- Walk around your very own 3D World with arrow keys
- Two Fullscren Resolutions available to user
- High Resolution Timer. Aghhh! Recode this, will ya?
- Best Way to Learn OpenGL Primitive Basics Now!
- Modify to your liking
- Free for any use!
- Coded by me!
- Have fun now! Don’t forget to send a post card when you’re there ! He He!
****************************************************************************/
#define WIN32_LEAN_AND_MEAN // This trims down the libraries.
/************************
-
Includes
- Includes and defines
************************/
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <GL/glu.h>
/**************************************
- Prototype Function Declarations
**************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
int DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
/**************************************************
- Declare various variables as global and static
**************************************************/
static int width;
static int height;
static int AskOnce=0;
/************************************************************
-
1.Main Windows Function Declaration and Definition
-
Create instance for window, register window class,
-
Program main loop with object scene animation.
************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int iCmdShow)
{ //Opening Brace For WinMain
// At Start of App, Offer Two Resolutions, 1024*768 or 640x480 In Fullscreen.
if (AskOnce == 0)
{
AskOnce = 1;
if (MessageBox(NULL," Use 1024x768 Resolution instead of 640x480?",“C++ 3D Engine”,MB_YESNO|MB_ICONINFORMATION)==IDYES)
{
width= 1024;
height= 768;
}
else
{
width= 640;
height= 480;
}
}
//WinMain´s Variable Declarations
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
//Variables For Screen
DWORD dWinStyle; //Var to hold Window Style
dWinStyle=WS_POPUP; // Windows Style
RECT WindowRect; // Grabs Rectangle Upper Left and Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
bool quit = FALSE; //Enter Main Program Loop By Default
static int setGlBuffers = 0; // Set variable to execute once Depth section
/*Ground Variables*/
float groundPosX, groundPosY, groundPosZ;
/*Pyramid Variables*/
float pyramidPosX, pyramidPosY, pyramidPosZ;
//Player Controlled Cube Variables
float cubePosInitX, cubePosInitY, cubePosInitZ;
float cubeAngleY, cubeAngleX;
/*float zoomZ = 0.0f;*/
float zoomY = 0.0f;
/* float zoomX = 0.0f;*/
const float piover180 = 0.0174532925f;
float heading;
float xpos;// = 0.0f;
float zpos;// =0.0f;
GLfloat yrot; // Y Rotation
float ypos;// = 0.0f;
GLfloat upDownBob = 0;
float speed = 0.05f;
int render = 0;
// Set Up Window Class
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
//Register The &wc Window Class
if (!RegisterClass(&wc)) // Failed Attempt At Registering Window Class
{
MessageBox(NULL,"Failed Attempt at Registering Window Class.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Exit And Return FALSE
}
DEVMODE dmScreenSettings;
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight =height;
dmScreenSettings.dmBitsPerPel = 16;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// Pop Up A Message Box To Inform User Resolution Failed
MessageBox(NULL,"Failed Resolution Attempt.
Choose A Lower Resolution. ",“ERROR”,MB_OK|MB_ICONSTOP);
return FALSE;
}
// Create Main Window
hWnd = CreateWindow
(
"GLSample",
"C++ 3D Engine", // Window caption
WS_CLIPSIBLINGS| // Must be set for OpenGL to work
WS_CLIPCHILDREN| // WS_CLIPCHILDREN and WS_CLIPSIBLINGS
dWinStyle, // Popup window variable
0, // Initial x position
0, // Initial y position
WindowRect.right-WindowRect.left, // Calculate Adjusted Window Width
WindowRect.bottom-WindowRect.top, // Calculate Adjusted Window Height
NULL, // Parent window handle
NULL, // Window menu handle
hInstance, // Program instance handle
NULL // Creation parameters
);
// Open window maximized
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd);
UpdateWindow (hWnd);
// Hide Mouse Pointer
ShowCursor(FALSE);
// enable OpenGL for the window
EnableOpenGL( hWnd, &hDC, &hRC );
// Program Main Loop
while ( !quit )
{
// check for messages
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
// handle or dispatch messages
if ( msg.message == WM_QUIT )
{
quit = TRUE;
}
else
{
TranslateMessage( &msg );
//Invoke Win CallBack Function
DispatchMessage( &msg );
}
}
else
{
//------------------------------------------
// OpenGL Animation Code Goes Here
//------------------------------------------
//High Resolution Timer Translates/Rotates Objects at same Rate
//On Any Resolution or PC…Errr Does’nt seem to work too well,
//so, recode it.
static LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency))
{
MessageBox(NULL," System Does´nt support High Resolution Timer. ",“ERROR”,MB_OK|MB_ICONSTOP);
return false;
}
LARGE_INTEGER startTime;
QueryPerformanceCounter(&startTime);
static LARGE_INTEGER lastTime = startTime; // put startTime into lastTime
LARGE_INTEGER currentTime;
QueryPerformanceCounter(¤tTime);
double timeStep = ((double)currentTime.QuadPart - (double)lastTime.QuadPart) / (double)frequency.QuadPart;
timeStep = timeStep/0.01;
lastTime = currentTime;
// player controlled variables
GLfloat xtrans = -xpos;
GLfloat ytrans = -ypos-0.25f;
GLfloat ztrans = -zpos;
GLfloat sceneroty = 360.0f - yrot;
int cube = 0;
/*Set depth testing, buffers and Viewport*/
glViewport ( 0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/*Set the Background color to black*/
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
/*Depth Buffer Setup*/
glClearDepth(1.0f);
//Projection Matrix for viewing volumes
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
glFrustum (-width/height, width/height, -1.0, 1.0, 1.0, 10000.0);
// Modelview Matrix for Objects
glMatrixMode (GL_MODELVIEW);
//Clear previous frame color and depth enable drawing of next frame
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//..............................
// XYZ AXIS USER REFERENCE
//..............................
// Give the user a 3 axis reference
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
/*Clear existing internal tansformations by loading identity matrix*/
glLoadIdentity ();
// Entire Group of Lines Can Be Translated(moved) together
float axisPosX =0.0f;
float axisPosY = 0.0f;
float axisPosZ = -1.0f; // move them into the scene -1 unit.
glTranslatef(0.0f , 0.0f , 0.0f);
glTranslatef( axisPosX, axisPosY, axisPosZ);
glColor3f(1.0,0.0,0.0); // Red
//X AXIS
glBegin(GL_LINE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f); // Point origin
glVertex3f(0.3f, 0.0f, 0.0f); // Towards Plus + X Axis
glEnd();
//Y AXIS
glBegin(GL_LINE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f); // Point origin
glVertex3f(0.0f, 0.3f, 0.0f); // Towards Plus + Y Axis
glEnd();
//Z AXIS
glBegin(GL_LINE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f); // Point origin
//Towards Negative - Z Axis and into scene
glVertex3f(0.05f, -0.15f, -0.7f); //(slightly off to be perceived)
glEnd();
/*restore OpenGl´s matrix stack transformations to continue to translatef*/
glPopMatrix ();
//.............................
// SUN
//.............................
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
/*Clear existing internal tansformations by loading identity matrix*/
glLoadIdentity ();
/* float sunRotAnglZ = 0.0f; */
GLfloat angle;
float degree;
float sunPosX = 0.0f, sunPosY = 35.0f, sunPosZ = -100.0f;
glRotatef(sceneroty,0.0f,1.0f,0.0f);
/*glTranslatef(xtrans + sunPosX, ytrans + sunPosY, ztrans + sunPosZ);*/
glTranslatef(0.0f , 0.0f , 0.0f);
glTranslatef(sunPosX, sunPosY, sunPosZ);
glTranslatef(xtrans , ytrans , ztrans);
glColor3f(0.5,0.6,0);
glScalef(10.0f, 10.0f, 10.0f);
glHint(GL_LINE_SMOOTH, GL_NICEST);
glBegin(GL_LINES);
for (degree=0.0; degree < 360.0; degree+=3.0)
{
angle = (GLfloat)degree*3.14159f/180.0f; // change degrees to radians
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(cos(angle), sin(angle), 0.0f);
}
glEnd();
/*restore OpenGl´s matrix stack transformations to continue to translatef*/
glPopMatrix ();
//...............................
//STARFIELD
//...............................
// Front Plane ("Group of points") can be moved together .
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
/*Clear existing internal tansformations by loading identity matrix*/
glLoadIdentity ();
GLfloat frontStarX= 0.0f;
GLfloat frontStarY= 80.0f;
GLfloat frontStarZ = -130.0f;
// draw random points.
glHint(GL_POINT_SMOOTH, GL_NICEST);
// srand(GL_POINTS);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(0.0f , 0.0f , 0.0f);
glTranslatef(frontStarX , frontStarY, frontStarZ);
glTranslatef(xtrans , ytrans , ztrans);
glPointSize(1.0);
glColor3f(1,1,1);
glBegin(GL_POINTS);
glVertex3f ( 111.0, 160.0, -180.0 );
glVertex3f ( -115.0, 115.0, -191.0 );
glVertex3f ( 110.0, 130.0, -102.0 );
glVertex3f ( -115.0, 110.0, -183.0 );
glVertex3f ( 210.0, 140.0, -184.0 );
glVertex3f ( -310.0, 125.0, -120.0 );
glVertex3f ( 351.0, 150.0, -175.0 );
glVertex3f (- 501.0, 115.0, -182.0 );
glVertex3f ( 701.0, 145.0, -183.0 );
glVertex3f ( -701.0, 115.0, -154.0 );
glVertex3f ( 811.0, 160.0, -180.0 );
glVertex3f ( -815.0, 115.0, -191.0 );
glVertex3f ( 910.0, 230.0, -102.0 );
glVertex3f ( -915.0, 210.0, -183.0 );
glVertex3f ( 1010.0, 540.0, -184.0 );
glVertex3f ( -1010.0, 525.0, -120.0);
glVertex3f ( 1551.0, 750.0, -175.0);
glVertex3f (- 1770.0, 715.0, -182.0);
glVertex3f ( 2420.0, 945.0, -183.0);
glVertex3f ( -2420.0, 915.0, -154.0);
glEnd();
/*restore OpenGl´s matrix stack transformations to continue to translatef*/
glPopMatrix ();
//BackPlane ("group of points") can be moved together as one.
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
/*Clear existing internal tansformations by loading identity matrix*/
glLoadIdentity ();
GLfloat backStarX= 0.0f;
GLfloat backStarY= 80.0f;
GLfloat backStarZ = 130.0f;
// draw random points.
glHint(GL_POINT_SMOOTH, GL_NICEST);
//srand(GL_POINTS);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(0.0f , 0.0f , 0.0f);
glTranslatef(backStarX, backStarY, backStarZ);
glTranslatef(xtrans , ytrans , ztrans);
glPointSize(1.0);
glColor3f(1,1,1);
glBegin(GL_POINTS);
glVertex3f ( -111.0, 160.0, 180.0 );
glVertex3f ( 115.0, 115.0, 191.0 );
glVertex3f ( -110.0, 130.0, 102.0 );
glVertex3f ( 115.0, 110.0, 183.0 );
glVertex3f ( -210.0, 140.0, 184.0 );
glVertex3f ( 310.0, 125.0, 120.0 );
glVertex3f ( -351.0, 150.0, 175.0 );
glVertex3f (501.0, 15.0, 182.0 );
glVertex3f ( -701.0, 145.0, 183.0 );
glVertex3f (701.0, 115.0, 154.0 );
glVertex3f ( -811.0, 260.0, 180.0 );
glVertex3f ( 815.0, 215.0, 191.0 );
glVertex3f ( -910.0, 330.0, 102.0 );
glVertex3f ( 915.0, 310.0, 183.0 );
glVertex3f ( -1010.0, 440.0, 184.0 );
glVertex3f ( 1010.0, 425.0, 120.0 );
glVertex3f ( -1551.0, 650.0, 175.0 );
glVertex3f (1770.0, 615.0, 182.0 );
glVertex3f ( -2420.0, 845.0, 183.0 );
glVertex3f ( 2480.0, 815.0, 154.0 );
glEnd();
/*restore OpenGl´s matrix stack transformations to continue to translatef*/
glPopMatrix ();
//.......................
//PUT GROUND IN SCENE
//........................
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
/*Clear existing internal tansformations by loading identity matrix*/
glLoadIdentity ();
//Initial ground position values
GLfloat initGroundPosX = 0.0f;
GLfloat initGroundPosY = -1.0f;
GLfloat initGroundPosZ = 0.0f;
/*rotate the scene inversely to camera*/
glRotatef(sceneroty,0,1.0f,0);
/*setup to move ground where it will be drawn*/
glTranslatef(0.0f , 0.0f , 0.0f);
glTranslatef(initGroundPosX , initGroundPosY , initGroundPosZ );
glTranslatef(xtrans , ytrans , ztrans);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
//Each of the 4 vertices of Ground has three pos and color coordinates
//left front vertex
glColor3f(0.5f, 0.6f, 1.0f); glVertex3f(-25.0, 0.0,25.0);
//right front vertex
glColor3f(1.0f, 0.4f, 0.3f); glVertex3f(25.0, 0.0, 25.0);
//right back vertex
glColor3f(0.2f, 0.6f, 1.0f); glVertex3f(25.0, 0.0,-25.0);
//left back vertex
glColor3f(0.6f, 0.4f, 0.7f); glVertex3f(-25.0, 0.0,-25.0);
glEnd();
/*restore OpenGl´s matrix stack transformations to continue to translatef*/
glPopMatrix ();
//.......................
//PUT PYRAMID IN SCENE
//......................
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
/*Clear existing internal tansformations by loading identity matrix*/
glLoadIdentity ();
//Initial pyramid positions
GLfloat initPyramidPosX = 0.0f;
GLfloat initPyramidPosY = 1.0f;
GLfloat initPyramidPosZ = -10.0f;
/*rotate the scene inversely to camera*/
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(0.0f , 0.0f , 0.0f);
glTranslatef(initPyramidPosX , initPyramidPosY, initPyramidPosZ );
glTranslatef(xtrans , ytrans , ztrans);
/*Join vertex 1,2,3..1,3,4 and 1,4,5(vertice 1 is common to the rest)*/
glBegin (GL_TRIANGLE_FAN);
/*assign color and position in 3d space to each vertex thru its 3 coords*/
glColor3f (0,.5, 1); glVertex3f ( 0, 3, 0);
glColor3f (1, .5, 0); glVertex3f (-3,-3, 3);
glColor3f (1, .3, 1); glVertex3f ( 3,-3, 3);
glColor3f (.2, 0, .7); glVertex3f ( 3,-3,-3);
glColor3f (0, 1, 0); glVertex3f (-3,-3,-3);
glColor3f (.8, .7, 0); glVertex3f (-3,-3, 3);
glEnd ();
/*restore OpenGl´s matrix stack transformations to continue to translatef*/
glPopMatrix ();
//........................................................
//Cube with nice mix of colors
//........................................................
glPushMatrix ();
glLoadIdentity();
cubePosInitX = -10.0f;
cubePosInitY = -0.5f;
cubePosInitZ = -15.5f;
glRotatef(sceneroty,0,1.0f,0);
/*move all vertices(the Cube) on axis into the screen*/
glTranslatef(0.0f , 0.0f , 0.0f);
glTranslatef (cubePosInitX , cubePosInitY, cubePosInitZ );
glTranslatef(xtrans , ytrans , ztrans);
/*assign color and position in 3d space to each vertex thru its 3 coords*/
glBegin(GL_QUADS); //front
glNormal3f(0.0, 0.0, 1.0);
glColor3f(1.0f, 0.0f, 0.0f);glVertex3f(-.5f, -.5f, .5f);
glColor3f(0.0f, 1.0f, 0.0f);glVertex3f( .5f, -.5f, .5f);
glColor3f(0.0f, 0.0f, 1.0f);glVertex3f( .5f, .5f, .5f);
glColor3f(1.0f, 1.0f, 0.0f);glVertex3f(-.5f, .5f, .5f);
glEnd();
glBegin(GL_QUADS); //back
glNormal3f(0.0, 0.0, -1.0);
glColor3f(0.0f, 1.0f, 0.0f);glVertex3f( .5f, -.5f, -.5f);
glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-.5f, -.5f, -.5f);
glColor3f(1.0f, 1.0f, 0.0f);glVertex3f(-.5f, .5f, -.5f);
glColor3f(0.0f, 1.0f, 1.0f);glVertex3f( .5f, .5f, -.5f);
glEnd();
glBegin(GL_QUADS); //left
glNormal3f(-1.0, 0.0, 0.0);
glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-.5f, -.5f, -.5f);
glColor3f(1.0f, 1.0f, 0.0f);glVertex3f(-.5f, -.5f, .5f);
glColor3f(0.0f, 1.0f, 1.0f);glVertex3f(-.5f, .5f, .5f);
glColor3f(1.0f, 0.0f, 1.0f);glVertex3f(-.5f, .5f, -.5f);
glEnd();
glBegin(GL_QUADS); //right
glNormal3f(1.0, 0.0, 0.0);
glColor3f(1.0f, 1.0f, 0.0f);glVertex3f( .5f, -.5f, .5f);
glColor3f(0.0f, 1.0f, 1.0f);glVertex3f( .5f, -.5f, -.5f);
glColor3f(1.0f, 0.0f, 1.0f);glVertex3f( .5f, .5f, -.5f);
glColor3f(1.0f, 0.0f, 0.0f);glVertex3f( .5f, .5f, .5f);
glEnd();
glBegin(GL_QUADS); //top
glNormal3f(0.0, 1.0, 0.0);
glColor3f(0.0f, 1.0f, 1.0f);glVertex3f(-.5f, .5f, .5f);
glColor3f(1.0f, 0.0f, 1.0f);glVertex3f( .5f, .5f, .5f);
glColor3f(1.0f, 0.0f, 0.0f);glVertex3f( .5f, .5f, -.5f);
glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(-.5f, .5f, -.5f);
glEnd();
glBegin(GL_QUADS); //bottom
glNormal3f(0.0, -1.0, 0.0);
glColor3f(1.0f, 0.0f, 1.0f);glVertex3f( .5f, -.5f, -.5f);
glColor3f(1.0f, 0.0f, 0.0f);glVertex3f( .5f, -.5f, .5f);
glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(-.5f, -.5f, .5f);
glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-.5f, -.5f, -.5f);
glEnd();
/*restore OpenGl´s matrix stack transformations to continue to translatef*/
glPopMatrix ();
/*Make Scene Visible*/
SwapBuffers( hDC );
/*Player Controlled Translations and Rotations, Ect..*/
float cubeX;
float cubeY;
float cubeZ;
if(GetKeyState(VK_UP) & 0x80