2

I've been messing around with LWJGL trying to create a .obj parser that will convert files exported from blender into OpenGL render code. I followed a tutorial similar to what i was doing, but it was written in c++, which i can hardly comprehend. I've managed to get everything working with the parser(I think) but when the time comes to actually render to model, i'm having a hard time creating a java equivalent of the c++ code. The tutorial uses this line of code:

glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);

Then (obviously) it draws the array. I'm pretty sure i need to convert Vecotor3f's from my list(The vertices) to a FloatBuffer, and i don;t need the second parameter(Because java will handle the size). I however, have no clue how to do this, i'm still finding my way around java, and have never used this class before.

user3311827
  • 179
  • 1
  • 9

1 Answers1

5
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertexData.put(vertices);
vertexData.flip();

glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);

Is the equivalent java code, where vertices is a float array containing your vertex data, vertexSize is the number of floats per vertex and amountOfVertices is self explanatory. You could switch amountOfVertices * vertexSize with vertices.length if you want as they should be equal.

Alex - GlassEditor.com
  • 14,957
  • 5
  • 49
  • 49
  • Couldn't you simply use `FloatBuffer.wrap(vertices)` instead? – Reto Koradi Jun 14 '14 at 02:57
  • Hi, thanks for the help. If i'm reading your response correctly, i need a float array of all of my vertices? I currently have my parser return the vertices as a List of Vector3f's . how can i get the verticies into a float array from a list of Vector3f objects? – user3311827 Jun 14 '14 at 02:59
  • @user3311827 you don't need them in a float[], you could put them in to the buffer sequentially with `vertexData.put(float f)` if you want, just remember to flip it after you add them all. – Alex - GlassEditor.com Jun 14 '14 at 03:04
  • 2
    @RetoKoradi: I believe there is an issue of Java.nio buffers potentially not being "direct" when you wrap them. Which makes passing a pointer to the memory to GL difficult. Probably does not much matter for buffer objects, but it is a big problem when using client memory. – Andon M. Coleman Jun 14 '14 at 03:20
  • Alright, that is working, but i can't see my model yet, probably a camera thing. Will try to get it working tomorrow. Thanks for the help! – user3311827 Jun 14 '14 at 03:24
  • @AndonM.Coleman: I experimented with direct vs. non-direct buffers on Android recently, since I couldn't find clear documentation on when direct buffers are needed. My theory, which was confirmed by these experiments, is that direct buffers are only required in cases where the memory has to live beyond the API call. The only case I could find for that is using `glVertexAttribPointer()` with client side vertex arrays. For everything else, non-direct buffers seem fine. Very possible that other Java platforms would behave differently than Android, though. – Reto Koradi Jun 14 '14 at 03:37
  • @Alex, I guess you miss a parameter in the glBufferData function, shouldn't it be: `void glBufferData( GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage);`? – Zieng Jun 13 '17 at 02:57
  • @Zieng lwjgl gets the size by calling `vertexData.remaining()` after it is passed, but there is another method `nglBufferData` with those 4 parameters. – Alex - GlassEditor.com Jun 13 '17 at 03:29