Switching activity

Problems with Canvas, OpenGL, etc...

Switching activity

Postby gberes » Thu Mar 03, 2011 11:40 pm

Hi, i have 2 activity in my game, one for the menu and the other is the game. When i click on new game, the game activity start and works perfect. But when i press the back button, my main menu does not visible correctly, the bottom 1/4 part of the screen rendered 4 times on the whole screen. What's wrong? My code:


Game activity class, the menu activity class is the same, only CGame replaced with CMenu
Code: Select all
public class CGameActivity extends Activity {
   
   private myGlSurfaceView glSurfaceView;
   private CGame game;
   
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Initiate the Open GL view and
        // create an instance with this activity

      game=new CGame(this, this);
        glSurfaceView = new myGlSurfaceView(this, game);

        // set our renderer to be the main renderer with
        // the current activity context
        glSurfaceView.setRenderer(new GlRenderer(this,game));
        setContentView(glSurfaceView);
       
    }

   @Override
   protected void onResume() {
      super.onResume();
      glSurfaceView.onResume();
   }

   @Override
   protected void onPause() {
      super.onPause();
      glSurfaceView.onPause();   
   }
   
}


The GLSurfaceView class:
Code: Select all
public class myGlSurfaceView extends GLSurfaceView {

   private CScene scene;
   
   public myGlSurfaceView(Context context, CScene scene) {
      super(context);

      this.scene=scene;
   }

    @Override
    public boolean  onTouchEvent  (MotionEvent  event){ 
       scene.TapControl(event);
           return true;
    }

}


My renderer:
Code: Select all
public class GlRenderer implements Renderer {

   private Context    context;
   private CScene      scene;
   long mLastTime;
   
   /** Constructor to set the handed over context */
   public GlRenderer(Context context, CScene scene) {
      this.context = context;
      this.scene=scene;
   }

   @Override
   public void onDrawFrame(GL10 gl) {
        long now = System.currentTimeMillis();
       
      if (mLastTime > now) return;
        float dt = (float) ((now - mLastTime) / 1000.0);
        mLastTime = now;
      scene.Update(dt);
      scene.Draw(gl);
   }

   @Override
   public void onSurfaceChanged(GL10 gl, int width, int height) {
      if(height == 0) {                   //Prevent A Divide By Zero By
         height = 1;                   //Making Height Equal One
      }
      gl.glViewport(0, 0, width, height);    //Reset The Current Viewport
      gl.glLoadIdentity();                //Reset The Projection Matrix
      gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
      
      GLU.gluOrtho2D(gl, 0, width, height, 0);

      gl.glMatrixMode(GL10.GL_MODELVIEW);    //Select The Modelview Matrix
      gl.glLoadIdentity();                //Reset The Modelview Matrix
   }

   @Override
   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      scene.LoadTextures(gl);
      gl.glEnable(GL10.GL_TEXTURE_2D);         //Enable Texture Mapping ( NEW )
      gl.glShadeModel(GL10.GL_SMOOTH);          //Enable Smooth Shading
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
      gl.glClearDepthf(1.0f);                //Depth Buffer Setup
      gl.glEnable(GL10.GL_DEPTH_TEST);          //Enables Depth Testing
      gl.glDepthFunc(GL10.GL_LEQUAL);          //The Type Of Depth Testing To Do
      
      gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
      gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
   }

}

I start the new activity with this:
Code: Select all
               Intent myIntent = new Intent(activity, CGameActivity.class);
                activity.startActivity(myIntent);
gberes
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 22, 2011 2:26 pm

Top

Re: Switching activity

Postby raj_J2ME » Wed Mar 09, 2011 10:33 am

Here is the same discussion happening and I hope this may help you.
http://www.anddev.org/other-coding-problems-f5/activity-back-to-home-t51621.html
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Switching activity

Postby gberes » Wed Mar 09, 2011 10:48 am

I can go back to main menu. Now the problem is that i can't see anything, opengl only clears the screen with the given color. The activity runs, the renderer calls the onDrawFrame method and it calls every sprites draw method, i can't see any errors in logcat, but my textures won't appear. Here is my current code:

My opengl renderer:
Code: Select all

public class GlRenderer implements Renderer {

private Context     context;
private CScene      scene;
long mLastTime;

public GlRenderer(Context context, CScene scene) {
    this.context = context;
    this.scene=scene;
}

@Override
public void onDrawFrame(GL10 gl) {
    long now = System.currentTimeMillis();

    if (mLastTime > now) return;
    float dt = (float) ((now - mLastTime) / 1000.0);
    mLastTime = now;
    scene.Update(dt);
    scene.Draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) {                       //Prevent A Divide By Zero By
        height = 1;                         //Making Height Equal One
    }
    gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
    gl.glLoadIdentity();                    //Reset The Projection Matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix

    GLU.gluOrtho2D(gl, 0, width, height, 0);

    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    gl.glLoadIdentity();    //Reset The Modelview Matrix

    scene.LoadTextures(gl);
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
    gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
}
}


My Sprite class:
Code: Select all
public class Sprite {

private FloatBuffer vertexBuffer;   // buffer holding the vertices

private FloatBuffer textureBuffer;  // buffer holding the texture coordinates
private float texture[] = new float[8];
/** The texture pointer */
private int[] textures = new int[1];

private float width;
private float height;
private float x;
private float y;

public Sprite(float _width, float _height, float xpos, float ypos){
    this(_width,_height,xpos,ypos,1.0f,1.0f);
}

public Sprite(float _width, float _height, float xpos, float ypos, float tex_width, float tex_height) {
    //.......
}

public void loadGLTexture(GL10 gl, Context c, Bitmap bitmap) {
    // generate one texture pointer
    gl.glGenTextures(1, textures, 0);
    // ...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    // create nearest filtered texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    // Use Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    // Clean up
    bitmap.recycle();
}

public void draw(GL10 gl) {
    // bind the previously generated texture
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    // Point to our buffers
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // Set the face rotation
    gl.glFrontFace(GL10.GL_CW);
    // Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    gl.glLoadIdentity();
    gl.glTranslatef((float)x, (float)y, 0);

    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0,4);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

}



The structure of my application: Each activity has one GLSurfaceView, and every GLSurfaceView contains a custom Scene. The activity first creates the Scene, which calls the constructor of the sprites. Then the activity creates the GLSurfaceView, which calls the scene's LoadTextures method (from onSurfaceChagned), where its load the bitmap for the sprites on the scene with loadGLTexture. Then the renderer of the GlSurfaceView call the scene's Draw method in onDrawFrame, and the scene's Draw method calls the Sprite's Draw method.

My Scene class:
Code: Select all
public class CScene{
Context context;
public String name;

    protected Activity activity;

    public CScene(Context _context, Activity activity, String name){
        context=_context;
        this.name=name;
        this.activity=activity;
    }


    public void Update(float dt){

    }
    public void Draw(GL10 gl){
    }

    public boolean TapControl(MotionEvent  event)
    {
        return true;
    }

    public void LoadTextures(GL10 gl) {

    }
}
gberes
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 22, 2011 2:26 pm

Re: Switching activity

Postby gberes » Wed Mar 09, 2011 3:45 pm

I finally figured out, these lines in the renderer's onSurfaceChanged mehotd are out of order:

gl.glLoadIdentity(); //Reset The Projection Matrix
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix

If i change their order everything works great.
gberes
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 22, 2011 2:26 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests