Android 渲染立方体

Android 渲染立方体,android,opengl-es-2.0,Android,Opengl Es 2.0,我使用一个对象导出器从Blender获得一个立方体模型。由于某些原因,我无法渲染立方体-可能是我对视图和投影矩阵缺乏理解。你能指出我的错误吗 但是,使用相同的渲染器渲染简单的三角形时,0.0f、0.49f、0.0f、-0.49f、0.0f、0.0f、0.49f、0.0f、0.0f完成得很好 public class CubeRenderer implements Renderer { public float xAngle = 0; public float yAngle = 0;

我使用一个对象导出器从Blender获得一个立方体模型。由于某些原因,我无法渲染立方体-可能是我对视图和投影矩阵缺乏理解。你能指出我的错误吗

但是,使用相同的渲染器渲染简单的三角形时,
0.0f、0.49f、0.0f、-0.49f、0.0f、0.0f、0.49f、0.0f、0.0f
完成得很好

public class CubeRenderer implements Renderer {

public float xAngle = 0;
public float yAngle = 0;

    public CubeRenderer() {
        cubeBuffer = ByteBuffer.allocateDirect(CubeModel.VERTICES.length * 8)
                .order(ByteOrder.nativeOrder()).asDoubleBuffer();
        cubeBuffer.put(CubeModel.VERTICES).position(0);
    }


@Override
public void onDrawFrame(GL10 arg0) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    GLES20.glUseProgram(iProgramHandle);
    cubeBuffer.position(0);
    GLES20.glVertexAttribPointer(iPositionHandle, 3, GLES20.GL_FLOAT, false, 0,
            cubeBuffer);
    GLES20.glEnableVertexAttribArray(iPositionHandle);
    Matrix.setIdentityM(m_fIdentity, 0);
    Matrix.rotateM(m_fIdentity, 0, -xAngle, 0, 1, 0);
    Matrix.rotateM(m_fIdentity, 0, -yAngle, 1, 0, 0);


    Matrix.multiplyMM(m_MVPMatrix, 0, m_fViewMatrix, 0, m_fIdentity, 0);

    Matrix.multiplyMM(m_MVPMatrix, 0, m_fProjMatrix, 0, m_MVPMatrix, 0);

    GLES20.glUniformMatrix4fv(iMVPMatrixHandle, 1, false, m_MVPMatrix, 0);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, CubeModel.VERTICES_COUNT);
}

@Override
public void onSurfaceChanged(GL10 arg0, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    Matrix.frustumM(m_fProjMatrix, 0, -ratio, ratio, -1.0f, 1.0f, 1.0f, 10.0f);
}

@Override
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glFrontFace(GLES20.GL_CCW);



    Matrix.setLookAtM(m_fViewMatrix, 0, 0.0f, 3.0f, 5.5f, 0.0f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f);

    iProgramHandle = Utils.LoadProgram(CubeModel.VERTEX_SHADER_CODE, CubeModel.FRAGMENT_SHADER_CODE);
    iColorHandle = GLES20.glGetAttribLocation(iProgramHandle, "a_color");
    iPositionHandle = GLES20.glGetAttribLocation(iProgramHandle, "a_position");
    iMVPMatrixHandle = GLES20.glGetUniformLocation(iProgramHandle, "u_MVPMatrix");
}

}


public class CubeModel {

    public final static String VERTEX_SHADER_CODE = "attribute vec4 a_position;\n"
            + "attribute vec4 a_color;" + "uniform mat4 u_MVPMatrix;\n"
            + "varying vec4 v_color;" 
            + "void main()" + "{"
            + "v_color = a_color;"
            + "gl_Position = u_MVPMatrix * a_position;" + 
            "}";

    public final static String FRAGMENT_SHADER_CODE = "precision mediump float;"
            + "varying vec4 v_color;" + "void main()" + "{"
            + "gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );" + "}";

    public final static int VERTICES_COUNT = 36;

    public final static double[] VERTICES = new double[]{
      // f 1 2 3 4
      0.499999812500094, -0.499999750000125, -0.499999875000062,
      0.499999812500094, -0.499999750000125, 0.499999625000187,
      -0.499999687500156, -0.499999750000125, 0.499999625000187,
      // f 1 2 3 4
      0.499999812500094, -0.499999750000125, -0.499999875000062,
      -0.499999687500156, -0.499999750000125, -0.499999875000062,
      -0.499999687500156, -0.499999750000125, 0.499999625000187,
      // f 5 8 7 6
      0.499999812500094, 0.499999750000125, -0.499999375000312,
      -0.499999687500156, 0.499999750000125, -0.499999875000062,
      -0.499999687500156, 0.499999750000125, 0.499999625000187,
      // f 5 8 7 6
      0.499999812500094, 0.499999750000125, -0.499999375000312,
      0.499999312500344, 0.499999750000125, 0.500000124999937,
      -0.499999687500156, 0.499999750000125, 0.499999625000187,
      // f 1 5 6 2
      0.499999812500094, -0.499999750000125, -0.499999875000062,
      0.499999812500094, 0.499999750000125, -0.499999375000312,
      0.499999312500344, 0.499999750000125, 0.500000124999937,
      // f 1 5 6 2
      0.499999812500094, -0.499999750000125, -0.499999875000062,
      0.499999812500094, -0.499999750000125, 0.499999625000187,
      0.499999312500344, 0.499999750000125, 0.500000124999937,
      // f 2 6 7 3
      0.499999812500094, -0.499999750000125, 0.499999625000187,
      0.499999312500344, 0.499999750000125, 0.500000124999937,
      -0.499999687500156, 0.499999750000125, 0.499999625000187,
      // f 2 6 7 3
      0.499999812500094, -0.499999750000125, 0.499999625000187,
      -0.499999687500156, -0.499999750000125, 0.499999625000187,
      -0.499999687500156, 0.499999750000125, 0.499999625000187,
      // f 3 7 8 4
      -0.499999687500156, -0.499999750000125, 0.499999625000187,
      -0.499999687500156, 0.499999750000125, 0.499999625000187,
      -0.499999687500156, 0.499999750000125, -0.499999875000062,
      // f 3 7 8 4
      -0.499999687500156, -0.499999750000125, 0.499999625000187,
      -0.499999687500156, -0.499999750000125, -0.499999875000062,
      -0.499999687500156, 0.499999750000125, -0.499999875000062,
      // f 5 1 4 8
      0.499999812500094, 0.499999750000125, -0.499999375000312,
      0.499999812500094, -0.499999750000125, -0.499999875000062,
      -0.499999687500156, -0.499999750000125, -0.499999875000062,
      // f 5 1 4 8
      0.499999812500094, 0.499999750000125, -0.499999375000312,
      -0.499999687500156, 0.499999750000125, -0.499999875000062,
      -0.499999687500156, -0.499999750000125, -0.499999875000062,
    };
}

你有一个
double
缓冲区,你告诉OpenGL它是一个GL\u FLOAT缓冲区,也许这是个问题


您可能需要使用
float
buffer来代替。

首先,万岁使用双精度来保持精确的舍入误差混合器让您负担了:-)。不过,说真的,你确定你应该使用双精度缓冲区吗?特别是因为你在
glvertexattributepointer
中指定了GL_FLOAT(相对于GL_DOUBLE)?是的,我想改用GL_FLOAT-谢谢你的建议。我只需要编辑导出脚本:)完全正确!谢谢你指出这一点。作为一个附带问题,你能提供一些关于理解setLookAt和frustumM方法的链接吗?特别是我对setLookAt的最后3个和frustum的近距离和远距离感到困惑。