Android OpenGL 点云
我一直在尝试使用
gl.glDrawElements(GL10.GL_POINTS, 4, GL10.GL_UNSIGNED_BYTE, vertexBuffer);
顶点缓冲区在屏幕上绘制 4 个点,但我无法让它工作。我想要绘制的只是点,因为最终我想要进行点云显示。如果我有大量的点(最终),顶点缓冲区是可行的方法吗?它们不会改变,但我想改变查看它们的视角和比例。
vertexBuffer 设置:
private float vertices[] = {
-3.0f, 1.0f, -2.0f, // 0, Top Left
-3.0f, -1.0f, 0.0f, // 1, Bottom Left
-2.0f, -1.0f, -2.0f, // 2, Bottom Right
-2.0f, 1.0f, 0.0f, // 3, Top Right
};
// Our vertex buffer.
private FloatBuffer vertexBuffer;
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
这是我当前对点的绘制调用(我不需要索引,因为形状绘制顺序对我来说并不重要):
public void draw(GL10 gl) {
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPointSize(3);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_POINTS, 4,
GL10.GL_UNSIGNED_BYTE, vertexBuffer);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE);
}
当我调用 draw() 时,程序当前崩溃;
谢谢你!
I've been trying to use
gl.glDrawElements(GL10.GL_POINTS, 4, GL10.GL_UNSIGNED_BYTE, vertexBuffer);
to draw 4 points on my screen with a vertex buffer, but I can't get it to work. All I want to draw is points, because eventually I want to make a point cloud display. If I have a large number of points (eventually), is vertex buffer the way to go? They won't be changing, but I will want to change the perspective and scale at which they are viewed.
vertexBuffer setup:
private float vertices[] = {
-3.0f, 1.0f, -2.0f, // 0, Top Left
-3.0f, -1.0f, 0.0f, // 1, Bottom Left
-2.0f, -1.0f, -2.0f, // 2, Bottom Right
-2.0f, 1.0f, 0.0f, // 3, Top Right
};
// Our vertex buffer.
private FloatBuffer vertexBuffer;
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
This is my current draw call for my points (I don't want indices because shape drawing order doesn't matter to me):
public void draw(GL10 gl) {
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPointSize(3);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_POINTS, 4,
GL10.GL_UNSIGNED_BYTE, vertexBuffer);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE);
}
The program currently crashes when I call draw();
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您完全错误地使用了
glDrawElements
。此函数需要一个索引数组,其中包含顶点数组的索引,而不是顶点数据本身(这就是glVertexPointer
的用途)。要么使用但在你的情况下你只渲染所有点并且不需要任何索引,所以你可以只调用
而不是
glDrawElements
。编辑:崩溃的具体原因是
glDrawElements
将提供的vertexBuffer
解释为一个4字节的数组,并且这些字节引用的顶点超出了范围顶点数据(0 到 3)。You make completely wrong use of
glDrawElements
. This function wants an index array containing indices into the vertex arrays and not the vertex data itself (that's whatglVertexPointer
is for). Either useBut in your case you just render all points and don't need any indices, so you can just call
instead of
glDrawElements
.EDIT: The specific reason it crashes is that
glDrawElements
interprets the suppliedvertexBuffer
as an array of 4 bytes and these bytes reference vertices out of the range of your vertex data (0 to 3).