【OpenGL】xcode+glfw画三角形

环境搭建

  1. 执行brew install glfw

  2. 项目中Build Settings中header Search Paths中添加glfw的include路径

  3. 项目中Build Phases中的Link Binary With Libraries中添加glfw的lib文件(路径/opt/homebrew/Cellar/glfw/3.4/lib/libglfw.3.4.dylib)及opengl.framework

代码实现

cpp 复制代码
#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, 0.5f);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();
        
        /* Swap front and back buffers */
        glfwSwapBuffers(window);

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

    glfwTerminate();
    return 0;
}
相关推荐
楼田莉子30 分钟前
C++算法专题学习——分治
数据结构·c++·学习·算法·leetcode·排序算法
ulias2121 小时前
各种背包问题简述
数据结构·c++·算法·动态规划
程序喵大人1 小时前
分享个C++线程池的实现源码
开发语言·c++·线程池
FL16238631292 小时前
[ubuntu][C++]onnxruntime安装cpu版本后测试代码
linux·c++·ubuntu
要做朋鱼燕2 小时前
【C++】 priority_queue 容器模拟实现解析
开发语言·c++·笔记·职场和发展
励志不掉头发的内向程序员2 小时前
C++进阶——继承 (1)
开发语言·c++·学习
mit6.8244 小时前
并查集|栈
c++
中国胖子风清扬4 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
岁忧5 小时前
(LeetCode 面试经典 150 题) 200. 岛屿数量(深度优先搜索dfs || 广度优先搜索bfs)
java·c++·leetcode·面试·go·深度优先
一枝小雨5 小时前
【OJ】C++ vector类OJ题
数据结构·c++·算法·leetcode·oj题