WIN10 OpenGL GLFW+GLAD 开发环境搭建
一.GLFW集成
x86平臺請下載32-bit Windows binaries
下載source package,提取當中的include文件夾
?
?
新建一個空的win32工程,將32-bit Windows binaries下載得到的壓縮包內的glfw3.lib拷貝到你自己的工作目錄.添加這些目錄(需要VS搜索庫和include文件的地方),我們首先進入Project Properties(工程屬性,在解決方案窗口里右鍵項目),然后選擇VC++ Directories(VC++ 目錄)選項卡(如下圖)。在下面的兩欄添加目錄:glfw.png
?
?
最后需要在Linker(鏈接器)選項卡里的Input(輸入)選項卡里添加glfw3.lib這個文件:
glfw3.png
二.配置GLAD
GLAD是一個開源的庫,它能解決我們上面提到的那個繁瑣的問題。GLAD的配置與大多數(shù)的開源庫有些許的不同,GLAD使用了一個在線服務。在這里我們能夠告訴GLAD需要定義的OpenGL版本,并且根據(jù)這個版本加載所有相關的OpenGL函數(shù)。
打開GLAD的在線服務,將語言(Language)設置為C/C++,在API選項中,選擇3.3以上的OpenGL(gl)版本(我們的教程中將使用3.3版本,但更新的版本也能正常工作)。之后將模式(Profile)設置為Core,并且保證生成加載器(Generate a loader)的選項是選中的。現(xiàn)在可以先(暫時)忽略拓展(Extensions)中的內容。都選擇完之后,點擊生成(Generate)按鈕來生成庫文件。
GLAD現(xiàn)在應該提供給你了一個zip壓縮文件,包含兩個頭文件目錄,和一個glad.c文件。將兩個頭文件目錄(glad和KHR)復制到你的Include文件夾中(或者增加一個額外的項目指向這些目錄),并添加glad.c文件到你的工程中。
三.測試代碼
#include "stdafx.h" #include <glad/glad.h> #include <GLFW/glfw3.h>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);// render// ------glClearColor(0.2f, 0.3f, 0.3f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);// 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); }完整源代碼可以在這里找到
完整測試工程可以在這里下載
作者:simpleDis
鏈接:https://www.jianshu.com/p/ede02de0c92b
來源:簡書
著作權歸作者所有。商業(yè)轉載請聯(lián)系作者獲得授權,非商業(yè)轉載請注明出處。
總結
以上是生活随笔為你收集整理的WIN10 OpenGL GLFW+GLAD 开发环境搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenGL 开发环境配置(Window
- 下一篇: VS2015下OpenGL库的配置