• 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
Corefanatic

DX11
Normal Interpolation issues on my generated terrain

9 posts in this topic

Hi all,
I am having a weird problem with normals on my generated terrain. I am not sure whether is it is shader or mesh issue, but here is how it looks:
 
 
 
 
Dx11FW%202013-01-22%2022-51-45-78.png
 
As you can see, I get this pattern along the edges of the triangles. This reminds me of per vertex shading  however I am aiming for per pixel shading.
 
Here are my vertex and pixel shaders:

 

VS:

cbuffer cbToProjection
{
	float4x4 matToProj;
}

struct VS_IN
{
	float4 Position : POSITION;
	float3 Normal: NORMAL;
};

struct VS_OUT
{
	float4 Position : SV_POSITION;
	float3 NormalWS: TEXCOORD1;
};

VS_OUT main(VS_IN IN)
{
	VS_OUT OUT;
	OUT.Position = mul(IN.Position,matToProj);
	OUT.NormalWS = normalize(IN.Normal);
	return OUT;
}

 

PS:

struct PS_IN
{
	float4 Position : SV_POSITION;
	float3 NormalWS: TEXCOORD1;
};

float4 main(PS_IN IN): SV_TARGET0 {
	float3 normal = normalize(IN.NormalWS);
	float3 toLight = normalize(float3(1,3,-2));
	float NDotL = saturate(dot(toLight,normal));
	float4 color = float4(1.0f,1.0f,1.0f,1.0f);	
	color.rgb *= NDotL;
	return color;
}

 

 

 

so what am I doing wrong? 

0

Share this post


Link to post
Share on other sites

From your shader code, it looks like you are doing this correctly (although you could remove the normalize call on the normal vector in the VS since you renormalize in the PS after rasterization).  My guess is that your terrain is defining three vertices for each triangle face, rather than one vertex at each grid point.  You can verify this by checking the number of vertices  you are passing in with your draw call, or you can also check this with PIX/Graphics Debugger to see how many primitives are generated from how many input vertices.

0

Share this post


Link to post
Share on other sites
From your shader code, it looks like you are doing this correctly (although you could remove the normalize call on the normal vector in the VS since you renormalize in the PS after rasterization).  My guess is that your terrain is defining three vertices for each triangle face, rather than one vertex at each grid point.  You can verify this by checking the number of vertices  you are passing in with your draw call, or you can also check this with PIX/Graphics Debugger to see how many primitives are generated from how many input vertices.

 

So, I moved the rendering into indexed rendering, so there is only one normal per vertex and still get same result. 

0

Share this post


Link to post
Share on other sites

Have you tried with different light directions? The dark areas simply look like they are dark because they are facing away from the parallel light you have hard-coded in your shader (ie. the dark sections are always on the -z axis in your image).

 

It may also be worth outputing the normal to the frame buffer so you can visually see any issues with the interpolation.

 

eg: change your color computation (in the pixel shader to) to:

 

color = normal * 0.5f + 0.5f;

return color;

 

You can also change your light vector so that it's always directly above the terrain (float3 toLight = float3( 0.0f, 1.0f, 0.0f );) which should give you a more even lighting across the terrain, again helping to see any issue with the normals. With the off-center light angle, it makes it difficult to say what is wrong really sorry :) But, certainly, your shader code looks fine. If it isn't the light direction confusing you, then it maybe the normals themselves.

 

My first guess is just that the light is at an angle really ;)

 

n!

0

Share this post


Link to post
Share on other sites

It could also be that I'm misunderstanding what you're complaining about.

 

Sometimes people build their terrain such that the vertices look like:

 

pW3fUDy.jpg

 

Whereas you can avoid some artifacts on terrain lighting if you structure your vertices like:

 

ZO7pxKa.jpg

 

n!

0

Share this post


Link to post
Share on other sites
From your shader code, it looks like you are doing this correctly (although you could remove the normalize call on the normal vector in the VS since you renormalize in the PS after rasterization).  My guess is that your terrain is defining three vertices for each triangle face, rather than one vertex at each grid point.  You can verify this by checking the number of vertices  you are passing in with your draw call, or you can also check this with PIX/Graphics Debugger to see how many primitives are generated from how many input vertices.

 

So, I moved the rendering into indexed rendering, so there is only one normal per vertex and still get same result. 

 

That may or may not mean that there is exactly one vertex normal being used at each grid point.  How many vertices are in your vertex buffer, how many indices in your index buffer, and how many primitives are you drawing?  Compare that with your grid size and make sure that you only have N+1 x N+1 vertices for a grid of size N x N.

0

Share this post


Link to post
Share on other sites

Have you tried with different light directions? The dark areas simply look like they are dark because they are facing away from the parallel light you have hard-coded in your shader (ie. the dark sections are always on the -z axis in your image).

 

It may also be worth outputing the normal to the frame buffer so you can visually see any issues with the interpolation.

 

eg: change your color computation (in the pixel shader to) to:

 

color = normal * 0.5f + 0.5f;

return color;

 

You can also change your light vector so that it's always directly above the terrain (float3 toLight = float3( 0.0f, 1.0f, 0.0f );) which should give you a more even lighting across the terrain, again helping to see any issue with the normals. With the off-center light angle, it makes it difficult to say what is wrong really sorry smile.png But, certainly, your shader code looks fine. If it isn't the light direction confusing you, then it maybe the normals themselves.

 

