Introduction
This article is all about getting started with OpenGL.
Getting started
- Start Visual Studio and select a new Win32 Application project, select empty project, click finish.
- Add a new file Main.cpp to your project.
- Go to Projects/Settings/Links tab and type opengl32.lib and glu32.lib in the object/library modules.
Includes
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
LONG WINAPI WndProc(HWND,UINT,WPARAM,LPARAM);HGLRC SetUpOpenGL(HWND hwnd);void DrawOpenGLScene(void);
Your WinMain()
should look like this:
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
static char szAppName[] = "OpenGL";
static char szTitle[]="Getting Started With OpenGL";
WNDCLASS wc; MSG msg; HWND hWnd;
wc.style =
CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc =
(WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance =
hInstance; wc.hIcon = NULL; wc.hCursor =
LoadCursor(NULL, IDC_ARROW); wc.hbrBackground =
(HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName =
szAppName;
RegisterClass( &wc );
hWnd = CreateWindow(
szAppName, szTitle, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL );
if ( !hWnd )
{
return( 0 );
}
ShowWindow( hWnd, 1 ); UpdateWindow( hWnd );
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage( &msg ); DispatchMessage( &msg ); }
return( msg.wParam );
}
Your window procedure looks like this:
LONG WINAPI WndProc( HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam )
{
HDC hDC;
static HGLRC hRC; // Note this is STATIC!
PAINTSTRUCT ps;
GLdouble gldAspect;
GLsizei glnWidth, glnHeight;
switch(msg)
{
case WM_CREATE:
// Select a pixel format and then
// create a rendering context from it.
hRC = SetUpOpenGL(hWnd);
return 0;
case WM_SIZE:
// Redefine the viewing volume and viewport
// when the window size changes.
// Make the RC current since we're going to
// make an OpenGL call here...
hDC = GetDC(hWnd);
wglMakeCurrent(hDC,hRC);
// get the new size of the client window
// note that we size according to the height,
// not the smaller of the height or width.
glnWidth = (GLsizei) LOWORD (lParam);
glnHeight = (GLsizei) HIWORD (lParam);
gldAspect =
(GLdouble)glnWidth/(GLdouble)glnHeight;
// set up a projection matrix to fill the
// client window
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// a perspective-view matrix...
gluPerspective(
30.0, // Field-of-view angle
gldAspect, // Aspect ratio of view volume
1.0, // Distance to near clipping plane
10.0 ); // Distance to far clipping plane
glViewport( 0, 0, glnWidth, glnHeight );
wglMakeCurrent( NULL, NULL );
ReleaseDC( hWnd, hDC );
return 0;
case WM_PAINT:
// Draw the scene.
// Get a DC, then make the RC current and
// associate with this DC
hDC = BeginPaint( hWnd, &ps );
wglMakeCurrent( hDC, hRC );
DrawOpenGLScene();
// we're done with the RC, so
// deselect it
// (note: This technique is not recommended!)
wglMakeCurrent( NULL, NULL );
EndPaint( hWnd, &ps );
return 0;
case WM_DESTROY:
// Clean up and terminate.
wglDeleteContext( hRC );
PostQuitMessage( 0 );
return 0;
}
// This function handles any messages that we didn't.
// (Which is most messages) It belongs to the OS.
return DefWindowProc( hWnd, msg, wParam, lParam );
}
OpenGL initialization code resides in SetUpOpenGL()
.
HGLRC SetUpOpenGL( HWND hWnd )
{
static PIXELFORMATDESCRIPTOR pfd = {
sizeof (PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL, PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 };
int nMyPixelFormatID;
HDC hDC;
HGLRC hRC;
hDC = GetDC( hWnd );
nMyPixelFormatID = ChoosePixelFormat( hDC, &pfd );
SetPixelFormat( hDC, nMyPixelFormatID, &pfd );
hRC = wglCreateContext( hDC );
ReleaseDC( hWnd, hDC );
return hRC;
}
The drawing code is represented by DrawOpenGLScene()
function.
void DrawOpenGLScene( )
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -5.0f );
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0); glVertex3f(0.0,0.0,0);
glVertex3f(0.0,1.0,0);
glVertex3f(1.0,0.0,0);
glColor3f(0.0,1.0,0.0); glVertex3f(0.0,0.0,0);
glVertex3f(0.0,1.0,0);
glVertex3f(-1.0,0.0,0);
glColor3f(0.0,0.0,1.0); glVertex3f(1.0,0.0,0);
glVertex3f(0.0,-1.0,0);
glVertex3f(-1.0,0.0,0);
glEnd();
glFlush();
}
That's it
That's all to get started with OpenGL.
References
- OpenGL Programming Guide - The Red Book.
- OpenGL Reference Manual - The Blue Book.