1. 程式人生 > >Directx11教程(7) 畫一個顏色立方體

Directx11教程(7) 畫一個顏色立方體

each 做了 files inline mis text 技術分享 new 完整

原文:Directx11教程(7) 畫一個顏色立方體

      前面教程我們通過D3D11畫了一個三角形,本章我們將畫一個顏色立方體,它的立體感更強。主要的變動是ModelClass類,在ModelClass中定義一個立方體需要的頂點信息,然後創建頂點緩沖和索引緩沖。

     在ModelClass.h中,我們定義一些宏來表示顏色,以便後面給頂點顏色屬性賦值時用。

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);

ModelClass.cpp的主要代碼如下

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;

    return true;
    }

CameraClass.cpp中Render函數做了小小變動,使得攝像機始終指向原點:

void CameraClass::Render()
    {

    // 設置攝像機的位置.
    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;

 

    return;
    }

程序運行後如下圖所示:

技術分享圖片

完整的代碼請參考:

工程文件myTutorialD3D11_6

代碼下載:

http://files.cnblogs.com/mikewolf2002/myTutorialD3D11.zip

Directx11教程(7) 畫一個顏色立方體