Directx11 tutorial (10) Draw a simple axis

Source: Internet
Author: User

In this tutorial, we will draw a simple coordinate axis in three-dimensional scenarios, and use red, green, and blue colors to represent the forward coordinate axes of X, Y, and Z axes.

To this end, we need to first create an axismodelclass class to represent the coordinate axis vertex.

The relationships between system classes are as follows:

The axismodelclass class is similar to the previous modelclass class. It only specifies three line segments when creating the vertex buffer and index buffer, indicating three coordinate axes.

The main code of axismodelclass. H is as follows:

# Pragma once

# Include <d3d11. h>
# Include <d3dx10math. h>
# Include "common. H"

Class axismodelclass
{
...

Void renderbuffers (id3d11devicecontext *);
// Vertex buffer and vertex index Buffer
Id3d11buffer * m_vertexbuffer, * m_indexbuffer;
Int m_vertexcount, m_indexcount;
};

The main code of axismodelclass. cpp is as follows:

# Include "axismodelclass. H"

...

Bool axismodelclass: 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 6
M_vertexcount = 6;

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

// 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;
}

// Set vertex data.
// X axis, red
Vertices [0]. Position = d3dxvector3 (0.0f, 0.0f, 0.0f );
Vertices [0]. color = Red;

Vertices [1]. Position = d3dxvector3 (10.0f, 0.0f, 0.0f );
Vertices [1]. color = Red;

// Y axis, green
Vertices [2]. Position = d3dxvector3 (0.0f, 0.0f, 0.0f );
Vertices [2]. Color = green;

Vertices [3]. Position = d3dxvector3 (0.0f, 10.0f, 0.0f );
Vertices [3]. Color = green;

// Z axis, blue
Vertices [4]. Position = d3dxvector3 (0.0f, 0.0f, 0.0f );
Vertices [4]. Color = blue;

Vertices [5]. Position = d3dxvector3 (0.0f, 0.0f, 10.0f );
Vertices [5]. Color = blue;

// SET index buffer data.
Indices [0] = 0;
Indices [1] = 1;
Indices [2] = 2;
Indices [3] = 3;
Indices [4] = 4;
Indices [5] = 5;

...
Return true;
}

Void axismodelclass: renderbuffers (id3d11devicecontext * devicecontext)
{
Unsigned int stride;
Unsigned int offset;

// Set the vertex buffer span and offset.
Stride = sizeof (vertextype );
Offset = 0;

// Bind the vertex buffer in the input assemberl phase for rendering.
Devicecontext-> iasetvertexbuffers (0, 1, & m_vertexbuffer, & stride, & offset );

// Bind the index buffer in the input assemberl phase to enable rendering.
Devicecontext-> iasetindexbuffer (m_indexbuffer, dxgi_format_r32_uint, 0 );

// Set the body meta semantics, render the line segment, and draw the coordinate axis

Note: The object element of the painting is a list of line segments.
Devicecontext-> iasetprimitivetopology (d3d11_primitive_topology_linelist );

Return;
}

To use the color macro definition, I removed the color defined in modelclass. h in the previous article, and created a common. h file,

Modelclass. H will contain common. h

The modelclass. H code is changed as follows:

# Pragma once

# Include <d3d11. h>
# Include <d3dx10math. h>
# Include "common. H"

Class modelclass
{

...

};

The code for common. H is as follows:

// Define some common colors
# 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); // 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 Code modified by graphicsclass. H is as follows:

# Pragma once

...

# Include "modelclass. H"
# Include "axismodelclass. H"
# Include "colorshaderclass. H"

...

Class graphicsclass
{
...

Modelclass * m_model;
Axismodelclass * m_axismodel;
Colorshaderclass * m_colorshader;

};

The graphicsclass. CPP Code is as follows:

# Include "graphicsclass. H"

Graphicsclass: graphicsclass (void)
{
M_d3d = 0;
M_camera = 0;
M_model = 0;
M_axismodel = 0;
M_colorshader = 0;

}

Bool graphicsclass: Initialize (INT screenwidth, int screenheight, hwnd)
{
...

// Create an axis model object.
M_axismodel = new axismodelclass;
If (! M_axismodel)
{
Return false;
}
// Initialize the coordinate axis model object.
Result = m_axismodel-> initialize (m_d3d-> getdevice ());
If (! Result)
{
MessageBox (hwnd, l "cocould not initialize the axis model object.", l "error", mb_ OK );
Return false;
}

...

Return true;
}

Bool graphicsclass: frame ()
{
Bool result;

// Call the render function to render 3D scenes
// Render is the private function of graphicsclass.
Result = render ();
If (! Result)
{
Return false;
}

Return true;
}

Bool graphicsclass: render ()
{

D3dxmatrix viewmatrix, projectionmatrix, worldmatrix;
Bool result;

// Set framebuffer to light blue.
M_d3d-> beginscene (0.0f, 0.0f, 0.5f, 1.0f );

// Obtain three matrices.
M_camera-> getviewmatrix (& viewmatrix );
M_d3d-> getworldmatrix (worldmatrix );
M_d3d-> getprojectionmatrix (projectionmatrix );

M_axismodel-> render (m_d3d-> getdevicecontext ());
// Use the shader for rendering.
Result = m_colorshader-> render (m_d3d-> getdevicecontext (), m_axismodel-> getindexcount (), worldmatrix, viewmatrix, projectionmatrix );
If (! Result)
{
Return false;
}
// Place model vertices and index Buffering in the pipeline for rendering.
M_model-> render (m_d3d-> getdevicecontext ());

// Use the shader for rendering.
Result = m_colorshader-> render (m_d3d-> getdevicecontext (), m_model-> getindexcount (), worldmatrix, viewmatrix, projectionmatrix );
If (! Result)
{
Return false;
}

// Present the image in framebuffer to the screen.
M_d3d-> endscene ();

Return true;
}

After the program is executed, as shown in:

 

Complete code can be found:

Project File mytutoriald3d11_9

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.