系統環境
Windows 7 Ultimate x64,Visual Studio Ultimate 2012 Update 4,和一塊支持OpenGL 4.x的顯卡。
准備工作
首先用GPU Caps Viewer檢查機器最高支持的OpenGL及GLSL版本。比如我的機器可以支持OpenGL 4.5和GLSL 4.5:
下載GLEW和GLFW的源碼。其中GLEW用來管理和載入OpenGL的各種擴展庫,GLFW用來創建窗口和控制鼠標及鍵盤的交互。我使用的版本分別是glew-1.12.0和glfw-3.1.1。下載地址分別為:
https://sourceforge.net/projects/glew/files/glew/1.12.0/glew-1.12.0.zip/download
http://sourceforge.net/projects/glfw/files/glfw/3.1.1/glfw-3.1.1.zip/download
下載並安裝CMAKE。用來輔助編譯GLFW。我使用的版本為cmake-3.2.1。下載地址為:
http://www.cmake.org/files/v3.2/cmake-3.2.1-win32-x86.exe
編譯GLFW
打開CMAKE。點擊“Browsw source…”選擇GLFW的根目錄,點擊“Browse build…”任意選取一個新建的文件夾作為輸出目錄。點擊“Configure”:
在彈出的窗口中選擇“Visual Studio 11 2012”,點擊“Finish”:
保持默認配置即可。如果想用頭文件+.dll的方式調用GLFW,可以勾選“BUILD_SHARED_LINKS”;不勾選則是默認的頭文件+.lib方式調用。為“CMAKE_INSTALL_PREFIX”選擇一個新的位置,用來存放用VS2012編譯輸出的結果。然后點擊“Generate”,VS2012項目文件就生成完畢了。
打開剛剛指定的Build目錄,用Visual Studio 2012打開GLFW.sln文件。為保險起見,先用Debug方式編譯ZERO_CHECK,如果沒有報錯,則用Release方式編譯ALL_BUILD,再編譯INSTALL。
如果編譯成功,在之前指定的GLFW文件夾內會多出兩個文件夾include和lib,這里面就包含着我們會用到的頭文件和鏈接庫文件:
把include中的GLFW文件夾拷貝到一個你覺得方便的位置,比如“E:\dev-lib\opengl4\include”:
把lib中的glfw3.lib文件拷貝到新的位置,比如“E:\dev-lib\opengl4\lib”:
編譯GLEW
用Visual Studio 2012打開GLEW目錄下build\vc10\glew.sln文件,第一次打開時會提示是否把VS2010項目轉換成VS2012項目,選擇是即可。選擇Release方式,點擊BUILD -> Build Solution:
如果編譯成功,會發現在bin\Release\Win32目錄下有3個新文件。把glew32.dll文件拷貝到C:\Windows\SysWOW64目錄下。
glewinfo.exe和visualinfo.exe可以用來查看系統對OpenGL各個特性的支持情況,比如:
把lib\Release\Win32目錄下的glew32.lib拷貝到之前的“E:\dev-lib\opengl4\lib”中。把include目錄中的GL文件夾拷貝到之前的“E:\dev-lib\opengl4\include”中。
新建項目
打開Visual Studio 2012,點擊FILE -> New -> Project…,選擇Visual C++ -> Win32 Console Application。比如命名為“ogl4-test”:
右擊項目名稱,點擊Properties。在Configuration Properties -> VC++ Directories中,為Include Directories添加“E:\dev-lib\opengl4\include;”;為Library Directories添加“E:\dev-lib\opengl4\lib;”。注意為Debug和Release都要添加:
在Configuration Properties -> Linker -> Input中,為Additional Dependencies添加“glew32.lib;glfw3.lib;opengl32.lib;”。注意為Debug和Release都要添加:
把以下代碼拷貝到0gl4-test.cpp文件中。代碼來自Anton's OpenGL 4 Tutorials
#include "stdafx.h" #include <GL/glew.h> //#define GLFW_DLL #include <GLFW/glfw3.h> int main(int argc, _TCHAR* argv[]) { // start GL context and O/S window using the GLFW helper library if (!glfwInit ()) { fprintf (stderr, "ERROR: could not start GLFW3\n"); return 1; } GLFWwindow* window = glfwCreateWindow (800, 600, "Hello Triangle", NULL, NULL); if (!window) { fprintf (stderr, "ERROR: could not open window with GLFW3\n"); glfwTerminate(); return 1; } glfwMakeContextCurrent (window); // start GLEW extension handler glewExperimental = GL_TRUE; glewInit (); // get version info const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string const GLubyte* version = glGetString (GL_VERSION); // version as a string printf ("Renderer: %s\n", renderer); printf ("OpenGL version supported %s\n", version); // tell GL to only draw onto a pixel if the shape is closer to the viewer glEnable (GL_DEPTH_TEST); // enable depth-testing glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer" float points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f }; GLuint vbo = 0; glGenBuffers (1, &vbo); glBindBuffer (GL_ARRAY_BUFFER, vbo); glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW); GLuint vao = 0; glGenVertexArrays (1, &vao); glBindVertexArray (vao); glEnableVertexAttribArray (0); glBindBuffer (GL_ARRAY_BUFFER, vbo); glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); const char* vertex_shader = "#version 400\n" "in vec3 vp;" "void main () {" " gl_Position = vec4 (vp, 1.0);" "}"; const char* fragment_shader = "#version 400\n" "out vec4 frag_colour;" "void main () {" " frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);" "}"; GLuint vs = glCreateShader (GL_VERTEX_SHADER); glShaderSource (vs, 1, &vertex_shader, NULL); glCompileShader (vs); GLuint fs = glCreateShader (GL_FRAGMENT_SHADER); glShaderSource (fs, 1, &fragment_shader, NULL); glCompileShader (fs); GLuint shader_programme = glCreateProgram (); glAttachShader (shader_programme, fs); glAttachShader (shader_programme, vs); glLinkProgram (shader_programme); // Loop until the user closes the window while (!glfwWindowShouldClose(window)) { // wipe the drawing surface clear glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram (shader_programme); glBindVertexArray (vao); // draw points 0-3 from the currently bound VAO with current in-use shader glDrawArrays (GL_TRIANGLES, 0, 3); // update other events like input handling glfwPollEvents (); // put the stuff we've been drawing onto the display glfwSwapBuffers (window); } // close GL context and any other GLFW resources glfwTerminate(); return 0; }
點擊“Local Windows Debugger”,就可以看見如下效果:
另一枚例子
這是一個用合成的Gerstner波繪制水面的例子,根據《水面的簡單渲染 – Gerstner波》修改而來。完整代碼下載地址:http://pan.baidu.com/s/1gdzoe4b,所需的配置文件下載地址:http://pan.baidu.com/s/1pJ81kyZ。項目托管地址:https://github.com/johnhany/OpenGLProjects/tree/master/GerstnerWave。
效果如下: