GLSurfaceView透明背景

时间:2014-10-13 14:07:12

标签: android opengl-es

我在我的项目中使用min3D库来可视化3D模型。该库基于openGL。

为此,我定义了一个管理GLSurfaceView的渲染器。此刻,我在我的应用程序中看到3D模型,但表面视图的背景是黑色的,我的目标是使其透明,这样我只会看到没有黑色背景的3D模型。

min3D库的例子,正如我所提到的其他SO问题和信息,告诉我们实现这一点的方法是:

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

和此:

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

但我不能让背景透明化。

首先,这是我的布局:

<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>

3D模型片段以这种方式在FrameLayout中设置:

@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;
}

最后,这个显示3D模型的片段以及我试图修改的东西使表面变得透明:

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;
    }
}

注意:此片段从属于min3D库的RendererFragment(扩展片段)扩展,这是此类最相关的代码:

@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);
}

1 个答案:

答案 0 :(得分:2)

我回答我自己的问题。经过大量搜索,我终于得到了一个有效的解决方案。只需在glSurfaceViewConfig()中包含此调用:

_glSurfaceView.setZOrderOnTop(true);