Directx11教程(55) 建立球形和锥形物体
本教程中,我們新建2個(gè)model class,SphereModelClass以及CylinderModelClass,分別用來(lái)表示球形和錐形物體。
程序執(zhí)行后的界面如下:
線框模式界面如下:
從線框模式可以看出,球形是由三個(gè)因素決定:半徑、經(jīng)度線、緯度線。
?????? 在SphereModelClass.cpp中,我們看到,初始化頂點(diǎn)緩沖和索引緩沖的函數(shù)為:InitializeBuffers(ID3D11Device* device,? float radius, int numSlices, int numStacks),它多了三個(gè)參數(shù),分別表示半徑、經(jīng)度切片的數(shù)量、緯度切面的數(shù)量。具體構(gòu)建球形頂點(diǎn)的操作在函數(shù)buildStacks(vertices, indices)中,主要就是把經(jīng)緯度切片的數(shù)目轉(zhuǎn)化成球坐標(biāo)系中的角度,求出球坐標(biāo)系中頂點(diǎn),再轉(zhuǎn)化到笛卡爾坐標(biāo)系中。
代碼如下:
void SphereModelClass::buildStacks(VertexList& vertices, IndexList& indices)
??? {
??? float phiStep = PI/m_NumStacks;
??? int numRings = m_NumStacks-1;
??? // 對(duì)于每個(gè)緯度環(huán),計(jì)算頂點(diǎn).
??? for(int i = 1; i <= numRings; ++i)
??????? {
??????? float phi = i*phiStep;
??????? // 環(huán)上的頂點(diǎn)
??????? float thetaStep = 2.0f*PI/m_NumSlices;
??????? for(int j = 0; j <= m_NumSlices; ++j)
??????????? {
??????????? float theta = j*thetaStep;
??????????? VertexType v;
??????????? // 球坐標(biāo)到笛卡爾坐標(biāo)的轉(zhuǎn)化
??????????? 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);
??????????? //球的紋理坐標(biāo)
??????????? 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 );
??????????? }
??????? }
??? // 球的極點(diǎn): 會(huì)出現(xiàn)紋理坐標(biāo)扭曲
???? 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;
??? // 計(jì)算索引(不考慮極點(diǎn))
??? 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);
??????????? }
??????? }
//北極點(diǎn)索引
??? for(int i = 0; i < m_NumSlices; ++i)
??????? {
??????? indices.push_back(northPoleIndex);
??????? indices.push_back(i+1);
??????? indices.push_back(i);
??????? }
//南極點(diǎn)索引
??? 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);
??????? }
??? }
????? 在CylinderModelClass.cpp中,我們看到InitializeBuffers(ID3D11Device* device,? float topRadius, float bottomRadius,???? float height, int numSlices, int numStacks),它多出了5個(gè)參數(shù),分別表示錐體的頂部圓半徑、底部圓半徑,高度、經(jīng)度切片的數(shù)量、緯度切片的數(shù)量。
具體計(jì)算頂點(diǎn)緩沖和索引緩沖由個(gè)函數(shù)組成,這三個(gè)函數(shù)的具體代碼請(qǐng)參考源文件:
buildStacks(vertices, indices);
buildTopCap(vertices, indices);
buildBottomCap(vertices, indices);
????
完整的代碼請(qǐng)參考:
工程文件myTutorialD3D11_50
代碼下載:
http://files.cnblogs.com/mikewolf2002/d3d1150-58.zip
http://files.cnblogs.com/mikewolf2002/pictures.zip
總結(jié)
以上是生活随笔為你收集整理的Directx11教程(55) 建立球形和锥形物体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [每日一题] 11gOCP 1z0-05
- 下一篇: PLC接入工业互联网解决方案