日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Directx11教程(55) 建立球形和锥形物体

發(fā)布時(shí)間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Directx11教程(55) 建立球形和锥形物体 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本教程中,我們新建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)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。