【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;
}
相关推荐
天赐学c语言16 分钟前
12.18 - 有效的括号 && C语言中static的作用
数据结构·c++·算法·leecode
Dream it possible!40 分钟前
LeetCode 面试经典 150_回溯_组合(99_77_C++_中等)
c++·leetcode·面试·回溯
再睡一夏就好1 小时前
深入解析Linux页表:从虚拟地址到物理内存的映射艺术
linux·运维·服务器·c语言·c++·页表·缺页异常
Starry_hello world1 小时前
C++ 线程 (3)
c++
雍凉明月夜2 小时前
c++ 精学笔记记录Ⅲ
c++·笔记·学习
oioihoii2 小时前
C++共享内存小白入门指南
java·c++·算法
这张生成的图像能检测吗2 小时前
(论文速读)LCT:用于RGB-D突出物体检测的轻型跨模态变压器
图像处理·目标检测·计算机视觉·深度估计·轻量化模型·跨模态融合·rgb-d
布茹 ei ai2 小时前
QtWeatherApp - 简单天气预报软件(C++ Qt6)(附源码)
开发语言·c++·qt·开源·开源项目·天气预报
Bruce_kaizy2 小时前
c++图论————图的基本与遍历
c++·算法·图论
Zmm147258369_2 小时前
好用的PC耐力板机构
c++