• Create Account

## Terrain not lighting correctly

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

### #1kidman171  Members

498
Like
0Likes
Like

Posted 19 October 2012 - 09:42 AM

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:

Here is the screenshot from Rastertek's tutorial:

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.

### #2Faelenor  Members

396
Like
0Likes
Like

Posted 19 October 2012 - 11:57 AM

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.

### #3BCullis  Members

1951
Like
0Likes
Like

Posted 19 October 2012 - 12:12 PM

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)

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #4Faelenor  Members

396
Like
0Likes
Like

Posted 19 October 2012 - 12:21 PM

The zip is working fine for me.

### #5BCullis  Members

1951
Like
0Likes
Like

Posted 19 October 2012 - 12:34 PM

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, 19 October 2012 - 12:35 PM.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #6kidman171  Members

498
Like
0Likes
Like

Posted 19 October 2012 - 12:47 PM

Sure here is the pixel shader:

SamplerState SampleType;
cbuffer LightBuffer
{
float4 ambientColor;
float4 diffuseColor;
float3 lightDirection;
};
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;
}



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.

### #7kidman171  Members

498
Like
0Likes
Like

Posted 19 October 2012 - 12:51 PM

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, 19 October 2012 - 12:59 PM.

### #8BCullis  Members

1951
Like
0Likes
Like

Posted 19 October 2012 - 01:41 PM

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?

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #9kidman171  Members

498
Like
0Likes
Like

Posted 19 October 2012 - 02:36 PM

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, 19 October 2012 - 02:37 PM.

### #10BCullis  Members

1951
Like
0Likes
Like

Posted 19 October 2012 - 03:56 PM

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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #11kidman171  Members

498
Like
0Likes
Like

Posted 19 October 2012 - 05:08 PM

Thanks for the advice I will do that.

### #12Daniel_RT  Members

484
Like
0Likes
Like

Posted 19 October 2012 - 06:20 PM

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

### #13kidman171  Members

498
Like
0Likes
Like

Posted 19 October 2012 - 08:25 PM

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.

### #14kidman171  Members

498
Like
0Likes
Like

Posted 19 October 2012 - 09:12 PM

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.

### #15kidman171  Members

498
Like
0Likes
Like

Posted 20 October 2012 - 11:49 AM

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

### #16Akashi  Members

293
Like
0Likes
Like

Posted 20 October 2012 - 03:59 PM

They look identical to me? What was the error?

Oh, .y, right. I gotcha'.

Edited by StoneMask, 20 October 2012 - 04:01 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.