Directx11 tutorial (19) Draw a simple terrain

Source: Internet
Author: User

We usually define a two-dimensional grid in the xz plane, and then the values of Y are calculated based on certain functions, such as the combination of sine and Cosine functions, you can get a seemingly good terrain or surface effect. In this tutorial, we modify modelclass. h and modelclass. cpp to obtain an approximate terrain.

In the code in this chapter, we define 300*300 = 90000 vertices, a total of (300-1) (300-1) * Two triangles, each of which has a size of 1.

The function with the Y value is as follows:

Float modelclass: getheight (float X, float Z) const
{
Return 0.3f * (z * sinf (0.1f * x) + x * cosf (0.1f * z ));
}

The main code of modelclass. H is as follows:

# Pragma once

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

Class modelclass
{
...

Int getindexcount ();
// Calculate the Y value based on the X and Z values of the vertex.
Float getheight (float X, float Z) const;

PRIVATE:
Bool initializebuffers (id3d11device *, Int, Int, float );
Void shutdownbuffers ();
Void renderbuffers (id3d11devicecontext *);
// Vertex buffer and vertex index Buffer
Id3d11buffer * m_vertexbuffer, * m_indexbuffer;
Int m_vertexcount, m_indexcount;

};

The main code of modelclass. cpp is as follows:

Bool modelclass: Initialize (id3d11device * device, int M, int N, float dx)
{
Bool result;

// Initialize the vertex buffer and the vertex index buffer.
Result = initializebuffers (device, M, N, dx );
If (! Result)
{
Return false;
}

Return true;
}

Void modelclass: Shutdown ()
{
// Release the vertex and index buffer.
Shutdownbuffers ();

Return;
}

Float modelclass: getheight (float X, float Z) const
{
Return 0.3f * (z * sinf (0.1f * x) + x * cosf (0.1f * z ));
}

Void modelclass: render (id3d11devicecontext * devicecontext)
{
// Buffer the vertex and index into the graphic pipeline for rendering.
Renderbuffers (devicecontext );

Return;
}

Int modelclass: getindexcount ()
{
// Returns the index vertex count.
Return m_indexcount;
}

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

// Calculate the number of vertices and index vertices
// First obtain the number of triangles, and then multiply by 3 to obtain the number of vertex indexes.
M_vertexcount = m * N;
M_indexcount = (m-1) * (n-1) * 2*3;

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

Float halfwidth = (n-1) * DX * 0.5f;
Float halfdepth = (m-1) * DX * 0.5f;

For (INT I = 0; I <m; ++ I)
{
Float z = halfdepth-I * DX;
For (Int J = 0; j <n; ++ J)
{
Float x =-halfwidth + J * DX;

// Calculate the Z value.
Float y = getheight (x, z );

Vertices [I * n + J]. Position = d3dxvector3 (x, y, z );

// Define the color based on the height
If (Y <-10.0f)
Vertices [I * n + J]. Color = beach_sand;
Else if (Y <5.0f)
Vertices [I * n + J]. Color = light_yellow_green;
Else if (Y <12.0f)
Vertices [I * n + J]. Color = dark_yellow_green;
Else if (Y <20366f)
Vertices [I * n + J]. Color = darkbrown;
Else
Vertices [I * n + J]. Color = white;
}
}

// Create an index buffer.
Indices = new unsigned long [m_indexcount];
If (! Indices)
{
Return false;
}

// Iterate each grid to calculate the index.
Int K = 0;
For (INT I = 0; I <m-1; ++ I)
{
For (Int J = 0; j <n-1; ++ J)
{
Indices [k] = I * n + J;
Indices [k + 1] = I * n + J + 1;
Indices [K + 2] = (I + 1) * n + J;

Indices [K + 3] = (I + 1) * n + J;
Indices [K + 4] = I * n + J + 1;
Indices [K + 5] = (I + 1) * n + J + 1;

K + = 6; // The next grid
}
}

// Set the vertex buffer description

...

Return true;
}

Modify the modelclass initialization code in graphicsclass. cpp:

// Initialize the model object.
Result = m_model-> initialize (m_d3d-> getdevice (), 300,300, 1.0f );
If (! Result)
{
MessageBox (hwnd, l "cocould not initialize the model object.", l "error", mb_ OK );
Return false;
}

After running the program, the effect is as follows. We can also use the/S/D/W key to move the camera to see the effect of the terrain.

We can also modify the rendering State fillmode settings in d3dclass. cpp. The effect of modifying to the box frame mode is as follows:

Modify the code in d3dclass. cpp as follows:

// Sets the grating description to specify how the polygon is rendered.
Rasterdesc. antialiasedlineenable = false;
Rasterdesc. cullmode = d3d11_cull_back;
Rasterdesc. depthbias = 0;
Rasterdesc. depthbiasclamp = 0.0f;
Rasterdesc. depthclipenable = true;
Rasterdesc. fillmode = d3d11_fill_wireframe;
Rasterdesc. frontcounterclockwise = false;
Rasterdesc. multisampleenable = false;
Rasterdesc. scissorenable = false;
Rasterdesc. slopescaleddepthbias = 0.0f;

 

Complete code can be found:

Project File mytutoriald3d11_13

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.