Lighting issues [Solved] -Thank you!!!

I am having show light on my models, when light is enabled either the texture is not lighted or if not texture, the mesh is black. I do have normals set, as I set cull face to back and they render correctly. As you can see I’ve tried very little lighting thing I’ve seen so far. Also the normals have been computed via Blender 3D.


        gl.glShadeModel(GL.GL_SMOOTH);

        gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position, 0); // 3, 3, 0
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, diffuse, 0); // 1, 1, 1, 1
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambient, 0); // 0, 0, 0, 1
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, specular, 0); // 1, 1, 1, 1
        gl.glLightf(GL.GL_LIGHT0, GL.GL_LINEAR_ATTENUATION, 0.25f);

        gl.glEnable(GL.GL_LIGHTING);
        gl.glEnable(GL.GL_LIGHT0);

        gl.glDepthFunc(GL.GL_LESS);
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glColorMaterial(GL.GL_BACK, GL.GL_DIFFUSE);
        gl.glEnable(GL.GL_COLOR_MATERIAL);

        // RENDER MESH
        gl.glEnable(GL.GL_CULL_FACE);
        gl.glCullFace(GL.GL_BACK);
        gl.glMaterialfv(GL.GL_BACK, GL.GL_EMISSION, ambient, 0); // 0,0,0,1
        gl.glMaterialfv(GL.GL_BACK, GL.GL_DIFFUSE, diffuse, 0); // 1,1,1,1
        gl.glMaterialfv(GL.GL_BACK, GL.GL_SPECULAR, specular, 0); // 1,1,1,1
        gl.glMaterialf(GL.GL_BACK, GL.GL_SHININESS, shininess); // 25
        
        gl.glCallList(1);

        gl.glDisable(GL.GL_CULL_FACE);
        
        gl.glDisable(GL.GL_COLOR_MATERIAL);
        gl.glDisable(GL.GL_DEPTH_TEST);
        gl.glDisable(GL.GL_LIGHTING);

As for my mesh code:


public void init(GL gl) {
        this.setDisplayId((short) gl.glGenLists(1));
        
        gl.glNewList(this.getDisplayId(), GL.GL_COMPILE);
        
        
        gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
        
        gl.glBegin(GL.GL_TRIANGLES);
        for (int i = 0; i < faces.length; i++) {
            float[] v1 = vertices[faces[i][0]];
            float[] v2 = vertices[faces[i][1]];
            float[] v3 = vertices[faces[i][2]];
            
            /*
             * Face Point 1
             */
            if(uv != null) { 
                float[] n = uv[faces[i][0]];
                gl.glTexCoord2f(n[0], n[1]); 
            }
            if(normals != null) { 
                float[] n = normals[faces[i][0]]; 
                gl.glNormal3f(n[0], n[1], n[2]);
            }
            gl.glVertex3f(v1[0], v1[1], v1[2]);
            
            /*
             * Face Point 2
             */
            if(uv != null) { 
                float[] n = uv[faces[i][1]]; 
                gl.glTexCoord2f(n[0], n[1]); 
            }
            if(normals != null) { 
                float[] n = normals[faces[i][1]]; 
                gl.glNormal3f(n[0], n[1], n[2]);
            }
            gl.glVertex3f(v2[0], v2[1], v2[2]);

            /*
             * Face Point 3
             */
            if(uv != null) { 
                float[] n = uv[faces[i][2]]; 
                gl.glTexCoord2f(n[0], n[1]); 
            } 
            if(normals != null) { 
                float[] n = normals[faces[i][2]]; 
                gl.glNormal3f(n[0], n[1], n[2]);
            }
            gl.glVertex3f(v3[0], v3[1], v3[2]);

        }
        
        
        gl.glEnd();
        
        gl.glEndList();
    }

Okay I wrote my own normalization to check if that was the issue:


public float[] normalizeTriangle(float[] v1, float[] v2, float[] v3) {
        float[] e1 = new float[3];
        float[] e2 = new float[3];
        float[] normal = new float[3];
        // Edge 1
        e1[0] = v2[0] - v1[0];
        e1[1] = v2[1] - v1[1];
        e1[2] = v2[2] - v1[2];
        // Edge 2
        e2[0] = v3[0] - v1[0];
        e2[1] = v3[1] - v1[1];
        e2[2] = v3[2] - v1[2];
        // Cross product Edges
        normal[0] = e1[1] * e2[2] - e1[2] * e2[1];
        normal[1] = e1[2] * e2[0] - e1[0] * e2[2];
        normal[2] = e1[0] * e2[1] - e1[1] * e2[0];
        // Normalize
        float t = normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2];
        if (t != 0 && t != 1) t = (float) (1 / Math.sqrt(t));
        normal[0] *= t;
        normal[1] *= t;
        normal[2] *= t;
        return normal;
    }

And I changed the mesh’s display list to reflect the new normals:


public void init(GL gl) {
        this.setDisplayId((short) gl.glGenLists(1));
        gl.glNewList(this.getDisplayId(), GL.GL_COMPILE);
        gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
        
        gl.glBegin(GL.GL_TRIANGLES);
        for (int i = 0; i < faces.length; i++) {
            float[] v1 = vertices[faces[i][0]];
            float[] v2 = vertices[faces[i][1]];
            float[] v3 = vertices[faces[i][2]];
            float[] normal = normalizeTriangle(v1, v2, v3);
            
            /*
             * Normal Vector
             */
            gl.glNormal3f(normal[0], normal[1], normal[2]);
            
            
            /*
             * Face Point 1
             */
            if(uv != null) { 
                float[] n = uv[faces[i][0]];
                gl.glTexCoord2f(n[0], n[1]); 
            }
            gl.glVertex3f(v1[0], v1[1], v1[2]);
            
            /*
             * Face Point 2
             */
            if(uv != null) { 
                float[] n = uv[faces[i][1]]; 
                gl.glTexCoord2f(n[0], n[1]); 
            }
            gl.glVertex3f(v2[0], v2[1], v2[2]);

            /*
             * Face Point 3
             */
            if(uv != null) { 
                float[] n = uv[faces[i][2]]; 
                gl.glTexCoord2f(n[0], n[1]); 
            } 
            gl.glVertex3f(v3[0], v3[1], v3[2]);

        }
        gl.glEnd();
        gl.glEndList();
    }

With Lighting Enabled

http://files.myopera.com/sutabi/albums/251631/Screenshot-1.png

Without Lighting Enabled

http://files.myopera.com/sutabi/albums/251631/Screenshot-2.png

Try making your shininess value in your material higher. I’ve used 90 and it’s worked with me. Also, try changing the ambient color on your light to something else and try passing in your calculated normal before each vertex call (doubt this will help since you had normals before each vertex before and it still didn’t work).

Lastly, the position array that you send to the light as its position should be a 4 element array (not 3). The 4th element is 1 if you want a point or spot light and 0 if you want a direction light. If it’s a direction light, the value stored for the position is treated as the direction vector. To specify a direction with a point or spot, you use GL.GL_SPOT_DIRECTION.

Awesome, thank you very much, also I had to enable gl.Enable(gl.GL_LIGHT0), BEFORE I started defining the properties of LIGHT0.

http://files.myopera.com/sutabi/albums/251631/1Screenshot-1.png