How to setup OpenGL in Win32 environment June 3, 2007
Posted by metalickl in Software Insights.trackback
How to setup OpenGL in Win32 environment
Because of different computer games have different qualifications in terms of graphics, compatibility, hardware and operation, so their programming are done differently, especially there is quite a few distinctive platforms and different companies also have their own preferences. Some popular platforms are Microsoft’s XBOX, Sony’s PlayStation, Nitendo’s Wii etcetera. When you want to play a specific game, you need to know which platform it runs on. For example, you need to own a Playstation to play its games.
There is one more platform, perhaps the most well known of all, is PC. Although the performance of PC is not as superb as those specialized gaming platforms, it still holds a great market share in gaming entertainment. Most of PC is based on Win32, and runs Windows. Many popular PC games are developed with DirectX because it is introduced by Microsoft and contains all the graphic displays, operations and controls. If you want to develop these kinds of games, you need to download a very large SDK (free) library. Today, I will describe another graphic engine, OpenGL.
OpenGL (Open Graphics Library) is a standard for a cross-platform graphics development. Some of the games developed by OpenGL are Doom3, Quake, and etcetera. Strictly speaking, OpenGL can be developed for any platform, and you don’t need to download a SDK, (if you have Visual Studio) because Microsoft cooperated with SGI, so it provides the running library.
Ok, you can now see the code attached; ‘I’ copied it from the Microsoft website. When you run it, you will see a little red light flashing and spinning.
You should have Visual Studio C++, start a new Win32 Project, and use any name you wish. Under Application Settings, choose Windows Application and Empty Project. When the project is initialized, you will see a folder called Source Files. Right Click it and choose Add New Items, choose Code in the popped up window. Then choose C++ File(.cpp), give a name, and hit ‘ok’.
Copy & paste the code included as followed. Then run it.
Source Code:
// MyOpenGL.cpp : Defines the entry point for the application.
//#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0×0501 // Change this to the appropriate value to target other versions of Windows.
#endif#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include < windows.h >
#include < GL/gl.h >
#include < GL/glu.h >
#include < gl/glaux.h >
#include < math.h >#pragma comment( lib, “opengl32.lib” )
#pragma comment( lib, “glu32.lib” )
#pragma comment( lib, “glaux.lib” )/* Windows globals, defines, and prototypes */
TCHAR szAppName[]=L”Win OpenGL”;
HWND ghWnd;
HDC ghDC;
HGLRC ghRC;#define SWAPBUFFERS SwapBuffers(ghDC)
#define BLACK_INDEX 0
#define RED_INDEX 13
#define GREEN_INDEX 14
#define BLUE_INDEX 16
#define WIDTH 600
#define HEIGHT 450LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
BOOL bSetupPixelFormat(HDC);/* OpenGL globals, defines, and prototypes */GLfloat LightAmbient[]= { 1.0f, 0.0f, 0.0f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.50f, .50f, -6.0f, 1.0f };
GLfloat Lightspecular[] = { 0.5f, 0.5f, 0.5f, 1.0f };GLfloat LightAmbient1[]= { 1.0f, 1.0f, 0.0f, 1.0f };
GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
GLfloat yrot=0.0f;
GLfloat xrot=0.0f;
GLfloat zrot=0.0f;/*Win32 code*/
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;/* Register the frame class */
wndclass.style = 0;
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (hInstance, szAppName);
wndclass.hCursor = LoadCursor (NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;if (!RegisterClass (&wndclass) )
return FALSE;/* Create the frame */
ghWnd = CreateWindow (szAppName,
L”OpenGL Sample”,
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
WIDTH,
HEIGHT,
NULL,
NULL,
hInstance,
NULL);/* make sure window was created */
if (!ghWnd)
return FALSE;/* show and update main window */
ShowWindow (ghWnd, nCmdShow);UpdateWindow (ghWnd);
/* animation loop */
while (1) {
/*
* Process all pending messages
*/while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
if (GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
return TRUE;
}
}
drawScene();
}
}/* main window procedure */
LONG WINAPI MainWndProc (
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
LONG lRet = 1;
PAINTSTRUCT ps;
RECT rect;switch (uMsg) {
case WM_CREATE:
ghDC = GetDC(hWnd);
if (!bSetupPixelFormat(ghDC))
PostQuitMessage (0);ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
GetClientRect(hWnd, &rect);
initializeGL(rect.right, rect.bottom);
break;case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;case WM_SIZE:
GetClientRect(hWnd, &rect);
resize(rect.right, rect.bottom);
break;case WM_CLOSE:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
ghRC = 0;
ghDC = 0;DestroyWindow (hWnd);
break;case WM_DESTROY:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);PostQuitMessage (0);
break;case WM_KEYDOWN:
switch (wParam) {
case VK_LEFT:
zrot+=1.0f; break;
case VK_RIGHT:
zrot-=1.0f;break;
case VK_UP:
xrot+=1.0f;break;
case VK_DOWN:
xrot-=1.0f;break;
}
default:
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}return lRet;
}BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDE***OR pfd, *ppfd;
int pixelformat;ppfd = &pfd;ppfd->nSize = sizeof(PIXELFORMATDE***OR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = 8;
ppfd->cDepthBits = 16;
ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;pixelformat = ChoosePixelFormat(hdc, ppfd);
SetPixelFormat(hdc, pixelformat, ppfd);
return TRUE;
}/* OpenGL code */GLvoid resize( GLsizei width, GLsizei height )
{
GLfloat aspect;glViewport( 0, 0, width, height );aspect = (GLfloat) width / height;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
}
//construct the basic objects, and put them in a list
GLvoid createObjects()
{
GLUquadricObj *quadObjBody;
GLUquadricObj *quadObjNet;
GLUquadricObj *quadObjTop;
GLUquadricObj *quadObjUp;
GLUquadricObj *quadObjDown1;
GLUquadricObj *quadObjDown2;
GLUquadricObj *quadObjHandle;
GLUquadricObj *quadObjHead;glNewList(1, GL_COMPILE);
glPushMatrix();
glScalef(0.7,0.7,0.7);
//draw the handle
glPushMatrix();
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient1);
glTranslatef(0.0f,.0f,-1.4f);
quadObjHandle = gluNewQuadric ();
gluQuadricDrawStyle (quadObjHandle, GLU_SILHOUETTE);
gluCylinder(quadObjHandle,0.02,0.02,0.5f,32,32);
quadObjHead = gluNewQuadric ();
gluSphere (quadObjHead, 0.05f, 64, 64);
glPopMatrix();
//draw the top
glPushMatrix();
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient1);
glTranslatef(0.0f,.0f,-1.05f);
quadObjUp = gluNewQuadric ();
gluQuadricDrawStyle (quadObjUp, GLU_SILHOUETTE);
gluCylinder(quadObjUp,0.25,0.3,0.1,32,32);quadObjTop = gluNewQuadric();
gluQuadricDrawStyle (quadObjTop, GLU_LINE);
gluDisk(quadObjTop,0.02f,0.25,10,10);
glPopMatrix();
//draw the body
glPushMatrix();
glScalef(1.0,1.0,.80);
quadObjNet = gluNewQuadric ();
gluQuadricDrawStyle (quadObjNet, GLU_LINE);
gluSphere (quadObjNet, 1.205f, 8, 64);glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
quadObjBody = gluNewQuadric ();
gluSphere (quadObjBody, 1.20f, 64, 64);
glPopMatrix();
//draw the bottom
glPushMatrix();
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient1);
glTranslatef(0.0f,.0f,0.95f); quadObjDown1 = gluNewQuadric ();
gluQuadricDrawStyle (quadObjDown1, GLU_SILHOUETTE);
gluCylinder(quadObjDown1,0.25,0.3,0.2,32,32); quadObjDown2 = gluNewQuadric ();
gluQuadricDrawStyle (quadObjDown2, GLU_SILHOUETTE);
gluCylinder(quadObjDown2,0.15,0.2,0.3,32,32);glPopMatrix();
glPopMatrix();
glEndList();
}
//initial, lighting, enable something
GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat aspect;glClearIndex( (GLfloat)BLACK_INDEX);
glShadeModel(GL_SMOOTH); //make the color smoothly
glClearColor( 0.50, 0.50, 0.50, 1.0 ); //set the screen clear color
glClearDepth(.0f); //set the depth of buffer
glEnable(GL_DEPTH_TEST); //allow depth test
glDepthFunc(GL_GREATER); //the type of depth test
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, Lightspecular);
glLightfv(GL_LIGHT0, GL_POSITION,LightPosition);
glEnable(GL_LIGHT0);glEnable(GL_NORMALIZE);
glFrontFace(GL_FRONT_AND_BACK);
glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / height;gluPerspective( 45.0, aspect, 5.0f, 10.0f );
glMatrixMode( GL_MODELVIEW );createObjects();
}//move
GLvoid drawScene(GLvoid)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );glLoadIdentity(); glPushMatrix();
glTranslatef(0.0f,0.0f,-4.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
glPushMatrix();
glRotatef(yrot,0.0f,1.0f,0.0f);
glPushMatrix();
glRotatef(90,1.0f,.0f,.0f);
glCallList(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();if(abs(yrot – 360.0f) < 0.01)
yrot=0.0f;
yrot+=0.5f;glFlush();
SWAPBUFFERS;
}
please excuse me for any spelling/grammar errors, I don’t proof read.
——————————
E-Insight, 2007

I get the following when compiling:
VC++ opens ’sdkddkver.h’ and reports, on line 175, that:
‘fatal error C1012: unmatched parentheses : missing ‘)’
I get the same error