在xcode上搭建OpenGL3.x运行环境

最近开始学习OpenGL,网上的教程太散乱,于是打算照着红宝书《OpenGL编程指南(第七版)》来学习。

于是在Mac上搭建一下Demo环境。比较方便的是,OS X上已经装了OpenGL 3.x所以非常简单。

首先,在xcode上建立os x的command line工程,即hello world。

然后,把以下两个库通过add files添加到工程里:

/System/Library/Frameworks/OpenGL.framework

/System/Library/Frameworks/GLUT.framework

前者就是OpenGL 3.x;后者是OpenGL的界面和交互库,因为OpenGL只负责计算,并没有显示功能。

注意,GLUT库已经过时,os x在10.9版本已经弃用,改成什么我还不知道。

接下来,在main.cpp里include以下头文件:

#include <OpenGL/gl.h>

#include <OpenGL/glu.h>

#include <GLUT/glut.h>

最后,把下面的代码贴到main.cpp里编译运行即可

 

void display(void)
{
    /* clear all pixels */
    glClear (GL_COLOR_BUFFER_BIT);
    
    /* draw white polygon (rectangle) with corners at
     * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
     */
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
    
    glVertex3f (0.25, 0.25, 0.0);
    glVertex3f (0.75, 0.25, 0.0);
    glVertex3f (0.75, 0.75, 0.0);
    glVertex3f (0.25, 0.75, 0.0);
    
    glEnd();
    
    /* don't wait!
     * start processing buffered OpenGL routines
     */
    glFlush ();
}

void init (void)
{
    /* select clearing color */
    glClearColor (0.0, 0.0, 0.0, 0.0);
    
    /* initialize viewing values */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

/*
 * Declare initial window size, position, and display mode
 * (single buffer and RGBA). Open window with "hello"
 * in its title bar. Call initialization routines.
 * Register callback function to display graphics.
 * Enter main loop and process events.
 */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0; /* ANSI C requires main to return int. */
}

运行后,命令行窗口会显示一个白色的方块如图:

OK,开始深入学习吧!

posted @ 2014-02-17 20:51  GAMTEQ  阅读(539)  评论(0编辑  收藏  举报