如何将3D对象的定义传递给app durign运行时?

时间:2016-11-20 10:45:24

标签: android opengl-es 3d

在我的应用程序中,我正在绘制一个简单的3D对象(在问题的最后,您将找到它的代码)。我正在使用GL10对象进行绘制。

现在我想更进一步,在运行时传递此类对象的定义。我想在方法中构建和注入和对象,从我的webservice获取定义。

任何提示如何开始?

public class SimpleBox {
private FloatBuffer box;
private FloatBuffer normals;
public SimpleBox() {
    float boxf[] =  {
            // FRONT
            -25.0f, -25.0f,  25.0f,
             25.0f, -25.0f,  25.0f,
            -25.0f,  25.0f,  25.0f,
             25.0f,  25.0f,  25.0f,
            // BACK
            -25.0f, -25.0f, -25.0f,
            -25.0f,  25.0f, -25.0f,
             25.0f, -25.0f, -25.0f,
             25.0f,  25.0f, -25.0f,
            // LEFT
            -25.0f, -25.0f,  25.0f,
            -25.0f,  25.0f,  25.0f,
            -25.0f, -25.0f, -25.0f,
            -25.0f,  25.0f, -25.0f,
            // RIGHT
             25.0f, -25.0f, -25.0f,
             25.0f,  25.0f, -25.0f,
             25.0f, -25.0f,  25.0f,
             25.0f,  25.0f,  25.0f,
            // TOP
            -25.0f,  25.0f,  25.0f,
             25.0f,  25.0f,  25.0f,
             -25.0f,  25.0f, -25.0f,
             25.0f,  25.0f, -25.0f,
            // BOTTOM
            -25.0f, -25.0f,  25.0f,
            -25.0f, -25.0f, -25.0f,
             25.0f, -25.0f,  25.0f,
             25.0f, -25.0f, -25.0f,
        };
    float normalsf[] =  {
            // FRONT
            0.0f, 0.0f,  1.0f,
            0.0f, 0.0f,  1.0f,
            0.0f, 0.0f,  1.0f,
            0.0f, 0.0f,  1.0f,
            // BACK
            0.0f, 0.0f,  -1.0f,
            0.0f, 0.0f,  -1.0f,
            0.0f, 0.0f,  -1.0f,
            0.0f, 0.0f,  -1.0f,
            // LEFT
            -1.0f, 0.0f,  0.0f,
            -1.0f, 0.0f,  0.0f,
            -1.0f, 0.0f,  0.0f,
            -1.0f, 0.0f,  0.0f,
            // RIGHT
            1.0f, 0.0f,  0.0f,
            1.0f, 0.0f,  0.0f,
            1.0f, 0.0f,  0.0f,
            1.0f, 0.0f,  0.0f,
            // TOP
            0.0f, 1.0f,  0.0f,
            0.0f, 1.0f,  0.0f,
            0.0f, 1.0f,  0.0f,
            0.0f, 1.0f,  0.0f,
            // BOTTOM
            0.0f, -1.0f,  0.0f,
            0.0f, -1.0f,  0.0f,
            0.0f, -1.0f,  0.0f,
            0.0f, -1.0f,  0.0f,
        };

    box = GraphicsUtil.makeFloatBuffer(boxf);
    normals = GraphicsUtil.makeFloatBuffer(normalsf);
}


public final void draw(GL10 gl) {   
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, box);
    gl.glNormalPointer(GL10.GL_FLOAT,0, normals);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
}
}

1 个答案:

答案 0 :(得分:1)

例如,您可以从服务器传递JSON,它将为您提供类似以下内容的数据:

    {
        vertex_count: 30,
        vertices: [
            {x: 0, y: 0, z: 0, nx: 0, ny: 1, nz: 0},
            {x: 1, y: 1, z: 1, nx: 0, ny: 1, nz: 0},
            {x: 0, y: 1, z: 0, nx: 0, ny: 1, nz: 0},
            {x: 1, y: 0, z: 0, nx: 0, ny: 1, nz: 0},
            {x: 1, y: 1, z: 0, nx: 0, ny: 1, nz: 0},
            ...
        ]
        ...
    }

这样您就可以创建2个缓冲区,使其与代码中的缓冲区相同。然后遍历JSON中的vertices并将值附加到2个浮点缓冲区。完成所有操作后,您只需将缓冲区分配给boxnormals,新对象将覆盖旧对象,以便显示新对象。