I am currently using Raster Tek's normal mapping tutorial after mine went haywire, but I am getting these strange mesh issues. I would assume that because the binormal and tangent calculations work per three vertices, the new mesh normals are being generated without regards to neighboring normals. I guess that the average of the surrounding normals must be calculated to generate a new central normal for every arbitary set of veritces. But the issue is, how do I know how many vertices attach to either vertex, there are an arbitrary, non uniform number of vertices.

I also noticed that in Frank.D.Lunas book Introduction to 3D Game Programming with Directx10, he does all his calculations for the normal map in the shader with no binormal/tangent calculation present in the code. I would preferably like to have all my calculations within the shader itself.

This is an image of the issue at hand:

Thanks

**0**

# Directx 10 Normal Map issues

Started by dxCUDA, Feb 12 2012 11:59 AM

6 replies to this topic

Sponsor:

###
#2
Members - Reputation: **1060**

Posted 12 February 2012 - 12:38 PM

I get the tangent of each vertex when i load my model in, then find the binormal in the pixel shaders using the interpolated tangent and normal for each vertex.

Here is the section i do the normal map stuff in my shader:

This was taken from a lesson on normal maps for direct3d 11, maybe you could find it usefull

http://www.braynzars...?p=D3D11NORMMAP

Oh yeah, to find which triangles use a vertex, first when creating your tangents for each triangle, keep track of the triangles, so that the number of tangents match with the number of triangles. then loop through each vertex in your vertex array. for each vertex, loop through each triangle. each triangle can be defined by 3 indices. if one of those 3 indices is the current vertex, then add that triangles tangent to any other triangles tangent that shares that vertex. After your finished looping through the triangles, just divide the tangent sum by the number of triangles that share that vertex, then normalize the result.

Something like this:

I can't promise the code above will work since i didn't actually try it out after i changed it from using the xna math library to using the d3dx library, but it's the idea i mean to get across

Here is the section i do the normal map stuff in my shader:

//Load normal from normal map float4 normalMap = ObjNormMap.Sample( ObjSamplerState, input.TexCoord ); //Change normal map range from [0, 1] to [-1, 1] normalMap = (2.0f*normalMap) - 1.0f; //Make sure tangent is completely orthogonal to normal input.tangent = normalize(input.tangent - dot(input.tangent, input.normal)*input.normal); //Create the biTangent float3 biTangent = cross(input.normal, input.tangent); //Create the "Texture Space" float3x3 texSpace = float3x3(input.tangent, biTangent, input.normal); //Convert normal from normal map to texture space and store in input.normal input.normal = normalize(mul(normalMap, texSpace));

This was taken from a lesson on normal maps for direct3d 11, maybe you could find it usefull

http://www.braynzars...?p=D3D11NORMMAP

Oh yeah, to find which triangles use a vertex, first when creating your tangents for each triangle, keep track of the triangles, so that the number of tangents match with the number of triangles. then loop through each vertex in your vertex array. for each vertex, loop through each triangle. each triangle can be defined by 3 indices. if one of those 3 indices is the current vertex, then add that triangles tangent to any other triangles tangent that shares that vertex. After your finished looping through the triangles, just divide the tangent sum by the number of triangles that share that vertex, then normalize the result.

Something like this:

D3DXVECTOR3 tangentSum = D3DXVECTOR3 (0.0f, 0.0f, 0.0f, 0.0f); int facesUsing = 0; float tX, tY, tZ; //Go through each vertex for(int i = 0; i < totalVerts; ++i) { //Check which triangles use this vertex for(int j = 0; j < meshTriangles; ++j) { if(indices[j*3] == i || indices[(j*3)+1] == i || indices[(j*3)+2] == i) { tX = tangentSum.x + tempTangent[j].x; tY = tangentSum.y + tempTangent[j].y; tZ = tangentSum.z + tempTangent[j].z; tangentSum = D3DXVECTOR3(tX, tY, tZ, 0.0f); //sum up face tangents using this vertex facesUsing++; } } tangentSum = tangentSum / facesUsing; D3DXVec3Normalize(&tangentSum , tangentSum); }

