Sign in to follow this  
Squirell

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;
    D3DXCreateSphere(pD3DDevice, radius, LOD, LOD, &pMeshTemp, &pAdjacency);
    
    //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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Squirell
No, 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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
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?

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this