In the DirectX mobile lighting sample, a cylinder is generated in the following manner:
for( DWORD i=0; i<50; i++ )
{
FLOAT theta = (2*D3DMX_PI*i)/(50-1);
pVertices[2*i+0].position = D3DMXVECTOR3( (float)sin(theta),-1.0f, (float)cos(theta) );
pVertices[2*i+0].normal = D3DMXVECTOR3( (float)sin(theta), 0.0f, (float)cos(theta) );
pVertices[2*i+1].position = D3DMXVECTOR3( (float)sin(theta), 1.0f, (float)cos(theta) );
pVertices[2*i+1].normal = D3DMXVECTOR3( (float)sin(theta), 0.0f, (float)cos(theta) );
}
Is there a similar way to generate vertices for a sphere in DirectX Mobile(as a triangle strip or otherwise)? (AFAIK there's no D3DMXCreateSphere method)
The final solution .Thanks to quarternion for all his help.
void CreateSphere()
{
const int iFactor = 20;
int iPos = 0;
arr_Vertices = new CUSTOMVERTEX[ui_VCount];
ui_ShapeCount = iFactor *iFactor * 2; // use when rendering
float arrV[iFactor* iFactor][3];
for (DWORD j= 0; j < iFactor; j ++)
{
FLOAT theta = (D3DMX_PI*j)/(iFactor);
for( DWORD i=0; i<iFactor; i++ )
{
iPos = j*iFactor+i;
FLOAT phi = (2*D3DMX_PI*i)/(iFactor);
arrV[iPos][0] = (float)(sin(theta)*cos(phi));
arrV[iPos][1] = (float)(sin(theta)*sin(phi));
arrV[iPos][2] = (float)(cos(theta));
/*std::cout << "[" << j <<"][" << i << "] = " << arrV[iPos][0]
<< "," << arrV[iPos][1] << "," << arrV[iPos][2] <<std::endl;*/
}
}
int iNext = 0;
for (DWORD j= 0; j < iFactor; j ++)
{
for( DWORD i=0; i<iFactor; i++ )
{
if (i == iFactor - 1)
iNext = 0;
else iNext = i +1;
iPos = (j*iFactor*6)+(i*6);
arr_Vertices[iPos].position = D3DMXVECTOR3( arrV[j*iFactor+i][0], arrV[j*iFactor+i][1], arrV[j*iFactor+i][2]);
arr_Vertices[iPos + 1].position = D3DMXVECTOR3( arrV[j*iFactor+iNext][0], arrV[j*iFactor+iNext][1], arrV[j*iFactor+iNext][2]);
if (j != iFactor -1)
arr_Vertices[iPos + 2].position = D3DMXVECTOR3( arrV[((j+1)*iFactor)+i][0], arrV[((j+1)*iFactor)+i][1], arrV[((j+1)*iFactor)+i][2]);
else
arr_Vertices[iPos + 2].position = D3DMXVECTOR3( 0, 0, -1); //Create a pseudo triangle fan for the last set of triangles
arr_Vertices[iPos].normal = D3DMXVECTOR3( arr_Vertices[iPos].position.x, arr_Vertices[iPos].position.y, arr_Vertices[iPos].position.z);
arr_Vertices[iPos + 1].normal = D3DMXVECTOR3( arr_Vertices[iPos+1].position.x, arr_Vertices[iPos+1].position.y, arr_Vertices[iPos+1].position.z);
arr_Vertices[iPos + 2].normal = D3DMXVECTOR3( arr_Vertices[iPos+2].position.x, arr_Vertices[iPos+2].position.y, arr_Vertices[iPos+2].position.z);
arr_Vertices[iPos + 3].position = D3DMXVECTOR3( arr_Vertices[iPos+2].position.x, arr_Vertices[iPos+2].position.y, arr_Vertices[iPos+2].position.z);
arr_Vertices[iPos + 4].position = D3DMXVECTOR3( arr_Vertices[iPos+1].position.x, arr_Vertices[iPos+1].position.y, arr_Vertices[iPos+1].position.z);
if (j != iFactor - 1)
arr_Vertices[iPos + 5].position = D3DMXVECTOR3( arrV[((j+1)*iFactor)+iNext][0], arrV[((j+1)*iFactor)+iNext][1], arrV[((j+1)*iFactor)+iNext][2]);
else
arr_Vertices[iPos + 5].position = D3DMXVECTOR3( 0,0,-1);
arr_Vertices[iPos + 3].normal = D3DMXVECTOR3( arr_Vertices[iPos+3].position.x, arr_Vertices[iPos+3].position.y, arr_Vertices[iPos+3].position.z);
arr_Vertices[iPos + 4].normal = D3DMXVECTOR3( arr_Vertices[iPos+4].position.x, arr_Vertices[iPos+4].position.y, arr_Vertices[iPos+4].position.z);
arr_Vertices[iPos + 5].normal = D3DMXVECTOR3( arr_Vertices[iPos+5].position.x, arr_Vertices[iPos+5].position.y, arr_Vertices[iPos+5].position.z);
//std::cout << "[" << iPos <<"] = " << arr_Vertices[iPos].position.x <<
// "," << arr_Vertices[iPos].position.y <<
// "," << arr_Vertices[iPos].position.z <<std::endl;
//std::cout << "[" << iPos + 1 <<"] = " << arr_Vertices[iPos + 1].position.x <<
// "," << arr_Vertices[iPos+ 1].position.y <<
// "," << arr_Vertices[iPos+ 1].position.z <<std::endl;
//std::cout << "[" << iPos + 2 <<"] = " << arr_Vertices[iPos].position.x <<
// "," << arr_Vertices[iPos + 2].position.y <<
// "," << arr_Vertices[iPos + 2].position.z <<std::endl;
}
}
}
Should be usable with only a few adjustments. This creates a TRIANGLELIST but could be altered to output a set of triangle strips
D3DXCreateSphere()
? At the very least you could copy the vertices it creates somewhere else if you don't want theID3DXMesh
object. MSDN Documentation here: msdn.microsoft.com/en-us/library/bb172795(VS.85).aspx – BedpostMesh.Sphere()
method for the Mobile DirectX API too? Or am I still not understanding :)? msdn.microsoft.com/en-us/library/… – Bedpost