【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;
}
相关推荐
AI+程序员在路上14 分钟前
单元测试与QTestLib框架使用
开发语言·c++·单元测试
比特森林探险记26 分钟前
Go 中的 Map 与字符处理指南
c++·算法·golang
whoarethenext34 分钟前
使用 C/C++ 和 OpenCV 实现滑动条控制图像旋转
c语言·c++·opencv
whoarethenext42 分钟前
使用 OpenCV (C++) 进行人脸边缘提取
c++·人工智能·opencv
Code_流苏2 小时前
C++课设:智能优惠快餐点餐系统
开发语言·c++·课设·期末大作业·快餐点餐系统·智能优惠算法
越城2 小时前
深入解析C++引用:从别名机制到函数特性实践
c++
qwertyuiop_i3 小时前
pe文件结构(TLS)
c++·tls·pe文件结构
岁忧4 小时前
(nice!!!)(LeetCode每日一题)2434. 使用机器人打印字典序最小的字符串(贪心+栈)
java·c++·算法·leetcode·职场和发展·go
无敌的小笼包4 小时前
第四讲:类和对象(下)
数据结构·c++
鑫鑫向栄5 小时前
[蓝桥杯]解谜游戏
数据结构·c++·算法·职场和发展·蓝桥杯