10.4 绘制地形

要绘制地形首先应调用SetEffectMaterial方法设置effect。然后设置顶点缓冲、索引缓冲和顶点声明。使用顶点声明告知device要使用的顶点格式,让它能正确地处理顶点:

// Set mesh vertex and index buffer 
GraphicsDevice.Vertices[0].SetSource(vb, 0, VertexPositionNormalTangentBinormal.SizeInBytes); 
GraphicsDevice.Indices = ib; 

// Set the vertex declaration 
GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionNormalTangentBinormal.VertexElements); 

下一步开始effect和pass。要绘制地形网格,你使用了GraphicsDevice的DrawIndexedPrimitives方法,这个方法可以绘制带有索引信息的图元。下面是Terrain类中Draw方法的完整代码:

public override void Draw(GameTime time) 
{
    // Configure TerrainEffect 
    SetEffectMaterial(); 
    
    // Set mesh vertex and index buffer 
    GraphicsDevice.Vertices[0].SetSource(vb, 0,VertexPositionNormalTangentBinormal.SizeInBytes); 
    GraphicsDevice.Indices = ib; 
    
    // Set the vertex declaration GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice, 
    VertexPositionNormalTangentBinormal.VertexElements); 
    
    effect.Begin(); 
    // Loop	through all effect passes 
    foreach (EffectPass pass in effect.CurrentTechniquePasses) 
    {
        pass.Begin(); 
        // Draw the mesh 
        GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 
        numVertices, 0, numTriangles); 
        pass.End();
    }
    effect.End();
} 

图10-10展示了地形渲染的最终结果。注意地形表面是平的,使用了法线贴图添加了石头图案的细节。

图10-10

图10-10 地形渲染的最后结果


发布时间:2009/4/22 下午12:50:47  阅读次数:5054

2006 - 2024,推荐分辨率 1024*768 以上,推荐浏览器 Chrome、Edge 等现代浏览器,截止 2021 年 12 月 5 日的访问次数:1872 万 9823 站长邮箱

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号