Hello Guest

Lights

  • 6 Replies
  • 8458 Views
Lights
« on: November 10, 2003, 17:55:11 »
Hi, I started with the Nehe07 tutorial but the lights didn't work as I expected I've tried to modify the code but still darkness. What am i doing wrong?

Code: [Select]

private final static void init() throws Exception {

//texture = loadTexture("data/crate.png");

Keyboard.create(); // Create The Keyboard

GL.glEnable(GL.GL_TEXTURE_2D); // Enable Texture Mapping
GL.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background  
GL.glClearDepth(1.0); // Depth Buffer Setup
GL.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
GL.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do

GL.glMatrixMode(GL.GL_PROJECTION); // Select The Projection Matrix
GL.glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(45.0f, (float) Display.getWidth() / (float) Display.getHeight(), 0.1f, 100.0f);
GL.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix

// Really Nice Perspective Calculations
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());


GL.glEnable(GL.GL_LIGHTING);

GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, temp.asFloatBuffer(). put(LightAmbient));
GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, temp.asFloatBuffer().put(LightDiffuse));
GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, temp.asFloatBuffer().put(Specular));
GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, temp.asFloatBuffer().put(LightPosition));

GL.glEnable(GL.GL_LIGHT0);

GL.glMaterial(GL.GL_FRONT, GL.GL_SPECULAR, temp.asFloatBuffer().put(specref));
GL.glMateriali(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, 100);

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

}


cheers

*

Offline princec

  • *****
  • 1933
    • Puppygames
Lights
« Reply #1 on: November 10, 2003, 18:13:31 »
temp.asFloatBuffer().put(Specular)

needs flipping before it can be passed to GL.

Cas :)

Lights
« Reply #2 on: November 10, 2003, 18:35:50 »
I did:

Code: [Select]

temp.asFloatBuffer().put(Specular).flip();
GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, temp.asFloatBuffer());


Still darkness.

Why should I flip the buffer?[/code]

Lights
« Reply #3 on: November 10, 2003, 19:15:47 »
The flip() is an NIO Buffer thing.  Basically it sets the current position as the end of the data and then resets the current position to the beginning of the buffer.  The concept is that you push a load of data into a buffer, then call flip.  Something can then read the buffer from beginning to end to read all the data that was written, and no more.  No need to pass the amount of data written - to the reader's eye the buffer is the same size as the amount that it contains.

The syntax looks a bit weird at first, but it will make sense eventually.  It might be worthwhile using setPosition()/setLimit() at first, as they have the same effect but are a lot more intuitive to use.

On the lighting front, are you sure the object you're drawing is in view?  Set the clear colour to red or something to see if your object is in view but unlit (big black hole in the background where the object is) or out of sight (unblemished red screen).
ellomynameis Charlie Dobbie.

Lights
« Reply #4 on: November 10, 2003, 19:33:58 »
The object is in view, but no lighting/shading. If I turn off the lighting the object is clearly viewable, but not shaded.

How come the other arrays for the lighting does not need flipping?

*

Offline princec

  • *****
  • 1933
    • Puppygames
Lights
« Reply #5 on: November 10, 2003, 20:00:55 »
They do - sorry, I should be clearer. All buffers need their position and limit setting correctly before passing into any LWJGL method. flip() tends to do this for you of course by setting the limit to the end of what you've just written in to the buffer and resetting the position back to the beginning, which is generally just what you need.

Cas :)

Lights
« Reply #6 on: November 10, 2003, 20:08:40 »
That did the trick. Thanks everyone.