Login  Register

Re: Can someone please help me complete this sample code?

Posted by JStoecker on Jul 28, 2011; 9:50pm
URL: https://forum.jogamp.org/Can-someone-please-help-me-complete-this-sample-code-tp3207414p3208010.html

>> gl.glGenBuffers(1, &positionBufferObject);

This is invalid syntax for a Java program and won't work.

In C or C++ you can have pointers to primitives like int, double, float, etc. That allows you to have functions that change the value of primitive type variables passed as arguments. For example:

void foo(int* x) {
  *x = 10;
}

int main() {
  int number = 5;
  foo(&number); // after this call number = 10
  return 0;
}

In Java, you can't have a method that will change the value of a primitive type variable like in the above example. You can, of course, pass an object as an argument and modify its contents. That's why you have the wrapper classes like Integer, Boolean, Double, etc.

OpenGL is a C library, and it is very common in C code to have functions where "output" is provided to a caller by changing the values of the arguments. You can see this with the glGenBuffers(GLsizei n, GLuint* buffers) function. This function will create n OpenGL-managed buffers and return some integers to the caller. These integers will be stored in the "buffers" argument, which could be 1 GLuint or an array of GLuint (pointers and arrays are closely linked in C).

So in C, you would call this as follows to create a single buffer:

GLuint buffer;
glGenBuffers(1, &buffer);

or multiple buffers:

GLuint buffers[3];
glGenBuffers(3, buffers);

In Java, you don't have this concept of a traditional pointers. There are two options in the JOGL API:

glGenBuffers(int n, int[] buffers, int buffers_offset)

This first choice takes an array and would be used like this (including the bind call to set the buffer):

int[] buffer = new int[1];
gl.glGenBuffers(1, buffer, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, buffer[0]);

or for multiple:

int[] buffers = new int[3];
gl.glGenBuffers(3, buffers, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, buffers[0]);

Why is there a third parameter? This is in case you want to start filling the array at an index after 0. For example, an array with size 5 and you want to put the buffer value in the 3rd element. This isn't a useful parameter in the C API because you can do pointer arithmetic: glGenBuffers(1, buffers+2);

The second option in JOGL: glGenBuffers(int n, IntBuffer buffers)

Here, IntBuffer is an object that can store values just like an array. There is no third parameter because Buffer objects in Java have a position field that determines where reading starts, so you could offset it like this (which would put the value in the third element):

IntBuffer buffers = IntBuffer.allocate(3);
gl.glGenBuffers(1, buffers.position(2));