OpenGL in Visual C++.NET Chang-Chieh Cheng Medical Image Processing Lab, Dept. of Computer Science, NCTU
Win32 API What is Win32 API: It is designed for use by C/C++ programs and is the most direct way to interact with a Windows system for software applications. Lower level access to a Windows system, mostly required for device drivers, is provided by the Native API in current versions of Windows. Categories Base Services Graphics Device Interface User Interface Common Dialog Box Library Common Control Library Windows Shell Network Services 2
Handle What is Handle? A four-byte integer used to identify a wide variety of objects. Handles refer to an internal data structure not accessible to Windows applications which contain information about an object. Common Handles HWND Handle to a window. HDC Handle to a device context (DC). HBITMAP Handle to a bitmap. HFILE Handle to a file. H??? Handle to???. 3
Procedures: Create a window Get HWND. Get HDC. Choose a Pixel Format Set the Pixel Format. Create a OpenGL rendering context (HGLRC). Makes a specified OpenGL rendering context(hglrc) the calling thread's current rendering context(hdc). HGLRC HDC Rendering 4
Environment configuration Create a new Windows Form Application Project. 5
In VC++.NET 2005 Common Language Runtime Support(/clr) 6
Environment configuration Library link opengl32.lib glut32.lib glu32.lib 7
Header include #include <GL/glut.h> #include <GL/gl.h> #include <GL/glu.h> 8
Create a window Form: form1 PictureBox: picturebox1 All init procedures in Constructor of Fom1 or Form1_load() event 9
Get HWND of picturebox1 HWND hwnd = (HWND)pictureBox1->Handle.ToInt32(); Get HDC of picturebox1 HDC hdc = GetDC( hwnd ); 10
Choose a Pixel Format for picturebox1 API: ChoosePixelFormat The ChoosePixelFormat function attempts to match an appropriate pixel format supported by a device context to a given pixel format specification. int ChoosePixelFormat( HDC hdc, CONST PIXELFORMATDESCRIPTOR * ppfd // pixel format for which a best match is sought ); 11
Choose a Pixel Format for picturebox PIXELFORMATDESCRIPTOR pfd = { sizeof(pixelformatdescriptor), // size of this pfd 1, // version number PFD_DRAW_TO_WINDOW PFD_SUPPORT_OPENGL PFD_DOUBLEBUFFER, // double buffered PFD_TYPE_RGBA, // RGBA type 32, // 32-bit color depth 0, 0, 0, 0, 0, 0, // color bits ignored 0, // no alpha buffer 0, // shift bit ignored 0, // no accumulation buffer 0, 0, 0, 0, // accum bits ignored 32, // 32-bit z-buffer 0, // no stencil buffer 0, // no auxiliary buffer PFD_MAIN_PLANE, // main layer 0, // reserved 0, 0, 0 // masks ignored }; int nformat = ChoosePixelFormat( hdc, &pfd ); 12
Set the Pixel Format API:SetPixelFormat The SetPixelFormat function sets the pixel format of the specified device context to the format specified by the ipixelformat index. BOOL SetPixelFormat( HDC hdc, int ipixelformat, // pixel format index (one-based) CONST PIXELFORMATDESCRIPTOR * ppfd // pointer to logical pixel format specification ); if(!setpixelformat( hdc, nformat, &pfd )) MessageBox::Show("Set Pixel Format Error", "Error"); 13
Create a OpenGL rendering context API: wglcreatecontext The wglcreatecontext function creates a new OpenGL rendering context, which is suitable for drawing on the device referenced by hdc. The rendering context has the same pixel format as the device context. HGLRC wglcreatecontext( HDC hdc); HGLRC hrc = wglcreatecontext( hdc ); 14
HGLRC HDC API: wglmakecurrent The wglmakecurrent function makes a specified OpenGL rendering context the calling thread's current rendering context. All subsequent OpenGL calls made by the thread are drawn on the device identified by hdc. You can also use wglmakecurrent to change the calling thread's current rendering context so it's no longer current. BOOL wglmakecurrent( HDC hdc, HGLRC hglrc); if (wglmakecurrent( hdc, hrc ) == false) MessageBox::Show("wglMakeCurrent Error", "Error"); 15
Rendering Render function is called when Timer expires, Mouse moved, Keyboard Pressed.or other event is triggered. If app has more than one render target(picture box), the wglmakecurrent(hdc, hglrc) must be called before render function. 16