Code:
#define WIN32_LEAN_AND_MEAN // This trims down the libraries.
// #define VC_LEANMEAN not needed in Dev-C++
#include<windows.h> // The windows header file.
#include<gl/gl.h> // Standard opengl include.
#include<gl/glu.h> // Opengl utilities.
#include<gl/glaux.h> // auxiliary functions.
// Function Prototypes
void RenderScene();
bool InitializeGL();
void Shutdown();
// Light properties for the diffuse light and specular light.
float diffuseLight[] = {0.8f, 0.8f, 0.8f, 1.0f};
float specularLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
// x, y, and z rotation of our object.
float objectXRot = 0.0f;
float objectYRot = 0.0f;
float objectZRot = 0.0f;
// Global device context.
HDC g_HDC;
void SetupPixelFormat(HDC hDC)
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of structure.
1, // always 1.
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGl
PFD_DOUBLEBUFFER, // support double buffering
PFD_TYPE_RGBA, // support RGBA
16, // bit color mode
0, 0, 0, 0, 0, 0, // ignore color bits
0, // no alpha buffer
0, // ignore shift bit
0, // no accumulation buffer
0, 0, 0, 0, // ignore accumulation bits.
16, // number of depth buffer bits.
0, // number of stencil buffer bits.
0, // 0 means no auxiliary buffer
PFD_MAIN_PLANE, // The main drawing plane
0, // this is reserved
0, 0, 0 }; // layer masks ignored.
// This chooses the best pixel format and returns index.
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
// This set pixel format to device context.
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC; // Rendering context.
static HDC hDC; // Device context.
int width, height; // The window width and height.
static POINT oldMousePos; // Last mouse position.
static POINT currentMousePos; // Current mouse position.
static bool isMouseActive; // Is the left mouse button down.
switch(message)
{
case WM_CREATE: // Windows creation.
hDC = GetDC(hwnd); // This gets the device context for our window.
g_HDC = hDC; // Assigns the global device context to this one.
SetupPixelFormat(hDC); // Call the pixel format function.
hRC = wglCreateContext(hDC); // Creates the rendering context.
wglMakeCurrent(hDC, hRC); // Makes the rendering context.
return 0;
break;
case WM_CLOSE: // Close message.
case WM_DESTROY:
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC); // Deletes the rendering context.
PostQuitMessage(0); // Says close the program.
return 0;
break;
case WM_SIZE: // re-size message.
height = HIWORD(lParam); // This gets the height of the window.
width = LOWORD(lParam); // This gets the width of the window.
if(height==0) // we don't want it to be possible for a
{ // height of 0. If it is 0 me make it 1.
height = 1;
}
glViewport(0, 0, width, height); // resets the viewport to new dimensions.
glMatrixMode(GL_PROJECTION); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
// calculate the aspect ratio of the window.
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
return 0;
break;
case WM_KEYDOWN:
switch(wParam)
{
// Close the app if the user presses escape.
case VK_ESCAPE:
PostQuitMessage(0);
break;
default:
break;
}
break;
default:
break;
}
// What this does is pass all of the unhandled messages to DefWindowProc.
return (DefWindowProc(hwnd, message, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg; // A message variable.
WNDCLASSEX windowClass; // Your Window class.
HWND hwnd; // The Window handle.
bool isFinished; // This will be used to check if the program is done or not.
// This is the Window class.
windowClass.cbSize = sizeof(WNDCLASSEX); // size of the WNDCLASSEX structure.
windowClass.style = CS_HREDRAW | CS_VREDRAW; // style of the window.
windowClass.lpfnWndProc = WndProc; // Address to the windows procedure.
windowClass.cbClsExtra = 0; // Extra class information.
windowClass.cbWndExtra = 0; // Extra window information.
windowClass.hInstance = hInstance; // Handle of application Instance.
// Handle of application Icon.
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);// mouse cursor
windowClass.hbrBackground = NULL; // background color.
windowClass.lpszMenuName = NULL; // name of the main menu.
windowClass.lpszClassName = "UltimateGameProgrammingClass";// window class name.
// icon when minimized.
windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// You must register you class with Windows.
if(!RegisterClassEx(&windowClass)) return 0;
// Create window...
hwnd = CreateWindowEx(NULL,// The extended window style.
"UltimateGameProgrammingClass",
"Lighting - By the Programming Ace.",// window name.
WS_OVERLAPPEDWINDOW | WS_VISIBLE |// The window style.
WS_SYSMENU | WS_CLIPCHILDREN |// window style.
WS_CLIPSIBLINGS,// window style.
0, 0,// window x, y coordinate.
640, 480,// window width and height.
NULL,// handle to parent window.
NULL,// menu.
hInstance,// handle to app instance.
NULL); // pointer to window creation data.
// If there was an error with creating the window, then close the program.
if(!hwnd) return 0;
ShowWindow(hwnd, SW_SHOW); // This shows the window.
UpdateWindow(hwnd); // This forces a paint message.
isFinished = false; // False = running, True = not running.
// If initialize fail (return false), then we don't want the program to run.
if(!InitializeGL()) isFinished = true;
// This is the messsage loop.
while(!isFinished)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
// If a quit message is received then stop rendering and quit the app.
if(msg.message == WM_QUIT)
{
isFinished = true;
}
TranslateMessage(&msg); // Translate any messages.
DispatchMessage(&msg); // Dispatch any messages.
}
else
{
RenderScene(); // Render a frame.
}
}
// Shutdown (free) all resources we used up.
Shutdown();
return (int)msg.wParam;
}
bool InitializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST); // Enable depth testing.
glEnable(GL_CULL_FACE); // Enable culling.
glFrontFace(GL_CCW); // Enable counter clockwise culling.
glEnable(GL_LIGHTING); // Enable lighting.
// Set up the information for the first light and set it in GL_LIGHT0.
// We will set the diffuse color and specular color.
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
// Enable the first light source. We can have up to 8 lights (GL_LIGHT0
// to GL_LIGHT7).
glEnable(GL_LIGHT0);
// Set up the material information for our objects.
// This will allow our objects to have a shininess to it from the light. Add to the realism.
// First we enable color material, set up the materials for ambient and diffuse colors,
// set the color of the specular of the material, then finally set the max amount of shinines.
// The highest number for the last function is 128 (for the max).
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
glMateriali(GL_FRONT, GL_SHININESS, 128);
return true;
}
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen.
glLoadIdentity(); // Reset modelview matrix for new frame.
// Translate back 5.
glTranslatef(0.0f, 0.0f, -5.0f);
// Set the color and rotate our object along every axis.
glColor3f(1.0f, 1.0f, 1.0f);
glRotatef(objectXRot, 1.0f, 0.0f, 0.0f);
glRotatef(objectYRot, 0.0f, 1.0f, 0.0f);
glRotatef(objectZRot, 0.0f, 0.0f, 1.0f);
// Draw the cube using an OpenGL function.
auxSolidCube(2.0f);
// Increase rotation values to move the object.
objectXRot += 0.1f;
objectYRot += 0.2f;
objectZRot += 0.1f;
SwapBuffers(g_HDC); // Display the new frame.
}
void Shutdown()
{
}
// Copyright June 2004
// All Rights Reserved!
// Allen Sherrod
// [email protected]
// www.UltimateGameProgramming.com
For the other errors I was talking about, download one of the raytracing demos(except the first one, that works) at ultimategameprogramming.com and put all the files in a project, try to compile it, it'll give those errors