Mungunbat Enkhbayar
Mungunbat Enkhbayar

Reputation: 181

GLThread or GLSurfaceview overlapping issue?

There are 2 GLSurfaceViews in my application(android, opengles 2.0). Two of them are overlapped so i can use top of it as a preview. My Problem is when i launch the app the top glsurfaceview`s contents are disappeared(i set a different background color from the background glview.So i can distinguish between disappearing of contents or the whole GLview) after short moment. I have no clue where to start looking for the issue. My codes are below.

Thanks in advance.

package jp.android.MyProject;

import android.app.Activity;
import android.widget.TextView;

import android.view.ViewGroup.LayoutParams;
import android.os.Bundle;


public class MyProjectActivity extends Activity {


MyOpenGLView myGLView;
PreviewGLView previewView;
private int sizeofPreview = 300;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myGLView = new MyOpenGLView(this);
    setContentView(myGLView);
    myGLView.requestFocus();
    myGLView.setFocusableInTouchMode(true);

    previewView = new PreviewGLView(this);
    addContentView(previewView,new LayoutParams(sizeofPreview, sizeofPreview));

    previewView.setBackgroundColor(0xFF000000);

    myGLView.textWindow = new TextView(this);
    addContentView(myGLView.textWindow,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    myGLView.touchWindow = new TextView(this);
    addContentView(myGLView.touchWindow, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    myGLView.touchWindow.setY(600.0f);
}

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

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

}

Upvotes: 0

Views: 715

Answers (1)

James Coote
James Coote

Reputation: 1973

I don't believe you can (or are supposed to) use two overlapping GLSurfaceView's in an android application. It just isn't designed that way

The better way to do it would be to put all your code for drawing the main scene and the preview scene into two different methods in your GLSurfaceView.Renderer

Then during onDrawFrame(GL10 gl), call the main draw method, then clear the depth buffer but not the colour buffer, then call the method to draw the preview over the top of the main scene. You should still be clearing both buffers at the start of the onDrawFrame(GL10 gl) in your Renderer

public class MyRenderer implements GLSurfaceView.Renderer {

/**
 * GLSurfaceView onto which stuff is rendered
 */
private GLSurfaceView mGLView;

/**
 * The current scene to render
 */
public MyScene mScene;

/**
 * The preview scene to draw over the top
 */
public MyScene mPreviewScene;

/**
 * 
 * @param The GLSurfaceView this renderer is attached to
 */
MyRenderer(GLSurfaceView mGLView;){
    this. mGLView =  mGLView;;
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    // clear the background
    gl.glClearColor(0, 0, 0, 1);

            // Enable depth testing
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthMask(true);
    gl.glDepthFunc(GL10.GL_LEQUAL);  

    // enable back face culling
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glFrontFace(GL10.GL_CCW);        
    gl.glCullFace(GL10.GL_BACK);

}

public void onSurfaceChanged(GL10 gl, int width, int height) {

    // set the new viewport width/height
    setViewportWidth(width);
    setViewportHeight(height);

}


public void onDrawFrame(GL10 gl) {

    // clear the canvas at the start of the draw call
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);


        // draw scene
        gl.glPushMatrix();

        // draw the foreground scene
        if(mScene!=null){
            mScene.draw(gl);
            // clear the depth buffer only!         
            gl.glClear(GL10.GL_DEPTH_BUFFER_BIT);
        }

        gl.glPopMatrix();

        gl.glPushMatrix();

        // draw the preview scene on top
        if(mPreviewScene!=null){
            mPreviewScene.draw(gl);
        }

        gl.glPopMatrix();


    return;

}
}

__

public interface MyScene{

    public void draw(GL10 gl);
}

__

public class MyOpenGLScene implements MyScene{

    // etc

}

__

public class PreviewGLScene implements MyScene{

    // etc

}

__

GLSurfaceView mGLView;
MyOpenGLScene myScene;
PreviewGLScene previewScene;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGLView = new GLSurfaceView(this);
    MyRenderer renderer = new MyRenderer(mGLView);

    myScene = new MyOpenGLScene();
    previewScene = new PreviewGLScene();

    renderer.mScene = myScene;
    renderer.mPreviewScene = previewScene;

    mGLView.setRenderer(renderer);

    setContentView(myGLView);
}

Note: the calls are all in OpenGL ES 1.0 here, but should be easy to work out the OpenGL ES 2.0 equivalents.

Also, you don't actually need to override GLSurfaceView (though if you have, and are doing the rendering in GLSurfaceView, you can just swap the methods I put into MyRenderer into a single GLSurfaceView and the result should be the same)

Upvotes: 1

Related Questions