Directx11 tutorial (55) Create spherical and conical objects

Source: Internet
Author: User

In this tutorial, we create two new model classes, spheremodelclass, and cylindermodelclass, which respectively represent the spherical and conical objects.

After the program is executed, the interface is as follows:

The box frame mode interface is as follows:

From the line-frame mode, we can see that the sphere is determined by three factors: radius, longitude line, and latitude line.

In spheremodelclass. in CPP, we can see that the initialize vertex buffer and index buffer function is initializebuffers (id3d11device * device, float radius, int numslices, int numstacks), which has three more parameters, the number of radius, longitude slices, and latitude slice. In the buildstacks (vertices, indices) function, the number of latitude and longitude slices is converted into the angle in the spherical coordinate system to find the vertices in the spherical coordinate system, then convert to the Cartesian coordinate system.

The Code is as follows:

Void spheremodelclass: buildstacks (vertexlist & vertices, indexlist & indices)
{
Float phistep = PI/m_numstacks;

Int numrings = m_NumStacks-1;

// Calculate the vertex for each latitude ring.
For (INT I = 1; I <= numrings; ++ I)
{
Float Phi = I * phistep;

// Vertex on the ring
Float thetastep = 2.0f * PI/m_numslices;
For (Int J = 0; j <= m_numslices; ++ J)
{
Float Theta = J * thetastep;

Vertextype V;

// Convert the spherical coordinates to Cartesian coordinates
V. position. x = m_radius * sinf (PHI) * cosf (theta );
V. position. Y = m_radius * cosf (PHI );
V. position. z = m_radius * sinf (PHI) * sinf (theta );

D3dxvec3normalize (& V. Normal, & V. position );

// The texture coordinate of the ball
V. texture. x = Theta/(2.0f * PI );
V. texture. Y = PHI/PI;

V. Kd = d3dxvector4 (0.2, 0.2, 0.1, 1.0 );
V. Ks = d3dxvector4 (0.2, 0.2, 0.2, 1.0 );
Vertices. push_back (v );
}
}

// The pole of the ball: The texture coordinate is distorted.
Vertextype T1;
T1.position = d3dxvector3 (0.0f,-m_radius, 0.0f );
T1.normal = d3dxvector3 (0.0f,-1.0f, 0.0f );
T1.texture = d3dxvector2 (0.0f, 1.0f );
T1.kd = d3dxvector4 (0.2, 0.2, 0.1, 1.0 );
T1.ks = d3dxvector4 (0.2, 0.2, 0.2, 1.0 );

Vertices. push_back (T1 );

T1.position = d3dxvector3 (0.0f, m_radius, 0.0f );
T1.normal = d3dxvector3 (0.0f, 1.0f, 0.0f );
T1.texture = d3dxvector2 (0.0f, 0.0f );

Vertices. push_back (T1 );

Int northpoleindex = (INT) vertices. Size ()-1;
Int southpoleindex = (INT) vertices. Size ()-2;

Int numringvertices = m_numslices + 1;

// Calculate the index (ignore the pole)
For (INT I = 0; I <m_NumStacks-2; ++ I)
{
For (Int J = 0; j <m_numslices; ++ J)
{
Indices. push_back (I * numringvertices + J );
Indices. push_back (I * numringvertices + J + 1 );
Indices. push_back (I + 1) * numringvertices + J );

Indices. push_back (I + 1) * numringvertices + J );
Indices. push_back (I * numringvertices + J + 1 );
Indices. push_back (I + 1) * numringvertices + J + 1 );
}
}

// Arctic Index
For (INT I = 0; I <m_numslices; ++ I)
{
Indices. push_back (northpoleindex );
Indices. push_back (I + 1 );
Indices. push_back (I );
}

// South Pole Index
Int baseindex = (numRings-1) * numringvertices;
For (INT I = 0; I <m_numslices; ++ I)
{
Indices. push_back (southpoleindex );
Indices. push_back (baseindex + I );
Indices. push_back (baseindex + I + 1 );
}
}

In cylindermodelclass. in CPP, we can see initializebuffers (id3d11device * device, float topradius, float bottomradius, float height, int numslices, int numstacks), which has five more parameters, the radius of the top and bottom of the cone, the height, the number of longitude slices, and the number of latitude slices.

The specific calculation vertex buffer and index buffer are composed of functions. For the code of the three functions, see the source file:

Buildstacks (vertices, indices );
Buildtopcap (vertices, indices );
Buildbottomcap (vertices, indices );

Complete code can be found:

Project File mytutoriald3d11_50

Download Code:

Http://files.cnblogs.com/mikewolf2002/d3d1150-58.zip

Http://files.cnblogs.com/mikewolf2002/pictures.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.