[OpenGL] 初识GLFW

来源:互联网 发布:yy网络怎么创建直播间 编辑:程序博客网 时间:2024/06/05 00:41

在上一篇博客里已经介绍过有关OpenGL和GLFW的内容了,现在开始安装并使用GLFW来运行第一个OpenGL的例子。

操作系统 MacOS 10.13.1
因为Xcode太大了,动不动就更新,所以我已经不用Xcode了,使用命令行工具gcc/g++来编译,下面的程序就是使用命令行进行链接库和编译的。

首先确定Mac里的命令行工具可用,使用命令 gcc --version

$ gcc --versionConfigured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1Apple LLVM version 9.0.0 (clang-900.0.38)Target: x86_64-apple-darwin17.2.0Thread model: posixInstalledDir: /Library/Developer/CommandLineTools/usr/bin

如果没有gcc,需要自行安装。

OpenGL的核心库系统里都自带了,感兴趣的可以去目录 /System/Library/Frameworks/OpenGL.framework/ 里看一下。

然后使用homebrew来安装 GLFW,如果没安装homebrew的需要先安装一下。

$ brew install glfw3

下载完就安装好了!

/usr/local/Cellar/ 目录下会多出来一个 glfw 的文件夹,相关的文件都在这个里面。

使用的时候引入头文件 glfw3.h ,然后链接库 glfw 就可以了。

运行OpenGL第一个例子

在网上复制一段代码,保存成文件 test.c

#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(480, 320, "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))      {          /* Draw a triangle */         glBegin(GL_TRIANGLES);         glColor3f(1.0, 0.0, 0.0);    // Red          glVertex3f(0.0, 1.0, 0.0);          glColor3f(0.0, 1.0, 0.0);    // Green          glVertex3f(-1.0, -1.0, 0.0);          glColor3f(0.0, 0.0, 1.0);    // Blue          glVertex3f(1.0, -1.0, 0.0);          glEnd();          /* Swap front and back buffers */          glfwSwapBuffers(window);          /* Poll for and process events */          glfwPollEvents();      } }

编译成可执行文件:

$ gcc test.c -o run -lglfw -framework OpenGL

运行:

./run

这里写图片描述

Bingo~

原创粉丝点击