Visual Studio OpenGL 配置方法

來源:互聯網
上載者:User

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")

會造成連結錯誤!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.