📄 tutorial_45.htm
字号:
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) wglGetProcAddress("glDeleteBuffersARB");
<span class="theme"> // 创建VBO对象</span>
g_pMesh->BuildVBOs();
}
#else
g_fVBOSupported = false;
#endif
<span class="theme"> //设置OpenGL状态</span>
glClearColor (0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth (1.0f);
glDepthFunc (GL_LEQUAL);
glEnable (GL_DEPTH_TEST);
glShadeModel (GL_SMOOTH);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable( GL_TEXTURE_2D );
glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
return TRUE;
}
</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">下面的函数用来检测是否包含特定的扩展名称</td>
<td class="r"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><span class="theme">// 返回是否支持指定的扩展</span>
bool IsExtensionSupported( char* szTargetExtension )
{
const unsigned char *pszExtensions = NULL;
const unsigned char *pszStart;
unsigned char *pszWhere, *pszTerminator;
pszWhere = (unsigned char *) strchr( szTargetExtension, ' ' );
if( pszWhere || *szTargetExtension == '\0' )
return false;
<span class="theme"> // 返回扩展字符串</span>
pszExtensions = glGetString( GL_EXTENSIONS );
<span class="theme"> // 在扩展字符串中搜索</span>
pszStart = pszExtensions;
for(;;)
{
pszWhere = (unsigned char *) strstr( (const char *) pszStart, szTargetExtension );
if( !pszWhere )
break;
pszTerminator = pszWhere + strlen( szTargetExtension );
if( pszWhere == pszStart || *( pszWhere - 1 ) == ' ' )
if( *pszTerminator == ' ' || *pszTerminator == '\0' )
<span class="theme">//如果存在返回True</span>
return true;
pszStart = pszTerminator;
}
return false;
}
</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_45_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_45_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_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>void Draw (void)<br>{<br> glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); <br> glLoadIdentity (); </pre>
<p> <span class="theme">// 显示当前的帧率</span><br>
if( GetTickCount() - g_dwLastFPS >= 1000 ) <br>
{<br>
g_dwLastFPS = GetTickCount(); <br>
g_nFPS = g_nFrames; <br>
g_nFrames = 0; </p>
<p> char szTitle[256]={0}; <br>
sprintf( szTitle, "Lesson 45: NeHe & Paul Frazee's VBO Tut - %d Triangles,
%d FPS", g_pMesh->m_nVertexCount / 3, g_nFPS );<br>
if( g_fVBOSupported ) <span class="theme">// 是否支持VBO</span><br>
strcat( szTitle, ", Using VBOs" );<br>
else<br>
strcat( szTitle, ", Not Using VBOs" );<br>
SetWindowText( g_window->hWnd, szTitle ); <span class="theme">// 设置窗口标题</span><br>
}<br>
g_nFrames++; <br>
<br>
<span class="theme">// 设置视口</span><br>
glTranslatef( 0.0f, -220.0f, 0.0f ); <br>
glRotatef( 10.0f, 1.0f, 0.0f, 0.0f ); <br>
glRotatef( g_flYRot, 0.0f, 1.0f, 0.0f ); </p>
<p> <span class="theme">// 使用顶点,纹理坐标数组</span><br>
glEnableClientState( GL_VERTEX_ARRAY ); <br>
glEnableClientState( GL_TEXTURE_COORD_ARRAY ); </p>
<p></p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">为了使用VBO,你必须告诉OpenGL内存中的那部分需要加载到VBO中。所以第一步我们要起用顶点数组和纹理坐标数组。接着我们必须告诉OpenGL去把数据的指针设置到特定的地方,glVertexPointer函数可以完成这个功能。<br>
我们分为启用和不启用VBO两个路径来渲染,他们都差不多,唯一的区别是当你需要把指针指向VBO缓存时,记得把数据指针设置NULL。
<p> </p>
</td>
<td class="r"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><span class="theme"> // 如果支持VBO扩展</span><br> if( g_fVBOSupported )<br> {<br> glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh->m_nVBOVertices );<br> glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // 设置顶点数组的指针为顶点缓存<br> glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh->m_nVBOTexCoords );<br> glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // 设置顶点数组的指针为纹理坐标缓存<br> } <br><span class="theme"> // 不支持VBO扩展</span><br> else<br> {<br> glVertexPointer( 3, GL_FLOAT, 0, g_pMesh->m_pVertices ); <br> glTexCoordPointer( 2, GL_FLOAT, 0, g_pMesh->m_pTexCoords ); <br> }</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">好了,渲染所有的三角形吧</td>
<td class="r"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>// 渲染<br> glDrawArrays( GL_TRIANGLES, 0, g_pMesh->m_nVertexCount ); <br></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_45_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" width="3%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="94%">最后,别忘了恢复到默认的OpenGL状态. </td>
<td class="r" width="3%"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre><br> glDisableClientState( GL_VERTEX_ARRAY ); <br> glDisableClientState( GL_TEXTURE_COORD_ARRAY ); <br>}<br></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%"><p>如果你想更多的了解VBO对象,我建议你读一下SGI的扩展说明:<br>
http://oss.sgi.com/projects/ogl-sample/registry<br>
它会给你更多的信息</p>
<p>好了,那就是这次的课程,如果你发现任何问题,请联系我.</p>
<table border="1" width="100%">
<tbody><tr>
<td width="27%"><img src="Tutorial_45_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%2045.mht">网页格式</a>
<a href="http://www.owlei.com/DancingWind/Res/pdf/OpenGL_Nehe_Course_Tutorial_45.pdf">PDF格式</a><br>
源码 <a href="http://www.owlei.com/DancingWind/Res/Src/45_vbo.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_44.htm">< 第44课</a></font></b></td>
<td align="right" width="50%"><b><font size="-1"><a href="http://www.owlei.com/DancingWind/Course/Tutorial_46.htm">第46课 > </a></font></b></td>
</tr>
</tbody>
</table>
</font>
</td>
<td class="r"><img alt="" src="Tutorial_45_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_45_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_45_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -