📄 tutorial_04.htm
字号:
<font color="#aaffaa" size="3">
<pre> glBegin(GL_TRIANGLES); <font color="#ffffaa">// 绘制三角形</font>
glColor3f(1.0f,0.0f,0.0f); <font color="#ffffaa">// 设置当前色为红色</font>
glVertex3f( 0.0f, 1.0f, 0.0f); <font color="#ffffaa">// 上顶点</font>
glColor3f(0.0f,1.0f,0.0f); <font color="#ffffaa">// 设置当前色为绿色</font>
glVertex3f(-1.0f,-1.0f, 0.0f); <font color="#ffffaa">// 左下</font>
glColor3f(0.0f,0.0f,1.0f); <font color="#ffffaa">// 设置当前色为蓝色</font>
glVertex3f( 1.0f,-1.0f, 0.0f); <font color="#ffffaa">// 右下</font>
glEnd(); <font color="#ffffaa">// 三角形绘制结束</font>
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/tl.jpg" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/tc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/tr.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_04_files/l.gif"><img src="Tutorial_04_files/l.gif" height="28" width="28"></td>
<td valign="top" width="100%">您
会注意下面的代码中我们增加了另一个glLoadIdentity()调用。目的是为了重置模型观察矩阵。如果我们没有重置,直接调用
glTranslate的话,会出现意料之外的结果。因为坐标轴已经旋转了,很可能没有朝着您所希望的方向。所以我们本来想要左右移动对象的,就可能变成
上下移动了,取决于您将坐标轴旋转了多少角度。试试将glLoadIdentity() 注释掉之后,会出现什么结果。 <p>重置模型观察矩阵之后,X,Y,Z轴都以复位,我们调用glTranslate。您会注意到这次我们只向右一了1.5单位,而不是上节课的3.0单位。因为我们重置场景的时候,焦点又回到了场景的中心(0.0处)。这样就只需向右移1.5单位就够了。<br>
当我们移到新位置后,绕X轴旋转四边形。正方形将上下转动。</p></td>
<td background="Tutorial_04_files/r.gif"><img src="Tutorial_04_files/r.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/bl.gif" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/bc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/br.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<font color="#aaffaa" size="3">
<pre> glLoadIdentity(); <font color="#ffffaa">// 重置模型观察矩阵</font>
glTranslatef(1.5f,0.0f,-6.0f); <font color="#ffffaa">// 右移1.5单位,并移入屏幕 6.0</font>
glRotatef(rquad,1.0f,0.0f,0.0f); <font color="#ffffaa">// 绕X轴旋转四边形</font>
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/tl.jpg" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/tc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/tr.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_04_files/l.gif"><img src="Tutorial_04_files/l.gif" height="28" width="28"></td>
<td valign="top" width="100%">下一段代码保持不变。在屏幕的右侧画一个蓝色的正方形</td>
<td background="Tutorial_04_files/r.gif"><img src="Tutorial_04_files/r.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/bl.gif" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/bc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/br.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<font color="#aaffaa" size="3">
<pre> glColor3f(0.5f,0.5f,1.0f); <font color="#ffffaa">// 一次性将当前色设置为蓝色</font>
glBegin(GL_QUADS); <font color="#ffffaa">// 绘制正方形</font>
glVertex3f(-1.0f, 1.0f, 0.0f); <font color="#ffffaa">// 左上</font>
glVertex3f( 1.0f, 1.0f, 0.0f); <font color="#ffffaa">// 右上</font>
glVertex3f( 1.0f,-1.0f, 0.0f); <font color="#ffffaa">// 左下</font>
glVertex3f(-1.0f,-1.0f, 0.0f); <font color="#ffffaa">// 右下</font>
glEnd(); <font color="#ffffaa">// 正方形绘制结束</font>
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/tl.jpg" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/tc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/tr.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_04_files/l.gif"><img src="Tutorial_04_files/l.gif" height="28" width="28"></td>
<td valign="top" width="100%">下两行是新增的。倘若把 rtri 和 rquad 想象为容器,那么在程序的开始我们创建了容器(
GLfloat rtri , 和 GLfloat rquad )。当容器创建之后,里面是空的。下面的第一行代码是向容器中添加0.2。因此每次当我们运行完前面的代码后,都会在这里使
rtri 容器中的值增长0.2。后面一行将 rquad 容器中的值减少0.15。同样每次当我们运行完前面的代码后,都会在这里使 rquad
容器中的值下跌0.15。下跌最终会导致对象旋转的方向和增长的方向相反。
<p>尝试改变下面代码中的+和-,来体会对象旋转的方向是如何改变的。并试着将0.2改成1.0。这个数字越大,物体就转的越快,这个数字越小,物体转的就越慢。</p></td>
<td background="Tutorial_04_files/r.gif"><img src="Tutorial_04_files/r.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/bl.gif" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/bc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/br.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<font color="#aaffaa" size="3">
<pre> rtri+=0.2f; <font color="#ffffaa">// 增加三角形的旋转变量</font>
rquad-=0.15f; <font color="#ffffaa">// 减少四边形的旋转变量</font>
return TRUE; <font color="#ffffaa">// 继续运行</font>
}
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/tl.jpg" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/tc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/tr.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_04_files/l.gif"><img src="Tutorial_04_files/l.gif" height="28" width="28"></td>
<td valign="top" width="100%">最后换掉窗口模式下的标题内容</td>
<td background="Tutorial_04_files/r.gif"><img src="Tutorial_04_files/r.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/bl.gif" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/bc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/br.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<font color="#aaffaa" size="3">
</font><pre><font color="#aaffaa" size="3"><font color="#ffffaa"> // 重建 OpenGL 窗口</font>
if (!CreateGLWindow("NeHe's 旋转实例",640,480,16,fullscreen))
</font></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/tl.jpg" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/tc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/tr.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_04_files/l.gif"><img src="Tutorial_04_files/l.gif" height="28" width="28"></td>
<td valign="top" width="100%">在这一课中,我试着尽量详细的解释如何让对象绕某个轴转动。改改代码,试着让对象绕着Z轴、X+Y轴或者所有三个轴来转动:)。如果您有什么意见或建议请给我EMAIL。如果您认为有什么不对或可以改进,请告诉我。我想做最好的OpenGL教程并对您的反馈感兴趣。<br>
<table border="1" width="100%">
<tbody><tr>
<td width="27%"><img src="Tutorial_04_files/logo%25203.jpg" align="middle" height="200" width="209"></td>
<td width="73%">版权与使用声明:<br>
我是个对学习和生活充满激情的普通男孩,在网络上我以DancingWind为昵称,我的联系方式是zhouwei02@mails.tsinghua.edu.cn,如果你有任何问题,都可以联系我。
<p>引子<br>
网络是一个共享的资源,但我在自己的学习生涯中浪费大量的时间去搜索可用的资料,在现实生活中花费了大量的金钱和时间在书店中寻找资料,于是我给自己起了
个昵称DancingWind,其意义是想风一样从各个知识的站点中吸取成长的养料。在飘荡了多年之后,我决定把自己收集的资料整理为一个统一的资源库。</p>
<p>版权声明<br>
所有DancingWind发表的内容,大多都来自共享的资源,所以我没有资格把它们据为己有,或声称自己为这些资源作出了一点贡献。故任何人都可以复
制,修改,重新发表,甚至以自己的名义发表,我都不会追究,但你在做以上事情的时候必须保证内容的完整性,给后来的人一个完整的教程。最后,任何人不能以
这些资料的任何部分,谋取任何形式的报酬。</p>
<p>发展计划<br>
在国外,很多资料都是很多人花费几年的时间慢慢积累起来的。如果任何人有兴趣与别人共享你的知识,我很欢迎你与我联系,但你必须同意我上面的声明。</p>
<p>感谢<br>
感谢我的母亲一直以来对我的支持和在生活上的照顾。<br>
感谢我深爱的女友田芹,一直以来默默的在精神上和生活中对我的支持,她甚至把买衣服的钱都用来给我买书了,她真的是我见过的最好的女孩,希望我能带给她幸福。</p>
<p>资源下载: <br>
文档 <a href="http://www.owlei.com/DancingWind/Res/mht/NeHe%20OpenGL%20Chinese%20Course%2004.mht">网页格式</a>
<a href="http://www.owlei.com/DancingWind/Res/pdf/OpenGL_Nehe_Course_Tutorial_04.pdf">PDF格式</a><br>
源码 <a href="http://www.owlei.com/DancingWind/Res/Src/04_Rotate.rar">RAR格式</a></p></td>
</tr>
</tbody></table>
<table border="0" width="100%">
<tbody>
<tr>
<td align="left" width="50%"><a href="http://www.owlei.com/DancingWind/Course/Tutorial_03.htm"><b><font size="-1"><
第03课</font></b></a></td>
<td align="right" width="50%"><a href="http://www.owlei.com/DancingWind/Course/Tutorial_05.htm"><b><font size="-1">第05课
></font></b></a></td>
</tr>
</tbody>
</table>
</td>
<td background="Tutorial_04_files/r.gif"><img src="Tutorial_04_files/r.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_04_files/bl.gif" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_04_files/bc.gif" height="28" width="100%"></td>
<td><img src="Tutorial_04_files/br.gif" height="28" width="28"></td>
</tr>
</tbody>
</table>
</center></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -