Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

Here is my two solutions. (You should see the line :glView.setRenderer method. That the basic difference of my two solutions.)

First Solution: cannot work when run. I emplements 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 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 :)

share|improve this question
1  
Obviously you do something in onDrawFrame on your second approach, but not the first one.. You can't just put that code in onResume.. that's not gonna work. – bummzack Feb 17 '12 at 8:55
@bummzack 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:20
1  
onDrawFrame is the rendering code that gets called every frame. onResume has nothing to do with rendering.. it's a callback for your Activity that's being called when the app resumes (eg. after start or pause). See docs. – bummzack Feb 17 '12 at 14:29
@bummzack Can you tell me more why onResume has nothing to do with render,because when app start or resume, it must excute onResume first. Thanks :) – hqt Feb 17 '12 at 14:35
Yes it calls onResume but that's not the place where you put OpenGL rendering code.. You restore your application state in onResume and you save it in onPause. Read the docs, it's all written there. – bummzack Feb 17 '12 at 14:57

closed as too localized by Tetrad Mar 8 at 0:38

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.