![](https://upload-images.jianshu.io/upload_images/1490569-f013681c795ea185.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/500/format/webp)
---------------- 本文 ---------------
注意:笔者是以 Win10 + VS2015 为例
内容:安装CMake、配置freeglut 、配置glew
版本:Window10、VS2015、CMake 3.8.0-rc-1、freeglut 3.0.0、glew 2.0.0
目标:使初学者不再为配置OpenGL相关环境苦恼
预热
需要说明的是:OpenGL不需要安装——它的实现早就在您的显卡中存在了。
想要开发OpenGL程序,需要的只是它的一系列接口,而在真正开发时,也不需要在网上找OpenGL的头文件,编译器已经把关于OpenGL的头文件放置在某一个位置中了。
查看OpenGL头文件的方式是用VS随便创建一工程(实例使用了预编译头),然后引入头文件:
#include <gl\GL.h>
然后右键打开文档、再在相应的选项卡上右键打开文件所在的文件夹,如下所示:
![](https://upload-images.jianshu.io/upload_images/1490569-b40b00e301b3fafd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-1f1725f1c8a30166.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-58f60929b110cb2e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
可以看到实例图片中,包含这两个OpenGL的头文件:
GL.h:OpenGL所使用的函数和常量声明。
GLU.h(OpenGL实用库)所使用的函数和常量声明。
不同的系统,这些文件的路径和名字(大小写)很可能是不同的,所以不用纠结这个问题,因为你若是直接引入这个头文件,编译则报错。
![](https://upload-images.jianshu.io/upload_images/1490569-0dc284d63d8df512.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
此时先引入windows头文件即可:
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: 在此处引用程序需要的其他头文件
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
然而,即使如此也并不能立即用代码创建一个窗口来瞧一瞧,因为:
OpenGL自身并不包含任何窗口任务或者处理用户输入的函数。
OpenGL也没有提供任何用于表达三维物体模型、或者读取图像文件(例如JPEG文件)的操作。
所以热身完毕。
准备
OpenGL既然没有包含那些东西,那么我们自然需要用第三方软件库来完成这些功能,例如创建窗口、接受鼠标和键盘事件等。备选的第三方软件库还必须和OpenGL一样支持跨平台移植,于是出现两个大家都在用的第三方软件库:freeGLUT 和 GLEW。
FreeGLUT 是GLUT的一个完全开源替代库。
GLEW是一个简单的OpenGL扩展工具库
本文与其说是OpenGL的开发环境配置教程,不如说是OpenGL相关的第三方软件库的开发环境配置教程。
下载
GLEW只需要去官网下载最新版本就可以了,里面包含了相关的头文件和编译好的lib、dll文件。
而FreeGLUT在官网中下载到的文件需要在本地进一步编译,才能被我们使用,所以本文使用CMake来帮助我们编译FreeGLUT。
各种官网地址
OpenGL: https://www.opengl.org/
FreeGLUT: http://freeglut.sourceforge.net/
GLEW: http://glew.sourceforge.net/
CMake: https://cmake.org/笔者提前准备了相应文件,不想麻烦的童鞋可以在这里下载它们
下载CMake:
![](https://upload-images.jianshu.io/upload_images/1490569-2f7d2d1e3c6728b1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-6befbf371eabc5cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-29d597440b34b80c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-c1f75e8ec4f68197.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
下载FreeGLUT:
![](https://upload-images.jianshu.io/upload_images/1490569-77d191cc684afc07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-0aee9165723f2bf2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
下载GLEW:
![](https://upload-images.jianshu.io/upload_images/1490569-a05541400fbd7fb9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
下载完成
![](https://upload-images.jianshu.io/upload_images/1490569-02d5c148e35bfcdc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/636/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-5012c8d32960e2c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
安装CMake
没什么特别的,一路next下来即可。
![](https://upload-images.jianshu.io/upload_images/1490569-468ff9c2f1c8b03c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/772/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-6abe9ff68b1d5e1d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/772/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-49203ff2969ca2af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/772/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-1e642ff991510e5a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/772/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-b683c619e1435e4a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/772/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-1ba3cb14a41c41d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/772/format/webp)
编译FreeGLUT文件
解压freeglut-3.0.0.tar,并创建CMake编译文件输出文件夹:
![](https://upload-images.jianshu.io/upload_images/1490569-1ff919fea7a5c152.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
打开CMake(cmake-gui), 如下图二者之一:
![](https://upload-images.jianshu.io/upload_images/1490569-3c4b46c436e76dd1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/192/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-7c4d3218785446ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/399/format/webp)
在 where is the source code 中选择解压freeglut的目录,注意这个目录内包含了CMakeLists.txt。
在where to build the binaries 中选择之前创建的CMake编译文件输出文件夹:
![](https://upload-images.jianshu.io/upload_images/1490569-8ff8451e0743ee13.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/714/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-cf29c2d7691808a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
点击Configure:
![](https://upload-images.jianshu.io/upload_images/1490569-d5bb98c63bcd01f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/714/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-c82ac0caff725c24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/502/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-3297bfeccefc34bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/867/format/webp)
注意,如果出现错误提示,则先点击File->Delete Cache之后,再点击Configure,然后选择不同的VS版本重试(前提是你的电脑确实安装了该版本的VS):
![](https://upload-images.jianshu.io/upload_images/1490569-9ded081583466e7b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/502/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-64e35d448914f3f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/273/format/webp)
Configur完成之后(注意勾选下图列出的项,特别是FREEGLUT_BUILD_SHARED_LIBS 和FREEGLUT_BULID_STATIC_LIBS),点击Generate生成VS工程文件:
![](https://upload-images.jianshu.io/upload_images/1490569-2f7894db3e23f9ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/867/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-b3b90102c46c783a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/867/format/webp)
打开CMake输出文件目录,找到freeglut.sln文件,双击打开:
![](https://upload-images.jianshu.io/upload_images/1490569-83a1628bef845479.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-04bf53f7c33911c4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-440a56b1638d8885.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/687/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-6e8d282c4ee3017e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
点击生成->生成解决方法:
![](https://upload-images.jianshu.io/upload_images/1490569-5d330b58ad70fbac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-f41688af10f0dec5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
注意此时编译出来的是debug、win32版本的freeglut3.0.0文件——需要release版本,需要在Release下,再次编译一下即可(不过,原来的32位文件就被覆盖掉了):
![](https://upload-images.jianshu.io/upload_images/1490569-a71d007302bd2e9e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
编译完成后,再次打开工程所在文件夹(即CMake输出文件夹);此时已经编译好了的freeglut文件就存放在lib、bin文件夹里面:
![](https://upload-images.jianshu.io/upload_images/1490569-6f371ea2f563269f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
如果需要64位lib文件和dll文件,只需重新使用CMake 编译生成,只不过选择编译器时选择Win64而已:
![](https://upload-images.jianshu.io/upload_images/1490569-9c561c5f53fc6dc8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-28468ed9021a03b6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-bd42562313e65479.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
至此,准备工作完成,各种文件皆已准备妥当:
![](https://upload-images.jianshu.io/upload_images/1490569-30de762633905361.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
以下内容未包含CMake生成的64位工程文件;笔者生成的64位的freeglut VS工程文件夹为:freeglut 2
![](https://upload-images.jianshu.io/upload_images/1490569-47a05df961c11bf7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
它编译出来的64 freegult 的 lib文件和 dll 文件在 下一篇中才会使用到。
搭建OpenGL开发环境
创建一个C++控制台工程,然后先运行或编译下程序:
![](https://upload-images.jianshu.io/upload_images/1490569-a777f90bb9d4de80.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-b024eda94345a756.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/999/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-5272527ea4915bbd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
配置包含目录、库目录
配置VS包含目录和库目录时,它们分别都需要设置两个路径——glew的include路径、freeglut的include路径以及glew的lib路径和freeglut的lib路径
点击项目->属性->VC++目录:
![](https://upload-images.jianshu.io/upload_images/1490569-9f966be094beb87e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-2be8a812d33b25e7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-728dccd1b7342e90.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
注意,这里的包含目录简单是说就是我们需要包含.h文件的文件夹,你也可以在把这些.h直接添加到工程中:
![](https://upload-images.jianshu.io/upload_images/1490569-d0e9a63eca7812ea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/632/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-538cda4212a84450.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-743ede24afce8ee8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-2ce8222768989e5f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/632/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-80d6af5ec0834936.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/632/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-ff4946bc0f5600b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
注意,freegult的库文件(.lib)文件是你自己用CMake、VS2015最终编译出来的,需要不同版本的 .lib 文件,只需要更改好后,重新编译即可。
上面已经详细的教给你钓鱼的方法,以后freeglut版本更新后,例如freegult 3.1.0,只需再次CMake、VS2015编译即可。
![](https://upload-images.jianshu.io/upload_images/1490569-8b3fb82ff4fe9acf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-7f509f5fe6865797.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/632/format/webp)
显式包含glew32.lib文件——以添加附加依赖项的方式:
![](https://upload-images.jianshu.io/upload_images/1490569-dbc41db45457d94b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-7b6ca807351036bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-242b5d18fed1ab98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
程序运行所必需的 .dll 链接文件:
![](https://upload-images.jianshu.io/upload_images/1490569-ab3af6c7b0e7e055.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
![](https://upload-images.jianshu.io/upload_images/1490569-4c49cc01da5b764a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
把这两个.dll文件复制到exe同目录下:
![](https://upload-images.jianshu.io/upload_images/1490569-c25ef2ba7f089adf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
至此,OpenGL 的开发环境配置完毕。
注意,本文使用了一种比较麻烦的配置方式,以后遇到其他的第三方库也可以这样配置。
这种方法比较安全,就算配置错误、复制错文件、误删文件,比较容易恢复,最坏的情景也不过是删除整个工程,重来一遍,而不会对系统造成伤害,在开发过程中埋下未知错误。
当然有一种一劳永逸的方法来配置OpenGL开发环境,这等到我们测试成功之后再介绍,不过这种方法不太安全,请你在确保你知道你在操作什么的时候再使用在这种一劳永逸的方法。