OpenGL学习 环境搭建Clion+glfw+glad+mingw

前言

opengl学习中 glfw+glad 是目前比较多的组合
如果你需要 glew+freeglut 也可以看看我另一篇openGL环境搭建
本质上异曲同工
Clion+glew+freeglut+mingw搭建

1. 下载库

glad

可以自己下载库也可以使用在线配置
这里是在线配置
glad在线配置
选择以下配置选项
要保证 opengl的Api版本在3.3以上才会兼容
模式选择core
拓展可以不选
在这里插入图片描述

然后generate 会得到一个zip文件下载后解压得到include 和src
在这里插入图片描述

glfw下载

glfw
可以自己Cmake编译
也可以下载预编译版本
我们下载预编译版本
在这里插入图片描述

2. 配置

1.可以把glad的include文件夹下的内容复制到glfw的include文件夹中

这样可以一起include

2.也可以重新衔接一个include文件夹

在项目中添加glad.c 文件 路径随意


cmake_minimum_required(VERSION 3.15)
project(C__test)

set(CMAKE_CXX_STANDARD 14)
link_directories("D:/computerGraph/glfw/lib-mingw-w64")
#1. glad的include和glfw的include文件夹合并了
include_directories("D:/computerGraph/glfw/include")
#2.glad的include文件夹另外设置 inlcude_directories("D:/computerGraph/glad/include")
# 添加 glad.c为可执行
add_executable(C__test main.cpp src/glad.c)
# 衔接库
target_link_libraries(C__test libglfw3.a)

3. 测试

learnopenGL源码地址

# 源码来源https://learnopengl.com/ 
#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);

    // 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);
}

4.测试图

参考资料 LearnOpenGL.com

发布了22 篇原创文章 · 获赞 2 · 访问量 881

猜你喜欢

转载自blog.csdn.net/weixin_41685373/article/details/104576808