1

I use two ways of Renderer to draw a triangle. One is work and one is none.

Here is my two solutions.

First Solution: cannot work when run. I emplements Renderer (You should see the line :glView.setRenderer method. That the basic difference of my two solutions.)

package com.test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class TriangleOpenGLTest extends Activity implements Renderer{

    FloatBuffer vertices;
    ByteBuffer byteBuffer;
    GL10 gl;
    GLSurfaceView glView;


    @Override 
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        byteBuffer = ByteBuffer.allocateDirect(3*2*4);
        byteBuffer.order(ByteOrder.nativeOrder());

        vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] { 0.0f, 0.0f, 319.0f, 0.0f, 160.0f, 479.0f});
        vertices.flip();

        glView = new GLSurfaceView(this);
        glView.setRenderer(this);
        setContentView(glView);

    }

    @Override
    public void onResume(){
        super.onResume();
            gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);
            gl.glColor4f(1, 0, 0, 1);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);       
    }

            @Override
        public void onDrawFrame(GL10 gl) {
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        }
}

Solution 2: I create a new sub-class for my Renderer.

package com.test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class TriangleOpenGLTest extends Activity {

    FloatBuffer vertices;
    ByteBuffer byteBuffer;
    GL10 gl;
    GLSurfaceView glView;


    @Override 
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        byteBuffer = ByteBuffer.allocateDirect(3*2*4);
        byteBuffer.order(ByteOrder.nativeOrder());

        vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] { 0.0f, 0.0f, 319.0f, 0.0f, 160.0f, 479.0f});
        vertices.flip();

        glView = new GLSurfaceView(this);
        glView.setRenderer(new DrawTriangle());
        setContentView(glView);

    }

    @Override
    public void onResume(){
        super.onResume();
    }

    public class DrawTriangle implements Renderer{

        public DrawTriangle(){

        }
        @Override
        public void onDrawFrame(GL10 gl) {

            gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);
            gl.glColor4f(1, 0, 0, 1);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);       

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            // TODO Auto-generated method stub

        }

    }

}

Who can explain for me what the difference between two above solutions,please.

thanks :)

hqt
  • 29,632
  • 51
  • 171
  • 250

1 Answers1

2

In the first solution you have an empty onDrawFrame() method. This is probably why it's not working. See GLSurfaceView.Renderer

Maz
  • 764
  • 5
  • 5
  • 2
    Oh. thanks so much. Can you explain for me, what the difference between onResume and onDrawFrame for this example. Because I think Android will always run to onResume as onDrawFrame. Thanks :) – hqt Feb 17 '12 at 14:24
  • When onResume is triggered this tells the GLSurfaceView to start calling its onDrawFrame() over and over. Since your first method does nothing in the onDrawFrame function, nothing draws. If you put your draw code that you have in the onResume function into onDrawFrame() it should start working. Note onResume is only called once, at least until a different lifecycle event induces a cycle change that comes back to it (log it and see). For more about android lifecycle see [Activity Life Cycle](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) – Maz Feb 18 '12 at 07:19