terrain and model lighting problem ?


Is anyone else seeing a lighting difference between models and GeoMip Terrain ?

I’m including two pictures, first one shows a model on a terrain (no texture) with directional light at azimuth 90 deg ( vector pointing from right to left ), the model looks like it is shaded correctly but my terrain is shaded on the opposite side. The second picture is for azimuth -90 ( vector pointing from left to right). Azimuth values of 0 and 180 look correct, it’s just that it appears that there are two light sources that will rotate in opposite directions as I change the azimuth of the light source. I’ve looked at different models and get the same results. wondering if I got flipped normals, but then culling looks good.

It seems that your model’s normals are flipped? Which normals actually look correct based on the position of the light?


Thanks for the reply.

The model looks correct for the light source direction.
The camera is facing along positive Y direction (North) and the model is facing along negative Y direction (South), the directional light vector is set pointing -1. 0. 0. (shining from east to west). In the second picture I point the light in the opposite direction. It appears that the terrain is lit the wrong way. Also note that for light vectors of (0. 1. 0., pointing North) and (0. -1. 0., pointing South) both terrain and model are shaded correctly, but if you rotate that light vector then shading moves in opposite directions for the model and terrain, with the model being the correct shading for the given light direction.

thanks for your help,


I think I might be on to something. I was looking at the Panda GeoMipTerrain.cxx get_normal function and noticed that the x component calculation would give the opposite expected result.

double drx = get_pixel_value(px, y) - get_pixel_value(nx, y);

In the equation above, if the next pixel to the right (px) has a z value that is less than the pixel to the left (nx) then the result x component would be negative, but in this situation you would want the x component to be positive, pointing in the positive x direction. So I changed my copy of the GeoMipTerrain.cxx
to do:

double drx =  get_pixel_value(nx, y) - get_pixel_value(px, y);

This fixes my shading problem, now both terrain and models look correct for all azimuths of directional light.

Let me know if I’m doing right or not…

here is the modified get_normal()

LVector3f GeoMipTerrain::
get_normal(int x, int y) {
  int nx = x - 1;
  int px = x + 1;
  int ny = y - 1;
  int py = y + 1;
  if (nx < 0) nx++;
  if (ny < 0) ny++;
  if (px >= int(_xsize)) px--;
  if (py >= int(_ysize)) py--;
  //double drx = get_pixel_value(px, y) - get_pixel_value(nx, y);
  double drx =  get_pixel_value(nx, y) - get_pixel_value(px, y);
  double dry = get_pixel_value(x, py) - get_pixel_value(x, ny);
  LVector3f normal(drx * 0.5, dry * 0.5, 1);

  return normal;

Hmm, maybe you’re right. I’m afraid to change it, though. and break games that rely on the current behaviour.

Heh I remember being confused about why my lighting seemed to be flipped over the x axis. I gave up trying to figure out what I was doing wrong and made the sun orbit around the x axis along the y so that the x normal stayed at 90 degrees. :laughing: