1

I'm using min3D library in my project to visualize a 3D model. This library is based in openGL.

For this, I defined a renderer which manages the GLSurfaceView. At this moment, I see the 3D model in my app, but the background of the surface view is black, and my goal is to make it transparent, this way I would only see the 3D model without the black background.

The min3D library examples, as other SO questions and info I've readed about this, tell that the way to achieve this is by doing this:

_glSurfaceView.setEGLConfigChooser(8,8,8,8, 16, 0);
_glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);

and this:

scene.backgroundColor().setAll(0x00000000);

But I don't get to make the background transparent.

First of all, this is my layout:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/valuesContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        ...        
    </LinearLayout>

    <FrameLayout
        android:id="@+id/model3d_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/valuesContainer"/>
</RelativeLayout>

The 3D model fragment is setted inside the FrameLayout this way:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.layout_row3d, container, false);

    Fragment modelFragment = new Obj3DView();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.replace(R.id.model3d_container, modelFragment).commit();

    return view;
}

And finally, this this the fragment which shows the 3D model and where I'm trying to modify things to make the surface trasnparent:

public class Obj3DView extends RendererFragment {

    private Object3dContainer rowObject3D;


    @Override
    protected void glSurfaceViewConfig() {
        // !important
        _glSurfaceView.setEGLConfigChooser(8,8,8,8, 16, 0);
        _glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    }


    /** Called when the activity is first created. */
    @Override
    public void initScene() {
        scene.backgroundColor().setAll(0x00000000);
        scene.lights().add(new Light());
        scene.lights().add(new Light());
        Light myLight = new Light();
        myLight.position.setZ(150);
        scene.lights().add(myLight);
        IParser myParser = Parser.createParser(Parser.Type.OBJ, getResources(), "com.masermic.rowingsoft:raw/row_obj",true);
        myParser.parse();
        rowObject3D = myParser.getParsedObject();
        rowObject3D.position().x = rowObject3D.position().y = rowObject3D.position().z = 0;
        rowObject3D.scale().x = rowObject3D.scale().y = rowObject3D.scale().z = 0.28f;
        // Depending on the model you will need to change the scale faceObject3D.scale().x = faceObject3D.scale().y = faceObject3D.scale().z = 0.009f;
        scene.addChild(rowObject3D);
    }

    @Override
    public void updateScene() {
        rowObject3D.rotation().x += 0.5;
        rowObject3D.rotation().z += 1;
        rowObject3D.rotation().y += 0.1;
    }
}

Note: This fragment extends from RendererFragment(extending fragment) which belongs to the min3D library, and this is the most relevant code of this class:

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

    _initSceneHander = new Handler();
    _updateSceneHander = new Handler();

    //
    // These 4 lines are important.
    //
    Shared.context(getActivity());
    scene = new Scene(this);
    Renderer r = new Renderer(scene);
    Shared.renderer(r);

    _glSurfaceView = new GLSurfaceView(getActivity());
    glSurfaceViewConfig();
    _glSurfaceView.setRenderer(r);
    _glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
masmic
  • 3,526
  • 11
  • 52
  • 105

1 Answers1

2

I answer my own question. After searching a lot, I finally got a solution that works. Just have to include this call inside glSurfaceViewConfig():

_glSurfaceView.setZOrderOnTop(true);
masmic
  • 3,526
  • 11
  • 52
  • 105