📄 tutorial_45.htm
字号:
};
//网格类
class CMesh
{
public:
// 网格数据
int m_nVertexCount; <span class="theme"> // 顶点个数</span>
CVert* m_pVertices; <span class="theme">// 顶点数据的指针</span>
CTexCoord* m_pTexCoords; <span class="theme">// 顶点的纹理坐标</span>
unsigned int m_nTextureId; <span class="theme">// 纹理的ID</span>
unsigned int m_nVBOVertices; <span class="theme">// 顶点缓存对象的名称</span>
unsigned int m_nVBOTexCoords; <span class="theme">// 顶点纹理缓存对象的名称</span>
AUX_RGBImageRec* m_pTextureImage; <span class="theme">// 高度数据</span>
public:
CMesh(); <span class="theme">// 构造函数</span>
~CMesh(); <span class="theme">// 析构函数</span>
<span class="theme"> // 载入高度图</span>
bool LoadHeightmap( char* szPath, float flHeightScale, float flResolution );
<span class="theme">// 返回单个点的高度</span>
float PtHeight( int nX, int nY );
<span class="theme"> // 创建顶点缓存对象</span>
void BuildVBOs();
};
</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>
<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>bool g_fVBOSupported = false; <span class="theme">// 是否支持顶点缓存对象</span>
CMesh* g_pMesh = NULL; <span class="theme">// 网格数据</span>
float g_flYRot = 0.0f; <span class="theme">// 旋转角度</span>
int g_nFPS = 0, g_nFrames = 0; <span class="theme">// 帧率计数器</span>
DWORD g_dwLastFPS = 0; <span class="theme">// 上一帧的计数 </span>
</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%"> 下面的代码加载高度图,它和34课的内容差不多,在这里不多加解释了:
</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 CMesh :: LoadHeightmap( char* szPath, float flHeightScale, float flResolution )
{
FILE* fTest = fopen( szPath, "r" );
if( !fTest )
return false;
fclose( fTest );
<span class="theme"> // 加载图像文件</span>
m_pTextureImage = auxDIBImageLoad( szPath );
<span class="theme"> // 读取顶点数据</span>
m_nVertexCount = (int) ( m_pTextureImage->sizeX * m_pTextureImage->sizeY * 6 / ( flResolution * flResolution ) );
m_pVertices = new CVec[m_nVertexCount];
m_pTexCoords = new CTexCoord[m_nVertexCount];
int nX, nZ, nTri, nIndex=0;
float flX, flZ;
for( nZ = 0; nZ < m_pTextureImage->sizeY; nZ += (int) flResolution )
{
for( nX = 0; nX < m_pTextureImage->sizeX; nX += (int) flResolution )
{
for( nTri = 0; nTri < 6; nTri++ )
{
flX = (float) nX + ( ( nTri == 1 || nTri == 2 || nTri == 5 ) ? flResolution : 0.0f );
flZ = (float) nZ + ( ( nTri == 2 || nTri == 4 || nTri == 5 ) ? flResolution : 0.0f );
m_pVertices[nIndex].x = flX - ( m_pTextureImage->sizeX / 2 );
m_pVertices[nIndex].y = PtHeight( (int) flX, (int) flZ ) * flHeightScale;
m_pVertices[nIndex].z = flZ - ( m_pTextureImage->sizeY / 2 );
m_pTexCoords[nIndex].u = flX / m_pTextureImage->sizeX;
m_pTexCoords[nIndex].v = flZ / m_pTextureImage->sizeY;
nIndex++;
}
}
}
<span class="theme"> // 载入纹理,它和高度图是同一副图像</span>
glGenTextures( 1, &m_nTextureId );
glBindTexture( GL_TEXTURE_2D, m_nTextureId );
glTexImage2D( GL_TEXTURE_2D, 0, 3, m_pTextureImage->sizeX, m_pTextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, m_pTextureImage->data );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
<span class="theme"> // 释放纹理数据</span>
if( m_pTextureImage )
{
if( m_pTextureImage->data )
free( m_pTextureImage->data );
free( m_pTextureImage );
}
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%">下面的代码用来计算(x,y)处的亮度</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">//计算(x,y)处的亮度</span>
float CMesh :: PtHeight( int nX, int nY )
{
int nPos = ( ( nX % m_pTextureImage->sizeX ) + ( ( nY % m_pTextureImage->sizeY ) * m_pTextureImage->sizeX ) ) * 3;
float flR = (float) m_pTextureImage->data[ nPos ]; <span class="theme">// 返回红色分量</span>
float flG = (float) m_pTextureImage->data[ nPos + 1 ]; <span class="theme">// 返回绿色分量</span>
float flB = (float) m_pTextureImage->data[ nPos + 2 ]; <span class="theme">// 返回蓝色分量</span>
return ( 0.299f * flR + 0.587f * flG + 0.114f * flB ); <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_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>void CMesh :: BuildVBOs()<br>{<br> glGenBuffersARB( 1, &m_nVBOVertices ); <span class="theme">// 创建一个顶点缓存,并把顶点数据绑定到缓存</span><br> glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices ); <br> glBufferDataARB( GL_ARRAY_BUFFER_ARB, m_nVertexCount*3*sizeof(float), m_pVertices, GL_STATIC_DRAW_ARB );</pre>
<p> glGenBuffersARB( 1, &m_nVBOTexCoords ); <span class="theme">// 创建一个纹理缓存,并把纹理数据绑定到缓存</span><br>
glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords ); <br>
glBufferDataARB( GL_ARRAY_BUFFER_ARB, m_nVertexCount*2*sizeof(float), m_pTexCoords,
GL_STATIC_DRAW_ARB );</p>
<p> <span class="theme">// 删除分配的内存</span><br>
delete [] m_pVertices; m_pVertices = NULL;<br>
delete [] m_pTexCoords; m_pTexCoords = NULL</p>
<pre> }</pre>
<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扩展。如果支持我们将把函数指针和它对应的函数关联起来,如果不支持将只返回数据。</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 Initialize (GL_Window* window, Keys* keys)
{
g_window = window;
g_keys = keys;
<span class="theme">// 载入纹理数据</span>
g_pMesh = new CMesh();
if( !g_pMesh->LoadHeightmap( "terrain.bmp",
MESH_HEIGHTSCALE,
MESH_RESOLUTION ) )
{
MessageBox( NULL, "Error Loading Heightmap", "Error", MB_OK );
return false;
}
<span class="theme"> // 检测是否支持VBO扩展</span>
#ifndef NO_VBOS
g_fVBOSupported = IsExtensionSupported( "GL_ARB_vertex_buffer_object" );
if( g_fVBOSupported )
{
<span class="theme"> // 获得函数的指针</span>
glGenBuffersARB = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress("glGenBuffersARB");
glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress("glBindBufferARB");
glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress("glBufferDataARB");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -