# Landscape Lighting looks crappy

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

## Recommended Posts

Here you go: height map, normal map (has been generated by my own algorithm - no 3rd party tools, it seems to be correct), the screenshot with strange artifacts and shader code:

Look at this "quaddy" lighting - is it a usual thing about GLSL lighting? Or maybe something is wrong? Please have a look at my shaders.

VS:

 uniform sampler2DRect elevationMap; uniform sampler2DRect normalMap; uniform vec2 gridScale; uniform vec2 gridOrigin; varying vec4 diffuse,ambient; varying vec3 normal,lightDir,halfVector; void main() { vec2 pos = gl_Vertex.xy; vec2 gridPosition = gridOrigin + pos * gridScale; float elevation = texture2DRect(elevationMap,gridPosition).b; gl_Position = gl_ModelViewProjectionMatrix * vec4(gridPosition.x,elevation * 255.0,gridPosition.y,1.0); vec3 tempNormal = texture2DRect(normalMap,gridPosition).rbg; normal = normalize(gl_NormalMatrix * tempNormal); lightDir = normalize(vec3(gl_LightSource[0].position)); halfVector = normalize(gl_LightSource[0].halfVector.xyz); diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient; //light ambient ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient; //global ambient }

FS:

 varying vec4 diffuse,ambient; varying vec3 normal,lightDir,halfVector; void main() { vec3 n,halfV; float NdotL,NdotHV; vec4 color = ambient; n = normalize(normal); NdotL = max(dot(n,lightDir),0.0); if(NdotL > 0.0) { color += diffuse * NdotL; halfV = normalize(halfVector); NdotHV = max(dot(n,halfV),0.0); color += gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV, gl_FrontMaterial.shininess); } gl_FragColor = color; }

##### Share on other sites
The third picture, what is it meant to represent? What are we looking at there?

Is that the elevated geometry?

The first two images look ok to me.

##### Share on other sites

The third picture, what is it meant to represent? What are we looking at there?

Is that the elevated geometry?

The first two images look ok to me.

Ok I'll make it clear:

Look how crappy the interpolation is, I've always believed that lighting is different from Gourad Shading - and drives better quality than Gourd even with low number of trinagles because of interpolation through normals. Moreover - as you can see I have plenty of triangles here and 1 vertex = 1 normal ratio - why it looks so crappy? Is it usual effect and lighting is always so crappy - and therefore the only way to increase quality is to make more normals? Or am I doing something wrong?

##### Share on other sites
I see a few things:
Have you baked the image->normal bias into your gl_NormalMatrix? Normals are stored in the map as [0..1] values and you are using them as [-1..1] values which requires as step like this:
 // unbias the normal stored in the map effectiveNormal = texNormal * 2 - 1; 
If this is part of your gl_NormalMatrix then you are set, if not then this will skew all of your normals into the positive octant.

second you caculate and normalize the normal and light direction per-vertex in the vertex shader, but you only renormalize the normal in the fragment shader. When interpolating the normal and light direction for the fragment shader both will end up denormalized and that will change your results from the dot product.

EDIT:

Damn, on second read you are doing a pure directional light (which should imply that lightdir is the same at every vertex) so something else is amiss. Though it does seem odd that your half vector is unchanged by the view direction. The view direction to each vertex would be different causing each vertex to have a unique half vector.

##### Share on other sites
!@#\$ you're right about unpacking normals, how could I forget it Yep, I've just fixed it - the quality has changed to better but some places still look crappy. I didn't understand the second part of your post about problems in FS, could explain again?

Edit: I've taken this code from Lightgouse 3D tutorial on shader lighting btw.

##### Share on other sites
Are you averaging (smoothing) normals for each vertex?

EDIT: I may be misinterpreting how you're getting the normal.

##### Share on other sites

Are you averaging (smoothing) normals for each vertex?

When I'm creating this normal map?

##### Share on other sites

I didn't understand the second part of your post about problems in FS, could explain again?

re lightdir:
For directional lighting I was wrong about needing to normalize the light dir. It should be the same for all vertices. For point lightsources, the direction to the light would change based on the position of the vertex and thus need to be normalized in the fragment shader.

re half-vector
For your specular highlight calculation, the half vector is the vector between the light direction and direction to the eye. Your light direction is constant (directional lighting) but the eye direction changes for every vertex depending on the position of the vertex:

vs:
 vec3 eye_dir = eye_pos - gl_vertex.xyz; vec3 half_vec = normalize(eyeDir + lightDir); 

This probably isn't causing a huge quality loss though.

You may have to except some "vertexy'ness" in your lighting given that you only have 1 normal to work with per vertex and there is no way to interpolate that spherically across the fragment.

##### Share on other sites

[quote name='Haroogan' timestamp='1299860815' post='4784430']
I didn't understand the second part of your post about problems in FS, could explain again?

re lightdir:
For directional lighting I was wrong about needing to normalize the light dir. It should be the same for all vertices. For point lightsources, the direction to the light would change based on the position of the vertex and thus need to be normalized in the fragment shader.

re half-vector
For your specular highlight calculation, the half vector is the vector between the light direction and direction to the eye. Your light direction is constant (directional lighting) but the eye direction changes for every vertex depending on the position of the vertex:

vs:
 vec3 eye_dir = eye_pos - gl_vertex.xyz; vec3 half_vec = normalize(eyeDir + lightDir); 

This probably isn't causing a huge quality loss though.

You may have to except some "vertexy'ness" in your lighting given that you only have 1 normal to work with per vertex and there is no way to interpolate that spherically across the fragment.
[/quote]

Hold on, so you say that I need more than 1 normal per-vertex? Then why lots of tutorials that use teapot have perfect per-pixel lighting (without normal mapping!)?

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632566
• Total Posts
3007107

×