GLSurfaceView透明背景 [英] GLSurfaceView transparent background

查看:264
本文介绍了GLSurfaceView透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用在我的项目min3D库可视化3D模型。这个库是基于OpenGL的。

对于这一点,我定义它管理GLSurfaceView渲染。这时,我看到在我的应用程序3D模型,但表面视图的背景是黑色的,我的目标是使它透明,这样我就只看到没有黑色背景的3D模型。

该min3D库的例子,其他SO的问题和信息,我readed这一点,告诉的方式来实现这一目标是通过执行此操作:

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

和这样的:

  scene.backgroundColor()SETALL(00000000)。

但我不明白,使背景透明。

首先,这是我的布局:

 < RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <的LinearLayout
        机器人:ID =@ + ID / values​​Container
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>
        ...
    < / LinearLayout中>    <的FrameLayout
        机器人:ID =@ + ID / model3d_container
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_below =@ + ID / values​​Container/>
< / RelativeLayout的>

3D模型片段设置好的了里面的FrameLayout这种方式:

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    查看查看= inflater.inflate(R.layout.layout_row3d,集装箱,FALSE);    片段modelFragment =新Obj3DView();
    FragmentTransaction交易= getChildFragmentManager()调用BeginTransaction()。
    transaction.replace(R.id.model3d_container,modelFragment).commit();    返回视图。
}

最后,足见其3D模型,其中我试图改变的事情,使表面trasnparent这个这个片段:

 公共类Obj3DView扩展RendererFragment {    私人Object3dContainer rowObject3D;
    @覆盖
    保护无效glSurfaceViewConfig(){
        //!重要
        _glSurfaceView.setEGLConfigChooser(8,​​8,8,8,16,0);
        。_glSurfaceView.getHolder()和setFormat(PixelFormat.TRANSLUCENT);
    }
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效initScene(){
        scene.backgroundColor()SETALL(00000000)。
        。scene.lights()增加(新灯());
        。scene.lights()增加(新灯());
        光myLight =新灯();
        myLight.position.setZ(150);
        。scene.lights()加(myLight);
        IParser myParser = Parser.createParser(Parser.Type.OBJ,getResources(),com.masermic.rowingsoft:生/ row_obj,真正的);
        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。;
        //根据型号的不同,你需要改变比例faceObject3D.scale()X = faceObject3D.scale()Y = faceObject3D.scale()Z = 0.009f。;
        scene.addChild(rowObject3D);
    }    @覆盖
    公共无效updateScene(){
        rowObject3D.rotation()X + = 0.5。
        。rowObject3D.rotation()Z + = 1;
        。rowObject3D.rotation()Y + = 0.1;
    }
}

注:此片段从RendererFragment(延长段)属于min3D图书馆延伸,这是该班的最相关的code:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    _initSceneHander =新的处理程序();
    _updateSceneHander =新的处理程序();    //
    //这4个线是很重要的。
    //
    Shared.context(getActivity());
    现场=新场景(本);
    渲染R =新的渲染(场景);
    Shared.renderer(R);    _glSurfaceView =新GLSurfaceView(getActivity());
    glSurfaceViewConfig();
    _glSurfaceView.setRenderer(R);
    _glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}


解决方案

我回答我的问题。搜索了很多之后,我终于得到了一个可行的解决方案。只是必须包括在 glSurfaceViewConfig此调用()

  _glSurfaceView.setZOrderOnTop(真);

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

解决方案

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

这篇关于GLSurfaceView透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