help with basic opengl 3d engine
i found a basic opengl 3d engine on the 'net....free for use....
i'm using dev-c++. i got it set up allright.....but nothing shows after i run it.......can i post the code here for you guys to check out?
heh, sure, just be sure to use the source tags(or post a link to a zip/tarball that has your source), and post a link to the game engine site.
http://www.roboguy.net(WIP) - lisperati - SICP - Haskell - Python - OCaml - Lambda the Ultimate - Good Math, Bad Math - Wiki (not Wikipedia) - Pure - Term-Rewriting Functional Language
don't remember the site ( it was " white paper" thing that said use, modify as you will ,no liscense )but......here's the stuff :
make " empty project"
libs needed in parameters:
lib/libadvapi32.a
lib/libglu32.a
lib/libkernel32.a
lib/libuser32.a
lib/libgdi32.a
lib/libwinspool.a
lib/libcomdlg32.a
lib/libadvapi32.a
lib/libshell32.a
lib/libole32.a
lib/liboleaut32.a
lib/libuuid.a
lib/libodbc32.a
lib/libodbccp32.a
lib/libopengl32.a
code :
#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 static to application
//**************************************************/
static int width;
static int height;
/************************************************************
* 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 (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
//These vars move entire scene wether
//individual objects are stationary or
//moving and "need" to go before piover,
//heading, xpos, ypos, ect. below.
float initScenePosX = 1.9f;
float initScenePosY = 1.9f;
float initScenePosZ = 1.9f; // put other values here to see effect
// Various scene Variables take Que from
// previous initScenePos vars
const float piover180 = 0.0174532925f;
float heading;
float xpos;
float zpos;
GLfloat yrot; // Y Rotation
float ypos; //
GLfloat Bob = 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
}
// 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
);
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 that the Resolution Failed
MessageBox(NULL,"Failed Resolution Attempt. \n Choose A Lower Resolution. ","ERROR",MB_OK|MB_ICONSTOP);
return FALSE;
}
// Open window
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd);
UpdateWindow (hWnd);
// Hide Mouse Pointer
ShowCursor(FALSE);
// Call enable OpenGL function for the window
EnableOpenGL( hWnd, &hDC, &hRC );
//Set depth testing, buffers and Viewport
glViewport ( 0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glShadeModel(GL_SMOOTH);
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, 1000.0);
// Modelview Matrix for Objects
glMatrixMode (GL_MODELVIEW);
// App. identification info
MessageBox(NULL, " Move Around---> Arrow Keys", "C++ OpenGL 3D Engine", MB_OK | MB_ICONINFORMATION);
// 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....Try different timers to see what happens
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;
//Clear previous frame color and depth enable drawing of next frame
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// player controlled variables
GLfloat xtrans = -xpos;
GLfloat ytrans = -ypos-0.25f;
GLfloat ztrans = -zpos;
GLfloat sceneroty = 360.0f - yrot;
float speed = 0.05f;
// Declare other 3D objects vars to translate and rotate
// The values to these vars can be defined and incremented
// after swapbuffers and before user keyboard section
GLfloat rotateCube;
// Clear existing internal tansformations by loading identity matrix for entire scene
glLoadIdentity ();
//.........................................
// XYZ AXIS
//
// Always stays put in the same place
// since it's placed before any scene
// translations or rotations
//..........................................
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
// Entire Group of Lines Can Be Translated(moved) together
GLfloat axisPosX =0.0f;
GLfloat axisPosY = 0.0f;
GLfloat axisPosZ = -1.0f; // move them into the scene -1 unit.
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 ();
//Rotate automatically the entire scene
glRotatef(sceneroty,0.0f,1.0f,0.0f);
//Translate automatically the entire scene
glTranslatef(xtrans , ytrans , ztrans);
//translate automatically the entire scene to it´s desired init position
glTranslatef(initScenePosX, initScenePosY, initScenePosZ);
// Access OpenGl´s matrix stack to save transformations for entire scene
glPushMatrix ();
//.............................
// SUN
//.............................
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
GLfloat angle;
float degree;
float sunPosX = 0.0f, sunPosY = 35.0f, sunPosZ = -100.0f;
glTranslatef(sunPosX, sunPosY, sunPosZ);
glColor3f(0.5,0.6,1);
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 ();
GLfloat frontStarX= 0.0f;
GLfloat frontStarY= 80.0f;
GLfloat frontStarZ = -130.0f;
// draw random points.
glHint(GL_POINT_SMOOTH, GL_NICEST);
glTranslatef(frontStarX , frontStarY, frontStarZ);
glPointSize(1.0);
glColor3f(1,.5,.5);
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 ();
GLfloat backStarX= 0.0f;
GLfloat backStarY= 80.0f;
GLfloat backStarZ = 130.0f;
glHint(GL_POINT_SMOOTH, GL_NICEST);
glTranslatef(backStarX, backStarY, backStarZ);
glPointSize(1.0);
glColor3f(1,1,.5);
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 ();
//Initial ground position values
GLfloat initGroundPosX = 0.0f;
GLfloat initGroundPosY = -1.0f;
GLfloat initGroundPosZ = 0.0f;
glTranslatef(initGroundPosX , initGroundPosY , initGroundPosZ );
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 ();
//Initial pyramid positions
GLfloat initPyramidPosX = 0.0f;
GLfloat initPyramidPosY = 1.0f;
GLfloat initPyramidPosZ = -10.0f;
glTranslatef(initPyramidPosX , initPyramidPosY, initPyramidPosZ );
/*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 ();
float cubePosInitX = -10.0f;
float cubePosInitY = 1.5f; // lift cube off ground
float cubePosInitZ = -15.5f;
// Translate the cube to it's individual init position in Scene
glTranslatef (cubePosInitX , cubePosInitY, cubePosInitZ );
//rotate automatically the cube on it's Y axis
glRotatef ( rotateCube , 0.0f, 1.0f , 0.0f);
// translate the cube in a circular path around pyramid as it
// spins on it's own Y axis
glTranslatef (0.0f, 0.0f, -20.0f );
/*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 */
glPopMatrix ();
//Restore OpenGl´s matrix stack transformations to continue to translatef entire scene
glPopMatrix ();
// glFlush();
/*Make Scene Visible*/
SwapBuffers( hDC );
// Other 3D Object Rotation and Translation Values
rotateCube += 0.5f* timeStep;
//Keyboard Player Controlled Translations and Rotations, Ect..
if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80)
{
xpos -= (float)sin(heading*piover180) * speed*timeStep;
zpos -= (float)cos(heading*piover180) * speed*timeStep;
if (Bob >= 359.0f)
{
Bob = 0.0f;
}
else
{
Bob += 20;
}
ypos = (float)sin(Bob * piover180)/20.0f* timeStep ;
}
if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80)
{
xpos += (float)sin(heading*piover180) * speed*timeStep;
zpos += (float)cos(heading*piover180) * speed*timeStep;
if (Bob <= 1.0f)
{
Bob = 359.0f;
}
else
{
Bob-= 20;
}
ypos = (float)sin(Bob * piover180)/20.0f * timeStep;
}
if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80)
{
heading -= 1.0f*timeStep;
yrot = heading;
}
if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80)
{
heading += 1.0f*timeStep;
yrot = heading;
}
// Control_F4 as a quick escape key sequence
if(GetKeyState(VK_CONTROL) & 0x80 && GetKeyState(VK_F4)& 0x80)
{
quit = TRUE; // set bool var quit, to true and exit
}
} //Closes Else of OpenGL Animation Loop
} // Closes While quit Is Not Equal To True
// This section of WinMain executes on the way out or if var quit is TRUE
// Shutdown OpenGL by calling disable function
DisableOpenGL( hWnd, hDC, hRC );
/*Destroy the Window*/
if (!DestroyWindow(hWnd)) // Window Destroyed?
{
MessageBox(NULL,"Failed Attempt At Destroying Window.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}
/*Unregister the Class*/
if (!UnregisterClass("GLSample",hInstance)) // Was Class Unregistered?
{
MessageBox(NULL,"Failed Attempt At Unregistering Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
return msg.wParam;
} // Closing Brace For WinMain
/*************************************************
* 2.Window Callback Function (Body)Definition
* Messages, user keyboard
* Event Driven
**************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_SYSCOMMAND:
switch(lParam)
{
case SC_SCREENSAVE: //Screen saver starting?
case SC_MONITORPOWER: //PowerSave Mode?
return 0;
}
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch ( wParam )
{
case VK_ESCAPE:
// Show Mouse Pointer to be able to Select
ShowCursor(TRUE);
if (MessageBox(NULL," Are You Sure You Want To Quit? "," Eureka C++ 3D Engine ",MB_YESNO|MB_ICONQUESTION)==IDYES)
{
PostQuitMessage(0); // set quit to TRUE
}
else
{
// Hide Mouse Pointer to Continue
ShowCursor(FALSE);
}
return 0;
}
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
}
}
/**************************************************************************
* 3. Enable OpenGL Function
* Pixel format description, device context and it´s Ogl render context,
***************************************************************************/
int EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
if ( (iFormat = ChoosePixelFormat(*hDC, &pfd)) == 0 )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if (SetPixelFormat(*hDC, iFormat, &pfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
/* make render context current with current device context*/
wglMakeCurrent( *hDC, *hRC );
return 0;
}
/**************************************************
* 4. Disable OpenGL Function
*
***************************************************/
int DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
// Restore default desktop mode with mouse pointer
ChangeDisplaySettings(NULL,0);
return 0;
}
make " empty project"
libs needed in parameters:
lib/libadvapi32.a
lib/libglu32.a
lib/libkernel32.a
lib/libuser32.a
lib/libgdi32.a
lib/libwinspool.a
lib/libcomdlg32.a
lib/libadvapi32.a
lib/libshell32.a
lib/libole32.a
lib/liboleaut32.a
lib/libuuid.a
lib/libodbc32.a
lib/libodbccp32.a
lib/libopengl32.a
code :
#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 static to application
//**************************************************/
static int width;
static int height;
/************************************************************
* 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 (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
//These vars move entire scene wether
//individual objects are stationary or
//moving and "need" to go before piover,
//heading, xpos, ypos, ect. below.
float initScenePosX = 1.9f;
float initScenePosY = 1.9f;
float initScenePosZ = 1.9f; // put other values here to see effect
// Various scene Variables take Que from
// previous initScenePos vars
const float piover180 = 0.0174532925f;
float heading;
float xpos;
float zpos;
GLfloat yrot; // Y Rotation
float ypos; //
GLfloat Bob = 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
}
// 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
);
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 that the Resolution Failed
MessageBox(NULL,"Failed Resolution Attempt. \n Choose A Lower Resolution. ","ERROR",MB_OK|MB_ICONSTOP);
return FALSE;
}
// Open window
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd);
UpdateWindow (hWnd);
// Hide Mouse Pointer
ShowCursor(FALSE);
// Call enable OpenGL function for the window
EnableOpenGL( hWnd, &hDC, &hRC );
//Set depth testing, buffers and Viewport
glViewport ( 0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glShadeModel(GL_SMOOTH);
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, 1000.0);
// Modelview Matrix for Objects
glMatrixMode (GL_MODELVIEW);
// App. identification info
MessageBox(NULL, " Move Around---> Arrow Keys", "C++ OpenGL 3D Engine", MB_OK | MB_ICONINFORMATION);
// 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....Try different timers to see what happens
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;
//Clear previous frame color and depth enable drawing of next frame
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// player controlled variables
GLfloat xtrans = -xpos;
GLfloat ytrans = -ypos-0.25f;
GLfloat ztrans = -zpos;
GLfloat sceneroty = 360.0f - yrot;
float speed = 0.05f;
// Declare other 3D objects vars to translate and rotate
// The values to these vars can be defined and incremented
// after swapbuffers and before user keyboard section
GLfloat rotateCube;
// Clear existing internal tansformations by loading identity matrix for entire scene
glLoadIdentity ();
//.........................................
// XYZ AXIS
//
// Always stays put in the same place
// since it's placed before any scene
// translations or rotations
//..........................................
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
// Entire Group of Lines Can Be Translated(moved) together
GLfloat axisPosX =0.0f;
GLfloat axisPosY = 0.0f;
GLfloat axisPosZ = -1.0f; // move them into the scene -1 unit.
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 ();
//Rotate automatically the entire scene
glRotatef(sceneroty,0.0f,1.0f,0.0f);
//Translate automatically the entire scene
glTranslatef(xtrans , ytrans , ztrans);
//translate automatically the entire scene to it´s desired init position
glTranslatef(initScenePosX, initScenePosY, initScenePosZ);
// Access OpenGl´s matrix stack to save transformations for entire scene
glPushMatrix ();
//.............................
// SUN
//.............................
/*access OpenGl´s matrix stack to save transformations*/
glPushMatrix ();
GLfloat angle;
float degree;
float sunPosX = 0.0f, sunPosY = 35.0f, sunPosZ = -100.0f;
glTranslatef(sunPosX, sunPosY, sunPosZ);
glColor3f(0.5,0.6,1);
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 ();
GLfloat frontStarX= 0.0f;
GLfloat frontStarY= 80.0f;
GLfloat frontStarZ = -130.0f;
// draw random points.
glHint(GL_POINT_SMOOTH, GL_NICEST);
glTranslatef(frontStarX , frontStarY, frontStarZ);
glPointSize(1.0);
glColor3f(1,.5,.5);
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 ();
GLfloat backStarX= 0.0f;
GLfloat backStarY= 80.0f;
GLfloat backStarZ = 130.0f;
glHint(GL_POINT_SMOOTH, GL_NICEST);
glTranslatef(backStarX, backStarY, backStarZ);
glPointSize(1.0);
glColor3f(1,1,.5);
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 ();
//Initial ground position values
GLfloat initGroundPosX = 0.0f;
GLfloat initGroundPosY = -1.0f;
GLfloat initGroundPosZ = 0.0f;
glTranslatef(initGroundPosX , initGroundPosY , initGroundPosZ );
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 ();
//Initial pyramid positions
GLfloat initPyramidPosX = 0.0f;
GLfloat initPyramidPosY = 1.0f;
GLfloat initPyramidPosZ = -10.0f;
glTranslatef(initPyramidPosX , initPyramidPosY, initPyramidPosZ );
/*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 ();
float cubePosInitX = -10.0f;
float cubePosInitY = 1.5f; // lift cube off ground
float cubePosInitZ = -15.5f;
// Translate the cube to it's individual init position in Scene
glTranslatef (cubePosInitX , cubePosInitY, cubePosInitZ );
//rotate automatically the cube on it's Y axis
glRotatef ( rotateCube , 0.0f, 1.0f , 0.0f);
// translate the cube in a circular path around pyramid as it
// spins on it's own Y axis
glTranslatef (0.0f, 0.0f, -20.0f );
/*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 */
glPopMatrix ();
//Restore OpenGl´s matrix stack transformations to continue to translatef entire scene
glPopMatrix ();
// glFlush();
/*Make Scene Visible*/
SwapBuffers( hDC );
// Other 3D Object Rotation and Translation Values
rotateCube += 0.5f* timeStep;
//Keyboard Player Controlled Translations and Rotations, Ect..
if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80)
{
xpos -= (float)sin(heading*piover180) * speed*timeStep;
zpos -= (float)cos(heading*piover180) * speed*timeStep;
if (Bob >= 359.0f)
{
Bob = 0.0f;
}
else
{
Bob += 20;
}
ypos = (float)sin(Bob * piover180)/20.0f* timeStep ;
}
if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80)
{
xpos += (float)sin(heading*piover180) * speed*timeStep;
zpos += (float)cos(heading*piover180) * speed*timeStep;
if (Bob <= 1.0f)
{
Bob = 359.0f;
}
else
{
Bob-= 20;
}
ypos = (float)sin(Bob * piover180)/20.0f * timeStep;
}
if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80)
{
heading -= 1.0f*timeStep;
yrot = heading;
}
if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80)
{
heading += 1.0f*timeStep;
yrot = heading;
}
// Control_F4 as a quick escape key sequence
if(GetKeyState(VK_CONTROL) & 0x80 && GetKeyState(VK_F4)& 0x80)
{
quit = TRUE; // set bool var quit, to true and exit
}
} //Closes Else of OpenGL Animation Loop
} // Closes While quit Is Not Equal To True
// This section of WinMain executes on the way out or if var quit is TRUE
// Shutdown OpenGL by calling disable function
DisableOpenGL( hWnd, hDC, hRC );
/*Destroy the Window*/
if (!DestroyWindow(hWnd)) // Window Destroyed?
{
MessageBox(NULL,"Failed Attempt At Destroying Window.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}
/*Unregister the Class*/
if (!UnregisterClass("GLSample",hInstance)) // Was Class Unregistered?
{
MessageBox(NULL,"Failed Attempt At Unregistering Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
return msg.wParam;
} // Closing Brace For WinMain
/*************************************************
* 2.Window Callback Function (Body)Definition
* Messages, user keyboard
* Event Driven
**************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_SYSCOMMAND:
switch(lParam)
{
case SC_SCREENSAVE: //Screen saver starting?
case SC_MONITORPOWER: //PowerSave Mode?
return 0;
}
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch ( wParam )
{
case VK_ESCAPE:
// Show Mouse Pointer to be able to Select
ShowCursor(TRUE);
if (MessageBox(NULL," Are You Sure You Want To Quit? "," Eureka C++ 3D Engine ",MB_YESNO|MB_ICONQUESTION)==IDYES)
{
PostQuitMessage(0); // set quit to TRUE
}
else
{
// Hide Mouse Pointer to Continue
ShowCursor(FALSE);
}
return 0;
}
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
}
}
/**************************************************************************
* 3. Enable OpenGL Function
* Pixel format description, device context and it´s Ogl render context,
***************************************************************************/
int EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
if ( (iFormat = ChoosePixelFormat(*hDC, &pfd)) == 0 )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if (SetPixelFormat(*hDC, iFormat, &pfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
/* make render context current with current device context*/
wglMakeCurrent( *hDC, *hRC );
return 0;
}
/**************************************************
* 4. Disable OpenGL Function
*
***************************************************/
int DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
// Restore default desktop mode with mouse pointer
ChangeDisplaySettings(NULL,0);
return 0;
}
I said use the source tags! no one likes huge posts like that! Forum FAQ
[Edited by - Roboguy on October 26, 2004 7:43:38 AM]
[Edited by - Roboguy on October 26, 2004 7:43:38 AM]
http://www.roboguy.net(WIP) - lisperati - SICP - Haskell - Python - OCaml - Lambda the Ultimate - Good Math, Bad Math - Wiki (not Wikipedia) - Pure - Term-Rewriting Functional Language
i am sorry for being so " n00bish".except for the fact that i had no clue what "source tags" are ......moderator....delete this topic.
It doesn't have to be deleted. Just edited. =)
Edit your post, and put [ source ] at the start of your code and [ /source ] at the end. Just don't have spaces between the square brackets and the words. You'll see all your code appears in a little box on the forum, and it even looks like C++ code.
It's cool, and it will keep people off your back, and encourage them to reply to you. :)
Good luck
Edit your post, and put [ source ] at the start of your code and [ /source ] at the end. Just don't have spaces between the square brackets and the words. You'll see all your code appears in a little box on the forum, and it even looks like C++ code.
It's cool, and it will keep people off your back, and encourage them to reply to you. :)
Good luck
We scratch our eternal itchA twentieth century bitchWe are grateful forOur Iron Lung
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement