首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GLSurfaceView透明背景

GLSurfaceView透明背景
EN

Stack Overflow用户
提问于 2014-10-13 14:07:12
回答 1查看 1.3K关注 0票数 1

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

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

min3D库的例子,就像我阅读过的其他问题和信息一样,告诉我们实现这一目标的方法是通过这样做:

代码语言:javascript
复制
_glSurfaceView.setEGLConfigChooser(8,8,8,8, 16, 0);
_glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);

这是:

代码语言:javascript
复制
scene.backgroundColor().setAll(0x00000000);

但我不能让背景变得透明。

首先,这是我的版面:

代码语言:javascript
复制
<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中设置的:

代码语言:javascript
复制
@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模型的片段,我试图通过修改物体来进行表面转移:

代码语言:javascript
复制
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;
    }
}

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

代码语言:javascript
复制
@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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-13 14:50:07

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

代码语言:javascript
复制
_glSurfaceView.setZOrderOnTop(true);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26341995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档