我正在尝试在 Android API 3 OpenGL(所以 OpenGL ES 1.0)中构建一个不同颜色的立方体。(我是 OpenGL 编程的初学者)。我设法让它在一个恒定的周期内旋转,但最大的问题是我无法画出所有的面孔。
确实,缺失的脸无论是
- 我绕 x 或 y 轴旋转
- 我在 onSurfaceCreated 方法中使用或不使用 GL10#glEnable(GL10.GL_DEPTH_TEST) 命令
那么,纠正这个问题的最简单方法是什么?(我曾想过使用 glClearDepth() 和 glCullFace(),但没有成功)。
这是从 GLSurfaceView.Renderer 继承的我的类的代码:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
public class MyGLRenderer implements Renderer {
public MyGLRenderer(){
buildCubeBuffers();
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glRotatef(0.6f, 1.0f, 0.0f, 0.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glVertexPointer(COORDS_PER_VERTEX, GL10.GL_FLOAT, 0, cubeBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//--- FRONT
gl.glColor4f(1.0f, 0.0f, 0.0f, 0.0f); //RED
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, VERTICES_NUMBER);
//--- RIGHT
gl.glColor4f(0.0f, 1.0f, 0.0f, 0.0f); // GREEN
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, VERTICES_NUMBER);
//--- BACK
gl.glColor4f(0.0f, 0.0f, 1.0f, 0.0f); // BLUE
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, VERTICES_NUMBER);
//--- LEFT
gl.glColor4f(1.0f, 1.0f, 0.0f, 0.0f); // YELLOW
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, VERTICES_NUMBER);
//--- TOP
gl.glColor4f(1.0f, 0.5f, 0.0f, 0.0f); // ORANGE
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, VERTICES_NUMBER);
//--- BOTTOM
gl.glColor4f(1.0f, 0.0f, 1.0f, 0.0f); // PURPLE
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, VERTICES_NUMBER);
//--
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) height = 1;
if (width < height)
gl.glViewport(0, 0, width, width);
else
gl.glViewport(0, 0, height, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
//////////////////////////////////////
/*
* If i comment next line :
* => Rotation on y axis
* yellow face is visible, red face is invisible
* Otherwise : yellow invisible and red visible !!!
* => Rotation on x axis
* purple face is visible, red face is invisible
* Otherwise : purple invisible and red visible !!!
* Why ???
*/
//////////////////////////////////////
gl.glEnable(GL10.GL_DEPTH_TEST);
}
private void buildCubeBuffers(){
final float cubeVertices[] = {
// front
-CUBE_UNITY_COORD, +CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 0 : Left-Top-Front
+CUBE_UNITY_COORD, +CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 1 : Right-Top-Front
-CUBE_UNITY_COORD, -CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 3 : Left-Bottom-Front
+CUBE_UNITY_COORD, -CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 2 : Right-Bottom-Front
// right
+CUBE_UNITY_COORD, +CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 1 : Right-Top-Front
+CUBE_UNITY_COORD, +CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 5 : Right-Top-Back
+CUBE_UNITY_COORD, -CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 2 : Right-Bottom-Front
+CUBE_UNITY_COORD, -CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 6 : Right-Bottom-Back
// back
+CUBE_UNITY_COORD, +CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 5 : Right-Top-Back
-CUBE_UNITY_COORD, +CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 4 : Left-Top-Back
+CUBE_UNITY_COORD, -CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 6 : Right-Bottom-Back
-CUBE_UNITY_COORD, -CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 7 : Left-Bottom-Back
// left
-CUBE_UNITY_COORD, +CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 4 : Left-Top-Back
-CUBE_UNITY_COORD, +CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 0 : Left-Top-Front
-CUBE_UNITY_COORD, -CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 7 : Left-Bottom-Back
-CUBE_UNITY_COORD, -CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 3 : Left-Bottom-Front
// top
-CUBE_UNITY_COORD, +CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 4 : Left-Top-Back
+CUBE_UNITY_COORD, +CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 5 : Right-Top-Back
-CUBE_UNITY_COORD, +CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 0 : Left-Top-Front
+CUBE_UNITY_COORD, +CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 1 : Right-Top-Front
// bottom
-CUBE_UNITY_COORD, -CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 3 : Left-Bottom-Front
+CUBE_UNITY_COORD, -CUBE_UNITY_COORD, +CUBE_UNITY_COORD, // point 2 : Right-Bottom-Front
-CUBE_UNITY_COORD, -CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 7 : Left-Bottom-Back
+CUBE_UNITY_COORD, -CUBE_UNITY_COORD, -CUBE_UNITY_COORD, // point 6 : Right-Bottom-Back
};
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(cubeVertices.length * FLOAT_CAPACITY_BYTES);
byteBuffer.order(ByteOrder.nativeOrder());
cubeBuffer = byteBuffer.asFloatBuffer();
cubeBuffer.put(cubeVertices);
cubeBuffer.rewind();
}
private final static float CUBE_UNITY_COORD = 0.5f;
private static FloatBuffer cubeBuffer;
private final static int FLOAT_CAPACITY_BYTES = 4;
private final static int COORDS_PER_VERTEX = 3;
private final static int VERTICES_NUMBER = 4;
}