• Advertisement
Sign in to follow this  

Terrain normals messed up, don't know why?

This topic is 1971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to generate normal maps for my chunked terrain at run time but it's not working. The normals for the level 0 chunk seem fine, but when i move closer to the terrain everything gets messed up. I believe some of the normals are being flipped.

Here:
http://youtu.be/ETd0HPRmSoQ

This is how i create my normal. I'm pretty sure the code is correct. (I skip the edges for now..)
[CODE]
for (int z = 1; z < mChunkSamples - 1; z++)
{
for (int x = 1; x < mChunkSamples - 1; x++)
{
D3DXVECTOR3 v0(x, pVertices[x + z*mChunkSamples].y , z);
D3DXVECTOR3 v1(x + 1, pVertices[(x + 1) + z*mChunkSamples].y ,z);

D3DXVECTOR3 v2(x, pVertices[x + (z + 1)*mChunkSamples].y , z + 1);
D3DXVECTOR3 n1 = v1 - v0;
D3DXVECTOR3 n2 = v2 - v0;

D3DXVECTOR3 n;
D3DXVec3Cross(&n, &n2, &n1);
D3DXVec3Normalize(&n, &n);
pNormalmap[x + z*mChunkSamples] = n;
}
}
[/CODE]

And this is how i create the normal map:
[CODE]
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = pNormalmap;
data.SysMemPitch = mChunkSamples * 12;
data.SysMemSlicePitch = 0;

Texture2dConfig config;
config.Width = mChunkSamples;
config.Height = mChunkSamples;
config.Format = DXGI_FORMAT_R32G32B32_FLOAT;
Texture2D* pTexture = mRenderer->CreateTexture2D(config, &data);

ShaderResourceViewConfig r_config;
r_config.Format = DXGI_FORMAT_R32G32B32_FLOAT;
r_config.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
r_config.Texture2D.MipLevels = 1;
r_config.Texture2D.MostDetailedMip = 0;

pChunk->mNormalMap = mRenderer->CreateShaderResourceView(pTexture, r_config);
[/CODE]

Now i thought that there is a problem with my normal maps, so i did a simple test. I filled the normal map with the vertex positions [x,z] : [-256, 256]
When i rendered, this is what i got:

http://youtu.be/5X5g1BX4ysU

Now why does this happen?

This is what is supposed to happen: (I think since i hardcoded the shader to output the vertex position as colour)

http://youtu.be/x-dng1UD85Y

With that i conclude that the problem is with the normal map itself. I think. I also don't know why the 3rd quadrant is black?

Help me :(

Share this post


Link to post
Share on other sites
Advertisement
[quote]
Now i thought that there is a problem with my normal maps, so i did a simple test. I filled the normal map with the vertex positions [x,z] : [-256, 256]
When i rendered, this is what i got:
[/quote]

I think you just replied to your question why the one quadrant is black : negative values positions will clip to 0.

In my opinion your problem is in the texture coordinates. It seems that your normal maps wraps multiple times over the terrain when you get closer. Ie. each patch uses texture coordinates from 0,0 to 1,1. Logically each patch will then fit the whole normal map texture over the terrain patch.

So you'll need to consider the texture coordinates for each terrain patch. You may calculate them in vertex shader quite easily, using the world position for example.

Cheers!

Share this post


Link to post
Share on other sites
[quote name='Waaayoff' timestamp='1346173855' post='4974173']
Actually i'm generating one normal map per chunk.
[/quote]

Oh ok ... I see. Your right-bottom chunk seems to be always correct anyway.

Have you tried to place the normals to your vertex structure? That should work as intended.

You could also render your terrain flat by setting the y value 0.0 at the shader (with the normal map) and see what's going on with the normal map.

Cheers! Edited by kauna

Share this post


Link to post
Share on other sites
Eventually i'm turning the normal map into a high resolution bump map so i kind of need it to work.

Share this post


Link to post
Share on other sites
[quote name='Waaayoff' timestamp='1346174565' post='4974177']
Eventually i'm turning the normal map into a high resolution bump map so i kind of need it to work.
[/quote]

Of course, but sometimes you'll need to step back to see what isn't working correctly.

Have you checked that you set the normal textures correctly for each patch?

Cheers!

Share this post


Link to post
Share on other sites
Oh ok. I sent the normals with the vertex and i got the correct results.

Maybe the problem is with the negative values being clipped to 0? Edited by Waaayoff

Share this post


Link to post
Share on other sites
[quote name='Waaayoff' timestamp='1346175363' post='4974183']
Oh ok. I sent the normals with the vertex and i got the correct results.

Maybe the problem is with the negative values being clipped to 0?
[/quote]

So, the pieces of code you have shown are working correctly. I don't think that the negative values are issue here. It is just that you can't output negative color value to the screen without it getting clipped to 0.

- I could be mistaken, but it seems to me that all the patches are rendered with the same texture (hard to say, because the terrain is pretty rough), that's why I asked you to render the terrain flat with the textures. You can see pretty easily if the texture is same everywhere. Also, that's why I asked you to verify that you are setting the textures correctly.

- You could also show some shader code.

Cheers! Edited by kauna

Share this post


Link to post
Share on other sites
Oh. My. God. Kill. Me. Now.

You were right. I forgot about some debugging code i left in my renderer where i bind the shader parameters only once. Changed that and everything worked. I feel so stupid right now.

Anyway, thank you!!!!!!!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement