Java Lighting and Direct FloatBuffers

Started by
6 comments, last by TheCrow33 12 years, 10 months ago
So I'm new to 3d graphics and opengl programming, which I am currently trying to learn in Java (because it's universal). So I've learned that I'm pretty much not going to find a Java specific tutorial that is worth any use to me as the ones I've found prove to be lacking certain important lines of code. Anyway I've been cross referencing several and I'm having trouble figuring out how to place a light using glLight(). The java API says that the function looks like: glLight(int, int, FloatBuffer); which is all fine and well but it also specifies FloatBuffer must be direct (basically meaning I can't use FloatBuffer.wrap() to instantiate it). The second problem is that FloatBuffer is an abstract so I can't call: FloatBuffer pos = new FloatBuffer(); Basically I'm saying I'm at a loss as to how the hell I'm supposed to create a FloatBuffer which holds the position I wish to supply it.

It is worth noting that I read in the API that a FloatBuffer will be direct if it is instantiated from a ByteBuffer[], but I'm lost as to how to convert a 3 dimensional coordinate to a bytebuffer, and on top of that if I could do that how do I go about typecasting that to a FloatBuffer?
Advertisement
After more confusing research I came to a conclusion:

ByteBuffer f = ByteBuffer.allocateDirect(32);
f.putFloat(0f).putFloat(0f).putFloat(1f).putFloat(0f);

glLight(GL_LIGHT0, GL_POSITION, f.asFloatBuffer());
To create specific type of byte buffers u have to use the factory class BufferUtil, there should be methods like newFloatBuffer.

If I remember right there should always be 2 methos of each kind one with Buffers and one with arrays
http://www.java-tips.org/other-api-tips/jogl/blending-nehe-tutorial-jogl-port-2.html
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

To create specific type of byte buffers u have to use the factory class BufferUtil, there should be methods like newFloatBuffer.

If I remember right there should always be 2 methos of each kind one with Buffers and one with arrays


Thanks for that bit of info, it seems my original solution won't actually do the trick though it does compile that way. There are two methods for this one, one is glLight(int, int, FloatBuffer) and the other is glLightf(int, int, float). But I couldn't figure out how I would store a RGB (or perhaps RGBA) value into a single float, so that method didn't make much sense to me. Anyway I'll give your solution a try.
So doing this using BufferUtils (the adaptation of BufferUtil for the lwjgl) I get an error which I don't understand. Here's the code:
FloatBuffer whiteSpec = BufferUtils.createFloatBuffer(4);
whiteSpec.put(new float[] {1.0f, 1.0f, 1.0f, 1.0f});
glLight(GL_LIGHT0, GL_SPECULAR, whiteSpec);


It all compiles just fine, but the third line (glLight()) throws an IllegalArgumentException with the message "Number of remaining buffer elements is 0, must be at least 4". It seems that it wants a FloatBuffer of at least size 4 (in float size), but if I put 4 floats in the buffer, then it wants 4 more free? This makes no sense to me, can anyone shed some light?
The Buffer classes in Java store a position counter, which gets incremented with each element which gets added to the buffer.
Also, when u want to read from the buffer it start reading from the current position.

So u need to set the position to 0 ( the start of the buffer).
U can call buffer.position(0) or buffer.rewind()

The Buffer classes in Java store a position counter, which gets incremented with each element which gets added to the buffer.
Also, when u want to read from the buffer it start reading from the current position.

So u need to set the position to 0 ( the start of the buffer).
U can call buffer.position(0) or buffer.rewind()



Yeah that does it, had no idea. Thanks

This topic is closed to new replies.

Advertisement