Terrain normals messed up, don't know why?

Started by
8 comments, last by kauna 11 years, 8 months ago
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:


This is how i create my normal. I'm pretty sure the code is correct. (I skip the edges for now..)

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;
}
}


And this is how i create the normal map:

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);


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:



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)



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 :(
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Advertisement

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!
Actually i'm generating one normal map per chunk.
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

Actually i'm generating one normal map per chunk.


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!
Eventually i'm turning the normal map into a high resolution bump map so i kind of need it to work.
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

Eventually i'm turning the normal map into a high resolution bump map so i kind of need it to work.


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!
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?
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "

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?


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!
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!!!!!!!!
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
I'm glad to be able to help you.

Best regards!

This topic is closed to new replies.

Advertisement