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,默认即可:

扫描二维码关注公众号,回复: 8647048 查看本文章

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

发布了134 篇原创文章 · 获赞 116 · 访问量 65万+

猜你喜欢

转载自blog.csdn.net/shixin_0125/article/details/103972463