LWJGL Forum

Programming => OpenGL => Topic started by: EdwardRF on April 13, 2005, 08:57:04

Title: How to place light? is there any sample code?
Post by: EdwardRF on April 13, 2005, 08:57:04
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!!!

             EdwardRF
Title: How to place light? is there any sample code?
Post by: kappa on April 13, 2005, 12:12:32
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_LIGHT1);
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 lwjgl.org[/code]
Title: How to place light? is there any sample code?
Post by: napier on April 13, 2005, 14:04:08
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());
        bb.put(bytearray).flip();
        return bb;
    }

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

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




[/code]
Title: How to place light? is there any sample code?
Post by: EdwardRF on April 14, 2005, 05:36:00
Thanks guys, these are really helpful!!!
Merci beaucoup!