androidbuttonopengl-essurfaceview

Need assistance in OpenGL in android


I've created 3 Java classes.

  1. one that has a glsurfaceview object and this calls the renderer class.
  2. this is the renderer class and this calls the cube class.
  3. this is the cube class.

If I run the app then the screen shows a rotating cube (did rotation in the rendering class) which is fine. But I want to control the direction of rotation of the cube and for that I've set 2 buttons. This is where I need help because I don't know to to make the buttons control the movement of the cube. I'm new to Android so if you could leave some code for me to examine then that would be just great.


Solution

  • Your Activity class (or your class that extends Activity) should look like this:

    public class stackoverflowTest extends Activity {
    
    GLSurfaceView glSurface;
    MyRenderer myRenderer;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main);
    
        myRenderer = new MyRenderer(this);        
        //Create an instance of the Renderer with this Activity
    
        glSurface = (GLSurfaceView) findViewById(R.id.graphics_glsurfaceview1);
        //Set our own Renderer and hand the renderer this Activity Context
    
        glSurface.setEGLConfigChooser(true);
    
        glSurface.setRenderer(myRenderer);
        //Set the GLSurface as View to this Activity
    
    
    }
    
    /** 
    * this is the method the button click calls
    */
    public void changeRotationDirection(View v){
    
        myRenderer.changeRotationDirection();
    
    
    }
    
    }
    

    Then in your renderer:

    public class MyRenderer implements Renderer {
    
    private float rotationDirection = 1.0f;
    
    
    public MyRenderer(Context context){
        this.context = context;
    }
    
    public void setRotationDirection(){
    
        if(rotationDirection==1.0f){
            rotationDirection=-1.0f;
        } else {
            rotationDirection=1.0f;
        }
    
    }
    
    @Override
    public void onDrawFrame(GL10 gl) {
            // GL calls
    
            gl.glRotatef(angle, rotateDirection, 0.0f, 0.0f);
    
            // draw cube
            gl.glDrawArrays( etc );
    
        }
    
    }
    

    Basically, you use glRotatef to rotate the cube just before you draw it. Use -ve vales for either the angle parameter (the first) or the x,y,z amount parameters to rotate in the opposite direction. Use method calls to the Renderer to communicate with it and update the scene. Use this approach with caution as the Renderer thread and Main/UI thread (from where the button call is made) can have synchronisation issues

    To make a button call the changeRotationDirection method, simply add android:onClick="changeRotationDirection" into the XML layout (of any view. Doesn't have to be just a button view). Any button methods declared in the XML layout have to be of the form public void [methodname](View [paramname]) and have to be in the Activity class from where the button is pressed

    For more advanced touch control, check as Erik suggested and also check out OnTouchListeners

    (Note: if you're using openGL-ES 2.0 or above (android 2.2+) then use GLES20.glRotatef())