我有一个加载在高度图中的程序,然后我使用三角形列表方法将顶点排序到一个数组,我的问题是如何将它更改为三角形条?我希望它是一个条带,所以第一行将从左到右,第二行从右到左等,使用for循环执行此操作。
到目前为止我的trianlge列表方法的代码(没有显示高度图加载器或顶点数量定义,也显示法线计算器,但我不需要任何帮助适应它):
for( int l = 0; l < m_HeightMapLength; ++l )
{
for( int w = 0; w < m_HeightMapWidth; ++w )
{
if( w < m_HeightMapWidth-1 && l < m_HeightMapLength-1 )
{
/*v0 = m_pHeightMap[mapIndex];
v1 = m_pHeightMap[mapIndex+m_HeightMapWidth];
v2 = m_pHeightMap[mapIndex+1];
v3 = m_pHeightMap[mapIndex+m_HeightMapWidth+1];
D3DXVECTOR3 vA = v0 - v1;
D3DXVECTOR3 vB = v1 - v2;
D3DXVECTOR3 vC = v3 - v1;
D3DXVECTOR3 vN1;
D3DXVec3Cross(&vN1, &vA, &vB);
D3DXVECTOR3 vN2;
D3DXVec3Cross(&vN2, &vB, &vC);*/
//D3DXVec3Normalize( &vN1, &vN1);
//D3DXVec3Normalize( &vN2, &vN2);
//T Left, Bot Left, T Right, T Right, Bot Left, Bot Right
//m_pMapVtxs[i++] = Vertex_Pos3fColour4ubNormal3f(D3DXVECTOR3( m_pHeightMap[mapIndex].x, m_pHeightMap[mapIndex].y, m_pHeightMap[mapIndex].z), MAP_COLOUR, D3DXVECTOR3(vN1.x, vN1.y, vN1.z));
//m_pMapVtxs[i++] = Vertex_Pos3fColour4ubNormal3f(D3DXVECTOR3( m_pHeightMap[mapIndex + m_HeightMapWidth].x, m_pHeightMap[mapIndex + m_HeightMapWidth].y, m_pHeightMap[mapIndex + m_HeightMapWidth].z), MAP_COLOUR, D3DXVECTOR3(vN1.x, vN1.y, vN1.z));
//m_pMapVtxs[i++] = Vertex_Pos3fColour4ubNormal3f(D3DXVECTOR3( m_pHeightMap[mapIndex+1].x, m_pHeightMap[mapIndex+1].y, m_pHeightMap[mapIndex+1].z), MAP_COLOUR, D3DXVECTOR3(vN1.x, vN1.y, vN1.z));
//m_pMapVtxs[i++] = Vertex_Pos3fColour4ubNormal3f(D3DXVECTOR3( m_pHeightMap[mapIndex+1].x, m_pHeightMap[mapIndex+1].y, m_pHeightMap[mapIndex+1].z), MAP_COLOUR, D3DXVECTOR3(vN2.x, vN2.y, vN2.z));
//m_pMapVtxs[i++] = Vertex_Pos3fColour4ubNormal3f(D3DXVECTOR3( m_pHeightMap[mapIndex + m_HeightMapWidth].x, m_pHeightMap[mapIndex + m_HeightMapWidth].y, m_pHeightMap[mapIndex + m_HeightMapWidth].z), MAP_COLOUR, D3DXVECTOR3(vN2.x, vN2.y, vN2.z));
//m_pMapVtxs[i++] = Vertex_Pos3fColour4ubNormal3f(D3DXVECTOR3( m_pHeightMap[mapIndex + m_HeightMapWidth+1].x, m_pHeightMap[mapIndex + m_HeightMapWidth+1].y, m_pHeightMap[mapIndex + m_HeightMapWidth+1].z), MAP_COLOUR, D3DXVECTOR3(vN2.x, vN2.y, vN2.z));
mapIndex ++;
for循环是我将其更改为条带的主要问题,我已经在纸上绘制了顶点,这就是我的主要问题,任何洞察都非常感激。
答案 0 :(得分:2)
我认为m_pHeightMap
是一个3d矢量。
条带将如下所示:
+--+--+--+--+
|/ |/ |/ |/ |
+--+--+--+--+
| \| \| \| \|
+--+--+--+--+
我们将从左下角开始,继续向右,然后向上一行并继续向左。但是让我们先来看看如何为单行定义一个条带:
for(int x = 0; i < m_HeightMapWidth; ++x)
{
add m_pHeightMap[x + y * m_HeightMapWidth]; //bottom vertex
add m_pHeightMap[x + (y + 1) * m_HeightMapWidth]; //top vertex
}
这适用于每隔一行。最后一个顶点将是行的右上顶点。在那里,我们必须添加一个退化的三角形。这是一个区域为0的三角形,将导致行分离。所以:
add m_pHeightMap[m_HeightMapWidth + m_HeightMapWidth - 1]; //once again the last vertex
对于上面的行,我们从右到左添加顶点:
for(int x = m_HeightMapWidth - 1; i >= 0; --x)
{
add m_pHeightMap[x + y * m_HeightMapWidth]; //bottom vertex
add m_pHeightMap[x + (y + 1 ) * m_HeightMapWidth]; //top vertex
}
请注意,第一个顶点是我们已经添加两次的顶点。第三次添加它将保留三角形的方向。
我们每行都这样做。总而言之:
for(int y = 0; y < m_HeightMapHeight - 1; ++y)
{
if(y % 2 == 0)
{
for(int x = 0; i < m_HeightMapWidth; ++x)
{
add m_pHeightMap[x + y * m_HeightMapWidth]; //bottom vertex
add m_pHeightMap[x + (y + 1) * m_HeightMapWidth]; //top vertex
}
add m_pHeightMap[(y + 2) * m_HeightMapWidth - 1]; //once again the last vertex
}
else
{
for(int x = m_HeightMapWidth - 1; i >= 0; --x)
{
add m_pHeightMap[x + y * m_HeightMapWidth]; //bottom vertex
add m_pHeightMap[x + (y + 1 ) * m_HeightMapWidth]; //top vertex
}
add m_pHeightMap[(y + 1) * m_HeightMapWidth]; //once again the last vertex
}
}
*代码未经测试