2
\$\begingroup\$

I'm new in game development. I have a view using GLSurfaceview and call in MainActivity.

I want to make transparent that view. I have tried setZOrderOnTop() method in MainActivity and glClearColor() but both are not working.

public class RubikActivity extends Activity {

        cubeView cv;
        GLSurfaceView gv;


        @SuppressLint("NewApi")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity);
        cv = (cubeView) findViewById(R.id.cubeView);
        cv.initialize(PreferenceManager.getDefaultSharedPreferences(this));
         cv.requestFocus();
            cv.setFocusableInTouchMode(true);
            cv.setEGLConfigChooser(8, 8, 8, 8, 8,0); 
          //  cv.setRenderer( new CubeRenderer(getApplicationContext(), font, mWorld, rCube, mMenu, prefs);



            cv.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        cv.setZOrderOnTop(true);

        }

cubeView.java

public cubeView(Context context, AttributeSet attrs) {
super(context, attrs);
font = new TextureFont(getContext(), R.drawable.roboto_regular, "roboto_regular_dims.txt");
mWorld = new GLWorld();

}

public void initialize(SharedPreferences prefs) {
rCube = new Rubec(mWorld, prefs.getInt("dim", 3));
mMenu = new CubeMenu(rCube, font);
_renderer = new CubeRenderer(getContext(), font, mWorld, rCube, mMenu, prefs);
rCube.setRenderer(_renderer);
mWorld.setRubeCube(rCube);
setRenderer(_renderer);

getHolder().setFormat(PixelFormat.TRANSLUCENT);
setZOrderOnTop(true);
}

cubeRenderer.java

@Override
public void onDrawFrame(GL10 g) {
GL11 gl = (GL11)g;

surfaceSetup(gl);

gl.glClearColor(1.0f, 1.0f, 1.0f,0);
gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
gl.glDisable(GL11.GL_DEPTH_TEST);
gl.glMatrixMode(GL11.GL_MODELVIEW);
gl.glShadeModel(GL11.GL_SMOOTH);
    gl.glLoadIdentity();


GLU.gluLookAt(gl, 0f, 0f, 7f,
          0f, 0f, 0f,
          0f, 1f, 0f);
gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelViewMatrix, 0);
if(!worldBoundsSet) {
    getWorldBounds();
}
gl.glColor4f(0.5f, 0.5f, 0.5f, 1);
gl.glEnable(GL11.GL_DEPTH_TEST);
gl.glEnableClientState(GL11.GL_COLOR_ARRAY);
    gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
menu.draw(gl);
mWorld.draw(gl);
gl.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
/*gl.glDisableClientState(GL11.GL_COLOR_ARRAY);*/
gl.glDisableClientState(GL11.GL_VERTEX_ARRAY);


//gl.glFlush();
}

enter image description here

I want the white color to show as transparent background in my layout. I am already use glClearColor(0,0,0,0) which shows as a black color and already use in xml file:

 <com.rtpl.rubikgame.cubeView
  android:id="@+id/cubeView1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:background="#000000"
  android:windowIsTranslucent="true"/>
</LinearLayout>

but nothing happened; when we use glClearColor(0,0,0,0) it shows this:

enter image description here

\$\endgroup\$
1
\$\begingroup\$
final GLSurfaceView glView = (GLSurfaceView) findViewById(R.id.myGlView);

glView.setZOrderOnTop(true);
glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glView.getHolder().setFormat(PixelFormat.RGBA_8888);

What this does

Displays GLSurfaceView on top of other views, this is required.
Configures OpenGL and the surface to allow transparent drawing.


Source

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

had the same issue another day, found the solution: in your renderer onDrawFrame() method add very last (it's important) string like this:

gl.glClearColor(0, 0, 0, 0);

Hope it would help someone. Also, to get transparent background in layout put "#80" before color code. It doesn't seem to work for glSurfaceView although, cause default renderers usually redraw view background.

Better later than never =)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That's not a string. \$\endgroup\$ – Bálint Nov 12 '16 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.