#include"Sphere.h" Sphere::Sphere():TriangleNum(0) { } Sphere::~Sphere() { } void Sphere::Create(const D3DXVECTOR3 &pt, int Radius) { TEST temp; std::vector> iList; std::vector tTest; //float degree[] = {90.0f, 80.0f, 70.0f, 60.0f, 50.0f, 40.0f, 30.0f, 20.0f, 10.0f, 0.0f}; //float degree[] = {90.0f, 75.5f, 50.0f, 22.5f, 0.0f}; float degree[] = {90.0f, 60.0f, 30.0f, 0.0f}; //float degree[] = {90.0f, 45.0f, 0.0f}; //float degree[] = {90.0f, 0.0f}; { int tNum = sizeof(degree) / sizeof(degree[0]); int tP = ((tNum - 2)<<2) + 4; TriangleNum += ((((tP - 1)<<2) + tP)<<1); TriangleNum += ((tNum - 2)<<1) * (tP<<1); TriangleNum += (((tNum - 1)<<1) - 1)<<2; } //一番上の頂点の定義 temp.p.x = pt.x; temp.p.y = pt.y + Radius; temp.p.z = pt.z; temp.n.y = 1; temp.color = 0xffffffff; tTest.push_back(temp); iList.push_back(tTest); tTest.~vector(); //半球を定義する頂点を算出 for(int i = 1, count = sizeof(degree) / sizeof(degree[0]); i < count; i++) { float tRadius = cosf(D3DXToRadian(degree[i])) * Radius; //円の半径 float tHeight = sinf(D3DXToRadian(degree[i])) * Radius; //円のY座標 for(int j = 0, count2 = (sizeof(degree) / sizeof(degree[0])); j < count2; j++) { temp.p.x = pt.x + cosf(D3DXToRadian(degree[j])) * tRadius; temp.p.y = pt.y + tHeight; temp.p.z = pt.z + sinf(D3DXToRadian(degree[j])) * tRadius; D3DXVECTOR3 nVec; nVec.x = temp.p.x - pt.x; nVec.y = temp.p.y - pt.y; nVec.z = temp.p.z - pt.z; D3DXVec3Normalize(&temp.n, &nVec); //法線の正規化 tTest.push_back(temp); } //1象限の頂点をz座標反転 (k = 0) //4象限の頂点をx座標反転 (k = 1) //3象限の頂点をz座標反転 (k = 2) for(int k = 0; k < 3; k++){ for(int L = 0, Now = tTest.size(), count3 = sizeof(degree) / sizeof(degree[0]) - 1; L < count3; L++) { temp = tTest[Now - 2 - L]; if(k % 2 == 0){ temp.p.z = pt.z - (tTest[Now - 2 - L].p.z - pt.z); temp.n.z = -temp.n.z; }else{ temp.p.x = pt.x - (tTest[Now - 2 - L].p.x - pt.x); temp.n.x = -temp.n.x; } if(!(k == 2 && L == count3 - 1)) tTest.push_back(temp); } } iList.push_back(tTest); tTest.~vector(); } //半球を定義する頂点を全てY座標反転。(境界球全体の頂点が出る) for(int i = 1, count = iList.size(); i < count; i++) { for(int j = 0, count2 = iList[count - 1 - i].size(); j < count2; j++) { temp = iList[count - 1 - i][j]; temp.p.y = pt.y - (temp.p.y - pt.y); temp.n.y = -temp.n.y; tTest.push_back(temp); } iList.push_back(tTest); tTest.~vector(); } //真上の頂点1つと、1段下の頂点をつなぐ for(int i = 0, count = iList[1].size(); i < count; i++) { sphere.push_back(iList[0][0]); sphere.push_back(iList[1][(i + (i % 2)) % count]); sphere.push_back(iList[1][(i + ((i + 1) % 2)) % count]); sphere.push_back(iList[1][(i + ((i + 1) % 2)) % count]); if(i != count - 1){ sphere.push_back(iList[0][0]); } } //真上と真下以外の頂点をつなぐ for(int i = 1, count = iList.size(); i < count - 2; i++) { sphere.push_back(iList[i][0]); for(int j = 0, count2 = iList[i].size(); j <= count2; j++) { sphere.push_back(iList[i][j % count2]); sphere.push_back(iList[i + 1][j % count2]); } sphere.push_back(iList[i + 1][0]); } sphere.push_back(iList[iList.size() - 1][0]); //真下の頂点1つと、1段上の頂点をつなぐ for(int i = 0, count = iList[iList.size() - 2].size(), Num = iList.size(); i < count; i++) { sphere.push_back(iList[Num - 1][0]); sphere.push_back(iList[Num - 2][(i + ((i + 1) % 2)) % count]); sphere.push_back(iList[Num - 2][(i + (i % 2)) % count]); sphere.push_back(iList[Num - 2][(i + (i % 2)) % count]); if(i != count - 1){ sphere.push_back(iList[Num - 1][0]); } } } void Sphere::Draw(LPDIRECT3DDEVICE9 device) { device->SetTexture(0, NULL); device->SetFVF(MY_FVF_SPHERE); device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,TriangleNum,&sphere[0],sizeof(TEST)); }