【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;
}
相关推荐
MSTcheng.几秒前
【C++】C++智能指针
开发语言·c++·智能指针
云深处@5 分钟前
【C++11】部分特性
开发语言·c++
movigo7_dou8 分钟前
工业相机镜头参数和选型
图像处理·数码相机
独望漫天星辰10 分钟前
C++ 树结构进阶:从工程化实现到 STL 底层与性能优化
开发语言·c++
HellowAmy14 分钟前
我的C++规范 - 鸡蛋工厂
开发语言·c++·代码规范
仰泳的熊猫18 分钟前
题目1453:蓝桥杯历届试题-翻硬币
数据结构·c++·算法·蓝桥杯
rainbow688918 分钟前
C++STL list容器模拟实现详解
开发语言·c++·list
会叫的恐龙21 分钟前
C++ 核心知识点汇总(第11日)(排序算法)
c++·算法·排序算法
符哥200832 分钟前
C++ 适合初学者的学习笔记整理
c++·笔记·学习
星火开发设计43 分钟前
this 指针:指向对象自身的隐含指针
开发语言·数据结构·c++·学习·指针·知识