androidopengl-esopengl-es-2.0glsurfaceview

my android GLSurfaceView.Renderer never calls onSurafaceCreated()


I am trying to follow the official android opengl es2 tutorial given here. However my renderer's onSurfaceCreated method is never called. Any advice?

//GLRenderer.java
package com.studio.test1;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;

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

public class GLRenderer implements GLSurfaceView.Renderer {
    private Triangle triangle;
    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame color
        Log.i("test","xxxxxxxx");
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        triangle = new Triangle();
    }

    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        //GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        Log.i("open","xxxxxx");
        triangle.draw();
    }

    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }
    public static int loadShader(int type, String shaderCode){

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }
}

Here is the main activity file.

//MainActivity.java
package com.studio.test1;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
//import android.view.Menu;
import android.view.SurfaceView;
import android.content.Context;
import android.util.Log;
import android.graphics.*;
import com.studio.graphics.Graph;
import com.studio.graphics.Image;
import com.studio.menu.*;

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

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

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SurfacePanel(this));
        Log.i("hello","world");
    }
    public class SurfacePanel extends GLSurfaceView {
        private int xmax;
        private int ymax;
        private final GLRenderer renderer;
        public SurfacePanel(Context context) {
            super(context);
            Log.i("making", "panel");
            setEGLContextClientVersion(2);
            renderer = new GLRenderer();
            setRenderer(renderer);
            //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder holder){

        }
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){

        }
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.v("surface","created");
            xmax = getWidth();
            ymax = getHeight();
        }

        public boolean onTouchEvent(MotionEvent e){
            int action = e.getActionMasked();
            Point z=new Point(e.getX(),e.getY());
            switch(action){
                case MotionEvent.ACTION_DOWN:
                    TouchMaster.onDown(z);
                    SwipeMaster.down(z);
                    break;
                case MotionEvent.ACTION_UP:
                    TouchMaster.onUp(z);
                    SwipeMaster.up(z);
                    break;
                case MotionEvent.ACTION_MOVE:
                    TouchMaster.onMove(z);
                    SwipeMaster.move(z);
            }
            return true;
        }
    }
}

Solution

  • If you extends GLSurfaceView then don't forget to call super when you override surfaceview's life cycle method such as onSurfaceCreate etc.