📄
字号:
id="_x0000_i1032" type="#_x0000_t75" alt="" href="CG_Txt_5_009.htm" style='width:21pt;
height:37.5pt' o:button="t">
<v:imagedata src="./第五章%20图形标准.files/image002.gif" o:href="http://learn.bitsde.com/hep/jisuanjituxing/material/CG_Gif_pub_021.gif"/>
</v:shape><![endif]--><![if !vml]><img border=0 width=28 height=50
src="./第五章%20图形标准.files/image002.gif" v:shapes="_x0000_i1032"><![endif]></span></a></span></b><b><span
lang=EN-US style='font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_009.htm"><span
style='color:gray'>OpenGL 的工作顺序</span></a><o:p></o:p></span></b></p>
<p style='line-height:200%'><span lang=EN-US>OpenGL的工作顺序就是一个从定义几何要素到把象素段写入帧缓冲区的过程。在屏幕上显示图象的主要步骤是以下3步:</span></p>
<p style='margin-right:36.0pt;margin-left:36.0pt;line-height:200%'><span
lang=EN-US>(1). 构造几何要素(点、线、多边形、图象、位图),创建对象的数学描述。在三维空间上放置对象,选择有利的场景观察点。</span></p>
<p style='margin-right:36.0pt;margin-left:36.0pt;line-height:200%'><span
lang=EN-US>(2). 计算对象的颜色,这些颜色可能直接定义,或由光照条件及纹理间接给出。</span></p>
<p style='margin-right:36.0pt;margin-left:36.0pt'><span lang=EN-US>(3). 光栅化,把对象的数学描述和颜色信息转换到屏幕的象素。</span></p>
<p align=center style='text-align:center'><b><span lang=EN-US style='font-size:
18.0pt;color:green'>5.1.2</span><span lang=EN-US style='color:green'> </span></b><b><span
lang=EN-US style='font-size:18.0pt;font-family:仿宋_GB2312;color:green'>OpenGL程序结构</span></b></p>
<p><span lang=EN-US style='font-size:18.0pt;mso-ascii-font-family:仿宋_GB2312;
mso-fareast-font-family:仿宋_GB2312'> </span><span lang=EN-US
style='font-size:18.0pt;font-family:仿宋_GB2312'> </span><span style='font-family:
楷体_GB2312'>这一节里,我们介绍一下<span lang=EN-US>OPenGL的基本程序结构。</span></span></p>
<p><b><span lang=EN-US style='font-size:10.0pt;font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_011.htm"><span
style='text-decoration:none;text-underline:none'><!--[if gte vml 1]><v:shape
id="_x0000_i1033" type="#_x0000_t75" alt="" href="CG_Txt_5_011.htm" style='width:21pt;
height:37.5pt' o:button="t">
<v:imagedata src="./第五章%20图形标准.files/image002.gif" o:href="http://learn.bitsde.com/hep/jisuanjituxing/material/CG_Gif_pub_021.gif"/>
</v:shape><![endif]--><![if !vml]><img border=0 width=28 height=50
src="./第五章%20图形标准.files/image002.gif" v:shapes="_x0000_i1033"><![endif]></span></a></span></b><b><span
lang=EN-US style='font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_011.htm"><span
style='color:gray'>基本语法</span></a><o:p></o:p></span></b></p>
<p style='line-height:200%'>常用的程序设计语言,如<span lang=EN-US>C、C++、Pascal、Fortran和Java等,都支持OpenGL的开发。这里只讨论C版本下OpenGL的语法。</span></p>
<p style='line-height:200%'><span lang=EN-US> OpenGL基本函数均使用gl作为函数名的前缀,如glClearColor();实用函数则使用glu作为函数名的前缀,如gluSphere()。OpenGL基本常量的名字以GL_开头,如GL_LINE_LOOP;实用常量的名字以GLU_开头,如GLU_FILL。一些函数如glColor*()(定义颜色值),函数名后可以接不同的后缀以支持不同的数据类型和格式。如glColor3b(...)、glColor3d(...)、glColor3f(...)和glColor3bv(...)等,这几个函数在功能上是相似的,只是适用于不同的数据类型和格式,其中3表示该函数带有三个参数,b、d、f分别表示参数的类型是字节型、双精度浮点型和单精度浮点型,v则表示这些参数是以向量形式出现的。</span></p>
<p style='line-height:200%'><span lang=EN-US> OpenGL定义了一些特殊标识符,如GLfloat,GLvoid。它们其实就是C中的float和void。在gl.h文件中可以看到以下定义:</span></p>
<p style='margin-right:108.0pt;margin-left:108.0pt'><span lang=EN-US>……</span></p>
<p style='margin-right:108.0pt;margin-left:108.0pt'><span lang=EN-US>typedef
float GLfloat;</span></p>
<p style='margin-right:108.0pt;margin-left:108.0pt'><span lang=EN-US>typedef
void GLvoid;</span></p>
<p style='margin-right:108.0pt;margin-left:108.0pt'><span lang=EN-US>……</span></p>
<p><span lang=EN-US> 一些基本的数据类型都有类似的定义项。</span></p>
<p><span lang=EN-US><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p><b><span lang=EN-US style='font-size:10.0pt;font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_012.htm"><span
style='text-decoration:none;text-underline:none'><!--[if gte vml 1]><v:shape
id="_x0000_i1034" type="#_x0000_t75" alt="" href="CG_Txt_5_012.htm" style='width:21pt;
height:37.5pt' o:button="t">
<v:imagedata src="./第五章%20图形标准.files/image002.gif" o:href="http://learn.bitsde.com/hep/jisuanjituxing/material/CG_Gif_pub_021.gif"/>
</v:shape><![endif]--><![if !vml]><img border=0 width=28 height=50
src="./第五章%20图形标准.files/image002.gif" v:shapes="_x0000_i1034"><![endif]></span></a></span></b><b><span
lang=EN-US style='font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_012.htm"><span
style='color:gray'>状态机制</span></a><o:p></o:p></span></b></p>
<p style='line-height:200%'><span lang=EN-US>OpenGL的工作方式是一种状态机制,它可以进行各种状态或模式设置,这些状态或模式在重新改变它们之前一直有效。例如,当前颜色就是一个状态变量,在这个状态改变之前,绘制的每个象素都将使用该颜色,直到当前颜色被设置为其它颜色为止。OpenGL中大量地使用了这种状态机制,如颜色模式、投影模式、单双显示缓存区的设置、背景色的设置、光源的位置和特性等等。许多状态变量可以通过glEnable()、glDisable()这两个函数来设置成有效或无效状态,如是否设置光照、是否进行深度检测等;在被设置成有效状态之后,绝大部分状态变量都有一个缺省值。通常情况下,可以用下列四个函数来获取某个状态变量的值:glGetBooleanv()、glGetDouble()、glGetFloatv()和glGetIntegerv()。究竟选择哪个函数应该根据所要获得的返回值的数据类型来决定。还有些状态变量有特殊的查询函数,如glGetLight*()、glGetError()和glPolygonStipple()等。另外,使用glPushAttrib()和glPopAttrib()函数,可以存储和恢复最近的状态变量的值。只要有可能,都应该使用这些函数,因为它们比其它查询函数的效率更高。</span></p>
<p align=center style='text-align:center'><span lang=EN-US><!--[if gte vml 1]><v:shape
id="_x0000_i1035" type="#_x0000_t75" alt="" style='width:369pt;height:147pt'>
<v:imagedata src="./第五章%20图形标准.files/image004.gif" o:href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Gif_5_211.gif"/>
</v:shape><![endif]--><![if !vml]><img border=0 width=492 height=196
src="./第五章%20图形标准.files/image004.gif" v:shapes="_x0000_i1035"><![endif]></span></p>
<p><span lang=EN-US> 部分利用glEnable()和glDisable()的函数和设定值。</span></p>
<p><span lang=EN-US><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p><b><span lang=EN-US style='font-size:10.0pt;font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_013.htm"><span
style='text-decoration:none;text-underline:none'><!--[if gte vml 1]><v:shape
id="_x0000_i1036" type="#_x0000_t75" alt="" href="CG_Txt_5_013.htm" style='width:21pt;
height:37.5pt' o:button="t">
<v:imagedata src="./第五章%20图形标准.files/image002.gif" o:href="http://learn.bitsde.com/hep/jisuanjituxing/material/CG_Gif_pub_021.gif"/>
</v:shape><![endif]--><![if !vml]><img border=0 width=28 height=50
src="./第五章%20图形标准.files/image002.gif" v:shapes="_x0000_i1036"><![endif]></span></a></span></b><b><span
lang=EN-US style='font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_013.htm"><span
style='color:gray'>程序的基本结构</span></a><o:p></o:p></span></b></p>
<p><span lang=EN-US>OpenGL程序的基本结构可分为三个部分:</span></p>
<p style='line-height:200%'><span lang=EN-US> 第一部分是<span
style='color:maroon'>初始化部分</span>。主要是设置一些OpenGL的状态开关,如颜色模式(RGBA或ALPHA)的选择,是否作光照处理(若有的话,还需设置光源的特性),深度检验,裁剪等等。这些状态一般都用函数glEnable(???),
glDisable(???)来设置,???表示特定的状态。</span></p>
<p style='line-height:200%'><span lang=EN-US> 第二部分<span
style='color:maroon'>设置观察坐标系下的取景模式和取景框位置大小</span>。主要利用了三个函数:</span></p>
<p style='line-height:200%'><span lang=EN-US> 函数void
glViewport(left,top,right,bottom):设置在屏幕上的窗口大小,四个参数描述屏幕窗口四个角上的坐标(以象素表示);</span></p>
<p style='line-height:200%'><span lang=EN-US> 函数void
glOrtho(left,right,bottom,top,near,far):设置投影方式为正交投影(平行投影),其取景体积是一个各面均为矩形的六面体;</span></p>
<p style='line-height:200%'><span lang=EN-US> 函数void
gluPerspective(fovy,aspect,zNear,zFar):设置投影方式为透视投影,其取景体积是一个截头锥体。</span></p>
<p style='line-height:200%'><span lang=EN-US> 第三部分是<span
style='color:maroon'>OpenGL的主要部分</span>,使用OpenGL的库函数构造几何物体对象的数学描述,包括点线面的位置和拓扑关系、几何变换、光照处理等等。</span></p>
<p style='line-height:200%'><span lang=EN-US> 以上三个部分是OpenGL程序的基本框架,即使移植到使用MFC的Windows程序中,也是如此。只是由于Windows自身有一套显示方式,需要进行一些必要的改动以协调这两种不同显示方式。</span></p>
<p><span lang=EN-US><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p><b><span lang=EN-US style='font-size:10.0pt;font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_014.htm"><span
style='text-decoration:none;text-underline:none'><!--[if gte vml 1]><v:shape
id="_x0000_i1037" type="#_x0000_t75" alt="" href="CG_Txt_5_014.htm" style='width:21pt;
height:37.5pt' o:button="t">
<v:imagedata src="./第五章%20图形标准.files/image002.gif" o:href="http://learn.bitsde.com/hep/jisuanjituxing/material/CG_Gif_pub_021.gif"/>
</v:shape><![endif]--><![if !vml]><img border=0 width=28 height=50
src="./第五章%20图形标准.files/image002.gif" v:shapes="_x0000_i1037"><![endif]></span></a></span></b><b><span
lang=EN-US style='font-family:幼圆;color:gray'><a
href="http://learn.bitsde.com/hep/jisuanjituxing/Chapter5/CG_Txt_5_014.htm"><span
style='color:gray'>一个简单的程序</span></a><o:p></o:p></span></b></p>
<p>请大家首先阅读下面这段程序</p>
<p><span lang=EN-US style='font-size:10.0pt;color:navy'><!--[if gte vml 1]><v:shape
id="_x0000_i1038" type="#_x0000_t75" alt="" style='width:10.5pt;height:10.5pt'>
<v:imagedata src="./第五章%20图形标准.files/image005.gif" o:href="http://learn.bitsde.com/hep/jisuanjituxing/material/CG_Gif_pub_031.gif"/>
</v:shape><![endif]--><![if !vml]><img border=0 width=14 height=14
src="./第五章%20图形标准.files/image005.gif" v:shapes="_x0000_i1038"><![endif]></span><span
style='color:navy'>程序<span lang=EN-US>5.1.1</span></span></p>
<p style='margin-top:6.0pt;margin-right:0cm;margin-bottom:6.0pt;margin-left:
0cm;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>#include
<GL/gl.h></span></p>
<p style='margin-top:6.0pt;margin-right:0cm;margin-bottom:6.0pt;margin-left:
0cm;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>#include
<GL/glaux.h></span></p>
<p style='margin-top:6.0pt;margin-right:0cm;margin-bottom:6.0pt;margin-left:
0cm;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>int
main(int argc, char** argv)</span></p>
<p style='margin-top:6.0pt;margin-right:0cm;margin-bottom:6.0pt;margin-left:
0cm;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>{</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>auxInitDisplayMode
(AUX_SINGLE | AUX_RGBA);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>auxInitPosition
(0, 0, 500, 500);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>auxInitWindow
(argv[0]);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glClearColor
(0.0, 0.0, 0.0, 0.0); //置背景色为黑色</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glClear(GL_COLOR_BUFFER_BIT);
//清除颜色缓冲区</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glColor3f(1.0,
1.0, 1.0); //设置象素颜色为白色</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glMatrixMode(GL_PROJECTION);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glLoadIdentity();</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glOrtho(-1.0,
1.0, -1.0, 1.0, -1.0, 1.0);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glBegin(GL_POLYGON);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glVertex2f(-0.5,
-0.5);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glVertex2f(-0.5,
0.5);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glVertex2f(0.5,
0.5);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glVertex2f(0.5,
-0.5);</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glEnd();</span></p>
<p style='margin-top:6.0pt;margin-right:36.0pt;margin-bottom:6.0pt;margin-left:
36.0pt;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>glFlush();</span></p>
<p style='margin-top:6.0pt;margin-right:0cm;margin-bottom:6.0pt;margin-left:
0cm;line-height:130%'><span lang=EN-US style='font-size:10.0pt;color:purple'>}</span></p>
<p style='line-height:200%'><span lang=EN-US> 上面例子中,函数体内的前三条语句调用了OpenGL辅助库(Auxiliary
Library),其作用是进行一些初始化的设置。具体的含义分别说明如下:</span></p>
<p><span lang=EN-US> void auxInitWindow(GLbyte* titleString);</span></p>
<p><span lang=EN-US> 该函数设置窗口的标题,titleString为在标题条中出现的字符串名</span></p>
<p><span lang=EN-US> void auxInitDisplayMode(GLbitfield
mask);</span></p>
<p style='line-height:200%'><span lang=EN-US> 该函数设置窗口的特征,mask对下列值的按行逐位作“或”(OR)运算,或者按列作“与”(AND)运算:</span></p>
<p><span lang=EN-US> AUX_RGBA AUX_INDEX 颜色的RGBA模型还是索引模型</span></p>
<p><span lang=EN-US> AUX_SINGLE AUX_DOUBLE 单、双显示缓冲区</span></p>
<p><span lang=EN-US> 还可激活以下任何缓冲,按列作“与”(AND)运算:</span></p>
<p><span lang=EN-US> &nbs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -