1. 程式人生 > >VS配置OpenGL開發環境:GLFW庫和GLAD庫的配置

VS配置OpenGL開發環境:GLFW庫和GLAD庫的配置

1.去GLFW官網下載相應版本的配置檔案:點選開啟連結,無論你的系統是32位還是64位,都建議下載32位的配置檔案,如下圖:


去GLAD線上服務獲得glad.h配置檔案點選開啟連結,按照下圖的資訊填寫,獲得glad.zip檔案:



2.將下載的檔案解壓,得到VS各個版本的配置庫,你可以根據你安裝的VS版本選擇,由於筆者安裝的是VS2013,所以選擇lib-vc2013,在解壓好的檔案裡面,我們只需要兩個資料夾裡的東西:include裡面的GLFW資料夾和lib-vc2013;解壓glad.zip後可以看到兩個資料夾:include和src,我們將include資料夾裡面的KHR和glad資料夾考到新建資料夾include裡面,如下圖所示:


再將lib-vc2013資料夾和include資料夾放同一資料夾下,如下圖:


好到這裡資料夾整理工作完畢,接下來看如何配置!!!

3.將lib-vc2013資料夾裡的glfw3.dll檔案複製到系統資料夾裡面:C:\Windows\System32和C:\Windows\SysWOW64。

4.開啟VS2013,新建一個控制檯應用程式,新建一個demo.cpp,並將glad.zip解壓得到的src資料夾裡面的glad.c新增到專案中,如下圖所示:


5.連結包含目錄和庫目錄,右鍵單擊解決方案:屬性->配置屬性->VC++目錄:將步驟2裡面的兩個資料夾路徑包含進來,如下圖:



6.連結庫:opengl32.lib和glfw3.lib。右鍵單擊解決方案:屬性->配置屬性->連結器->附加依賴項,如下圖:


到這裡,配置大功告成,下面進行第一個例子的執行!!!

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);
}
執行結果: