Directx11 tutorial (7) Draw a color cube

Source: Internet
Author: User

In the previous tutorial, we drew a triangle through d3d11. In this chapter, we will draw a color cube with a stronger three-dimensional effect. The main change is the modelclass class, which defines the vertex information required by a cube in modelclass, and then creates vertex buffering and index buffering.

In modelclass. H, we define some macros to represent colors, so that they can be used later to assign values to the vertex color attributes.

The modelclass. H code is as follows:

# Pragma once

# Include <d3d11. h>
# Include <d3dx10math. h>

// Define some common colors
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); // blue-green
Const d3dxvector4 magenta (1.0f, 0.0f, 1.0f, 1.0f); // Magenta

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

...

The main code of modelclass. cpp is as follows:

Bool modelclass: initializebuffers (id3d11device * device)
{
Vertextype * vertices;
Unsigned long * indices;
D3d11_buffer_desc vertexbufferdesc, indexbufferdesc;
D3d11_subresource_data vertexdata, indexdata;
Hresult result;

// First, we create two temporary buffers to store vertex and index data for later use ..

// Set the vertex buffer size to 8, a cube.
M_vertexcount = 8;

// Set the index buffer size.
M_indexcount = 36;

// Create a temporary vertex buffer.
Vertices = new vertextype [m_vertexcount];
If (! Vertices)
{
Return false;
}

// Create an index buffer.
Indices = new unsigned long [m_indexcount];
If (! Indices)
{
Return false;
}
// Create a clockwise triangle and a left-hand rule
// Set vertex data.
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;

// SET index buffer data.
Indices [0] = 0; // front
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; // left
Indices [13] = 5;
Indices [14] = 1;
Indices [15] = 4;
Indices [16] = 1;
Indices [17] = 0;

Indices [18] = 3; // right side
Indices [19] = 2;
Indices [20] = 6;
Indices [21] = 3;
Indices [22] = 6;
Indices [23] = 7;

Indices [24] = 1; // above
Indices [25] = 5;
Indices [26] = 6;
Indices [27] = 1;
Indices [28] = 6;
Indices [29] = 2;

Indices [30] = 4; // The following
Indices [31] = 0;
Indices [32] = 3;
Indices [33] = 4;
Indices [34] = 3;
Indices [35] = 7;

// Set the vertex buffer description
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;
}

In cameraclass. CPP, the render function makes a small change so that the camera always points to the origin:

Void cameraclass: render ()
{

...

// Set the camera position.
Position. x = m_positionx;
Position. Y = m_positiony;
Position. z = m_positionz;

// Set the camera lookat direction.
// Lookat. x = 0.0f;
// Lookat. Y = 0.0f;
// Lookat. z = 1.0f;
// Set the camera to always point to the origin
D3dxvec3normalize (& lookat, & position );
Lookat = lookat * (-1 );

// Obtain the Euler rotation yaw (Y axis), pitch (X axis), and roll (Z axis) of The radian unit ).
Pitch = m_rotationx * 0.0174532925f;
Yaw = m_rotationy * 0.0174532925f;
Roll = m_rotationz * 0.0174532925f;

...

 

Return;
}

As shown in figure:

Complete code can be found:

Project File mytutoriald3d11_6

Download Code:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.