【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;
}
相关推荐
byxdaz8 小时前
C++内存序
c++
优雅的潮叭8 小时前
c++ 学习笔记之 malloc
c++·笔记·学习
苦藤新鸡10 小时前
8.最长的无重复字符的子串
c++·力扣
꧁Q༒ོγ꧂11 小时前
C++ 入门完全指南(四)--函数与模块化编程
开发语言·c++
汉克老师11 小时前
GESP2025年12月认证C++八级真题与解析(判断题8-10)
c++·快速排序··lcs·gesp八级·gesp8级
qq_4335545412 小时前
C++ manacher(求解回文串问题)
开发语言·c++·算法
HL_风神13 小时前
设计原则之迪米特
c++·学习·设计模式
HL_风神13 小时前
设计原则之合成复用
c++·学习·设计模式
汉克老师13 小时前
GESP2025年12月认证C++八级真题与解析(单选题10-12)
c++·递归··gesp八级·gesp8级
bkspiderx14 小时前
C++中的map容器:键值对的有序管理与高效检索
开发语言·c++·stl·map