我目前正在关注这个关于OpenGL的Android Developer教程,但它只显示背景颜色。
在本教程之前,已成功绘制三角形。
GLRenderer.java
public class GLRenderer implements GLSurfaceView.Renderer
{
private final float[] MVPMatrix = new float[16];
private final float[] projectionMatrix = new float[16];
private final float[] viewMatrix = new float[16];
private Triangle triangle;
@Override
public void onSurfaceCreated(GL10 p1, javax.microedition.khronos.egl.EGLConfig p2)
{
// TODO: Implement this method
GLES20.glClearColor(0.2f, 0.2f, 0.2f, 1.0f); // RGBA, gives a dark gray background color
triangle = new Triangle();
}
@Override
public void onSurfaceChanged(GL10 p1, int w, int h)
{
// TODO: Implement this method
GLES20.glViewport(0, 0, w, h);
float ratio = (float) w / h;
Matrix.frustumM(projectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
@Override
public void onDrawFrame(GL10 p1)
{
// TODO: Implement this method
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // clears the screen
Matrix.setLookAtM(viewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
triangle.draw(MVPMatrix);
}
public static int loadShader(int type, String shaderCode)
{
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
Triangle.java
public class Triangle
{
private FloatBuffer vertexBuffer;
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main(){" +
" gl_Position = uMVPMatrix * vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
static final int COORDS_PER_VERTEX = 3;
static float TRIANGLE_COORDS[] = {
0.0f, 0.6f, 0.0f, // top
-0.5f, -0.3f, 0.0f, // bottom left
0.5f, -0.3f, 0.0f // bottom right
};
float color[] = {0.8f, 0.8f, 0.8f, 1.0f}; // RGBA, default gray color
private final int glProgram;
private int positionHandle;
private int colorHandle;
private int MVPMatrixHandle;
private final int vertexCount = TRIANGLE_COORDS.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4;
public Triangle()
{
ByteBuffer bb = ByteBuffer.allocateDirect(TRIANGLE_COORDS.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(TRIANGLE_COORDS);
vertexBuffer.position(0);
int vertexShader = GLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = GLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
glProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(glProgram, vertexShader);
GLES20.glAttachShader(glProgram, fragmentShader);
GLES20.glLinkProgram(glProgram);
}
public void draw(float[] mvpMatrix)
{
GLES20.glUseProgram(glProgram);
positionHandle = GLES20.glGetAttribLocation(glProgram, "vPosition");
GLES20.glEnableVertexAttribArray(positionHandle);
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
colorHandle = GLES20.glGetUniformLocation(glProgram, "vColor");
GLES20.glUniform4fv(colorHandle, 1, color, 0);
MVPMatrixHandle = GLES20.glGetUniformLocation(glProgram, "uMVPMatrix");
GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
GLES20.glDisableVertexAttribArray(positionHandle);
}
}
我创建了一个包含View
的自定义GLSurfaceView
,以便我可以设置添加UI元素。
这里有什么问题?我试图通过改变一些部分来调试它,但所有部分只显示相同的输出。
修改
这是我创建的自定义View
。
GLView.java
public class GLView extends GLSurfaceView
{
public GLView (Context context, AttributeSet attrs)
{
super(context, attrs);
setEGLContextClientVersion(2); // Checking for OpenGL versions (1.1, 2.0, 3.0, and 3.1) in later versions
GLRenderer glRenderer = new GLRenderer();
setRenderer(glRenderer);
setRenderMode(RENDERMODE_WHEN_DIRTY);
requestRender();
}
}