Main
Tutorials/Code
Demos
Links
Info
Contact me

//Include the needed headers
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

HDC hDC=NULL; //Device context for our window
HGLRC hRC=NULL; //Rendering context for our window
HWND hWnd=NULL; //Handle for ourn window

char *szAppName="NitroGL OpenGL Tutorial 1"; //Window title

RECT WindowRect; //window rect

int Width=640, Height=480; //Window width and height
BOOL Done=FALSE; //is the application done?

//decalres for our application
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void Render(void);
void Init(void);
void Create(void);
void Destroy(void);
void Resize(int Width, int Height);

//WinMain, this is where it all starts
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
//Window class
WNDCLASS wc;
//Window message
MSG msg;

//Set the window class
wc.style=CS_VREDRAW|CS_HREDRAW|CS_OWNDC;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(hInstance, IDI_WINLOGO);
wc.hCursor=LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground=NULL;
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;

//Register it
RegisterClass(&wc);

//Set the window hieght and width
WindowRect.left=0;
WindowRect.right=Width;
WindowRect.top=0;
WindowRect.bottom=Height;

//Tell it that theres an overlapped window, but don't have the visual features :)
//And set the width and height to there true value
AdjustWindowRectEx(&WindowRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW|WS_EX_WINDOWEDGE);

//Create the window
hWnd=CreateWindowEx(WS_EX_APPWINDOW|WS_EX_WINDOWEDGE, szAppName, szAppName, WS_OVERLAPPED|WS_SYSMENU|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 0, 0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, hInstance, NULL);

//Show the window
ShowWindow(hWnd, SW_SHOW);
//Make window forground
SetForegroundWindow(hWnd);

//Create the OpenGL context
Create();
//Set the viewport for OpenGL
Resize(Width, Height);
//Init your stuff here (eg. load texture/model/etc.)
Init();

//Main program loop
while(!Done)
{
//Get messages
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//If current message = WM_QUIT, then Done=TRUE (our application quits)
if(msg.message==WM_QUIT)
Done=TRUE;
//Other wise pass the message
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
//Render sence
Render();
//Swap buffers (double buffer)
SwapBuffers(hDC);
}
}

//Destroy the OpenGL context
Destroy();
//Destroy the window
DestroyWindow(hWnd);
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
break;

case WM_CLOSE:
PostQuitMessage(0);
break;

case WM_DESTROY:
break;

case WM_KEYDOWN:
switch(wParam)
{
//if escape is pressed, quit
case VK_ESCAPE:
PostQuitMessage(0);
break;

default:
break;
}
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

void Render(void)
{
//Clear buffers
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

//Save the matrix
glPushMatrix();

//Save it again
glPushMatrix();

//Translate the matrix along the Z axis (-2.0 away from the screen)
glTranslatef(0.0f, 0.0f, -2.0f);
//Begin drawing triangles
glBegin(GL_TRIANGLES);
//Set a vertex (2 point)
glVertex2f(0.0f, 1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
//End drawing
glEnd();
//Restore the matrix
glPopMatrix();

//Do it again
glPopMatrix();
}

void Init(void)
{
//Enable culling (removes the back of triangles/polygons/quads)
glEnable(GL_CULL_FACE);
//Enable depth sorting
glEnable(GL_DEPTH_TEST);
}

void Create(void)
{
PIXELFORMATDESCRIPTOR pfd={ sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 8, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 };
unsigned int PixelFormat;
//Get our window's device context
hDC=GetDC(hWnd);
//Find a Pixel Format for our window (as set by the pfd above)
PixelFormat=ChoosePixelFormat(hDC, &pfd);
//Set the Pixel Format
SetPixelFormat(hDC, PixelFormat, &pfd);
//Create the rendering context
hRC=wglCreateContext(hDC);
//Make that rendering context current
wglMakeCurrent(hDC, hRC);
}

void Destroy(void)
{
//Set current rendering context to NULL (nothing)
wglMakeCurrent(NULL, NULL);
//Delete the Rendering context
wglDeleteContext(hRC);
//Release the device context
ReleaseDC(hWnd, hDC);
}

void Resize(int Width, int Height)
{
//Remove that nasty divide by zero
if(Height==0)
Height=1;

//Set the viewport
glViewport(0,0, Width, Height);
//Change to the Projection matrix mode
glMatrixMode(GL_PROJECTION);
//Reset to the delault matrix
glLoadIdentity();
//Set a Perspective view
gluPerspective(90.0f, (float)Width/Height, 0.1f, 100.0f);
//Change to the Model view matrix mode
glMatrixMode(GL_MODELVIEW);
//Reset to the default matrix
glLoadIdentity();
}


Here is the MS VC++ 6.0 project and source for this tutorial (the code in that will look better)
Copyright 2001 Matt Williams
Hosted by www.Geocities.ws

1