Clion使用GLFW

GLFW

点击跳转 

 在GLFW下载页中,有两个下载项

  •  64-bit文件:当你准备制作64位的程序时,下载这个选项。需注意:只有64位操作系统才能运行
  •  32-bit文件:当你准备制作32位的程序时,下载这个选项。该选项可运行在 32、64、x86系统下

本教程为32-bit文件包

 

Clion

配置glfw文件

在clion中新建一个项目,默认会创建main.cpp和CMakeLists.txt

将glfw下载好的文件操作

  • 在clion项目中创建Dependencies文件夹
  • 在Dependencies中创建GLFW文件夹
  • 将glfw文件夹中的include文件夹复制进GLFW
  • 将glfw文件夹中的lib-mingw-w64复制到GLFW

 目录如图所示,我将main.cpp放在了src目录下并修改了文件名称,其实都一样

配置CMakeLists.txt

cmake_minimum_required(VERSION 3.28)
project(learn_opengl)

set(CMAKE_CXX_STANDARD 17)

// 引入glfw的include include_directories(Dependencies
/GLFW/include)
// 链接glfw的lib link_directories(Dependencies
/GLFW/lib) add_executable(learn_opengl src/Application.cpp)
// 链接glfw3 和 opengl32 target_link_libraries(learn_opengl glfw3 opengl32)

Application.cpp

此代码为https://www.glfw.org/documentation.html ,加入了三角形绘制

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

 

结果

 

posted @ 2024-12-17 17:01  Cyber_nnno  阅读(139)  评论(0)    收藏  举报