开发者

Android OpenGL Point Clouds

开发者 https://www.devze.com 2023-03-13 06:54 出处:网络
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 d

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_F开发者_如何学PythonACE);
}

The program currently crashes when I call draw();

Thank you!


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号