Code:
/* Steven Billington
*/
/* Here we set the libraries to be used in the program, and
trim some of the excess off of windows
*/
#define WIN32_LEAN_AND_MEAN
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
/* Include all needed headers for the project. Windows brings in
everything needed for the basic application, and the other three
bring in OpenGL related functions.
*/
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
/* These are our global variables. These are ok in this program,
but i wouldn't reccomend making a habit out of using global
variables.
angle: Holds current angle of rotating triangle
g_HDC: Global Device Context
*/
float angle = 0.0f;
HDC g_HDC;
/* Function: SetupPixelFormat
Purpose: This function gets the setup for the pixel format,
go figure right?
*/
void SetupPixelFormat(HDC hDC)
{
int nPixelFormat; /* Pixel format index*/
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), //size of structure
1, //version, always set to one
PFD_DRAW_TO_WINDOW | //support window
PFD_SUPPORT_OPENGL | //support opengl
PFD_DOUBLEBUFFER, //support double buffering
PFD_TYPE_RGBA, //RGBA color mode
32, //32bit color mode
0,0,0,0,0,0, //ignore color bits, not used
0, //no alpha buffer
0, //ignore shift bit
0, //no accumulation buffer
0,0,0,0, //ignore accumulation buffers
16, //16 bit zbuffer size
0, //no stencil buffer
0, //no auxiliary buffer
PFD_MAIN_PLANE, //main drawing plane
0, //reserved
0,0,0 }; //layer masks ignored
/* Choose best matching pixel format, return index*/
nPixelFormat = ChoosePixelFormat(hDC,&pfd);
/* Set pixel format to device context*/
SetPixelFormat(hDC,nPixelFormat,&pfd);
}
/* Function: WndProc
Purpose: This is our Windows Procedure Event Handler
*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC; //rendering context
static HDC hDC; //device context
char string[] = "Hello World!"; //display text
int width, height; //window width, height
switch(message)
{
case WM_CREATE: //window is being created
hDC = GetDC(hwnd); //get current windows device context
g_HDC = hDC;
SetupPixelFormat(hDC); //call your pixel format setup function
//create rendering context and make it current
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
return 0;
break;
case WM_CLOSE: //window is closing
//deselect rendering context and delete it
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hRC);
//send WM_QUIT to messagr queue
PostQuitMessage(0);
return 0;
break;
case WM_SIZE:
height = HIWORD(lParam); //get height and width
width = LOWORD(lParam);
if (height == 0) //don't want a divide by 0
{
height = 1;
}
//reset the viewport to new dimensions
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION); //set projection matrix
glLoadIdentity(); //reset proj matrix
//calculate aspect ratio of window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW); //set modelview matrix
glLoadIdentity(); //reset mdeolv matrix
return 0;
break;
default:
break;
}
return (DefWindowProc(hwnd,message,wParam,lParam));
}
/* Function: WinMain
Purpose: Do i need to state this? Ok, its the main function.
*/
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX windowClass; //window class
HWND hwnd; //window handle
MSG msg; //message
bool done; //flag saying when app is complete
/* Fill out the window class structure*/
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
/* Register window class*/
if (!RegisterClassEx(&windowClass))
{
return 0;
}
/* Class registerd, so now create window*/
hwnd = CreateWindowEx(NULL, //extended style
"MyClass", //class name
"A Real OGL Win App", //app name
WS_OVERLAPPEDWINDOW | //window style
WS_VISIBLE |
WS_SYSMENU |
WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
100,100, //x/y coords
400,400, //width,height
NULL, //handle to parent
NULL, //handle to menu
hInstance, //application instance
NULL); //no extra parameter's
/* Check if window creation failed*/
if (!hwnd)
{
return 0;
}
ShowWindow(hwnd,SW_SHOW); //displays window
UpdateWindow(hwnd); //update window
done = false; //inititalize condition variable
//main message loop
while(!done)
{
PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
if (msg.message == WM_QUIT)
{
done = true;
}
else
{
//do rendering here
//clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //reset modelview matrix
angle = angle + 0.1f; //increase rotation angle counter
if (angle >= 360.0f) //reset angle counter
{
angle = 0.0f;
}
glTranslatef(0.0f,0.0f,-5.0f); //move back 5 units
glRotatef(angle,0.0f,0.0f,1.0f); //rotate along z-axis
glColor3f(1.0,0.0f,0.0f); //set color to red
glBegin(GL_TRIANGLES); //draw the triangle
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glEnd();
SwapBuffers(g_HDC); //bring back buffer to foreground
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;;
}
^ This? What kind of project are you running it in then?