OpenGL学习笔记 (1) —— 在VS2008下的两个简单例子

2011-04-04 wcdj

 

(1) 使用系统默认自带的OpenGL库文件

 

首先在VS2008下新建一个Win32的GUI空白程序,然后,在工程属性->配置属性->链接器->输入->附加依赖项 中添加OpenGL所需要的lib文件,之间用空格隔开:OpenGL32.lib GLu32.lib GLaux.lib (略)

 

 

(2) 下载使用GLUT (The OpenGL Utility Toolkit) 开发包

下载地址:
GLUT and OpenGL Utility Libraries
http://www.opengl.org/resources/libraries/
Windows环境下的GLUT下载地址:(大小约为150k)
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

About GLUT

GLUT - The OpenGL Utility Toolkit

GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL. GLUT makes it considerably easier to learn about and explore OpenGL programming. GLUT provides a portable API so you can write a single OpenGL program that works across all PC and workstation OS platforms.

GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT is well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits. GLUT is simple, easy, and small.

The GLUT library has both C, C++ (same as C), FORTRAN, and Ada programming bindings. The GLUT source code distribution is portable to nearly all OpenGL implementations and platforms. The current version is 3.7. Additional releases of the library are not anticipated.

GLUT is not open source. Mark Kilgard maintains the copyright. There are a number of newer and open source alternatives.

The current version of the GLUT API is 3.
The current source code distribution is GLUT 3.7.

The toolkit supports:

    * Multiple windows for OpenGL rendering
    * Callback driven event processing
    * Sophisticated input devices
    * An 'idle' routine and timers
    * A simple, cascading pop-up menu facility
    * Utility routines to generate various solid and wire frame objects
    * Support for bitmap and stroke fonts
    * Miscellaneous window management functions


安装 glut 后的一个例子:

 

 

下面对各行语句进行说明:
(1)
首先,需要包含头文件 #include <GL/glut.h>,这是GLUT的头文件。本来OpenGL程序一般还要包含<GL/gl.h>和<GL/glu.h>,但GLUT的头文件中已经自动将这两个文件包含了,不必再次包含。
(2)
然后,看main函数,int main(int argc, char *argv[]),这个是带命令行参数的main函数。
注意main函数中的各语句,除了最后的return之外,其余全部以glut开头。这种以glut开头的函数都是GLUT工具包所提供的函数,下面对用到的几个函数进行介绍。
1、glutInit,对GLUT进行初始化,这个函数必须在其它的GLUT使用之前调用一次。其格式比较死板,一般照抄这句glutInit(&argc, argv)就可以了。
2、 glutInitDisplayMode,设置显示方式,其中GLUT_RGB表示使用RGB颜色,与之对应的还有GLUT_INDEX(表示使用索引颜色)。GLUT_SINGLE表示使用单缓冲,与之对应的还有GLUT_DOUBLE(使用双缓冲)。更多信息,请自己Google。当然以后的教程也会有一些讲解。
3、glutInitWindowPosition,这个简单,设置窗口在屏幕中的位置。
4、glutInitWindowSize,这个也简单,设置窗口的大小。
5、glutCreateWindow,根据前面设置的信息创建窗口。参数将被作为窗口的标题。注意:窗口被创建后,并不立即显示到屏幕上。需要调用glutMainLoop才能看到窗口。
6、glutDisplayFunc,设置一个函数,当需要进行画图时,这个函数就会被调用。(这个说法不够准确,但准确的说法可能初学者不太好理解,暂时这样说吧)。
7、glutMainLoop,进行一个消息循环。(这个可能初学者也不太明白,现在只需要知道这个函数可以显示窗口,并且等待窗口关闭后才会返回,这就足够了。)

在glutDisplayFunc函数中,我们设置了“当需要画图时,请调用myDisplay函数”。于是myDisplay函数就用来画图。观察myDisplay中的三个函数调用,发现它们都以gl开头。这种以gl开头的函数都是OpenGL的标准函数,下面对用到的函数进行介绍。
1、glClear,清除。GL_COLOR_BUFFER_BIT表示清除颜色,glClear函数还可以清除其它的东西,但这里不作介绍。
2、glRectf,画一个矩形。四个参数分别表示了位于对角线上的两个点的横、纵坐标。
3、glFlush,保证前面的OpenGL命令立即执行(而不是让它们在缓冲区中等待)。其作用跟fflush(stdout)类似


参考
OpenGL官网
http://www.opengl.org/

Nehe的教程
(1) EN版:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01
http://users.polytech.unice.fr/~buffa/cours/synthese_image/DOCS/Tutoriaux/Nehe/lesson1.htm
(2) CN版:
OpenGL教程之新手上路
http://blog.youkuaiyun.com/lovetangtang/archive/2006/01/16/580530.aspx

VS2008 OpenGL 配置
http://lujun5918.blog.163.com/blog/static/287227712011013114410553/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值