📄 tutorial_47.htm
字号:
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">如果你想使用片断脚本,使用CG_GL_FRAGMENT变量。如果返回的变量为CG_PROFILE_UNKNOW表示没有可用的配置文件,则不能编译你需要的Cg程序。</td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><span class="theme">// 从文件中载入Cg程序</span><br> cgProgram = cgCreateProgramFromFile(cgContext, CG_SOURCE, "CG/Wave.cg", cgVertexProfile, "main", 0);</pre>
<p> <span class="theme">// 检测是否成功</span><br>
if (cgProgram == NULL)<br>
{<br>
CGerror Error = cgGetError();<br>
MessageBox(NULL, cgGetErrorString(Error), "Error", MB_OK);<br>
return FALSE; <br>
}<br>
</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">我们尝试从源文件中创建一个Cg程序,并返回编译后的结果。 </td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><span class="theme">// 载入脚本</span><br> cgGLLoadProgram(cgProgram);<br></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">下面我们把顶点脚本载入到显存,并准备帮定给GPU。所有的脚本在使用前必须加载。</td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><span class="theme"> // 把数据变量地址发送给Cg程序</span><br> position = cgGetNamedParameter(cgProgram, "IN.position");<br> color = cgGetNamedParameter(cgProgram, "IN.color");<br> wave = cgGetNamedParameter(cgProgram, "IN.wave");<br> modelViewMatrix = cgGetNamedParameter(cgProgram, "ModelViewProj");</pre>
<p> return TRUE; <br>
</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">在初始化的最后,我们必须告诉Cg脚本在那里去获得输入的数据,我们需要把变量的指针传递过去,下面的函数完成了这个功能。
<p>程序结束时,记得释放我们创建的内容。</p>
</td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>cgDestroyContext(cgContext); </pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">下面的代码使用空格切换是否使用Cg程序</td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>if (g_keys->keyDown [' '] && !sp)<br> {<br> sp=TRUE;<br> cg_enable=!cg_enable;<br> }</pre>
<pre>if (!g_keys->keyDown [' '])<br> sp=FALSE;<br></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">现在我们已经完成了所有的准备工作了,到了我们实际绘制网格的地方了,按照惯例我们还是先设置我们得视口。
<p> </p>
</td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><br> gluLookAt(0.0f, 25.0f, -45.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0);<br></pre>
<pre><span class="theme">// 把当前Cg程序的模型变化矩阵告诉当前程序</span><br> cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%"><p>上面我们要做的事就是把当前Cg程序的模型变化矩阵告诉当前程序。</p>
<p>结下来如果使用cg程序,则把顶点的颜色设置为绿色</p></td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><span class="theme"> // 如果使用Cg程序</span><br> if (cg_enable)<br> {<br><span class="theme"> // 使用顶点脚本配置文件</span><br> cgGLEnableProfile(cgVertexProfile); <br><span class="theme"> // 帮定到当前的顶点脚本</span><br> cgGLBindProgram(cgProgram);<br><span class="theme"> // 设置绘制颜色</span><br> cgGLSetParameter4f(color, 0.5f, 1.0f, 0.5f, 1.0f);<br> }<br></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">下面我们来绘制我们的网格。 </td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><span class="theme"> // 开始绘制我们的网格</span><br> for (int x = 0; x < SIZE - 1; x++)<br> {<br> glBegin(GL_TRIANGLE_STRIP);<br> for (int z = 0; z < SIZE - 1; z++)<br> {<br><span class="theme"> // 设置Wave参数</span><br> cgGLSetParameter3f(wave, wave_movement, 1.0f, 1.0f);<br> <span class="theme"> //设置输入的顶点</span><br> glVertex3f(mesh[x][z][0], mesh[x][z][1], mesh[x][z][2]); <br> glVertex3f(mesh[x+1][z][0], mesh[x+1][z][1], mesh[x+1][z][2]); <br> wave_movement += 0.00001f; <br> if (wave_movement > TWO_PI) <br> wave_movement = 0.0f;<br> }<br><span class="theme"> //经过Cg程序处理,进行绘制</span><br> glEnd();<br> }<br></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">上面的代码完成具体的绘制操作,对于每一个顶点,我们动态的传入波动系数和输入原始的顶点数据。在绘制开始前,顶点脚本接受所有的顶点数据并处理,接着进行光栅华操作。
<p>别忘了在绘制完成后,关闭我们启用的顶点脚本,否则在绘制其它的模型时会让你得到不想要的结果。</p>
</td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>if (cg_enable)<br> cgGLDisableProfile(cgVertexProfile); <span class="theme">// 禁用顶点脚本配置文件</span></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%"><p>好了上面就是所有的内容了,简单吧.Cg就是这么简单 </p>
<table border="1" width="100%">
<tbody><tr>
<td width="27%"><img src="Tutorial_47_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%2047.mht">网页格式</a>
<a href="http://www.owlei.com/DancingWind/Res/pdf/OpenGL_Nehe_Course_Tutorial_47.pdf">PDF格式</a><br>
源码 <a href="http://www.owlei.com/DancingWind/Res/Src/47_Cg.rar">RAR格式</a></p></td>
</tr>
</tbody></table>
<font class="text"><table border="0" width="100%">
<tbody>
<tr>
<td align="left" width="50%"><b><font size="-1"><a href="http://www.owlei.com/DancingWind/Course/Tutorial_46.htm">< 第46课</a></font></b></td>
<td align="right" width="50%"><b><font size="-1"><a href="http://www.owlei.com/DancingWind/Course/Tutorial_48.htm">第48课 > </a></font></b></td>
</tr>
</tbody>
</table>
</font>
</td>
<td class="r"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_47_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -