Reputation: 1513
I am following the "Building an OpenGLES environment" docs. I'm at the point of just making a triangle appear. The application runs but the triangle isn't there.
Main activity
package com.example.testopengl;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
private GLSurfaceView mGLView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new MyGLSurfaceView(this);
setContentView (mGLView);
}
}
Renderer class
package com.example.testopengl;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
public class MyGLRenderer implements GLSurfaceView.Renderer {
private Triangle mTriangle;
public void onSurfaceCreated (GL10 unused, EGLConfig config) {
GLES20.glClearColor(0.4f, 0.4f, 0.4f, 0.4f);
mTriangle = new Triangle();
}
public void onDrawFrame (GL10 unused) {
GLES20.glClear (GLES20.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceChanged (GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
mTriangle.draw();
}
public static int loadShader (int type, String shaderCode) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
Surface View class
package com.example.testopengl;
import android.content.Context;
import android.opengl.GLSurfaceView;
public class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView (Context context) {
super (context);
setEGLContextClientVersion(2);
setRenderer(new MyGLRenderer());
setRenderMode (GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
}
Triangle class
package com.example.testopengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
public class Triangle {
private final String vertexShaderCode =
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {"+
" gl_FragColor = vColor;" +
"}";
private FloatBuffer vertexBuffer;
private final int mProgram;
private int mPositionHandle;
private int mColorHandle;
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[] = {
0.0f, 0.622008459f, 0.0f,
-0.5f, -0.311004243f, 0.0f,
0.5f, -0.311004243f, 0.0f
};
private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4;
float color [] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f};
public Triangle() {
ByteBuffer bb = ByteBuffer.allocateDirect (
triangleCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(triangleCoords);
vertexBuffer.position(0);
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glLinkProgram(mProgram);
}
public void draw() {
GLES20.glUseProgram(mProgram);
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vcolor");
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
Upvotes: 0
Views: 909
Reputation: 54642
The main problem is that your onDrawFrame()
method in the renderer only does a clear, but doesn't render anything. Your mTriangle.draw()
call should be in onDrawFrame()
instead of in onSurfaceChanged()
.
I believe you're also missing a call to glEnableVertexAttribArray()
.
Upvotes: 1