I can't promise the code above will work since i didn't actually try it out after i changed it from using the xna math library to using the d3dx library, but it's the idea i mean to get across

Braynzar Soft - DirectX Lessons & Game Programming Resources!

###
#3
Members - Reputation: **141**

Posted 12 February 2012 - 01:01 PM

I can semi-understand what Frank.D.Luna is doing in his shader now. Thanks a bunch, this is really helpful; I'll be sure to sub your website, fantastic resource for a newb like myself. Many thanks again, you saved me hours of painfully averaging normals with a few lines of shader code ;]

###
#4
Members - Reputation: **141**

Posted 14 February 2012 - 09:46 AM

iedoc,

I've made progress on my normal map thanks to your suggestions and help, which were greatly appreciated.

So I changed from working the bitangent and tangent out in code and just working out the tangent and then the bitangent in the shader.

However, as you can see, there are some graphical anomalies on the mesh still, especially if you look at the center of the chest. After tweaking the code I fail to see currently what the issue is or how I resolve it.

Any suggestions for what may cause something like this?

Thanks

(your website is excellent by the way, thanks for spending the time to create such a useful and informative resource

I've made progress on my normal map thanks to your suggestions and help, which were greatly appreciated.

So I changed from working the bitangent and tangent out in code and just working out the tangent and then the bitangent in the shader.

However, as you can see, there are some graphical anomalies on the mesh still, especially if you look at the center of the chest. After tweaking the code I fail to see currently what the issue is or how I resolve it.

Any suggestions for what may cause something like this?

Thanks

(your website is excellent by the way, thanks for spending the time to create such a useful and informative resource

###
#5
GDNet+ - Reputation: **319**

Posted 14 February 2012 - 06:55 PM

This is caused because there is a seam where the tangents between the faces get mirrored, so you're basically adding +1 to -1 along the seam. When you're averaging the normals between faces, you need to check if the bitangents of the two faces are facing opposite directions, and if so account for it. You can do this by either by not adding them (which will create a hard edge, but a correctly lit one), or duplicating those vertices and adding the opposite values to each faces vertex (so basically one side will get +1 added to it's vertex and the other will get -1 added).

###
#6
Members - Reputation: **141**

Posted 14 May 2012 - 08:21 AM

Currently been busy with university projects so haven't had much time to work on this issue. I believe that I have all calculations within the shader now, however, something is going wrong and I believe it is with the tangent vector. I've been experimenting with the order of operations and different methods of producing output for the bitangent, but that seems to be sound. I am certain I am heading this in the right direction, it just needs someone with experienced eyes to see the mistake I've made here somewhere.

Help would be appreciated, thanks!

Help would be appreciated, thanks!

//-------------------------------------------------------------------------------------- // Lighting //-------------------------------------------------------------------------------------- //transformation matrices matrix World; matrix View; matrix Projection; //TEXTURE VARIABLES //-------------------------------------------------------------------------------------- //color map texture Texture2D tex2D; //bump map texture Texture2D bump2D; //texture sampler state SamplerState linearSampler { Filter = min_mag_mip_linear; AddressU = Wrap; AddressV = Wrap; }; //LIGHTING STRUCTURES AND VARIABLES //-------------------------------------------------------------------------------------- struct DirectionalLight { float4 color; float3 dir; }; struct Material { float Ka, Kd, Ks, A; }; //lighting vars DirectionalLight light; Material material; float4 ambientLight; float3 eye; float4x4 gTexMtx; //RASTERIZER STATES //-------------------------------------------------------------------------------------- RasterizerState rsSolid { FillMode = Solid; CullMode = NONE; FrontCounterClockwise = false; }; //VERTEX AND PIXEL SHADER INPUTS //-------------------------------------------------------------------------------------- struct VS_INPUT { float4 p : POSITION; float2 t : TEXCOORD; float3 n : NORMAL; float3 tangent : TANGENT; }; struct PS_INPUT_PP_BLINNPHONG { float4 p : SV_POSITION; float2 t : TEXCOORD; float3 n : TEXCOORD1; float3 h : TEXCOORD2; float3 tangent : TANGENT; }; //-------------------------------------------------------------------------------------- // Blinn-Phong Lighting Reflection Model //-------------------------------------------------------------------------------------- float4 calcBlinnPhongLighting( Material M, float4 LColor, float3 N, float3 L, float3 H ) { float4 Ia = M.Ka * ambientLight; float4 Id = M.Kd * saturate( dot(N,L) ); float4 Is = M.Ks * pow( saturate(dot(N,H)), M.A ); return Ia + (Id + Is) * LColor; } //-------------------------------------------------------------------------------------- // PER PIXEL LIGHTING //-------------------------------------------------------------------------------------- PS_INPUT_PP_BLINNPHONG VS_PIXEL_LIGHTING_BLINNPHONG( VS_INPUT input ) { PS_INPUT_PP_BLINNPHONG output; //set position into clip space input.p = mul( input.p, World ); output.p = mul( input.p, View ); output.p = mul( output.p, Projection ); // Output vertex attributes for interpolation across triangle. //output.t = mul(float4(input.t, 0.0f, 1.0f), gTexMtx); //set texture coords output.t = input.t; //set required lighting vectors for interpolation float3 V = normalize( eye - (float3) input.p ); output.n = normalize( mul(input.n, (float3x3)World) ); output.h = normalize( -light.dir + V ); // Calculate the tangent vector against the world matrix only and then normalize the final value. output.tangent = mul(input.tangent, (float3x3)World); output.tangent = normalize(output.tangent); return output; } float4 PS_PIXEL_LIGHTING_BLINNPHONG( PS_INPUT_PP_BLINNPHONG input ) : SV_Target { //renormalize interpolated vectors input.n = normalize( input.n ); input.h = normalize( input.h ); //calculate lighting float4 I = calcBlinnPhongLighting( material, light.color, input.n, -light.dir, input.h ); //with texturing return I * tex2D.Sample(linearSampler, input.t); } float4 PS_NORMAL_MAP( PS_INPUT_PP_BLINNPHONG input) : SV_Target { float4 textureColor; float4 bumpMap; float3 bumpNormal; float3 bitangent; float lightIntensity; float4 color; textureColor = tex2D.Sample(linearSampler, input.t); bumpMap = bump2D.Sample(linearSampler, input.t); //renormalize interpolated vectors input.n = normalize( input.n ); input.h = normalize( input.h ); // Expand the range of the normal value from (0, +1) to (-1, +1). bumpMap = (2.0f * bumpMap) - 1.0f; //input.n = normalize( input.n ); input.tangent = normalize(input.tangent - dot(input.tangent, input.n)*input.n); bitangent = cross(input.n, input.tangent); float3x3 texSpace = float3x3(input.tangent, bitangent, input.n); // Calculate the normal from the data in the bump map. // bumpNormal = input.n + bumpMap.x * input.tangent + bumpMap.y * B; //Convert normal from normal map to texture space and store in input.normal //float3 bumpedNormal = normalize(mul(bumpMap, texSpace)); bumpNormal = normalize(mul(bumpMap, texSpace)); // Normalize the resulting bump normal. //bumpNormal = normalize(bumpNormal); input.n = normalize(mul(bumpMap, texSpace)); //lightIntensity = saturate(dot(bumpedNormal, -light.dir)); lightIntensity = saturate(dot(bumpNormal, -light.dir)); //calculate lighting float4 I = calcBlinnPhongLighting( material, light.color, input.n, -light.dir, input.h ); //with texturing return lightIntensity * I * textureColor; //return I * textureColor; } //----- //-------------------------------------------------------------------------------------- // Techniques //-------------------------------------------------------------------------------------- technique10 RENDER_PL_BLINNPHONG { pass P0 { SetVertexShader( CompileShader( vs_4_0, VS_PIXEL_LIGHTING_BLINNPHONG() ) ); SetGeometryShader( NULL ); // SetPixelShader( CompileShader( ps_4_0, PS_PIXEL_LIGHTING_BLINNPHONG() ) ); SetPixelShader( CompileShader( ps_4_0, PS_NORMAL_MAP() ) ); SetRasterizerState( rsSolid ); } }