# D3DXCreateSphere Help

## Recommended Posts

I'm trying to create a sphere for my program from D3DXCreateSphere. It creates the sphere and renders in correctly. However, I would like to texture the sphere. The way I am currently doing it does not work at all (my guess is the texture coordinates are all messed up because it renders as a solid color depending on the textures' background). How should I go about texturing this sphere? (NOTE: The textures do load in correctly it is the texturing the sphere that is messing up) Here is my current code:

bool CMesh::CreateSphere(float radius, int LOD, char* texturename)
{
LPD3DXMESH pMeshTemp;

//Generate textures, materials, and normals
NumMaterials = 1;

//Create two arrays. One to hold the materials and only to hold the textures
pMeshMaterials = new D3DMATERIAL9[NumMaterials];
pMeshTextures  = new LPDIRECT3DTEXTURE9[NumMaterials];

for(DWORD i = 0; i < NumMaterials; i++)
{
//Create basic material
pMeshMaterials[i].Diffuse.r = 1.0f;
pMeshMaterials[i].Diffuse.g = 1.0f;
pMeshMaterials[i].Diffuse.b = 1.0f;
pMeshMaterials[i].Diffuse.a = 0.0f;

pMeshMaterials[i].Ambient.r = 0.0f;
pMeshMaterials[i].Ambient.g = 0.0f;
pMeshMaterials[i].Ambient.b = 0.0f;
pMeshMaterials[i].Ambient.a = 0.0f;

pMeshMaterials[i].Specular.r = 0.0f;
pMeshMaterials[i].Specular.g = 0.0f;
pMeshMaterials[i].Specular.b = 0.0f;
pMeshMaterials[i].Specular.a = 0.0f;

pMeshMaterials[i].Emissive.r = 0.0f;
pMeshMaterials[i].Emissive.g = 0.0f;
pMeshMaterials[i].Emissive.b = 0.0f;
pMeshMaterials[i].Emissive.a = 0.0f;

// Create the texture
if(FAILED(D3DXCreateTextureFromFile(pD3DDevice, texturename, &pMeshTextures[i])))
{

pMeshTextures[i] = NULL;
}
}

//Make sure that the normals are setup for our mesh
pMeshTemp->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, pD3DDevice, &pMesh);
D3DXComputeNormals(pMesh, NULL);
return true;
}

bool CMesh::Render()
{

for(int i = 0; i < NumMaterials; i++)
{
pD3DDevice->SetMaterial(&pMeshMaterials[i]);
pD3DDevice->SetTexture(0, pMeshTextures[i]);

if(FAILED(pMesh->DrawSubset(i)))
{
Log->LogError("Failed to draw mesh");
return false;
}
}

return true;
}



##### Share on other sites
The problem is that meshes created with that function don't have texture coordinates, so you have to clone the FVF with the function... mesh.cloneFVF or something of the sort.

I honestly don't know how to do it, but that's your problem. I'll search the forums to see if I can find the answer...

EDIT: Checked my SDK (I only have the VB6 one... sorry!) but the prototype looks like this:
object.CloneMeshFVF( _     Options As Long, _     FVF As Long, _     Device As Direct3DDevice8) As D3DXMesh

Basically, you have to clone the mesh you created a new one, but be sure to include D3DFVF_TEX1 which will give it texture coordinates. And uh, don't forget all your other vertex formats (just use the FVF you're using for everything else).

I hope that works (to any degree)!

##### Share on other sites
No, its still not working, when I cloned the mesh to get the normals I included D3DFVF_TEX1 already... Any other ideas?

##### Share on other sites
Well, in the worst case scenario you can always just make a sphere in an X file and import that. It's probably not the best way to do it and I know how you probably feel. I was trying to do the same thing you are but never had much success and it wasn't vital to what I needed to do (just for debugging anyway) so I just decided to forgo it. I'm interested to see useful replies to this however.

##### Share on other sites
Quote:
 Original post by SquirellNo, its still not working, when I cloned the mesh to get the normals I included D3DFVF_TEX1 already... Any other ideas?

Cloning won't magically compute your texture coordinates [smile], you have to do it yourself. Check Robert Dunlop's X-Zone for a tutorial on spherical mapping.

##### Share on other sites
yeah check out Robert Dunlops site, I got a lot of help from his site. but other than that if you use the fvf of your cloned mesh and use a buffer pointer couldn't you lock it and just change the tex or tu-tv values?

##### Share on other sites
Quote:
 Original post by Anonymous Posteryeah check out Robert Dunlops site, I got a lot of help from his site. but other than that if you use the fvf of your cloned mesh and use a buffer pointer couldn't you lock it and just change the tex or tu-tv values?

That's exactly what he should do [smile]. He just needs to make sure he calculates his tu/tv offset into the vertex structure correctly based on the FVF.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628284
• Total Posts
2981831

• 10
• 10
• 10
• 11
• 17