Converting C++ OpenGl to Java(LWJGL), glBufferData();

情到浓时终转凉″ 提交于 2019-12-08 03:47:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!