• Advertisement
Sign in to follow this  

Terrain not lighting correctly

This topic is 1919 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

Hey guys I have been following the terrain tutorials on Rastertek.com. I'm on Tutorial #3 http://www.rastertek.com/tertut03.html where he teaches how to add basic lighting to terrain. What I have been doing is following his code examples and putting it into my code base which is basically the same as his but slightly more compact. Right Now I have a terrain class named TerrainComponent.cpp and it handles loading the terrain from a heightmap, creating the pixel and vertex shaders for the terrain, and rendering it.The problem is that my terrain is coming out completely white with no lighting at all! Here is a solid and a wire frame screenshot of my terrain:

terrainbadlighting.jpg

terrainbadlightingw.jpg

Here is the screenshot from Rastertek's tutorial:

pic0115.gif

The code is a little long so I attached a zip file containing TerrainComponent.cpp and the terrain vertex and pixel shaders.
I have compared my code to his for hours and hours and I cannot find what I am doing wrong. Please help!!

P.S. Let me know if you need to see more code.

Share this post


Link to post
Share on other sites
Advertisement
Stupid answer, but are you sure that your light direction is pointing in a right direction? Like not pointing from bottom to top for example.

Share this post


Link to post
Share on other sites
I can't open the zip file, winzip is claiming it's no openable as is. However, if your terrain is returning all-white, then the light either isn't getting factored in (so that no shading takes place), or the shader is pushing the pixel's color value up so that the return of your PS function is a float4(1,1,1,1). Can you post the pixel shader code? (without a zip attachment)

Share this post


Link to post
Share on other sites

The zip is working fine for me.


Yup, nevermind, I used a different browser and it worked fine. Blah.

Edit: OP, what are you setting ambientcolor and diffusecolor to when you call the render? Edited by BCullis

Share this post


Link to post
Share on other sites
Sure here is the pixel shader:


SamplerState SampleType;
cbuffer LightBuffer
{
float4 ambientColor;
float4 diffuseColor;
float3 lightDirection;
float padding;
};
struct PSInput
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
};
float4 PSMain( PSInput input ) : SV_TARGET
{
float3 lightDir;
float lightIntensity;
float4 color;

color = ambientColor;
lightDir = -lightDirection;
lightIntensity = saturate( dot(input.normal, lightDir) );
if( lightIntensity > 0.0f )
{
color += (diffuseColor * lightIntensity);
}
color = saturate( color );
return color;
}



And the vertex shader:

cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
struct VSInput
{
float4 position : POSITION;
float3 normal : NORMAL;
};
struct PSInput
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
};
PSInput VSMain( VSInput input )
{
PSInput output;
input.position.w = 1.0f;
output.position = mul( input.position, worldMatrix );
output.position = mul( output.position, viewMatrix );
output.position = mul( output.position, projectionMatrix );

output.normal = mul( input.normal, (float3x3) worldMatrix );
output.normal = normalize( output.normal );
return output;
}


@Faelenor: I figured it might be something simple like this. I have the same values set for the light direction as Rastertek tutorial: D3DXVECTOR3( 0.0f, 0.0f, 0.75f );
I messed around with these values and when I change the z value to a lower number the terrain gets darker but it is still one solid color.

Share this post


Link to post
Share on other sites
I have a sun class (which will hopefully one day represent the sun) and it is the same as the Light Class in the rastertek tutorial. Currently it holds the diffuse direction and ambient values. During the initialization I set these values with this code:


mpSun->setAmbientColor( 0.005f, 0.005f, 0.005f, 1.0f );
mpSun->setDiffuseColor( 1.0f, 1.0f, 1.0f, 1.0f );
mpSun->setDirection( 0.0f, 0.0f, 0.75f );


and each frame when I render the terrain I make this call:


result = mpTerrain->render
( mpCore->getContext(),
world,
view,
proj,
mpSun->getAmbientColor(),
mpSun->getDiffuseColor(),
mpSun->getDirection()
);
Edited by kidman171

Share this post


Link to post
Share on other sites
What do you get if you set direction to something like (1, -1, 1)?
Also, someone check my math: should the light direction be normalized before calculating the dot product to get a more accurate result re: angle between two vectors? Or does that matter with the followup saturate() call?

Share this post


Link to post
Share on other sites
Thanks guys for trying to help me solve my problem.
@ BCullis: I set the direction to (1, -1, 1) and it is still completely white. I also tried normalizing the normals before the dot product calculation but once again I saw no visible changes. I have a feeling the pixel shader is outputting (1, 1, 1, 1) but I don't know where the bad calculations are coming from.

Would it be helpful if I upload the entire Visual Studio solution? Edited by kidman171

Share this post


Link to post
Share on other sites
I wouldn't go that far. This is a good time to get familiar with tools like PIX, try capturing some frames with it and check the values of your vertices and the constants.

Share this post


Link to post
Share on other sites
Going to take a wild guess as I cannot see much or download the zip on my phone lol. Anyway, its a solid colour, shouldn't it be blended with the terrain colour? Sampling the terrain texture and multiply with the final colour? Also not sure if its just weird naming but intensity is a constant value that's applied to the diffuse light n dot l calculation :S

Share this post


Link to post
Share on other sites
Thanks for your input Daniel. I am not attempting to apply any textures at this point, just passing in white as the diffuse color and lighting the terrain based on a light direction. And I just followed the tutorial on Rastertek.com, he used the word intensity so I did as well, that's all I know about the word choice.

Share this post


Link to post
Share on other sites
Well I have made some discoveries using the PIX tool. The value of every normal is (0, 0, -1) going into and out of the vertex shader. As a sanity check I ran PIX with the executable from the tutorial and the normals going into the vertex shader are all different (e.g (-0.050, 0.999, 0.0) ). I definitely have something wrong with normals before they even get passed into the graphics card.

Share this post


Link to post
Share on other sites
Okay I solved it, it was just a simple typo.

Just for the record, when I was calculating the normals I had this line:

normals[index].x = (vector1[1] * vector2[2]) - (vector1[2] * vector2[1]);
normals[index].y = (vector1[2] * vector2[0]) - (vector1[0] * vector2[2]);
normals[index].y = (vector1[0] * vector2[1]) - (vector1[1] * vector2[0]);


It should be:

normals[index].x = (vector1[1] * vector2[2]) - (vector1[2] * vector2[1]);
normals[index].y = (vector1[2] * vector2[0]) - (vector1[0] * vector2[2]);
normals[index].z = (vector1[0] * vector2[1]) - (vector1[1] * vector2[0]);


It's funny how small typos can make you lose days of coding time. I guess that's the joys of programming!
Thanks to those who tried to help me anyway :)

Share this post


Link to post
Share on other sites
They look identical to me? What was the error?

Oh, .y, right. I gotcha'. Edited by StoneMask

Share this post


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

  • Advertisement