【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;
}
相关推荐
MoRanzhi12031 小时前
12. NumPy 数据分析与图像处理入门
大数据·图像处理·人工智能·python·矩阵·数据分析·numpy
什么半岛铁盒1 小时前
C++项目:仿muduo库高并发服务器------EventLoop模块的设计
linux·服务器·c++·mysql·ubuntu
mmz12071 小时前
动态规划 练习(c++)
c++·算法·动态规划
Roc-xb2 小时前
解决Compile Run插件运行c/c++中文乱码问题
c语言·开发语言·c++
凤年徐2 小时前
【C++模板编程】从泛型思想到实战应用
java·c语言·开发语言·c++
深思慎考2 小时前
LinuxC++项目开发日志——基于正倒排索引的boost搜索引擎(5——通过cpp-httplib库建立网页模块)
linux·c++·搜索引擎
freedom_1024_3 小时前
【C++】深入理解const 成员函数
c++
dream_home84074 小时前
构建生产级多模态数据集:视觉与视频模型(参照LLaVA-OneVision-Data和VideoChat2)
图像处理·人工智能·计算机视觉
钱彬 (Qian Bin)5 小时前
企业级实战:构建基于Qt、C++与YOLOv8的模块化工业视觉检测系统(基于QML)
c++·qt·yolo·qml·工业质检·qt 5.15.2
会开花的二叉树5 小时前
上手 cpp-httplib:轻量级 C++ HTTP 库的安装与实战指南
开发语言·c++·http