Android OpenGL黑屏问题可能是由于多种因素引起的。下面列出一些常见的解决方案:
@Override
public void surfaceCreated(GL10 gl) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Enable depth test
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
// Set the viewport
GLES20.glViewport(0, 0, mSurfaceWidth, mSurfaceHeight);
}
其中,GLES20是Android中的OpenGL ES 2.0的API接口,可以使用glClearColor方法设置背景颜色、glEnable方法启用深度测试,glViewport方法设置OpenGL的视口。
attribute vec4 vPosition;
void main() {
gl_Position = vPosition;
}
这个简单的着色器只是将坐标传递给渲染管道,但它必须被正确地编写并链接到程序中。
// Generate and bind the Vertex Buffer Object
int[] vboIds = new int[1];
GLES20.glGenBuffers(1, vboIds, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[0]);
// Allocate memory and load data into the VBO
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER