Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Critique my normal calculations, they're backwards but work!

Threaded View

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2010
    Posts
    16

    Critique my normal calculations, they're backwards but work!

    Hi, I just recently was able to render coherent terrains after quite a long time of trying. I'm not sure why this works but it does, I simply switched the equation around:

    instead of: < what every example i've seen says to do
    normal = (v1 - v2) * (v2 - v3)



    i tried
    normal = (v1 * v2) - (v2 * v3)

    and it looks good

    why is this so? I don't understand this and it baffles me. did the forces of OpenGL come together and i have a working bug, i've been experimenting with light positioning, and it doesn't appear as if the position of my light matches what I feed it. for example. If i set my light position to:
    x = 2571.15
    y = 6000
    z = 3064.18
    ??? = 45

    it appears as if the center of the light source is closer to
    x = z = 60

    at least that is where the center of my spot_light_cutoff point is in relation to the terrain that i am drawing.

    - I am calling glLightfv after a switch to the modelview matrix, and after i translate to my camera position.


    __________________________________________________
    Another question i have is:

    Code cpp:
        GLfloat poslight[4] = {x, y, z, ??? };
        glLightfv(GL_LIGHT0, GL_POSITION, poslight );

    what is the fourth parameter for?
    __________________________________________________




    * Note, this normal list jumps by four because i'm rendering quads

    Code cpp:
    void ModelObject::CalculateNormals()
    {
        for(int i=0;i<normal_count;i+=12) {
            float v1_x = vertexes[i+0];
            float v1_y = vertexes[i+1];
            float v1_z = vertexes[i+2];
     
     
            float v2_x = vertexes[i+3];
            float v2_y = vertexes[i+4];
            float v2_z = vertexes[i+5];
     
     
            float v3_x = vertexes[i+6];
            float v3_y = vertexes[i+7];
            float v3_z = vertexes[i+8];
     
     
     
            normals[i+0] =  (v1_x * v2_x) - (v2_x * v3_x);
            normals[i+1] =  (v1_y * v2_y) - (v2_y * v3_y);
            normals[i+2] =  (v1_z * v2_z) - (v2_z * v3_z);
     
     
            normals[i+3] =  (v1_x * v2_x) - (v2_x * v3_x);
            normals[i+4] =  (v1_y * v2_y) - (v2_y * v3_y);
            normals[i+5] =  (v1_z * v2_z) - (v2_z * v3_z);
     
     
            normals[i+6] =  (v1_x * v2_x) - (v2_x * v3_x);
            normals[i+7] =  (v1_y * v2_y) - (v2_y * v3_y);
            normals[i+8] =  (v1_z * v2_z) - (v2_z * v3_z);
     
     
            normals[i+9] =  (v1_x * v2_x) - (v2_x * v3_x);
            normals[i+10] = (v1_y * v2_y) - (v2_y * v3_y);
            normals[i+11] = (v1_z * v2_z) - (v2_z * v3_z);
     
        }
     
     
    // I believe i should normalize to unit length here
     
        // Smooth the normals (for heightmap only)
        for(int i=0;i<normal_count;i+=12) {
            float n1_x = normals[i+0];
            float n1_y = normals[i+1];
            float n1_z = normals[i+2];
     
     
            float n2_x = normals[i+3];
            float n2_y = normals[i+4];
            float n2_z = normals[i+5];
     
     
            float n3_x = normals[i+6];
            float n3_y = normals[i+7];
            float n3_z = normals[i+8];
     
     
            float n4_x = normals[i+9];
            float n4_y = normals[i+10];
            float n4_z = normals[i+11];
     
            float n1 = n1_x + n2_x + n3_x + n4_x;
            n1 /= 4;
            normals[i+0] = normals[i+3] = normals[i+6] = normals[i+9] = n1;
     
            float n2 = n1_y + n2_y + n3_y + n4_y;
            n2 /= 4;
            normals[i+1] = normals[i+4] = normals[i+7] = normals[i+10] = n2;
     
            float n3 = n1_z + n2_z + n3_z + n4_z;
            n3 /= 4;
            normals[i+2] = normals[i+5] = normals[i+8] = normals[i+11] = n3;
     
     
     
        }
    }

    thank you!
    Last edited by Dark Photon; 11-01-2012 at 05:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •