gpt4 book ai didi

android - 渲染立方体

转载 作者:搜寻专家 更新时间:2023-11-01 09:03:51 25 4
gpt4 key购买 nike

我使用对象导出器从 Blender 获取立方体模型。出于某种原因,我无法渲染立方体——这可能是我缺乏对 View 和投影矩阵的理解。你能指出我的错误吗?

使用相同的渲染器渲染一个简单的三角形,但是,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_FLOAT 缓冲区,也许这是个问题?

您可能想改用float 缓冲区。

关于android - 渲染立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13374515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com