Search code examples
javaopengllightingopengl-compat

Why, when i scale polygons my light intensity changes?


I need to easy change my polygon scale in OpenGL project, but for some reason, the light intensity is directly proportional to the size of the polygons.

I tried to move light direction to different places. Change light intensity and div normal vectors on scaler, but it doesn't help.

In my Code scaler mean polygon compression => if scaler = 2 , poly become 0.5 of size

Vertices coords:

verticesMatrix[i][k] = new Vector3f((float) (i - cellCount / 2) / scaler, 
                      (float) 0, (float) (k - cellCount / 2) / scaler);

Calc normals:

 public void calcNormal(Vector3f s1, Vector3f s2, Vector3f s3) {
        Vector3f a = new Vector3f(s2.x - s1.x, s2.y - s1.y, s2.z - s1.z);
        Vector3f b = new Vector3f(s3.x - s2.x, s3.y - s2.y, s3.z - s2.z);
        Vector3f normal = new Vector3f(a.y * b.z - a.z * b.y, a.z * b.x - a.x 
        * b.z, a.x * b.y - a.y * b.x);
        glNormal3f(normal.x, normal.y, normal.z);
    }

Light setup:

private static float[] lightPosition = {-2.19f, 100.36f, 11.45f, 1f};

 private static void setUpLighting() {
        glShadeModel(GL_SMOOTH);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glLightModel(GL_LIGHT_MODEL_AMBIENT, 
        BufferTools.asFlippedFloatBuffer(new float[]{0.0f, 0.0f, 0.0f, 1f}));
        glLight(GL_LIGHT0, GL_POSITION, 
        BufferTools.asFlippedFloatBuffer(lightPosition));
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
        glEnable(GL_COLOR_MATERIAL);
        glColorMaterial(GL_FRONT, GL_DIFFUSE);
    }

I need to have same light using different scaler. I don't know how it can influence on light, because i checked all code, and i don't use scaler with light. Result image: https://yadi.sk/i/kxQLvRSTP7LYVA


Solution

  • I need to normolize normal like this:

    public void calcNormal(Vector3f s1, Vector3f s2, Vector3f s3) {
            Vector3f a = new Vector3f(s2.x - s1.x, s2.y - s1.y, s2.z - s1.z);
            Vector3f b = new Vector3f(s3.x - s2.x, s3.y - s2.y, s3.z - s2.z);
            Vector3f normal = new Vector3f(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
            float length = (float)Math.pow(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z, 0.5f); 
            glNormal3f(normal.x / length, normal.y / length, normal.z / length);
        }