Visual Studio OpenGL 配置方法
OpenGL開發庫的檔案包含:
動態連結程式庫檔案(.dll)
glut32.dll、glu.dll、glut.dll、OPENGL.DLL。
標頭檔(.h)
GL.H、GLAUX.H、GLEXT.H、GLU.H、GLUT.H、WGLEXT.H。
庫檔案(.lib)
GLAUX.LIB、Glu32.lib、Glu.lib、glut32.lib、glut.lib、Opengl32.lib、opengl.lib。
配置方法:
1.將開發庫中的.h檔案拷貝到Visual C++ 6.0的\Include\GL目錄中
2.將.lib檔案拷貝到Visual C++ 6.0的\lib目錄中
3.將.dll檔案拷貝到作業系統的system32目錄中
程式碼範例:
/*<br /> Name: GLSAMPLE<br /> Author: Blaine Hodge<br /> Description: OpenGL sample. Read the fileInclude\Gl\ReadMe.txt<br /> for informations on usingOpenGL.<br /> Date: -<br /> Copyright: Public domain<br />*/</p><p>//Includes</p><p>#include <windows.h><br />#include <gl/gl.h></p><p>#pragma comment(lib, "opengl32.lib")<br />#pragma comment(lib, "opengl.lib")<br />#pragma comment(lib, "glu32.lib")<br />#pragma comment(lib, "glut32.lib")<br />#pragma comment(lib, "glaux.lib")<br />#pragma comment(lib, "glu.lib")</p><p>//Function Declarations</p><p>LRESULT CALLBACK<br />WndProc(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam);<br />VOIDEnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);<br />VOIDDisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);</p><p>//WinMain</p><p>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCEhPrevInstance,<br /> LPSTR lpCmdLine, int iCmdShow)<br />{<br /> WNDCLASS wc;<br /> HWND hWnd;<br /> HDC hDC;<br /> HGLRC hRC;<br /> MSG msg;<br /> BOOL bQuit = FALSE;<br /> float theta =0.0f;</p><p> // register windowclass<br /> wc.style = CS_OWNDC;<br /> wc.lpfnWndProc = WndProc;<br /> wc.cbClsExtra = 0;<br /> wc.cbWndExtra = 0;<br /> wc.hInstance = hInstance;<br /> wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );<br /> wc.hCursor = LoadCursor( NULL, IDC_ARROW );<br /> wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH );<br /> wc.lpszMenuName = NULL;<br /> wc.lpszClassName = "GLSample";<br /> RegisterClass( &wc );</p><p> // create mainwindow<br /> hWnd = CreateWindow(<br /> "GLSample","OpenGL Sample",<br /> WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,<br /> 0, 0, 256, 256,<br /> NULL, NULL, hInstance, NULL);</p><p> // enable OpenGLfor the window<br /> EnableOpenGL( hWnd, &hDC, &hRC );</p><p> // program mainloop<br /> while(!bQuit)<br /> {<br /> // check formessages<br /> if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))<br /> {<br /> // handleor dispatch messages<br /> if(msg.message == WM_QUIT)<br /> {<br /> bQuit = TRUE;<br /> }<br /> else<br /> {<br /> TranslateMessage(&msg);<br /> DispatchMessage(&msg);<br /> }</p><p> }<br /> else<br /> {<br /> // OpenGLanimation code goes here</p><p> glClearColor(0.0f, 0.0f, 0.0f, 0.0f);<br /> glClear(GL_COLOR_BUFFER_BIT);</p><p> glPushMatrix();<br /> glRotatef(theta, 0.0f, 0.0f, 1.0f);<br /> glBegin(GL_TRIANGLES);<br /> glColor3f( 1.0f, 0.0f, 0.0f );glVertex2f( 0.0f, 1.0f );<br /> glColor3f( 0.0f, 1.0f, 0.0f );glVertex2f( 0.87f, -0.5f );<br /> glColor3f( 0.0f, 0.0f, 1.0f );glVertex2f( -0.87f, -0.5f );<br /> glEnd();<br /> glPopMatrix();<br /> SwapBuffers( hDC );</p><p> theta += 1.0f;</p><p> }<br /> }<br /> // shutdown OpenGL<br /> DisableOpenGL( hWnd, hDC, hRC );<br /> // destroy thewindow explicitly<br /> DestroyWindow( hWnd );<br /> returnmsg.wParam;<br />}</p><p>//Window Procedure</p><p>LRESULT CALLBACKWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br />{<br /> switch (message)<br /> {<br /> caseWM_CREATE:<br /> return0;</p><p> caseWM_CLOSE:<br /> PostQuitMessage( 0 );<br /> return0;</p><p> caseWM_DESTROY:<br /> return0;</p><p> caseWM_KEYDOWN:<br /> switch(wParam)<br /> {<br /> caseVK_ESCAPE:<br /> PostQuitMessage( 0 );<br /> return0;<br /> }<br /> return0;</p><p> default:<br /> returnDefWindowProc(hWnd, message, wParam, lParam);<br /> }<br />}</p><p>//Enable OpenGL</p><p>VOID EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC )<br />{<br /> PIXELFORMATDESCRIPTOR pfd;<br /> int iFormat;</p><p> // get the devicecontext (DC)<br /> *hDC = GetDC( hWnd );</p><p> // set the pixelformat for the DC<br /> ZeroMemory( &pfd, sizeof( pfd ) );<br /> pfd.nSize = sizeof(pfd );<br /> pfd.nVersion = 1;<br /> pfd.dwFlags = PFD_DRAW_TO_WINDOW |<br /> PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;<br /> pfd.iPixelType = PFD_TYPE_RGBA;<br /> pfd.cColorBits = 24;<br /> pfd.cDepthBits = 16;<br /> pfd.iLayerType = PFD_MAIN_PLANE;<br /> iFormat = ChoosePixelFormat( *hDC, &pfd);<br /> SetPixelFormat( *hDC, iFormat, &pfd );</p><p> // create andenable the render context (RC)<br /> *hRC = wglCreateContext( *hDC );<br /> wglMakeCurrent( *hDC, *hRC );<br />}</p><p>//Disable OpenGL</p><p>VOID DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC )<br />{<br /> wglMakeCurrent( NULL, NULL );<br /> wglDeleteContext( hRC );<br /> ReleaseDC( hWnd, hDC );<br />}
注意:
忘記
#pragmacomment(lib, "XXX.lib")
會造成連結錯誤!