# Glenn Conner

Member

6

1. ## problem generating texture coordinates for sphere

Thanks Dawoodoz,I assume that code works with arbitrary geometry and as such is a bit too complex for me to comprehend given my lack of experience in 3d graphics I ended up solving the problem, and I'll post my solution here just in case anyone else ever has this issue. For a sphere you can calculate the tangent and binormal given a normal and a position (x,y,z) as follows //calculate TBN per pixel instead of per vertex to get rid of artifacts at the poles of spheres float theta = acos(input.LocalPosition.z/Radius); float phi = atan2(input.LocalPosition.y/Radius,input.LocalPosition.x/Radius); theta+=D3DX_PI/2; D3DXVECTOR3 tangent(sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)); D3DXVECTOR3 binormal; D3DXVec3Cross(&binormal,&normal,&tangent); However given the geometry of the D3DX sphere you will always get tangent and binormal interpolation artifacts at the two poles on the Z axis, the only way I found to get around this was to calculate the TBN in the pixel shader i.e. per pixel instead of per vector in the mesh geometry, the hlsl for this is as follows //calculate TBN per pixel instead of per vertex to get rid of artifacts at the poles of spheres float theta = acos(input.LocalPosition.z/Radius); float phi = atan2(input.LocalPosition.y/Radius,input.LocalPosition.x/Radius); theta+=PI/2; float3 tangent = float3(sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)); float3 binormal = cross(input.Normal,tangent); using this technique generates pixel perfect tangents and binormals across the entire sphere, so my bumpmapping code finally works correctly!
2. ## problem generating texture coordinates for sphere

I've been trying to generate texture coordinates for a sphere created using D3DXCreateSphere in order to be able to add tangent/binormal data to the mesh however I've run into some problems. The tangents appear to be being calculated correctly except for one strip of the sphere where z~0 and x <=0 heres the problem with tangents shown in RGB [attachment=5420:problem.jpg] The code I'm using to create the sphere and generate the texture coordinates is as follows HR(D3DXCreateSphere(gd3dDevice, 1.0, 20,20, &mSceneMesh, 0)); // Get the vertex declaration for the NMapVertex. D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE]; UINT numElems = 0; HR(NMapVertex::Decl->GetDeclaration(elems, &numElems)); // Clone the mesh to the NMapVertex format. ID3DXMesh* clonedMesh = 0; HR(mSceneMesh->CloneMesh(mSceneMesh->GetOptions(), elems, gd3dDevice, &clonedMesh)); // lock the vertex buffer NMapVertex *pVerts; HR(clonedMesh->LockVertexBuffer(0,(void **)&pVerts)); // get vertex count int numVerts=clonedMesh->GetNumVertices(); // compute texture coordinates for (int i=0;i<numVerts;++i) { pVerts->tex0.x = (atan2(pVerts->pos.z,pVerts->pos.x)/(D3DX_PI* 2.0f)); pVerts->tex0.y = (asin(pVerts->pos.y)/D3DX_PI); ++pVerts; } // unlock the vertex buffer clonedMesh->UnlockVertexBuffer(); // Now use D3DXComputeTangentFrameEx to build the TNB-basis for each vertex // in the mesh. HR(D3DXComputeTangentFrameEx( clonedMesh, // Input mesh D3DDECLUSAGE_TEXCOORD, 0, // Vertex element of input tex-coords. D3DDECLUSAGE_BINORMAL, 0, // Vertex element to output binormal. D3DDECLUSAGE_TANGENT, 0, // Vertex element to output tangent. D3DDECLUSAGE_NORMAL, 0, // Vertex element to output normal. clonedMesh->GetOptions(), // Options 0, // Adjacency 0.01f, 0.25f, 0.01f, // Thresholds for handling errors &mSceneMesh, // Output mesh 0)); // Vertex Remapping Anyone have any ideas?
3. ## problem generating tangent/binormals for normal mapping

Hi, I've been trying to get normal mapping on a sphere working for a few days now with no success, I've narrowed the problem (i think) down to the fact that the tangent/binormal's on my mesh are all set to 0, but to my knowledge they should be being set correctly. I've torn out most of my pixel/vertex shader code so that now it just displays the binormal vector in RGB (this is how I know the binormals/tangents are always 0), however weirdly the normals are getting set correctly. Weirder still If I load a .x mesh from a file and process it, the binormals/tangents are set correctly, however if I use the same code on a mesh generated from the d3dXCreateSphere function the binormals/tangents are always 0. Any help would be much appreciated, my code is below. My vertex declaration D3DVERTEXELEMENT9 NMapVertexElements[] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0}, {0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0}, {0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, {0, 48, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, D3DDECL_END() }; HR(gd3dDevice->CreateVertexDeclaration(NMapVertexElements, &NMapVertex::Decl)); creating the mesh //This is the weird bit, If I use a mesh loaded from a .x file, and run it through this code, the tangent/binormals get set, however if I create the mesh myself using the d3dxCreateSphere function, //then the tangents/binormals don't get set properly //LoadXFile("BasicColumnScene.x", &tempMesh, mSceneMtrls, mSceneTextures); HR(D3DXCreateSphere(gd3dDevice, 1.0, 64,64, &mSceneMesh, 0)); // Get the vertex declaration for the NMapVertex. D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE]; UINT numElems = 0; HR(NMapVertex::Decl->GetDeclaration(elems, &numElems)); // Clone the mesh to the NMapVertex format. ID3DXMesh* clonedMesh = 0; HR(mSceneMesh->CloneMesh(mSceneMesh->GetOptions(), elems, gd3dDevice, &clonedMesh)); // Now use D3DXComputeTangentFrameEx to build the TNB-basis for each vertex // in the mesh. HR(D3DXComputeTangentFrameEx( clonedMesh, // Input mesh D3DDECLUSAGE_TEXCOORD, 0, // Vertex element of input tex-coords. D3DDECLUSAGE_BINORMAL, 0, // Vertex element to output binormal. D3DDECLUSAGE_TANGENT, 0, // Vertex element to output tangent. D3DDECLUSAGE_NORMAL, 0, // Vertex element to output normal. clonedMesh->GetOptions(), // Options 0, // Adjacency 0.01f, 0.25f, 0.01f, // Thresholds for handling errors &mSceneMesh, // Output mesh 0)); // Vertex Remapping
4. ## problem generating tangent/binormals for normal mapping

Thanks! I set some texture coordinates before calling D3DXComputeTangentFrameEx (don't know if I set them correctly though) and now my tangents and binormals are being set.
5. ## problem with perlin noise hlsl shader

Thanks for the suggestion, but the problem turned out to be the c++ code I was using to generate the permutation and gradient textures, I was packing the color values into the texture data in the wrong order. For the permutation texture I was doing this imageData[index] = D3DCOLOR_ARGB(BB,BA,AB,AA); when it should have been imageData[index] = D3DCOLOR_ARGB(BB,AA,AB,BA); Likewise for the gradient texture I was doing this imageData[index] = D3DCOLOR_ARGB(q,w,v,u); when it should have been imageData[index] = D3DCOLOR_ARGB(u,v,w,q);

×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!