Directional Lighting Fun

I’m having some issues with DirectionalLight and I’m not sure if it’s the light or my normal generation that’s the problem. I generate a normal for each vetex based on 8 surrounding points. I have the following code for generating a normal for each vertex:


      public Vector3f calculateNormal(float x, float y, float z) {
            // We'll add/subtract this to/from x and z to find our surrounding 8 points
            int vectorCalcRadius = scale;

            // Points
            Point3f a = new Point3f(x, y, z),
                  b =
                        new Point3f(
                              x,
                              terrain.getY(x, (z + vectorCalcRadius)),
                              z + vectorCalcRadius),
                  c =
                        new Point3f(
                              x + vectorCalcRadius,
                              terrain.getY(
                                    (x + vectorCalcRadius),
                                    (z + vectorCalcRadius)),
                              z + vectorCalcRadius),
                  d =
                        new Point3f(
                              x + vectorCalcRadius,
                              terrain.getY((x + vectorCalcRadius), z),
                              z),
                  e =
                        new Point3f(
                              x + vectorCalcRadius,
                              terrain.getY(
                                    (x + vectorCalcRadius),
                                    (z - vectorCalcRadius)),
                              z - vectorCalcRadius),
                  f =
                        new Point3f(
                              x,
                              terrain.getY(x, (z - vectorCalcRadius)),
                              z - vectorCalcRadius),
                  g =
                        new Point3f(
                              x - vectorCalcRadius,
                              terrain.getY(
                                    (x - vectorCalcRadius),
                                    (z - vectorCalcRadius)),
                              z - vectorCalcRadius),
                  h =
                        new Point3f(
                              x - vectorCalcRadius,
                              terrain.getY((x - vectorCalcRadius), z),
                              z),
                  i =
                        new Point3f(
                              x - vectorCalcRadius,
                              terrain.getY(
                                    (x - vectorCalcRadius),
                                    (z + vectorCalcRadius)),
                              z + vectorCalcRadius);

            // Co-planar vectors
            Vector3f ab = new Vector3f(),
                  ac = new Vector3f(),
                  ad = new Vector3f(),
                  ae = new Vector3f(),
                  af = new Vector3f(),
                  ag = new Vector3f(),
                  ah = new Vector3f(),
                  ai = new Vector3f();
            ab.sub(a, b);
            ac.sub(a, c);
            ad.sub(a, d);
            ae.sub(a, e);
            af.sub(a, f);
            ag.sub(a, g);
            ah.sub(a, h);
            ai.sub(a, i);

            // Surface normal vectors
            Vector3f bc = new Vector3f(),
                  cd = new Vector3f(),
                  de = new Vector3f(),
                  ef = new Vector3f(),
                  fg = new Vector3f(),
                  gh = new Vector3f(),
                  hi = new Vector3f(),
                  ib = new Vector3f();
            bc.cross(ab, ac);
            cd.cross(ac, ad);
            de.cross(ad, ae);
            ef.cross(ae, af);
            fg.cross(af, ag);
            gh.cross(ag, ah);
            hi.cross(ah, ai);
            ib.cross(ai, ab);
            bc.normalize();
            cd.normalize();
            de.normalize();
            ef.normalize();
            fg.normalize();
            gh.normalize();
            hi.normalize();
            ib.normalize();

            // Normalized sum of vectors
            Vector3f n = new Vector3f();
            n.add(bc);
            n.add(cd);
            n.add(de);
            n.add(ef);
            n.add(fg);
            n.add(gh);
            n.add(hi);
            n.add(ib);
            n.normalize();

            return n;
      }

as for the light…


            DirectionalLight dlight = new DirectionalLight();
            dlight.setDirection(new Vector3f(1.f, -1.f, 1.f));

and as for the material on the object…


            Material mat = new Material();
            mat.setDiffuseColor(1.f, 1.f, 1.f);
            mat.setAmbientColor(0.7f, 0.7f, 0.7f);
            mat.setLightingEnable(true);

The result is close but as you’ll see there are some issues. Notice the big white splotches on certain points in the terrain? What’s up with that?

See the white on the mountain in the foreground and the chain in the background.

http://www.janusresearch.com/~michael.wright/projects/npst/terrain_lighting_test_02.png

Closeup on the chain…

http://www.janusresearch.com/~michael.wright/projects/npst/terrain_lighting_test_01.png

Any thoughts?

Looks like specular material.
Check your material settings.

[quote]Looks like specular material.
Check your material settings.
[/quote]
Is there a default specular color? As you can see from the code sample, I’m not setting a specular component on the material.

It looks like, however, if I set specular color to black, it works as expected. Any thoughts? Should I have to set spec to black to get the expected results?

If you get the expected results, then yes :slight_smile:
Welcome to the wonderful world of graphics programming :wink:

This looks like exactly what I’m trying to do right now and can’t get working.

I’m trying to apply these kinds of shadows and lighting to a landscape based on the demo. I’ve used the same settings and code as Mike above but all I get is a very dark scene.

I’m just discovering xith and graphics programming in general so there’s probably something I’m missing (hopefully).

I’ve made sure lighting and shadows are turned on in the rendering options, added the light to the scene, added GeometryArray.NORMALS to the IndexedTriangleArray, and call calculateNormal in the initVert method. Is there anything else I need to do?

If I remove GeometryArray.NORMALS, the terrain is lit much more brightly, but flatly.

If anyone has any help or advice it would be great!