1

I am currently learning OpenGL using lwjgl. I created a Box that is built of vertices. I also created a light class that should set the parameters of the light and finally enable the light.

I could get the light working but there is one problem. If I rotate the Box around any axis the light does not update the surface of the box. That means:

If my light is in front of my box... the frontside of my box is white and the backside of my box is dark. If I rotate my Box around 180° my new frontside should be white and my old frontside(now it is the backside) should be dark. BUT it is not! Why? Do I need to create the light on every time I render my scene again?

Here is my Code to create the light:

//creating buffers
    FloatBuffer matSpecular = BufferUtils.createFloatBuffer(4);
    matSpecular.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip(); 

    FloatBuffer lightPosition = BufferUtils.createFloatBuffer(4);
    lightPosition.put(1.0f).put(1.0f).put(1.0f).put(0.0f).flip();   

    FloatBuffer whiteLight = BufferUtils.createFloatBuffer(4);
    whiteLight.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip();  

    FloatBuffer lModelAmbient = BufferUtils.createFloatBuffer(4);
    lModelAmbient.put(0.5f).put(0.5f).put(0.5f).put(1.0f).flip();

    //-----------------

    GL11.glShadeModel(GL11.GL_SMOOTH);
    GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, matSpecular);
    GL11.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS, 50.0f);

    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, lightPosition);
    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, whiteLight);
    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, whiteLight);
    GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, lModelAmbient);

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

    GL11.glEnable(GL11.GL_COLOR_MATERIAL);
    GL11.glColorMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE);

And this is how i create draw my box:

GL11.glPushMatrix();

    GL11.glLoadIdentity();
    GL11.glTranslatef(_position.x, _position.y, _position.z);
    GL11.glRotatef(_rotation, 0, 0, 1);
    GL11.glRotatef(_rotation, 0, 1, 0);

    GL11.glColor3f(1f, 1f, 1f);

    GL11.glBegin(GL11.GL_TRIANGLES);

    for(int index : cubeIndices)
    {
        Vector3f vertex = _vertices[index];
        GL11.glVertex3f(vertex.x, vertex.y, vertex.z);
    }

    GL11.glEnd();

    GL11.glPopMatrix();
Florian
  • 5,918
  • 3
  • 47
  • 86
  • I don't know much about lighting in OpenGL, but it is a state based library so you need to update it every frame. It looks like you are though. –  Dec 30 '12 at 01:33
  • What about the normals?Do you define the surface normals somewhere? – Michael IV Dec 30 '12 at 11:40
  • no i don't. i am new at opengl... i never read about normals. – Florian Dec 30 '12 at 12:02

0 Answers0