mac 下 不用Xcode, 用gcc自己编译OpenGL工程

最近想在mac做OpenGL,但我又不想用xcode,xcode开销太大了,一点都不爽快,于是我尝试着用vim开发,gcc编译。经过2天的配置,终于配置出来了。

大概步骤如下:

1)安装代码编辑、编译工具,编辑器我用的是vim,这个系统默认安装好了。编译器我用的是gcc,这个好像也是自带的,还是装xcode的时候装的,不清楚,因为我装这些之前已经装好xcode了,可以敲命令:gcc -version查看。没有的话,可以先装个homebrew工具(参考homebrew安装)真的好用,谁用谁知道,然后brew install gcc49安装gcc49,vim也可以用homebrew安装。

2)安装OpenGL,不过听说mac系统已经安装好了,我不知道是系统自带还是xcode带进来的,我查了一下,确实有,include默认路径里包括glut库,在Library中还可以找的到glut的framework。可以直接调用。但是,glut库已经不更新了,直接用也可以通过,但是编译器会弹出一大堆的警告,说glut已经废弃之类的话。为了fashion一点,我决定再装freeglut或glfw,网上查了一下,说freeglut还不稳定,我也看了一下api,感觉freeglut还是封装的过了点,渲染的while循环都封装起来了,而glfw还保留了opengl原始的渲染逻辑,看的清楚一点,所以我选择了glfw,同样的道理,命令行:brew install glfw3,搞定,只能说homebrew确实好用。。。

3)命令行vim firstOpengl.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();
      }
 
                                                                                                   
:qw保存,敲命令:gcc firstOpengl.c -o run -lglfw3 -framework OpenGL

再敲命令:./run运行

好了,一个运行在mac上,用vim编辑,gcc编译的opengl工程跑起来了。

多说一句:再按 command+Q,可退出窗口回到终端。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值