Mac使用Xcode配置openGL

Mac使用Xcode配置openGL

博主这学期有图形学课要用到OpenGL,于是首先就开始配置开发环境了。应该说网上Windows上配置OpenGL教程比较多,Mac版的比较少。博主特来分享配置过程。

介绍

OpenGL(Open Graphics Library)是指定义了一个跨编程语言、跨平台的编程接口规格的专业的图形程序接口。它用于三维图像(二维的亦可),是一个功能强大,与硬件无关,调用方便的底层图形库。

在编程的时候,一般会采用基于OpenGL封装的几个库,它们提供了OpenGL本身没有的功能。

很过教程都是基于GLUT的,但Xcode上会显示deprecate的warning,主要因为GLUT从1998年不再更新了,使用也有一定隐患。

现在使用的一般为GLEW,GLFW,FreeGLUT(兼容GLUT)。

本文介绍GLEW和GLFW(二者可以组合使用)的配置过程,要配置FreeGLUT也完全类似。

配置过程

安装homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装GLEW和GLFW

brew install glew
brew install glfw

brew安装的目录在/usr/local/Cellar下,后面会使用到路径。

新建一个Xcode Command Line C++项目

  • 修改Build Settings的Header Search Path和Library Search Path,分别添加两个库的头文件和库文件路径(请自行忽略里面的freeglut,因为博主也配好了,与步骤无关)

1146398-20170920091024103-117796881.png

1146398-20170920091035400-1712801637.png

  • 在Build Phases里面添加库文件

1146398-20170920091045946-925744915.png

  • 在使用时只要把头文件包含进来就可以了
#include <GL/glew.h>
#include <GLFW/glfw3.h>

测试

由于博主也刚开始学,于是只能从网上找了一段代码来测试环境。

#include <iostream>
#include <GL/glew.h>
#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 */
        
        /* Swap front and back buffers */
        
        glfwSwapBuffers(window);
        
        /* Poll for and process events */
        
        glfwPollEvents();
        
    }
    glfwTerminate();
    
    return 0;
}

1146398-20170920091120290-1317105774.png

显示了一个标题为hello world的黑框,0 warnings, 0 errors。

配置成功!!!

转载于:https://www.cnblogs.com/fanghao/p/7559768.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值