Matt
Matt

Reputation: 1211

OpenGL light not working

I am using this code to draw different cubes with different colors using the LWJGL:

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(rcol.x, rcol.y, rcol.z); // Color Vector
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Top)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Left Of The Quad (Top)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Top)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);             // Bottom Right Of The Quad (Top)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Top Right Of The Quad (Bottom)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Top Left Of The Quad (Bottom)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Left Of The Quad (Bottom)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Right Of The Quad (Bottom)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Right Of The Quad (Front)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Left Of The Quad (Front)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Front)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Right Of The Quad (Front)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Left Of The Quad (Back)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Right Of The Quad (Back)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Back)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Left Of The Quad (Back)

        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Right Of The Quad (Left)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);     // Top Left Of The Quad (Left)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Left Of The Quad (Left)
        GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Right Of The Quad (Left)

        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Right)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Top Left Of The Quad (Right)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Right)
        GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z);         // Bottom Right Of The Quad (Right)
    GL11.glEnd();

It draws the cube fine and the color is perfect, but as soon as I add light to it, everything turns white and looks horrible. Here is my lighting code:

GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);

float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };  // Ambient Light Values
float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };      // Diffuse Light Values
float lightPosition[] = { 20.0f, 15.0f, 20.0f, 1.0f }; // Light Position
float lightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Light Position

ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder())
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, (FloatBuffer)temp.asFloatBuffer().put(lightSpecular).flip());   
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light

Does anyone know what I am doing wrong here? Here is come example pictures: No Lighting Lighting

Upvotes: 2

Views: 536

Answers (1)

brian_d
brian_d

Reputation: 11360

You need to define the normal vector to your quads with glNormal3f. The normal vector is used for lighting calculations.

The normal vector is perpendicular to your surface. So, for example, with your first quad:

GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Right Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z);         // Top Left Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);         // Bottom Left Of The Quad (Top)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z);             // Bottom Right Of The Quad (Top)

The y coordinate is constant, so a normal vector perpendicular to this surface could
be (0, 1, 0) or (0, -1, 0).

Upvotes: 6

Related Questions