📄 tutorial_47.htm
字号:
<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>
<p>vfconn main(appdata IN, uniform float4x4 ModelViewProj)<br>
{<br>
vfconn OUT; <span class="theme">// 保存我们输出顶点的数据</span></p>
<p> <span class="theme">// 计算顶点y的坐标</span><br>
IN.position.y = ( sin(IN.wave.x + (IN.position.x / 5.0) ) + sin(IN.wave.x +
(IN.position.z / 4.0) ) ) * 2.5f;<br>
<br>
<span class="theme">// 保存到输出数据中</span><br>
OUT.HPos = mul(ModelViewProj, IN.position);</p>
<p> <span class="theme">// 不改变输入的颜色</span><br>
OUT.Col0.xyz = IN.color.xyz;</p>
<p> return OUT;<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%"><p>完成了上面的代码,记得保存一下.</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>#include <cg\cg.h> <br>#include <cg\cggl.h> </pre>
<pre>#pragma comment( lib, "cg.lib" ) <br>#pragma comment( lib, "cggl.lib" ) </pre>
<p></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>#define SIZE 64 <span class="theme"> // 定义网格的大小</span><br>bool cg_enable = TRUE, sp; <span class="theme">// 开关Cg程序</span><br>GLfloat mesh[SIZE][SIZE][3]; <span class="theme">// 保存我们的网格</span><br>GLfloat wave_movement = 0.0f; <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%">下面我们来定义一些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> CGcontext cgContext; <span class="theme">// 用来保存cg脚本</span></pre>
<p></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%">我们需要的第一个变量是CGcontext,这个变量是多个Cg脚本的容器,一般来说,你获得你可以用函数从这个容器中获得你想要的脚本
<p>接下来我们定义一个CGprogram变量,它用来保存我们得顶点脚本</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>CGprogram cgProgram; <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%">接下来我们需要一个变量来设置如何编译这个顶点脚本</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>CGprofile cgVertexProfile; <span class="theme"> // 被顶点脚本使用</span></pre>
<p></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>CGparameter position, color, modelViewMatrix, wave; <span class="theme"> // 脚本中需要的参数</span></pre>
<p></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%">在初始化阶段我们先要创建我们网格数据</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> glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); </pre>
<p> for (int x = 0; x < SIZE; x++)<br>
{<br>
for (int z = 0; z < SIZE; z++)<br>
{<br>
mesh[x][z][0] = (float) (SIZE / 2) - x; <br>
mesh[x][z][1] = 0.0f; <br>
mesh[x][z][2] = (float) (SIZE / 2) - z; <br>
}<br>
}</p>
<p></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%">我们设置多边形的现实模式为线框图,接着遍历没有顶点,设置其高度。
<p>接下来,我们初始化Cg程序</p>
<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><span class="theme">// 设置Cg</span><br> cgContext = cgCreateContext(); <span class="theme">// 创建一个Cg容器</span></pre>
<p> <span class="theme">// 测试是否创建成功</span><br>
if (cgContext == NULL)<br>
{<br>
MessageBox(NULL, "Failed To Create Cg Context", "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>cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX); <span class="theme">// 配置在OpenGL中使用顶点缓存</span></pre>
<p> <span class="theme">// 检测Cg程序的是否创建成功</span><br>
if (cgVertexProfile == CG_PROFILE_UNKNOWN)<br>
{<br>
MessageBox(NULL, "Invalid profile type", "Error", MB_OK);<br>
return FALSE; <br>
}</p>
<p> cgGLSetOptimalOptions(cgVertexProfile); <span class="theme">// 启用配置文件</span></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -