Android OpenGL 点云

发布于 2024-11-15 13:29:37 字数 1474 浏览 2 评论 0原文

我一直在尝试使用

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-11-22 13:29:37

您完全错误地使用了glDrawElements。此函数需要一个索引数组,其中包含顶点数组的索引,而不是顶点数据本身(这就是 glVertexPointer 的用途)。要么使用

private unsigned byte indices[] = { 0, 1, 2, 3 };
...
gl.glDrawElements(GL10.GL_POINTS, 4, GL10.GL_UNSIGNED_BYTE, indices);

但在你的情况下你只渲染所有点并且不需要任何索引,所以你可以只调用

gl.glDrawArrays(GL10.GL_POINTS, 0, 4);

而不是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 what glVertexPointer is for). Either use

private unsigned byte indices[] = { 0, 1, 2, 3 };
...
gl.glDrawElements(GL10.GL_POINTS, 4, GL10.GL_UNSIGNED_BYTE, indices);

But in your case you just render all points and don't need any indices, so you can just call

gl.glDrawArrays(GL10.GL_POINTS, 0, 4);

instead of glDrawElements.

EDIT: The specific reason it crashes is that glDrawElements interprets the supplied vertexBuffer as an array of 4 bytes and these bytes reference vertices out of the range of your vertex data (0 to 3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文