2012-11-14 68 views
2

我用一个对象导出器从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, 
    }; 
} 
+0

首先,使用双精度来维护_exact_四舍五入错误的欢呼声Blender已经为你负担了:-)。说真的,你确定你应该使用双精度缓冲区,尤其是因为你在'glVertexAttribPointer'中指定了GL_FLOAT(而不是GL_DOUBLE)? –

+0

是的,要用GL_FLOAT代替 - 谢谢你的建议。我只需要编辑导出脚本:) – midnight

回答

3

你已经有了一个double缓冲区,你告诉OpenGL的,它是GL_FLOAT的缓冲,也许这是一个问题?

您可能希望使用float缓冲区。

+0

!谢谢你指出。作为一个侧面的问题 - 你能建议理解setLookAt,frustumM方法的任何链接吗?特别是我对setLookAt的最后3个以及近于和远离的视锥体感到困惑。 – midnight