My first guess is just that the light is at an angle really ;)

 

n!

 

Setting the light to 0,1,0 doesn't help, the problem persists. Rendering the normals into the frame buffer shows the same problem.

 

 

 

It could also be that I'm misunderstanding what you're complaining about.

 

Sometimes people build their terrain such that the vertices look like:

 

pW3fUDy.jpg

 

Whereas you can avoid some artifacts on terrain lighting if you structure your vertices like:

 

ZO7pxKa.jpg

 

n!

 

I am building the mesh the way shown in the first picture, will try the other way that thanks.

 

 

From your shader code, it looks like you are doing this correctly (although you could remove the normalize call on the normal vector in the VS since you renormalize in the PS after rasterization).  My guess is that your terrain is defining three vertices for each triangle face, rather than one vertex at each grid point.  You can verify this by checking the number of vertices  you are passing in with your draw call, or you can also check this with PIX/Graphics Debugger to see how many primitives are generated from how many input vertices.

 

So, I moved the rendering into indexed rendering, so there is only one normal per vertex and still get same result. 

 

That may or may not mean that there is exactly one vertex normal being used at each grid point.  How many vertices are in your vertex buffer, how many indices in your index buffer, and how many primitives are you drawing?  Compare that with your grid size and make sure that you only have N+1 x N+1 vertices for a grid of size N x N.

 

For a 16x16 grid, there are 256 vertices, index buffer holds 1350 indices

0

Share this post


Link to post
Share on other sites

It could also be that I'm misunderstanding what you're complaining about.

 

Sometimes people build their terrain such that the vertices look like:

 

pW3fUDy.jpg

 

Whereas you can avoid some artifacts on terrain lighting if you structure your vertices like:

 

ZO7pxKa.jpg

 

n!

 

So, I tried building the mesh as in the second image, unfortunately the problems did not go away. 

 

16x16 mesh:

 

Dx11FW%202013-01-24%2022-50-58-53.png

 

normals:

 

Dx11FW%202013-01-24%2023-01-47-90.png

0

Share this post


Link to post
Share on other sites
Hey, thanks for the images. The lighting in the first one shows your problem much clearer for me.It looks like your normals are incorrect. For terrain normals I take the height sample x,y and compute the normal for that sample with:

float h0 = GetSample( x + 0, y - 1 );
float h1 = GetSample( x - 1, y + 0 );
float h2 = GetSample( x + 1, y + 0 );
float h3 = GetSample( x + 0, y + 1 );

Vector3 normal;

normal.x = h1 - h2;
normal.y = separation; // separation = distance between samples (I use 1.0f).
normal.z = h0 - h3;

normal.Normalize();

return normal;

If that doesn't help, perhaps posting your normal calculation code?

n! Edited by nfactorial
0

Share this post


Link to post
Share on other sites

Hey, thanks for the images. The lighting in the first one shows your problem much clearer for me.It looks like your normals are incorrect. For terrain normals I take the height sample x,y and compute the normal for that sample with:

float h0 = GetSample( x + 0, y - 1 );
float h1 = GetSample( x - 1, y + 0 );
float h2 = GetSample( x + 1, y + 0 );
float h3 = GetSample( x + 0, y + 1 );

Vector3 normal;

normal.x = h1 - h2;
normal.y = separation; // separation = distance between samples (I use 1.0f).
normal.z = h0 - h3;

normal.Normalize();

return normal;

If that doesn't help, perhaps posting your normal calculation code?

n!

 

Are you using y-up for your normals, but z-up for your sample locations in this response?

 

EDIT:  Also, I think your y value for the normal should be 2 * the separation.  According to this post, at least http://www.gamedev.net/topic/163625-fast-way-to-calculate-heightmap-normals/

Edited by Vexal
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

  • Similar Content

    • By lonewolff
      Hi Guys,
      I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
      Any advice would be greatly appreciated
    • By MarcusAseth
      bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }  
      As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
      I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
      How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
      Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device) 
    • By ProfL
      Can anyone recommend a wrapper for Direct3D 11 that is similarly simple to use as SFML? I don't need all the image formats etc. BUT I want a simple way to open a window, allocate a texture, buffer, shader.
    • By lucky6969b
      Q1:
      Since there is no more fixed pipeline rendering in DX11, for every part of rendering in DX11, do I need to create a brand-new vertex shader and pixel shader... or at least I have to find one relevant online. If you work on skinned meshes and other effects originally worked in DX9 fixed pipeline, do I have to rework everything by now?
       
      Q2:
      For assimp, if it originally was designed for DX9, like it is coupled to a DX9 device for creating meshes and materials etc. Do I have to add in the DX11 device in the assimp, or can I just leave the assimp to remain in DX9 and after the meshes are loaded, I just convert the vertex buffers and index buffers into DX11 buffers?
      Thanks
      Jack
    • By MarcusAseth
      This header is mentioned in the book I'm reading but there is no documentation on msdn... Is it like an... outdated and abandoned header?
      If so, what's the current default/recomended library for handling errors with directX?
  • Popular Now