• 热门专题

Directx11教程(7) 画一个颜色立方体

作者:迈克老狼2012  发布日期:2012-03-21 20:52:09
Tag标签:Directx11  颜色立方体  
  • 前面我们通过D3D11画了一个三角形,本章我们将画一个颜色立方体,它的立体感更强。

    主要的变动就是ModelClass类。

    ModelClass.h代码如下:

     

    #pragma once
    
    #include <d3d11.h> 
    #include <d3dx10math.h>
    
    //定义一些常用颜色 
    const D3DXVECTOR4 WHITE(1.0f, 1.0f, 1.0f, 1.0f); 
    const D3DXVECTOR4 BLACK(0.0f, 0.0f, 0.0f, 1.0f); 
    const D3DXVECTOR4 RED(1.0f, 0.0f, 0.0f, 1.0f); 
    const D3DXVECTOR4 GREEN(0.0f, 1.0f, 0.0f, 1.0f); 
    const D3DXVECTOR4 BLUE(0.0f, 0.0f, 1.0f, 1.0f); 
    const D3DXVECTOR4 YELLOW(1.0f, 1.0f, 0.0f, 1.0f); 
    const D3DXVECTOR4 CYAN(0.0f, 1.0f, 1.0f, 1.0f); //蓝绿色 
    const D3DXVECTOR4 MAGENTA(1.0f, 0.0f, 1.0f, 1.0f); //洋红色
    
    const D3DXVECTOR4 BEACH_SAND(1.0f, 0.96f, 0.62f, 1.0f); 
    const D3DXVECTOR4 LIGHT_YELLOW_GREEN(0.48f, 0.77f, 0.46f, 1.0f); 
    const D3DXVECTOR4 DARK_YELLOW_GREEN(0.1f, 0.48f, 0.19f, 1.0f); 
    const D3DXVECTOR4 DARKBROWN(0.45f, 0.39f, 0.34f, 1.0f);
    
    class ModelClass 
        { 
        private: 
            struct VertexType 
                { 
                D3DXVECTOR3 position; 
                D3DXVECTOR4 color; 
                };
    
        public: 
            ModelClass(void); 
            ModelClass(const ModelClass &);
    
            ~ModelClass(void); 
            bool Initialize(ID3D11Device*); 
            void Shutdown(); 
            void Render(ID3D11DeviceContext*);
    
            int GetIndexCount();
    
        private: 
            bool InitializeBuffers(ID3D11Device*); 
            void ShutdownBuffers(); 
            void RenderBuffers(ID3D11DeviceContext*); 
           //顶点缓冲和顶点索引缓冲 
            ID3D11Buffer *m_vertexBuffer, *m_indexBuffer; 
            int m_vertexCount, m_indexCount;
    
        };
    
    

    ModelClass.cpp的代码如下:

     

    #include "ModelClass.h"
    
    
    ModelClass::ModelClass(void) 
        { 
        m_vertexBuffer = 0; 
        m_indexBuffer = 0;
    
        }
    
    ModelClass::ModelClass(const ModelClass & others) 
        {
    
        }
    
    ModelClass::~ModelClass(void) 
        { 
        }
    
    bool ModelClass::Initialize(ID3D11Device* device) 
        { 
        bool result;
    
    
        // 初始化顶点缓冲和顶点索引缓冲. 
        result = InitializeBuffers(device); 
        if(!result) 
            { 
            return false; 
            }
    
        return true; 
        }
    
    void ModelClass::Shutdown() 
        { 
       // 释放顶点和索引缓冲. 
        ShutdownBuffers();
    
        return; 
        }
    
    void ModelClass::Render(ID3D11DeviceContext* deviceContext) 
        { 
       // 把顶点和索引缓冲放入图形管线,准备渲染. 
        RenderBuffers(deviceContext);
    
        return; 
        }
    
    int ModelClass::GetIndexCount() 
        { 
        //返回索引顶点计数 
        return m_indexCount; 
        }
    
    bool ModelClass::InitializeBuffers(ID3D11Device* device) 
        { 
        VertexType* vertices; 
        unsigned long* indices; 
        D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc; 
        D3D11_SUBRESOURCE_DATA vertexData, indexData; 
        HRESULT result;
    
        //首先,我们创建2个临时缓冲存放顶点和索引数据,以便后面使用。. 
    
        // 设置顶点缓冲大小为8,一个正方体. 
        m_vertexCount = 8;
    
        // 设置索引缓冲大小. 
       m_indexCount = 36;
    
        // 创建顶点临时缓冲. 
        vertices = new VertexType[m_vertexCount]; 
        if(!vertices) 
            { 
            return false; 
            }
    
       // 创建索引缓冲. 
        indices = new unsigned long[m_indexCount]; 
        if(!indices) 
            { 
            return false; 
            } 
       //创建顺时针方向的三角形,左手规则 
        // 设置顶点数据. 
        vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);  
        vertices[0].color = WHITE;
    
        vertices[1].position = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);  
        vertices[1].color = BLACK;
    
        vertices[2].position = D3DXVECTOR3(1.0f, 1.0f, -1.0f);  
        vertices[2].color = RED;
    
        vertices[3].position = D3DXVECTOR3(1.0f, -1.0f, -1.0f);  
        vertices[3].color = GREEN;
    
        vertices[4].position = D3DXVECTOR3(-1.0f, -1.0f, 1.0f);  
        vertices[4].color = BLUE;
    
        vertices[5].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);  
        vertices[5].color = YELLOW;
    
        vertices[6].position = D3DXVECTOR3(1.0f, 1.0f, 1.0f);  
        vertices[6].color = CYAN;
    
        vertices[7].position = D3DXVECTOR3(1.0f, -1.0f, 1.0f);  
        vertices[7].color = MAGENTA;
    
       // 设置索引缓冲数据. 
        indices[0] = 0;  // 前面 
        indices[1] = 1; 
        indices[2] = 2;  
        indices[3] = 0; 
        indices[4] = 2; 
        indices[5] = 3;  
    
        indices[6] = 4;  // 后面 
        indices[7] = 6; 
        indices[8] = 5;  
        indices[9] = 4; 
        indices[10] = 7; 
        indices[11] = 6;
    
        indices[12] = 4;  // 左面 
        indices[13] = 5; 
        indices[14] = 1;  
        indices[15] = 4; 
        indices[16] = 1; 
        indices[17] = 0;
    
        indices[18] = 3;  //右面 
        indices[19] = 2; 
        indices[20] = 6;  
        indices[21] = 3; 
        indices[22] = 6; 
        indices[23] = 7;
    
        indices[24] = 1;  // 上面 
        indices[25] = 5; 
        indices[26] = 6;  
        indices[27] = 1; 
        indices[28] = 6; 
        indices[29] = 2;
    
        indices[30] = 4; // 下面 
        indices[31] = 0; 
        indices[32] = 3;  
        indices[33] = 4; 
        indices[34] = 3; 
        indices[35] = 7;
    
    
        // 设置顶点缓冲描述 
        vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT; 
        vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount; 
        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 
        vertexBufferDesc.CPUAccessFlags = 0; 
        vertexBufferDesc.MiscFlags = 0; 
        vertexBufferDesc.StructureByteStride = 0;
    
        // 指向保存顶点数据的临时缓冲. 
        vertexData.pSysMem = vertices; 
        vertexData.SysMemPitch = 0; 
        vertexData.SysMemSlicePitch = 0;
    
        // 创建顶点缓冲. 
        result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer); 
        if(FAILED(result)) 
            { 
            return false; 
            }
    
        // 设置索引缓冲描述. 
        indexBufferDesc.Usage = D3D11_USAGE_DEFAULT; 
        indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount; 
        indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; 
        indexBufferDesc.CPUAccessFlags = 0; 
        indexBufferDesc.MiscFlags = 0; 
        indexBufferDesc.StructureByteStride = 0;
    
        // 指向存临时索引缓冲. 
        indexData.pSysMem = indices; 
        indexData.SysMemPitch = 0; 
        indexData.SysMemSlicePitch = 0;
    
        // 创建索引缓冲. 
        result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer); 
        if(FAILED(result)) 
            { 
            return false; 
            }
    
        // 释放临时缓冲. 
        delete [] vertices; 
        vertices = 0;
    
        delete [] indices; 
        indices = 0;
    
        return true; 
        }
    
    void ModelClass::ShutdownBuffers() 
        { 
        // 释放顶点缓冲. 
        if(m_indexBuffer) 
            { 
            m_indexBuffer->Release(); 
            m_indexBuffer = 0; 
            }
    
        // 释放索引缓冲 
        if(m_vertexBuffer) 
            { 
            m_vertexBuffer->Release(); 
            m_vertexBuffer = 0; 
            }
    
        return; 
        }
    
    void ModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext) 
        { 
        unsigned int stride; 
        unsigned int offset;
    
    
       // 设置顶点缓冲跨度和偏移. 
        stride = sizeof(VertexType); 
        offset = 0;
    
        //在input assemberl阶段绑定顶点缓冲,以便能够被渲染 
        deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
    
        //在input assemberl阶段绑定索引缓冲,以便能够被渲染 
        deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
    
        // 设置体元语义,渲染三角形列表. 
        deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    
        return; 
        }
    
    

    CameraClass.cpp中Render函数做了小小变动:

     

    void CameraClass::Render() 
        { 
        D3DXVECTOR3 up, position, lookAt; 
        float yaw, pitch, roll; 
        D3DXMATRIX rotationMatrix;
    
    
        // 设置up向量为(0,1,0). 
        up.x = 0.0f; 
        up.y = 1.0f; 
        up.z = 0.0f;
    
        // 设置摄像机的位置. 
        position.x = m_positionX; 
        position.y = m_positionY; 
        position.z = m_positionZ;
    
        // 设置摄像机lookat的方向. 
        //lookAt.x = 0.0f; 
        //lookAt.y = 0.0f; 
        //lookAt.z = 1.0f; 
        //设置摄像机始终指向原点 
        D3DXVec3Normalize(&lookAt, &position); 
        lookAt = lookAt * (-1);
    
       // 得到弧度单位的欧拉旋转 yaw (Y axis), pitch (X axis), 以及 roll (Z axis) . 
        pitch = m_rotationX * 0.0174532925f; 
        yaw   = m_rotationY * 0.0174532925f; 
        roll  = m_rotationZ * 0.0174532925f;
    
        // 得到旋转矩阵. 
        D3DXMatrixRotationYawPitchRoll(&rotationMatrix, yaw, pitch, roll);
    
        // 变换lookat和up向量,一般view能够在原点被正确旋转. 
        D3DXVec3TransformCoord(&lookAt, &lookAt, &rotationMatrix); 
        D3DXVec3TransformCoord(&up, &up, &rotationMatrix);
    
       // 平移旋转后的摄像机位置. 
        lookAt = position + lookAt;
    
        // 创建view矩阵. 
        D3DXMatrixLookAtLH(&m_viewMatrix, &position, &lookAt, &up);
    
        return; 
        }
    
    

    程序运行后如下图所示:

     

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规