码迷,mamicode.com
首页 > 其他好文 > 详细

龙书11_chapter_6 二:HillsDemo解析

时间:2015-05-27 15:43:08      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

 书中HillsDemo 关键是类的结构,GeometryGenerator的组成和应用;

MeshData的构成,来存储Mesh的vertex和index数据;

class GeometryGenerator
{
public:
    struct Vertex
    {
        Vertex(){}
        Vertex(const XMFLOAT3& p):Position(p){}
        Vertex(float px,float py,float pz):Position(px,py,pz){}
        XMFLOAT3 Position;
    };
    struct MeshData
    {
        std::vector<Vertex> Vertices; //
        std::vector<UINT> Indices;   //
    };
    void CreateGrid(float width,float depth,UINT m,UINT n,MeshData &meshData);
void CreateBox()...
void Create_Other_Mesh()...
//...Create_Other_Mesh };

GeometryGenerator.CPP文件中对CreateGrid进行具体操作,那么,怎么应用到具体vertexBuffer和indexBuffer中去呢?

首先是初始化中雷打不动的三个接口:

bool HillsApp::Init()
{
    if(!D3DApp::Init())
        return false;
    BuildGeometryBuffers();//Buffer
    BuildFX();//FX
    BuildVertexLayout();//Layout
    return true;
} 
void HillsApp::BuildGeometryBuffers()
{
    GeometryGenerator::MeshData grid;        //定义Hills的MeshData
    GeometryGenerator geoGen;
    geoGen.CreateGrid(160.0f, 160.0f, 50, 50, grid);//数据输入到MeshData
    mGridIndexCount = grid.Indices.size();
    // Extract the vertex elements we are interested and apply the height function to
    // each vertex.  In addition, color the vertices based on their height so we have
    // sandy looking beaches, grassy low hills, and snow mountain peaks.
    std::vector<Vertex> vertices(grid.Vertices.size()); //顶点数据的存for(size_t i = 0; i < grid.Vertices.size(); ++i)
    {
        XMFLOAT3 p = grid.Vertices[i].Position;
        p.y = GetHeight(p.x, p.z);
        vertices[i].Pos   = p; 
        // Color the vertex based on its height.
        if( p.y < -10.0f )
        {
            // Sandy beach color.
            vertices[i].Color = XMFLOAT4(1.0f, 0.96f, 0.62f, 1.0f);
        }
        else if( p.y < 5.0f )
        {
            // Light yellow-green.
            vertices[i].Color = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f);
        }
        else if( p.y < 12.0f )
        {
            // Dark yellow-green.
            vertices[i].Color = XMFLOAT4(0.1f, 0.48f, 0.19f, 1.0f);
        }
        else if( p.y < 20.0f )
        {
            // Dark brown.
            vertices[i].Color = XMFLOAT4(0.45f, 0.39f, 0.34f, 1.0f);
        }
        else
        {
            // White snow.
            vertices[i].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    D3D11_BUFFER_DESC vbd;
    vbd.Usage = D3D11_USAGE_IMMUTABLE;      
    vbd.ByteWidth = sizeof(Vertex) * grid.Vertices.size();
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    D3D11_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = &vertices[0];       //找到Hills的顶点数据地址
    HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB)); //Hills数据给vertexBuffer
    // Pack the indices of all the meshes into one index buffer.
    D3D11_BUFFER_DESC ibd;
    ibd.Usage = D3D11_USAGE_IMMUTABLE;
    ibd.ByteWidth = sizeof(UINT) * mGridIndexCount;
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
    D3D11_SUBRESOURCE_DATA iinitData;
    iinitData.pSysMem = &grid.Indices[0];
    HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB));
}

 

 

学习点:GeometryGenerator类的构成,以及对MeshData结构的应用。

           定义了MeshData这个子类,定义并实现了不同Mesh数据的接口,

          在应用中,CreateBuffer时候,定义相应的MeshData,获取不同Mesh的vertexData和IndexData

         

 

龙书11_chapter_6 二:HillsDemo解析

标签:

原文地址:http://www.cnblogs.com/dust-fly/p/4533479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!