Lights

Started by Numknuf, November 10, 2003, 17:55:11

Previous topic - Next topic

Numknuf

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?

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

princec

temp.asFloatBuffer().put(Specular)

needs flipping before it can be passed to GL.

Cas :)

Numknuf

I did:

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


Still darkness.

Why should I flip the buffer?[/code]

cfmdobbie

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.

Numknuf

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?

princec

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 :)

Numknuf

That did the trick. Thanks everyone.