Windows7+VS2012下OpenGL 4的环境配置

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:

id="iframe_0.8438952273643598" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/gpu-caps-viewer.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8438952273643598',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 408px; height: 583px;">

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

id="iframe_0.8271061345109023" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/cmake-1.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8271061345109023',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 544px; height: 658px;">       

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

                id="iframe_0.22868912363139393" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/cmake-2.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.22868912363139393',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 605px; height: 654px;">       

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

id="iframe_0.532804574426321" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/cmake-3.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.532804574426321',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 542px; height: 655px;">

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

id="iframe_0.9553105630766385" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/vs2012-glfw.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9553105630766385',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 924px; height: 769px;"> 

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

id="iframe_0.8917844937472885" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/glfw-directory.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8917844937472885',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 834px; height: 435px;">       

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

id="iframe_0.14830359125378356" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/glfw-include.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.14830359125378356',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 815px; height: 428px;">       

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

id="iframe_0.7854555186303311" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/glfw-lib.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7854555186303311',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 839px; height: 439px;">


编译GLEW

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

id="iframe_0.43994285825952284" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/vs2012-glew.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.43994285825952284',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 924px; height: 480px;">     

如果编译成功,会发现在bin\Release\Win32目录下有3个新文件。把glew32.dll文件拷贝到C:\Windows\SysWOW64目录下。

id="iframe_0.17201029844931437" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/glew-bin.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.17201029844931437',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 853px; height: 437px;">       

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

id="iframe_0.9096049747081323" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/glewinfo.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9096049747081323',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 727px; height: 732px;">       

把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”:

id="iframe_0.6414979522452131" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/vs2012-new-project.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6414979522452131',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 924px; height: 643px;">       

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

id="iframe_0.2916629829714654" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/project-properties.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2916629829714654',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 924px; height: 663px;">       

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

id="iframe_0.7228504866287562" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/linker-input.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7228504866287562',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 924px; height: 667px;">

        把以下代码拷贝到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”,就可以看见如下效果:

id="iframe_0.978680235197976" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/triangle.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.978680235197976',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 851px; height: 727px;">

 


另一枚例子

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

        效果如下:

id="iframe_0.8924721453220488" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://johnhany.net/wp-content/uploads/2015/03/gerstner-waves.jpg?_=5557636%22%20style=%22border:none;max-width:924px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8924721453220488',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-style: none; border-width: initial; width: 842px; height: 727px;">

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


http://www.cnblogs.com/rainbow70626/p/5557636.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值