系统环境

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

  1. #include "stdafx.h"
  2. #include <GL/glew.h>
  3. //#define GLFW_DLL
  4. #include <GLFW/glfw3.h>
  5.  
  6. int main(int argc, _TCHAR* argv[])
  7. {
  8. // start GL context and O/S window using the GLFW helper library
  9. if (!glfwInit ()) {
  10. fprintf (stderr, "ERROR: could not start GLFW3\n");
  11. return ;
  12. }
  13.  
  14. GLFWwindow* window = glfwCreateWindow (, , "Hello Triangle", NULL, NULL);
  15. if (!window) {
  16. fprintf (stderr, "ERROR: could not open window with GLFW3\n");
  17. glfwTerminate();
  18. return ;
  19. }
  20. glfwMakeContextCurrent (window);
  21.  
  22. // start GLEW extension handler
  23. glewExperimental = GL_TRUE;
  24. glewInit ();
  25.  
  26. // get version info
  27. const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
  28. const GLubyte* version = glGetString (GL_VERSION); // version as a string
  29. printf ("Renderer: %s\n", renderer);
  30. printf ("OpenGL version supported %s\n", version);
  31.  
  32. // tell GL to only draw onto a pixel if the shape is closer to the viewer
  33. glEnable (GL_DEPTH_TEST); // enable depth-testing
  34. glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
  35.  
  36. float points[] = {
  37. 0.0f, 0.5f, 0.0f,
  38. 0.5f, -0.5f, 0.0f,
  39. -0.5f, -0.5f, 0.0f
  40. };
  41.  
  42. GLuint vbo = ;
  43. glGenBuffers (, &vbo);
  44. glBindBuffer (GL_ARRAY_BUFFER, vbo);
  45. glBufferData (GL_ARRAY_BUFFER, * sizeof (float), points, GL_STATIC_DRAW);
  46.  
  47. GLuint vao = ;
  48. glGenVertexArrays (, &vao);
  49. glBindVertexArray (vao);
  50. glEnableVertexAttribArray ();
  51. glBindBuffer (GL_ARRAY_BUFFER, vbo);
  52. glVertexAttribPointer (, , GL_FLOAT, GL_FALSE, , NULL);
  53.  
  54. const char* vertex_shader =
  55. "#version 400\n"
  56. "in vec3 vp;"
  57. "void main () {"
  58. " gl_Position = vec4 (vp, 1.0);"
  59. "}";
  60.  
  61. const char* fragment_shader =
  62. "#version 400\n"
  63. "out vec4 frag_colour;"
  64. "void main () {"
  65. " frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
  66. "}";
  67.  
  68. GLuint vs = glCreateShader (GL_VERTEX_SHADER);
  69. glShaderSource (vs, , &vertex_shader, NULL);
  70. glCompileShader (vs);
  71. GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
  72. glShaderSource (fs, , &fragment_shader, NULL);
  73. glCompileShader (fs);
  74.  
  75. GLuint shader_programme = glCreateProgram ();
  76. glAttachShader (shader_programme, fs);
  77. glAttachShader (shader_programme, vs);
  78. glLinkProgram (shader_programme);
  79.  
  80. // Loop until the user closes the window
  81. while (!glfwWindowShouldClose(window))
  82. {
  83. // wipe the drawing surface clear
  84. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  85. glUseProgram (shader_programme);
  86. glBindVertexArray (vao);
  87. // draw points 0-3 from the currently bound VAO with current in-use shader
  88. glDrawArrays (GL_TRIANGLES, , );
  89. // update other events like input handling
  90. glfwPollEvents ();
  91. // put the stuff we've been drawing onto the display
  92. glfwSwapBuffers (window);
  93. }
  94.  
  95. // close GL context and any other GLFW resources
  96. glfwTerminate();
  97.  
  98. return ;
  99. }

点击“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

效果如下:

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

