Windows7+VS2012下OpenGL 4的环境配置

系统环境

        Windows 7 Ultimate x64,Visual Studio Ultimate 2012 Update 4,和一块支持OpenGL 4.x的显卡。


准备工作

        首先用GPU Caps Viewer检查机器最高支持的OpenGL及GLSL版本。比如我的机器可以支持OpenGL 4.5和GLSL 4.5:

gpu-caps-viewer

        下载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”:

cmake-1       

在弹出的窗口中选择“Visual Studio 11 2012”,点击“Finish”:

               cmake-2       

保持默认配置即可。如果想用头文件+.dll的方式调用GLFW,可以勾选“BUILD_SHARED_LINKS”;不勾选则是默认的头文件+.lib方式调用。为“CMAKE_INSTALL_PREFIX”选择一个新的位置,用来存放用VS2012编译输出的结果。然后点击“Generate”,VS2012项目文件就生成完毕了。

cmake-3

        打开刚刚指定的Build目录,用Visual Studio 2012打开GLFW.sln文件。为保险起见,先用Debug方式编译ZERO_CHECK,如果没有报错,则用Release方式编译ALL_BUILD,再编译INSTALL。

vs2012-glfw 

      如果编译成功,在之前指定的GLFW文件夹内会多出两个文件夹include和lib,这里面就包含着我们会用到的头文件和链接库文件:

glfw-directory       

 把include中的GLFW文件夹拷贝到一个你觉得方便的位置,比如“E:dev-libopengl4include”:

glfw-include       

把lib中的glfw3.lib文件拷贝到新的位置,比如“E:dev-libopengl4lib”:

glfw-lib


编译GLEW

        用Visual Studio 2012打开GLEW目录下buildvc10glew.sln文件,第一次打开时会提示是否把VS2010项目转换成VS2012项目,选择是即可。选择Release方式,点击BUILD -> Build Solution:

vs2012-glew     

如果编译成功,会发现在binReleaseWin32目录下有3个新文件。把glew32.dll文件拷贝到C:WindowsSysWOW64目录下。

glew-bin       

 glewinfo.exe和visualinfo.exe可以用来查看系统对OpenGL各个特性的支持情况,比如:

glewinfo       

把libReleaseWin32目录下的glew32.lib拷贝到之前的“E:dev-libopengl4lib”中。把include目录中的GL文件夹拷贝到之前的“E:dev-libopengl4include”中。


新建项目

       打开Visual Studio 2012,点击FILE -> New -> Project…,选择Visual C++ -> Win32 Console Application。比如命名为“ogl4-test”:

vs2012-new-project       

右击项目名称,点击Properties。在Configuration Properties -> VC++ Directories中,为Include Directories添加“E:dev-libopengl4include;”;为Library Directories添加“E:dev-libopengl4lib;”。注意为Debug和Release都要添加:

project-properties       

在Configuration Properties -> Linker -> Input中,为Additional Dependencies添加“glew32.lib;glfw3.lib;opengl32.lib;”。注意为Debug和Release都要添加:

linker-input

        把以下代码拷贝到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
");
        return 1;
    } 

    GLFWwindow* window = glfwCreateWindow (800, 600, "Hello Triangle", NULL, NULL);
    if (!window) {
        fprintf (stderr, "ERROR: could not open window with GLFW3
");
        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
", renderer);
    printf ("OpenGL version supported %s
", 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
"
        "in vec3 vp;"
        "void main () {"
        "  gl_Position = vec4 (vp, 1.0);"
        "}";

    const char* fragment_shader =
        "#version 400
"
        "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”,就可以看见如下效果:

triangle


另一枚例子

        这是一个用合成的Gerstner波绘制水面的例子,根据《水面的简单渲染 – Gerstner波》修改而来。完整代码下载地址:http://pan.baidu.com/s/1gdzoe4b,所需的配置文件下载地址:http://pan.baidu.com/s/1pJ81kyZ。项目托管地址:https://github.com/johnhany/OpenGLProjects/tree/master/GerstnerWave

        效果如下:

gerstner-waves

原文链接:Windows7+VS2012下OpenGL 4的环境配置

原文地址:https://www.cnblogs.com/rainbow70626/p/5557636.html