OpenGL:Tutorials:Windows Setup
From GPWiki(Redirected from OpenGL Windows Setup)
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] Setting up an OpenGL Project in Win32 with Visual Studio .NETSample Code for this tutorial Similar alternative code, OpenGL Windows Framework [edit] Creating a Project
[edit] Transforming the project into an OpenGL compatible project
#include <gl/gl.h> #include <gl/glu.h> #include <gl/glaux.h>
HDC hdc = GetDC(hWnd); if (hdc == NULL) return FALSE;
BOOL SetPF(HDC hdc) { PIXELFORMATDESCRIPTOR pfd; pfd.cAccumBits = 0; pfd.cAccumAlphaBits = 0; pfd.cAccumBlueBits = 0; pfd.cAccumGreenBits = 0; pfd.cAccumRedBits = 0; pfd.cAlphaBits = 8; pfd.cAuxBuffers = 0; pfd.cColorBits = 32; pfd.cDepthBits = 24; pfd.cStencilBits = 0; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; pfd.iLayerType = PFD_MAIN_PLANE; pfd.iPixelType = PFD_TYPE_RGBA; pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; // choose pixel format returns the number most similar pixel format available int n = ChoosePixelFormat(hdc, &pfd); // set pixel format returns whether it sucessfully set the pixel format return SetPixelFormat(hdc, n, &pfd); }
if (SetPF(hdc) == FALSE) return FALSE;
HGLRC hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc); [edit] Testing that OpenGL is renderingThe simplest way to test that OpenGL is rendering to you window is to clear the color buffer to something other than the default and swap the buffers glClearColor(1.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); SwapBuffers(hdc); [edit] Practical ConsiderationsUnder circumstances such as a game you'll probably want to modify the message loop from while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } to something like while (1) { // add drawing code here SwapBuffers(hdc); if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) && !TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { if (msg.wParam == IDM_EXIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } } so that you can render continuously rather than whenever a paint message comes it, alternatively you could use the paint message structure and just post another paint message whenever one comes in. (I haven't tried this but it should work the same way) Also, if you use that exact structure you should make sure to remove the system menu from the window creation call so that the user must go to File->Exit rather than closing the window. The above structure will not stop the process when the window closes. One solution I've used is to create a boolean "exitstate" variable, set to true when either the window is destroyed or file->exit is called, and is used as the condition of the while loop: while(!exitstate)... Doug Sheets (doug.sheets@gmail.com) November 19th, 2004 |


