[英]GLSurfaceView transparent background
I'm using min3D library in my project to visualize a 3D model. 我在项目中使用min3D库来可视化3D模型。 This library is based in openGL. 该库基于openGL。
For this, I defined a renderer which manages the GLSurfaceView. 为此,我定义了一个管理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. 此时,我在应用程序中看到了3D模型,但是表面视图的背景是黑色的,而我的目标是使其透明,这样,我只会看到没有黑色背景的3D模型。
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: min3D库示例以及其他我已阅读过的关于SO的问题和信息都表明,实现此目标的方法是这样做:
_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: 通过以下方式在FrameLayout中设置3D模型片段:
@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: 最后,这是显示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;
}
}
Note: This fragment extends from RendererFragment(extending fragment) which belongs to the min3D library, and this is the most relevant code of this class: 注意:此片段是从属于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);
}
I answer my own question. 我回答我自己的问题。 After searching a lot, I finally got a solution that works. 经过大量搜索,我终于找到了可行的解决方案。 Just have to include this call inside glSurfaceViewConfig()
: 只需在glSurfaceViewConfig()
包含此调用glSurfaceViewConfig()
:
_glSurfaceView.setZOrderOnTop(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.