LWJGL Lighting Bug

morning

My head hurts. I have encountered a bug while implementing lighting in to my world. The light is just above the camera and was based on this tutorial: https://www.youtube.com/watch?v=P9RIIByS4y0, the shaders use i also got form this tutorial.

source: https://github.com/OskarVeerhoek/YouTube-tutorials/blob/master/src/episode_34/PerPixelLightingDemo.java
shaders (pixel_phong_lighting): https://github.com/OskarVeerhoek/YouTube-tutorials/tree/master/res/shaders

with out the lighting shader the world looks like this:
https://lh6.googleusercontent.com/4RYVt00EV4CPeYKox8gE6cxkan4A7Ro1mN9CgnNuTXI=w374-h210-p-no
https://lh6.googleusercontent.com/-7JMu-me2TJQZ2lE67a1FV9klW7wCXyf0xRbhWTfLQs=w375-h210-p-no

but when I use the shader it becomes this:
https://lh6.googleusercontent.com/H2uM4YkIeNra9qnHQMYVQPIAxl7Kc6-Rzs1SJtvoqkU=w374-h210-p-no
https://lh5.googleusercontent.com/KvzwxBY-z5YoW489LqiLzo-gybwrIRdCLjI-eRHTii0=w375-h210-p-no

The trees just have a green colour binded to them and they have normals (done by belnder). The terrain height map does not have any normals though so Q1, may this be the reason for the problems and if so how would calculate the normal positions?
If not what else may cause this to happen?

Please let me know if there is any more info needed and what might the problem be :slight_smile:

thanks in advance,
siD

Looks like the shader definitely expects a normal to work properly.

To calculate the normals of a triangle (which your landscape will lots of), use the below which is quoted from https://math.stackexchange.com/questions/305642/how-to-find-surface-normal-of-a-triangle

"The cross product of two sides of the triangle equals the surface normal. So, if V = P2 - P1 and W = P3 - P1, and N is the surface normal, then:

Nx=(Vy∗Wz)−(Vz∗Wy)
Ny=(Vz∗Wx)−(Vx∗Wz)
Nz=(Vx∗Wy)−(Vy∗Wx)"

Don’t forget to normalize it.

I have tried implementing this but I’m not sure I have done it right. There is no longer that white line thing through the world, it is all a uniform black.

This what I use to render the height map:


GL11.glMaterialf(GL_FRONT, GL_SHININESS, 120);
for (int z = 0; z < values.length - 1; z++) {

     float t0 = (float) z / (values.length - 1);
     float t1 = (float) (z + 1) / (values.length - 1);

     GL11.glBegin(GL_TRIANGLE_STRIP);
     {
          for (int x = 0; x < values[z].length; x++) {
               float s = (float) x / (values[z].length - 1);

               Vector3f normal = this.getNormal(
                    new Vector3f(x - 1, interpolateHeight(x - 1, z), z),
                    new Vector3f(x, interpolateHeight(x, z), z),
                    new Vector3f(x, interpolateHeight(x, z + 1), z + 1)
               );
               GL11.glNormal3f(normal.getX(), normal.getY(), normal.getZ());
     
               GL11.glTexCoord2f(s, t0);
               GL11.glVertex3f(x, interpolateHeight(x, z), z);
               GL11.glTexCoord2f(s, t1);
               GL11.glVertex3f(x, interpolateHeight(x, z + 1), z + 1);
          }
     }
     GL11.glEnd();
     GL11.glFlush();
}

And this to get the normal:


    private Vector3f getNormal(Vector3f p1, Vector3f p2, Vector3f p3) {

        Vector3f v = new Vector3f(
                p2.getX() - p1.getX(),
                p2.getY() - p1.getY(),
                p2.getZ() - p1.getZ()
        );
        Vector3f w = new Vector3f(
                p3.getX() - p1.getX(),
                p3.getY() - p1.getY(),
                p3.getZ() - p1.getZ()
        );

        return (Vector3f) new Vector3f(
                (v.getY() * w.getZ()) - (v.getZ() * w.getY()),
                (v.getZ() * w.getX()) - (v.getX() * w.getZ()),
                (v.getX() * w.getY()) - (v.getY() * w.getX())
        ).normalise();
    }

To my logic the normals are not where the supposed to be cause by a casting issue in getNormal() or an issue when rendering the height map.