Hi,
I have a problem with setting up the light in my scene. I just want to have some nice flat surfaces without textures that get shaded by a light.
Is there anyting I did forget or did wrong in my code because the objects stay totaly black:
GL.glLightModel(GL.GL_LIGHT_MODEL_AMBIENT, temp.asFloatBuffer().put(ambientLight));
GL.glEnable(GL.GL_COLOR_MATERIAL);
GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, temp.asFloatBuffer().put(ambientLight0));
GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, temp.asFloatBuffer().put(diffuseLight0));
GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION,temp.asFloatBuffer().put(lightPos0));
GL.glEnable(GL.GL_LIGHT0);
GL.glEnable(GL.GL_LIGHTING);
and this in front of my objects:
GL.glColorMaterial(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE);
Thanks for help
Are you setting up your normals?
You must flip() your buffers after put()ting stuff in them. This sets the buffer's position and limit correctly which is what we pass into OpenGL.
Cas :)
That sounds pretty much like a problem :wink:
I did the necesary changes in my code but now I have compilingproblems. An exception occured in the native code.
The code looks like:
public static FloatBuffer fbuff = FloatBuffer.allocate(16);
fbuff.put(diffuseLight0).flip();
GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, fbuff);
Oh my god!
Don't know if it will be your end-all fix, but buffers passed to OpenGL must be allocated with allocateDirect(), not the allocate() method.
Try this instead:
public static FloatBuffer fbuff = ByteBuffer.allocateDirect(16).order(ByteOrder.nativeOrder()).asFloatBuffer();
fbuff.put(diffuseLight0).flip();
GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, fbuff);
Great, finaly the light works. I thank you guys very much.
But this is a very complicated way of seting up the light.
Does anyone have a more "normal" way of doing this?
I haven't used any of OpenGL's lighting for any lighting purposes in my current project. I have either used lightmapping, using ARB multitexture, or by calculating shade values and coloring geometry with the glColor3f() method at render-time.
There is no easier way of setting up lights in OpenGL. However, coz you're a Java programmer, you will write a Light class that does it for you and squirrels away the nasty code so you can't see it any more, and you'll forget all about how it works in a few weeks and just get on with some other problem :)
Cas :)