Game Development Community

Sphere Mesh, advice on how to texture and rotate it

by Ryan Mick · in Torque 3D Professional · 03/31/2010 (2:51 pm) · 5 replies

I am looking into applying a texture to a sphere mesh that is being rendered in a gui control. THe code that actually renders the mesh is:
const Sphere::TriangleMesh *sphereMesh = sphere.getMesh(mSphereDetail); 
	S32 numPoly = sphereMesh->numPoly;
	S32 totalPoly = 0;

	GFXVertexBufferHandle<GFXVertexPC> verts(GFX, numPoly*3, GFXBufferTypeVolatile);
	verts.lock();
	S32 vertexIndex = 0;
	for (S32 i=0; i<numPoly; i++)
	{
		totalPoly++;

		verts[vertexIndex].point = sphereMesh->poly[i].pnt[0];
		verts[vertexIndex].color = mSphereColor;
		vertexIndex++;

		verts[vertexIndex].point = sphereMesh->poly[i].pnt[1];
		verts[vertexIndex].color = mSphereColor;
		vertexIndex++;

		verts[vertexIndex].point = sphereMesh->poly[i].pnt[2];
		verts[vertexIndex].color = mSphereColor;
		vertexIndex++;
	}
	verts.unlock();

	GFXStateBlockDesc desc2;
	desc2.setCullMode(GFXCullNone);
	desc2.zEnable = false;
	desc2.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);

	GFX->setStateBlockByDesc( desc2 );
	GFX->setupGenericShaders( GFXDevice::GSColor );

	GFX->setVertexBuffer( verts );
	GFX->drawPrimitive( GFXTriangleList, 0, totalPoly );

What I want to do is first apply a texture to the mesh, which I think can be done by calling GFX->setTexture before calling drawPrimitives but I am not sure. I looked through the source code for an example of this but could not find any.

Secondly, after the texture is applied I want to rotate the sphere mesh. The rotation will eventually be based on the players transform.

The end result I am trying to achieve is to create a compass/artificial horizon effect for the updated GuiRadarTSCtrl. Any help in pointing me in the right direction would be greatly appreciated.

Thanks,
Ryan

#3
09/07/2010 (11:45 pm)
From memory the sphere generator doesn't do texture coordinates. But assuming you can generate those, you'd want to change your vertex buffer to a GFXVertexPCT (Point-Colour-TexCoord) and set the uvs per vertex, then ensure your state block has samplers enabled and use GFX->setTexture to set the texture prior to drawing the primitives.

#4
09/08/2010 (4:55 pm)
@Ryan

My suggestion would be to have an artist create a mesh and instead use TSShapeInstance to render your compass from your GUI control.
#5
09/08/2010 (9:21 pm)
@Daniel, thanks for the tip. I think I can determine the texture coordinates with a little work.

@Tom, interesting idea. Are there any examples in the code currently where this is done? I like to learn by example, so if you can point me in the right direction that would be great. Looks like guiShapeEdPreview may have some good examples though.