我使用对象导出器从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 :(得分:3)
你有一个double
缓冲区,你告诉OpenGL它是GL_FLOAT的缓冲区,也许这是一个问题?
您可能希望使用float
缓冲区。