How to place light? is there any sample code?

Started by EdwardRF, April 13, 2005, 08:57:04

Previous topic - Next topic


Hi friends,
 I am stucked in how to place light in the 3d world, and is there any good code sample for me learn, i do not even know how to create direct float buffer properly in java. HELP!!!



float lightAmbient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	float lightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
            float lightPosition[] = { 0.0f, 1.0f, 1.0f, 0.0f };

 GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
	    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light         
	    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());  

GL11.glEnable ( GL11.GL_LIGHTING ) ;

hope it  helps, for a better example have a look at the lwjgl nehe tutorials, which can be found in the WIKI on[/code]


It's a good idea to wrap up the NIO buffer calls in functions.  These can be a source of confusion especially if you're new to them.  LWJGL has a class I think called BufferUtils that provides some helper functions for creating ByteBuffers, FloatBuffers, etc.  I rolled my own:

    public static ByteBuffer allocBytes(int howmany) {
         return ByteBuffer.allocateDirect(howmany * SIZE_BYTE).order(ByteOrder.nativeOrder());

     public static IntBuffer allocInts(int howmany) {
         return ByteBuffer.allocateDirect(howmany * SIZE_INT).order(ByteOrder.nativeOrder()).asIntBuffer();

     public static FloatBuffer allocFloats(int howmany) {
         return ByteBuffer.allocateDirect(howmany * SIZE_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();

     public static DoubleBuffer allocDoubles(int howmany) {
         return ByteBuffer.allocateDirect(howmany * SIZE_DOUBLE).order(ByteOrder.nativeOrder()).asDoubleBuffer();

     public static ByteBuffer allocBytes(byte[] bytearray) {
         ByteBuffer bb = ByteBuffer.allocateDirect(bytearray.length * SIZE_BYTE).order(ByteOrder.nativeOrder());
         return bb;

     public static IntBuffer allocInts(int[] intarray) {
         IntBuffer ib = ByteBuffer.allocateDirect(intarray.length * SIZE_FLOAT).order(ByteOrder.nativeOrder()).asIntBuffer();
         return ib;

     public static FloatBuffer allocFloats(float[] floatarray) {
         FloatBuffer fb = ByteBuffer.allocateDirect(floatarray.length * SIZE_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
         return fb;

penGL/Java/LWJGL demos and code:


Thanks guys, these are really helpful!!!
Merci beaucoup!