經過本節,咱們來學習一下在Win10 6四、VS1013環境下搭建OpenGL的開發環境。html
(1)選擇一個編譯環境:git
如今在windows中OpenGL的主流編譯工具備Visual Studio,Broland C++ Builder,Dev C++等,咱們選擇Visual Studio2013做爲開發環境。github
(2)用到的資源:windows
咱們使用了GLEW,freeglut以及GLTools這三個庫。下面是它們的簡介:緩存
1)GLEW:ide
The OpenGL Extension Wrangler Library (GLEW) is a cross-platform open-source C/C++ extension loading library. GLEW provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform. OpenGL core and extension functionality is exposed in a single header file. GLEW has been tested on a variety of operating systems, including Windows, Linux, Mac OS X, FreeBSD, Irix, and Solaris.函數
GLEW是一個跨平臺的OpenGL的擴展庫,支持多個操做系統。工具
2)freeglut:oop
FreeGLUT is a free-software/open-source alternative to the OpenGL Utility Toolkit (GLUT) library。(FreeGLUT是一個免費軟件/開源替代OpenGL實用工具包(GLUT)庫。)學習
3)GLTools:
主要是爲了得到這個項目編譯後的gltools.lib文件。
(3)開發環境的配置:
1)下載文件:
文件名 | 個人文件 | 官網下載 |
1.GLEW | http://files.cnblogs.com/files/MenAngel/glew-1.7.0-win64.zip | http://glew.sourceforge.net/ |
2.freeglut需編譯包(不用不行) | http://files.cnblogs.com/files/MenAngel/freeglut-3.0.0.tar.gz | http://freeglut.sourceforge.net/ |
3.GLTools需編譯包(不用也行) | http://files.cnblogs.com/files/MenAngel/GLTools-master.zip | https://codeload.github.com/HazimGazov/GLTools/zip/master |
4.freeglut(只有freeglut.lib和.dll) | http://files.cnblogs.com/files/MenAngel/freeglut_2.8.0.zip | http://download.csdn.net/detail/wocaowogannimei/8082851 |
5.GL和GLTools.lib包 | http://files.cnblogs.com/files/MenAngel/GL%E5%92%8Cgltools.dll.lib.7z |
自定義包:GL可直接用,並且還包含GLTools.lib |
2)解壓後將獲得的文件分別進行配置:
處理glew:
include文件夾下是頭文件,將位於include文件夾下的GL文件夾複製到"Visual Studio 2013安裝目錄\VC\include" 下;
lib文件夾下的文件複製到"Visual Studio 2013安裝目錄\VC\lib";
bin文件夾下的文件複製到C:\Windows\SysWOW64;(32位的系統放在System32)
處理GLTools
對於GLTools,主要是得到GLTools.lib文件, 而後將GLTools.lib複製到Visual Studio 12.0\VC\lib下。
咱們能夠經過編譯GLTools項目來得到GLTools.lib,也能夠直接用別人編譯項目得到的GLTools:
1.下載地址:http://download.csdn.net/download/u010255642/6896993
2.編譯方法:
參見文檔:http://www.cnblogs.com/wangguchangqing/p/4425745.html
參見文檔:http://www.ithao123.cn/content-967031.html
拷貝的結果:
至此,OpenGL開發環境配置完成。
(4)一個OpenGL的測試項目:
1)在VS2013項目中新建一個Visual C++類型的win32控制檯程序,空項目:
![](http://static.javashuo.com/static/loading.gif)
2)建立空項目以後,並且選擇不要預編譯頭部,在OpenGL測試項目上右擊打開屬性頁:
3).在連接器的輸入頁面的附加依賴項中添加gltools.lib,glew32s.lib,freeglut.lib;在忽略特定默認庫中添加LIBCMT和MSVCRT。
4)在源文件上選擇右擊,新建一個C++文件,命名爲text1.c。(默認是.cpp文件)
![](http://static.javashuo.com/static/loading.gif)
5)拷貝下面的測試代碼到text1.c中:
1 //#include"stdafx.h" 錯誤緣由:VS2013/2012默認就包含了「stdafx.h」吧,因此沒必要再寫一次。 2 #include <GL/glut.h> 3 void init(void) 4 { 5 glClearColor(0.0, 0.0, 0.0, 0.0);//設置背景顏色爲黑色 6 glShadeModel(GL_SMOOTH);//設置爲光滑明暗模式 7 } 8 void myDisplay(void) 9 { 10 glClear(GL_COLOR_BUFFER_BIT);// 將緩存清除爲預先的設置值,即黑色 11 //glTranslatef(0.8, 0.0, 0.0);//平移函數,暫時能夠不用 12 glBegin(GL_TRIANGLES);//開始畫三角形 13 glColor3f(1.0, 0.0, 0.0);//設置第一個頂點爲紅色 14 glVertex2f(-1.0, -1.0);//設置第一個頂點的座標 15 glColor3f(0.0, 1.0, 0.0);//設置第二個頂點爲綠色 16 glVertex2f(0.0, -1.0);//設置第二個頂點的座標 17 glColor3f(0.0, 0.0, 1.0);//設置第三個頂點爲藍色 18 glVertex2f(-0.5, 1.0);//設置第三個頂點的座標 19 glEnd();//三角形結束 20 glFlush();//強制OpenGL函數在有限時間內運行 21 } 22 void myReshape(GLsizei w, GLsizei h) 23 { 24 glViewport(0, 0, w, h);//設置視口 25 glMatrixMode(GL_PROJECTION);//指明當前矩陣爲GL_PROJECTION 26 glLoadIdentity();//將當前矩陣置換爲單位陣 27 if (w <= h) 28 gluOrtho2D(-1.0, 1.5, -1.5, 1.5*(GLfloat)h / (GLfloat)w);//定義二維正視投影矩陣 29 else 30 gluOrtho2D(-1.0, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5); 31 glMatrixMode(GL_MODELVIEW);//指明當前矩陣爲GL_MODELVIEW 32 } 33 int main(int argc, char ** argv) 34 { 35 /*初始化*/ 36 glutInit(&argc, argv); 37 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//單緩衝、RGB模式 38 glutInitWindowSize(400, 400); 39 glutInitWindowPosition(200, 200); 40 glutCreateWindow("三角形");//窗口標題 41 init(); 42 /*繪製與顯示*/ 43 glutReshapeFunc(myReshape);//窗口大小發生改變時採起的行爲 44 glutDisplayFunc(myDisplay);//顯示繪製圖形 45 glutMainLoop();//循環 46 return(0); 47 }
點擊本地windows調試器運行的結果爲:
加上這句話會出現上圖所示錯誤:
![](http://static.javashuo.com/static/loading.gif)
錯誤緣由:VS2013/2012默認就包含了「stdafx.h」吧,因此沒必要再寫一次。