• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
dxCUDA

Directx 10 Normal Map issues

6 posts in this topic

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
[img]http://www.uploadimage.co.uk/images/844323normalFail.png[/img]
0

Share this post


Link to post
Share on other sites
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:

[code]//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));[/code]

This was taken from a lesson on normal maps for direct3d 11, maybe you could find it usefull
[url="http://www.braynzarsoft.net/index.php?p=D3D11NORMMAP"]http://www.braynzars...?p=D3D11NORMMAP[/url]

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:

[code]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);
}[/code]

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
0

Share this post


Link to post
Share on other sites
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 ;]
0

Share this post


Link to post
Share on other sites
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?



[url="http://postimage.org/image/tyta0640t/"][img]http://s14.postimage.org/tyta0640t/image.jpg[/img][/url]

Thanks

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

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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!

[CODE]
//--------------------------------------------------------------------------------------
// 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 );
}
}
[/CODE]
0

Share this post


Link to post
Share on other sites
I seemed to have solved some of the problem, but it still is having an effect.
[img]http://s18.postimage.org/i5be67u4p/16_5_12.png[/img]
0

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  
Followers 0