欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

程序员文章站 2022-07-14 08:24:08
...

1.去GLFW官网下载相应版本的配置文件:点击打开链接,无论你的系统是32位还是64位,都建议下载32位的配置文件,如下图:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

去GLAD在线服务获得glad.h配置文件点击打开链接,按照下图的信息填写,获得glad.zip文件:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

2.将下载的文件解压,得到VS各个版本的配置库,你可以根据你安装的VS版本选择,由于笔者安装的是VS2013,所以选择lib-vc2013,在解压好的文件里面,我们只需要两个文件夹里的东西:include里面的GLFW文件夹和lib-vc2013;解压glad.zip后可以看到两个文件夹:include和src,我们将include文件夹里面的KHR和glad文件夹考到新建文件夹include里面,如下图所示:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置VS配置OpenGL开发环境:GLFW库和GLAD库的配置

再将lib-vc2013文件夹和include文件夹放同一文件夹下,如下图:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

好到这里文件夹整理工作完毕,接下来看如何配置!!!

3.将lib-vc2013文件夹里的glfw3.dll文件复制到系统文件夹里面:C:\Windows\System32和C:\Windows\SysWOW64。

4.打开VS2013,新建一个控制台应用程序,新建一个demo.cpp,并将glad.zip解压得到的src文件夹里面的glad.c添加到项目中,如下图所示:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

5.链接包含目录和库目录,右键单击解决方案:属性->配置属性->VC++目录:将步骤2里面的两个文件夹路径包含进来,如下图:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

6.链接库:opengl32.lib和glfw3.lib。右键单击解决方案:属性->配置属性->链接器->附加依赖项,如下图:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置

到这里,配置大功告成,下面进行第一个例子的运行!!!

7.实现一个OpenGL窗口:hello window。

demo.cpp代码:

#include <glad\glad.h>
#include <GLFW\glfw3.h>

#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
	// glfw: initialize and configure
	// ------------------------------
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif

	// glfw window creation
	// --------------------
	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
	if (window == NULL)
	{
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	// glad: load all OpenGL function pointers
	// ---------------------------------------
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	// render loop
	// -----------
	while (!glfwWindowShouldClose(window))
	{
		// input
		// -----
		processInput(window);

		// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
		// -------------------------------------------------------------------------------
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	// glfw: terminate, clearing all previously allocated GLFW resources.
	// ------------------------------------------------------------------
	glfwTerminate();
	return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	// make sure the viewport matches the new window dimensions; note that width and 
	// height will be significantly larger than specified on retina displays.
	glViewport(0, 0, width, height);
}
运行结果:

VS配置OpenGL开发环境:GLFW库和GLAD库的配置