【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;
}
相关推荐
Larry_Yanan1 小时前
QML学习笔记(四十)QML的ApplicationWindow和StackView
c++·笔记·qt·学习·ui
Kratzdisteln3 小时前
【C语言】Dev-C++如何编译C语言程序?从安装到运行一步到位
c语言·c++
Dream it possible!4 小时前
LeetCode 面试经典 150_栈_有效的括号(52_20_C++_简单)(栈+哈希表)
c++·leetcode·面试··哈希表
kyle~5 小时前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++
HY小海5 小时前
【C++】AVL树实现
开发语言·数据结构·c++
仰泳的熊猫5 小时前
LeetCode:701. 二叉搜索树中的插入操作
数据结构·c++·算法·leetcode
郝学胜-神的一滴7 小时前
Linux系统函数stat和lstat详解
linux·运维·服务器·开发语言·c++·程序人生·软件工程
owCode8 小时前
3-C++中类大小影响因素
开发语言·c++
程序猿Eason9 小时前
U587038 背包 题解
c++·算法·动态规划
爱吃芒果的蘑菇9 小时前
C++之WebSocket初体验
网络·c++·websocket·网络协议