Question

May I know how to I change the 3D object in the GLSurfaceView when the button was clicked? My application keep closing whenever I run it on my device. I have 2 classes which implements GLSurfaceView.Renderer which is LessonOneRenderer and LessonOneRenderer2. And I put this code in the button onClick

mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);
                mGLSurfaceView.setRenderer(new LessonOneRenderer2());

but it doesnt work.

Here is my code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id= "@+id/linearlayout1" >

    <Button
        android:id="@+id/buttonID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="A Button" />

    <com.example.test3.MyGLSurfaceView
        android:id="@+id/glSurfaceViewID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.23" />

</LinearLayout>

==============

package com.example.test3;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements android.view.View.OnClickListener
{
    /** Hold a reference to our GLSurfaceView */
    private GLSurfaceView mGLSurfaceView;
    private Button btn;


    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);


        //mGLSurfaceView = new GLSurfaceView(this);
         //setContentView(R.layout.activity_main);
        //mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);



        // Check if the system supports OpenGL ES 2.0.
        final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
        final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

        if (supportsEs2) 
        {
            // Request an OpenGL ES 2.0 compatible context.
            //mGLSurfaceView.setEGLContextClientVersion(2);

            // Set the renderer to our demo renderer, defined below.
            //mGLSurfaceView.setRenderer(new LessonOneRenderer());
            //mGLSurfaceView.setRenderer
            setContentView(R.layout.activity_main);
            //mGLSurfaceView.setRenderer(new LessonOneRenderer());
            //MyGLSurfaceView.mRenderer = new LessonOneRenderer();
            mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);

            mGLSurfaceView.setRenderer(new LessonOneRenderer());

        } 
        else 
        {
            // This is where you could create an OpenGL ES 1.x compatible
            // renderer if you wanted to support both ES 1 and ES 2.
            return;
        }
        //setContentView(R.layout.activity_main);
        //setContentView(mGLSurfaceView);
        btn = (Button)findViewById(R.id.buttonID);
        btn.setOnClickListener(this);
    }

    @Override
    protected void onResume() 
    {
        // The activity must call the GL surface view's onResume() on activity onResume().
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause() 
    {
        // The activity must call the GL surface view's onPause() on activity onPause().
        super.onPause();
        mGLSurfaceView.onPause();
    }   

    public void onClick(View v)
    {
        if(v == btn)
        {
            //finish();
            mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);
            mGLSurfaceView.setRenderer(new LessonOneRenderer2());
        }
    }

}

======================

package com.example.test3;

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


public class MyGLSurfaceView extends GLSurfaceView{

    public MyGLSurfaceView(Context context) {
        super(context);
        setEGLContextClientVersion(2);
    }

    public MyGLSurfaceView(Context context, AttributeSet attrs){
        super(context,attrs);
        setEGLContextClientVersion(2);

    }

}

Thank you.

Was it helpful?

Solution

You can only set renderer on a GLSurfaceView once.

And also, if you assign mGLSurfaceView in onCreate, you don't have to assign it in onClick again. Meaning you dont have to call

mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);

in onClick again, since you already got the value in onCreate. What you can do is:

LessonOneRenderer renderer = new LessonOneRenderer ();//keep reference of the renderer you set
mGLSurfaceView.setRenderer(renderer);
...
public void onClick(View v){
    renderer.click();//do 3D transformation here.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top