---
title: "opengl超级宝典代码学习"
author: "sogotobj"
date: "2011-11-01T18:14:00Z"
tags: []
slug: "opengl"
draft: false
lastmod: "2011-11-09T20:00:34Z"
id: 1247787
url: "https://sogotobj.iteye.com/blog/1247787"
---

说明如下：

这个书就是著名的opengl蓝宝书，中文第四版，英文已经出到第五版，但是第五版的中文翻译版还没有。

运行环境是Mac系统的10.7，就是狮子版本，Xcode是4.2。

本书的支持网站：http://www.starstonesoftware.com/OpenGL/fourthEdition.htm

在上面的网址下载为Mac准备的源代码。

解开。在目录SB-MacwBin\\examples\\projects\\apple\\下面可以看到各个章节的源代码，我们第一个分析的代码是在第二章下面的simple下面。第一章的例子对于刚学的人太深了，不建议先看。

打开simple下面的工程，首先做更改如下：

![](http://hi.csdn.net/attachment/201111/1/0_1320140882Qeej.gif)

不然不能编译

然后我们需要分析的代码在simple.cpp中，

代码如下：

///////////////////////////////////////////////////////////

// Called to draw scene

void RenderScene(void)

{

// Clear the window with current clearing color

glClear(GL\_COLOR\_BUFFER\_BIT);

// Flush drawing commands

glFlush();

}

///////////////////////////////////////////////////////////

// Setup the rendering state

void SetupRC(void)

{

glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

}

///////////////////////////////////////////////////////////

// Main program entry point

int main(int argc, char* argv\[])

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT\_SINGLE | GLUT\_RGBA);

glutCreateWindow("Simple");

glutDisplayFunc(RenderScene);

SetupRC();

glutMainLoop();

return 0;

}

首先要做的是调用函数glutInit()。

VoidglutInit(int\*argc,char\*\*argv)；

参数：

Argc：一个指针，指向从main（）函数传递过来的没更改的argc变量。

Argv：一个指针，指向从main（）函数传递过来的没更改的argv变量。

glut开头的函数是输入和窗口函数，并不是OpenGL核心库一部分，由于OpenGL是跨平台的API，只是图像硬件的软件接口，所以任何系统相关的东西，比如窗口，菜单等等都不是OpenGL的核心部分，它们都被放在OpenGL实用工具集（OpenGl Utility ToolKit，GLUT）中。

glutInit应该在所有glut和OpenGL函数之前调用，进行初始化的工作。

glutInitDisplayMode函数功能：设置初始显示模式。

　　函数原型：void glutInitDisplayMode(unsigned int mode);

这里对这个不过多分析。

glutCreateWindow创建一个缺省大小的窗口300\*300；

glutDisplayFunc是用来注册回调函数的。

glClear(GL\_COLOR\_BUFFER\_BIT);用来设置一种颜色，用来刷新窗口。

glFlush函数用来执行所有已经设定的opengl命令。

glClearColor用来刷新的颜色。

glutMainLoop使程序一直运行，知道进程退出。