OpenGL 开发环境配置(Windows) - Visual Studio 2017 + GLFW + GLAD 详细图文教程
OpenGL 開發環境配置(Windows) - Visual Studio 2017 + GLFW + GLAD 詳細圖文教程
大部分 OpenGL 是直接面向設備商的,如果開發者需要使用 OpenGL 進行開發,一般需要使用已有的庫,本文使用的是GLFW,它提供了一些渲染物體所需的最低限度的接口。
同時,我們還需要 GLAD,因為 OpenGL 只是一個標準/規范,具體的實現是由驅動開發商針對特定顯卡實現的,對于開發者而言,GLAD 也可以讓開發更為方便。
準備條件
??? 操作系統:Windows 10
??? 編譯器:Visual Studio 2017(VC++ 2017)
??? CMake 工具
??? GLFW庫
??? GLAD庫
??? Visual Studio和CMake的安裝略去
??? 相關鏈接:
??? Visual Studio官網
??? CMake官網下載
配置步驟
Step1. 下載并編譯GLFW庫
首先訪問GLFW官網下載頁,下載源代碼并解壓,如下圖所示
glfw源碼
接下來打開CMake程序,設置source code為GLFW解壓目錄,build目錄為GLFW解壓目錄下新建的build文件夾:
cmake1
點擊configure,默認即可:
cmake2
再次點擊configure按鈕:
cmake3
最后點擊Generate:
cmake4
可以在build目錄下生成Visual Studio的解決方案:
build目錄
打開解決方案,直接編譯即可:
編譯
編譯成功
在build\src\Debug\目錄下得到編譯后的庫文件:
庫文件
Step2. 下載GLAD庫
轉到GLAD在線服務頁面,修改語言為C/C++,選擇OpenGL,API選擇使用的對應OpenGL版本,Profile選擇Core,勾上Generate a loader,點擊GENERATE:
glad1
glad2
下載壓縮包:
glad3
如何查看OpenGL版本
下載使用OpenGL Extension Viewer,即可查看OpenGL版本:
OpenGL版本
??? 相關鏈接:
??? OpenGL Extension Viewer在Softonic的下載頁
Step3. 配置Visual Studio工程
將GLFW源碼中的include\文件下的內容、GLFW編譯后的庫、下載的GLAD庫,放入opengl文件夾,組織如下:
組織方式
新建Visual C++空項目,將opengl文件夾放入工程目錄:
放入
配置VS工程如下:
配置1
配置2
配置3
添加opengl\src\glad.c源文件:
源文件
Step4. 編寫代碼并測試
代碼如下:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main() {
??? glfwInit();
??? glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
??? glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
??? glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
??? GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
??? if (window == NULL) {
??????? cout << "Failed to create GLFW window" << endl;
??????? glfwTerminate();
??????? return -1;
??? }
??? glfwMakeContextCurrent(window);
??? if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
??????? std::cout << "Failed to initialize GLAD" << std::endl;
??????? return -1;
??? }
??? glViewport(0, 0, 800, 600);
??? glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
??? while (!glfwWindowShouldClose(window)) {
??????? glfwSwapBuffers(window);
??????? glfwPollEvents();
??? }
??? glfwTerminate();
??? return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
??? glViewport(0, 0, width, height);
}
??? 1
??? 2
??? 3
??? 4
??? 5
??? 6
??? 7
??? 8
??? 9
??? 10
??? 11
??? 12
??? 13
??? 14
??? 15
??? 16
??? 17
??? 18
??? 19
??? 20
??? 21
??? 22
??? 23
??? 24
??? 25
??? 26
??? 27
??? 28
??? 29
??? 30
??? 31
??? 32
??? 33
??? 34
??? 35
??? 36
??? 37
??? 38
??? 39
??? 40
??? 41
??? 42
編譯運行,結果如下:
結果
至此,配置完成。
參考鏈接
Learn OpenGL CN - 入門 - 創建窗口
————————————————
版權聲明:本文為CSDN博主「sigmarising」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/sigmarising/article/details/80470054
總結
以上是生活随笔為你收集整理的OpenGL 开发环境配置(Windows) - Visual Studio 2017 + GLFW + GLAD 详细图文教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenGL 关于旧版glut和新版本g
- 下一篇: WIN10 OpenGL GLFW+GL