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

Directx11教程(7) 画一个颜色立方体
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码 

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说Directx11教程(7) 画一个颜色立方体,希望能够帮助大家进步!!!

原文:
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;
    }

程序运行后如下图所示:

image

完整的代码请参考:

工程文件myTutorialD3D11_6

代码下载:

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

posted on
2019-05-06 02:10
NET未来之路 阅读(
...) 评论(
...)
编辑
收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10817464.html

本文来源weixin_30595035,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/29851

发表评论