Reputation: 1683
This is the code I wrote in order to get 2D drawing to work. The activity and the view and renderer are setup the correct way. I get a completely black screen, but with no polygons on it. Nothing is drawn. This is about to drive me crazy...
public class OpenGLActivity extends Activity {
private GLSurfaceView surfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
this.surfaceView = new OpenGLSurfaceView(this);
setContentView(this.surfaceView);
}
@Override
protected void onPause() {
super.onPause();
surfaceView.onPause();
}
@Override
protected void onResume() {
super.onResume();
surfaceView.onResume();
}
}
public class OpenGLSurfaceView extends GLSurfaceView {
private OpenGLSurfaceRenderer renderer;
public OpenGLSurfaceView(Context context) {
super(context);
this.renderer = new OpenGLSurfaceRenderer();
setRenderer(renderer);
}
}
public class OpenGLSurfaceRenderer implements Renderer {
private FloatBuffer vertexBuffer;
private float[] vertices = {
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 10.0f, 0.0f,
0.0f, 0.0f, 0.0f,
10.0f, 10.0f, 0.0f,
10.0f, 0.0f, 0.0f
};
public OpenGLSurfaceRenderer() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * (Float.SIZE >> 3));
vertexBuffer = byteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
public void onDrawFrame(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gl.glTranslatef(0.0f, 0.0f, 0.5f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
// Clear the projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
// Set up orthographic projection mode (2D drawing)
gl.glOrthof(0, width, height, 0, 0, 1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
}
Upvotes: 0
Views: 1088
Reputation: 38727
I see four problems:
The last parameter to glDrawArrays() should be 8, not 4. It's the number of vertices you are providing, not the number of triangles in your triangle strip.
Your orthographic projection makes your world units equivalent to pixels. Your first 4 vertexes describe triangles that are 1-pixel wide. Maybe you are getting a single white pixel in the bottom left corner of the screen?
You advance the model projection along the Z-axis every frame. After 2 frames it will be out of the view frustrum.
Most importantly, you do not set the right ordering on your ByteBuffer. Insert this line into your OpenGlRenderer() constructor:
public OpenGLSurfaceRenderer() { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4); byteBuffer.order(ByteOrder.nativeOrder()); <------ vertexBuffer = byteBuffer.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); }
Upvotes: 1