johnsonwi
johnsonwi

Reputation: 159

Android OpenGL hello world

By following the short tutorial http://developer.android.com/training/graphics/opengl/environment.html#glsurfaceview. My example does not work in the emulator. I wish to have a basic opengl example working in the emulator but it continues to be a problem and even the instructions by the developers fail to work.

I have three classes:

package com.test.flushrummy;

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

public class MainActivity extends Activity {

    private GLSurfaceView m_GlView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        m_GlView = new MyGLSurfaceView(this);
        setContentView(m_GlView);
    }

}

package com.test.flushrummy;

import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;

public class MyGLRenderer implements Renderer {

    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }

    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onSurfaceCreated(GL10 gl,
            javax.microedition.khronos.egl.EGLConfig config) {
        // TODO Auto-generated method stub
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

}

package com.test.flushrummy;

import android.content.Context;
import android.opengl.GLSurfaceView;

class MyGLSurfaceView extends GLSurfaceView {

    public MyGLSurfaceView(Context context) {
        super(context);
        setRenderer(new MyGLRenderer());

        // Create an OpenGL ES 2.0 context
        setEGLContextClientVersion(2);

        // Render the view only when there is a change in the drawing data
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

}

I have in my manifest.

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />

project.properties:

target=android-19

"use gpu host" is snabled in avd.

Upvotes: 1

Views: 1719

Answers (3)

drumbumLOLcatz
drumbumLOLcatz

Reputation: 131

I had this same problem, and this is what was wrong, so it might be your problem too:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //COMMENT OUT THIS COMMAND
    //setContentView(R.layout.activity_main);

    /*if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    */
    //Setup surface view in this activity
    mView = new GLSurfaceView(this);
    mView.setEGLContextClientVersion(2);

    mView.setRenderer(new GraphicRenderer());
    mView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    setContentView(mView);

}

The template code setContentView() needs to be commented out. then it won't crash on loading.

Upvotes: 0

johnsonwi
johnsonwi

Reputation: 159

setEGLContextClientVersion(2);
setRenderer (new MyGLRenderer());
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

It appears the order of method invokes is important.

Upvotes: 1

nitesh goel
nitesh goel

Reputation: 6406

add the GPU emulation hardware property and set its value to yes in your emulation. Add them and try it.

when you create a new virtual device there is a hardware section . add new to it . there is a option of GPU emulation add this.

Upvotes: 1

Related Questions