我一直在努力做android dev-guide on OPENGLES20, 但是当我运行代码时,我只得到绘制的背景颜色,而不是三角形(直到拉伸的三角形照片我才完成)。
如何让三角形出现? (在Android 2.2设备上运行应用程序)
以下是代码:
public class HelloOpenGLES20Renderer implements Renderer {
private FloatBuffer triangleVB;
private final String vertexShaderCode = "attribute vec4 vPosition; \n"
+ "void main(){ \n" + " gl_Position = vPosition; \n"
+ "} \n";
private final String fragmentShaderCode = "precision mediump float; \n"
+ "void main(){ \n"
+ " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n"
+ "} \n";
private int mProgram;
private int maPositionHandle;
private int loadShader(int type, String shaderCode) {
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
GLES20.glUseProgram(mProgram);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT,
false, 12, triangleVB);
GLES20.glEnableVertexAttribArray(maPositionHandle);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
initShapes();
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_VERTEX_SHADER,
fragmentShaderCode);
mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glLinkProgram(mProgram);
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
}
private void initShapes() {
float[] triangleCoords = new float[] {
// X,Y,Z
-0.5f, -0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0 };
ByteBuffer vbb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
vbb.order(ByteOrder.nativeOrder());
triangleVB = vbb.asFloatBuffer();
triangleVB.put(triangleCoords);
triangleVB.position(0);
}
}
答案 0 :(得分:2)
我认为你没有正确地遵循教程100%。例如onSurfaceCreated(...)
loadShader(GLES20.GL_VERTEX_SHADER..)
您拨打loadShader(GLES20.GL_FRAGMENT_SHADER..)
两次,而后者应为{{1}}。