• 龙书11_chapter_6 二:HillsDemo解析


     书中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

             

  • 相关阅读:
    1、SASS / SCSS 总结简介
    Chrome 中的 JavaScript 断点设置和调试技巧
    《Javascript 设计模式》-读书笔记
    CSS基线之道
    高性能 CSS3 动画
    《编写高质量代码》web前端开发修炼之道-读书笔记
    《Effective JavaScript》编写高质量的Javascript代码的68个有效方法-读书笔记
    Sea.js-模块化开发框架
    JSLine和JSHint代码质量检查工具和uglifyjs压缩JS的
    在Windows平台上安装Node.js及NPM模块管理
  • 原文地址:https://www.cnblogs.com/dust-fly/p/4533479.html
Copyright © 2020-2023  润新知