Sign in to follow this  
smally

D3DXCreate... No Tex Coords

Recommended Posts

smally    122
Hi, I'm currently experimenting with some new shaders and would like to use the D3DXCreateBox, Sphere, Teapot etc functions for quick use. But they arn't provided with texture coords. I know how to change them into the my own Pos,Norm,Tex Declaration but how to I generate texture coordinates for them? I can make do with defining tex coords for the box, but the others are kinda difficult.

Share this post


Link to post
Share on other sites
Tom KQT    1704
Also cylinder should be easy, but you're right, for sphere and even teapot, it is a problem ;)
There are more ways how to map textures (that means how to generate texcoords) on such complicated surfaces. Man understands this problem better after messing around with some 3D modelling SW (3ds max, Maya etc.).
The link Gage64 gave you shows one method for a sphere, which seems to be the simplest one. Which of course brings some drawbacks, for instance a nasty looking "North and South Poles".

Share this post


Link to post
Share on other sites
smally    122
Thanks fo the link, very nice start off.

This is what I've got so far:
LPD3DXMESH d3d::CreateMappedSphere(float radius, unsigned int slices, unsigned int stacks, IDirect3DDevice9* device)
{
LPD3DXMESH outMesh = 0;

D3DXCreateSphere(device, radius, slices, stacks, &outMesh, 0);

D3DVERTEXELEMENT9 elements[MAXD3DDECLLENGTH];
unsigned int numElements = 0;
VertPosNormTex::Decl->GetDeclaration(elements, &numElements);

LPD3DXMESH temp = 0;
outMesh->CloneMesh(D3DXMESH_SYSTEMMEM, elements, device, &temp);

SAFE_RELEASE(outMesh);

VertPosNormTex* verts = 0;
temp->LockVertexBuffer(0, (void**)&verts);

for (DWORD i = 0; i < temp->GetNumVertices(); ++i)
{
float u = asinf(verts[i].n.x) / PI + 0.5f;
float v = asinf(verts[i].n.y) / PI + 0.5f;

verts[i].t0.x = u;
verts[i].t0.y = v;
}

temp->UnlockVertexBuffer();

temp->CloneMesh(D3DXMESH_MANAGED | D3DXMESH_WRITEONLY, elements, device, &outMesh);

SAFE_RELEASE(temp);

return outMesh;
}

which shows this: http://img167.imageshack.us/img167/6832/sphericaltexkr1.jpg

I've also found another version in a book (Intro to Direct3D game programming, Frank D. Luna)
for (DWORD i = 0; i < temp->GetNumVertices(); ++i)
{
D3DXVECTOR3 p = verts[i].p;

float a = atan2f( p.z, p.x );
float b = acosf( p.y / sqrtf(p.x*p.x + p.y*p.y + p.z*p.z) );

float u = a / (2.0f*PI) ;
float v = b / PI;

verts[i].t0.x = u;
verts[i].t0.y = v;
}


which shows this: http://img167.imageshack.us/img167/1817/sphericaltex1ng8.jpg

fortunately that squashed part only happens on that side. Does anyone have any more resources, particular for the teapot

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