Windows7+VS2012下OpenGL 4的环境配置的更多相关文章

  1. OS X 下 OpenGL 4.x 环境配置

    配置: OS X 10.10 + CMake 3.2.2 + GLFW 3.1.1 + OpenGL 4.1 + Xcode 6.0 本文主要介绍如何在 OS X 系统下进行环境配置,使得 Xcode ...

  2. OpenGL C#绘图环境配置

    OpenGL C#绘图环境配置   OpenGL简介 OpenGL作为一种图形学编程接口已经非常流行, 虽然在大型游戏方面DirectX有一定的市场占有率, 但由于OpenGL的开放性,可移植性等优点 ...

  3. LibOpenCM3(一) Linux下命令行开发环境配置

    目录 LibOpenCM3(一) Linux下命令行开发环境配置 本文使用 Linux 环境, 硬件为 STM32F103 系列开发板 LibOpenCM3 介绍 LibOpenCM3 是GPL协议( ...

  4. 【OpenGL】VS2010环境配置 [转]

    基于OpenGL标准开发的应用程序运行时需有动态链接库OpenGL32.DLL.Glu32.DLL,这两个文件在安装Windows NT时已自动装载到C:\WINDOWS\SYSTEM32目录下(这里 ...

  5. windows系统下简单nodej.s环境配置 安装

    国内目前关注最高,维护最好的一个关于nodejs的网站应该是http://www.cnodejs.org/ windows系统下简单nodejs环境配置. 第一步:下载安装文件 下载地址:官网 htt ...

  6. win系统下nodejs安装及环境配置

    第一步:下载安装文件下载nodejs,官网:http://nodejs.org/download/,我这里下载的是node-v0.10.28-x86.msi,如下图: 第二步:安装nodejs下载完成 ...

  7. windows 7 64bit 下apache php mysql 环境配置

    在64位环境下安装apache,php和配置过程 准备好安装包.(64位版本) Apache 下载地址:http://www.apachelounge.com/download/ Php 下载地址:h ...

  8. ubuntu下Qt之android环境配置以及一些常见问题解决

    准备材料有: 1. qt for android 5.×版本,下载地址如下,可以选择一个合适自己机器型号的版本进行下载. 地址:http://www.qt.io/download-open-sourc ...

  9. 【cocos 2d-x】VS2012+win7+cocos2d-x3.0beta2开发环境配置

    本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.com ...

随机推荐

  1. PAT 01-1

    #include <stdio.h> int main() { int i; int k; ]; scanf("%d", &k); ; i < k; i+ ...

  2. 多线程基础 (八)NSOperation相关

    额外的参考学习可以学习:http://www.cnblogs.com/YouXianMing/p/3707403.html 1.NSOperation简介   NSOperation的作用 配合使用N ...

  3. Stronger (What Doesn't Kill You)

    今天听一个歌曲,挺不错的.以前一直不知道意思.这次把歌词摘抄下来. 试听音乐: 原版MV: You know the bed feels warmer 你知道被窝里的温暖 Sleeping here ...

  4. UVa 112 - Tree Summing(树的各路径求和,递归)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  5. HashMap总结

    最近朋友推荐的一个很好的工作,又是面了2轮没通过,已经是好几次朋友内推没过了,觉得挺对不住朋友的.面试反馈有一方面是有些方面理解思考的还不够,平时也是项目进度比较紧,有些方面赶进度时没有理解清楚的后面 ...

  6. Hibernate之Criteria的完整用法

    Criteria的完整用法 QBE (Query By Example) Criteria cri = session.createCriteria(Student.class); cri.add(E ...

  7. C#照片批量压缩小工具

    做了一个照片批量压缩工具,其实核心代码几分钟就完成了,但整个小工具做下来还是花了一天的时间.中间遇到了大堆问题,并寻求最好的解决方案予以解决.现在就分享一下这个看似简单的小工具所使用的技术. 软件界面 ...

  8. centos7安装python-pip

    在使用centos7的软件包管理程序yum安装python-pip的时候会报一下错误: No package python-pip available. Error: Nothing to do 说没 ...

  9. 超链接的那些事(三): 属性target

    a标签的属性之一 target 1. 定义     规定在何处打开链接文档. 如果a标签中有target属性,浏览器将会载入和显示用这个标签的 href 属性命名的.名称与这个目标吻合的框架或者窗口中 ...

  10. Linux date命令的用法

    在linux shell编程中,经常用到日期的加减运算以前都是自己通过expr函数计算,很麻烦.其实date命令本身提供了日期的加减运算非常方便. 例如:得到昨天的时间date  --date=&qu ...