1.下载资源
- freeglut-MSVC-3.0.0-2.mp
- glew-2.1.0-win32.zip
- Cmake(虽然我电脑是64位也是下载Windows win32-x86 Installer版本)点击下载的.msi文件,根据指示进行安装
- glfw(点击Source package进行下载)
下载好的压缩包都要进行解压。
2.建立文件夹放置源代码和依赖库
推荐的方式是建立一个新的目录包含所有的第三方库文件和头文件,并且在你的IDE或编译器中指定这些文件夹。在这里存放OpenGL工程用到的所有第三方库和头文件。这样我的所有第三方库都在同一个位置(并且可以共享至多台电脑)。然而这要求你每次新建一个工程时都需要告诉IDE/编译器在哪能找到这些目录。
glew和freeglut的添加参考https://blog.csdn.net/qq_33276623/article/details/51424014。
-
配置方法一 freeglut+glew
(1)glew
新建一个文件夹,取名Dependencies(当然你也可以取别的名字),在Dependencies下再建一个目录glew。
到之前解压出的glew-2.1.0目录下,有一个include\GL目录,里面有四个.h文件,把这四个文件拷贝到Dependencies\glew目录下。
在到glew-2.1.0\lib\Release目录,因为我是64位系统,所以选择x64目录下的glew32.lib和glew32s.lib拷贝到Dependencies\glew目录下。
(2)freeglut
新建一个freeglut文件夹在Dependencies下。
到之前下载解压出的freeglut目录下,include\GL内,有4个.h文件,将它们拷贝到Dependencies\freeglut。
到之前下载解压出的freeglut目录下,lib\x64内,有一个freeglut.lib文件,同样拷贝到Dependencies\freeglut。
(3)配置VS项目
VS2015新建一个项目,选择Visual C++ 和 空项目。名字自己起,目录中不要有空格,然后在项目中新建一个 main.cpp文件。将Dependencies文件夹复制到项目目录下(和main.cpp同级)。
然后回到VS2015,在 解决方案资源管理器 选中我们的项目,点击菜单项目-显示所有文件,再刷新一下 解决方案资源管理器 ,会看到Dependencies出现,右键点击包括在项目中,再分别打开下面的目录,看看前面有红色图标的项目,也分别点包括在项目中。
再选中项目,右键属性,打开属性窗口,选择链接器-常规,在附加库目录,输入 Dependencies\freeglut;Dependencies\glew
再选择链接器-输入,在附加依赖项,加上 opengl32.lib;freeglut.lib;glew32.lib;
点击确定,全部配置完。Debug时记得选择x64,测试代码:
#include "Dependencies\glew\glew.h"
#include "Dependencies\freeglut\freeglut.h"
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 1.0f, 0.0f);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("First_GL!");
glutDisplayFunc(myDisplay);
glutMainLoop();
}
按F5运行,弹出缺少freeglut.dll的错误。再回到下载的freeglut\bin\x64目录,把freeglut.dll拷贝到VS项目的x64\Debug目录即可。
-
配置方法二 glfw+glew
新建一个Dependencies文件夹。
(1)编译glfw的源代码生成库
新建一个glfw文件夹在Dependencies下。
到之前下载解压出的glfw目录下,include\GLFW内,有2个.h文件,将它们拷贝到Dependencies\glfw。
到之前下载解压出的glfw目录下,新建一个build文件夹
然后打开Cmake,在Where is the source code栏里,选择GLFW源代码所在目录(即下载的glfw文件夹);在Where to build the binaries栏里,选择要将项目文件生成在哪个文件夹里(即新建的build文件夹)。然后点击Configure。
点击Generate,等待生成完毕。如果底下显示"Generating done"则生成成功。接下来可以直接点击Open Project,或找到生成的GLFW.sln(或者其它扩展名)手动打开。
点击上面的生成,选择生成解决方案。
等待编译完成后,在glfw的build/src/Debug文件夹里找到glfw3.lib的文件,同样拷贝到Dependencies\glfw。
(2)glew
在Dependencies下再建一个目录glew32。
到之前解压出的glew-2.1.0目录下,有一个include\GL目录,里面有四个.h文件,把这四个文件拷贝到Dependencies\glew目录下。
在到glew-2.1.0\lib\Release目录,选择Win32目录下的glew32s.lib拷贝到Dependencies\glew32目录下。
(3)配置VS项目
VS2015新建一个项目,选择Visual C++ 和 空项目。名字自己起,目录中不要有空格,然后在项目中新建一个 main.cpp文件将Dependencies文件夹复制到项目目录下(和main.cpp同级)。
然后回到VS2015,在 解决方案资源管理器 选中我们的项目,点击菜单项目-显示所有文件,再刷新一下 解决方案资源管理器 ,会看到Dependencies出现,右键点击包括在项目中,再分别打开下面的目录,看看前面有红色图标的项目,也分别点包括在项目中。
再选中项目,右键属性,打开属性窗口,选择链接器-常规,在附加库目录,输入Dependencies\glfw;Dependencies\glew32
再选择链接器-输入,在附加依赖项,加上 opengl32.lib;glfw3.lib;glew32s.lib;
点击确定配置完毕。注意,这种方法运行的时候就要选择x86方式。测试代码:
#include <iostream>
// GLEW
#define GLEW_STATIC
#include "Dependencies\glew32\glew.h"
// GLFW
#include "Dependencies\glfw\glfw3.h"
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
运行结果:
PS:除了上述方法还可以建立Dependencies文件夹,在文件夹下建立include和libs文件夹,把glfw的.h文件和glew的.h文件放入include文件夹,把glew文件夹内win32的glew32s.lib和生成的glfw3.lib放入libs文件夹。在进行VS项目配置时,包含目录选择include文件夹,库目录选择libs文件夹,链接器-输入-附加依赖项输入opengl32.lib;glfw3.lib;glew32s.lib;
测试代码的头文件使用:
#include <glew.h>
#include <glfw3.h>