问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/24216130/converting-c-opengl-to-javalwjgl-glbufferdata