min3d引擎使用指南(Android)

這節講  3D模型的背景透明。  先上效果圖。

 

然後在加XML文件,相信大家看得懂---背景透明的效果

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.keyshare3d.MainActivity"
    tools:ignore="MergeRootFrame" >

    <FrameLayout
        android:id="@+id/frame3d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </FrameLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView\n\n\n\n\n\n\n\n\n\n\n這是透明背景\nXML中的分層顯示\t \t \t PixelFormat.TRANSLUCENT\n_glSurfaceView.setZOrderOnTop(true);" />

</FrameLayout>


雖然只有一個TextView和 FrameLayout,但互相間的背景,並沒有覆蓋。

然後是MainActivty.java

 

ublic class MainActivity extends RendererActivity  {
	Object3dContainer faceObject3D;     //3D對象  
	Object3dContainer faceObject_2;     //3D對象  
	Object3dContainer _bg;
	Screen screen; //= new Screen(getApplicationContext());	//攝像頭對象,這裏沒什麼用。本來是想背景關聯照相機的。
	Object3dContainer _plane;
	Light _lightRed;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        screen = new Screen(getApplicationContext());//放在這裏不崩潰
        FrameLayout ll = (FrameLayout) this.findViewById(R.id.frame3d);//在xml中,指定顯示的控件,這裏是一個FrameLayout。        
        ll.addView(_glSurfaceView);//加入到_glSurfaceView中顯示。   
        SurfaceHolder holder = null;
		screen.surfaceCreated(holder);
        
        Toast.makeText(MainActivity.this, "執行正常", Toast.LENGTH_SHORT).show();  
    }
    public void initScene() {
    	_lightRed = new Light();
    	_lightRed.ambient.setAll(0x88110000);
		_lightRed.diffuse.setAll(0xffff0000);
		_lightRed.position.setZ(2);
		_lightRed.position.setX(2);
		_lightRed.type(LightType.POSITIONAL);
		scene.lights().add(_lightRed);
    	scene.backgroundColor().setAll(0x00000000);
        scene.lights().add(new Light()); //設置燈光
        
        IParser myParser = Parser.createParser(Parser.Type.MAX_3DS,
				getResources(), "com.example.keyshare3d:raw/ffffff", false);
        myParser.parse();  //Start parsing the 3D object
        faceObject3D = myParser.getParsedObject();  
        faceObject3D.scale().x = 0.007f;//模型大小縮放
		faceObject3D.scale().y = 0.007f;
		faceObject3D.scale().z = 0.007f;
		
        faceObject3D.position().x =0;//設置初始位置,平截頭體中xyz的座標位置
        faceObject3D.position().y =0;
        faceObject3D.position().z =0;   
       
        scene.camera().target = faceObject3D.position();//設置鏡頭位置 -對準模型
        
        Bitmap b = Utils.makeBitmapFromResourceId(this, R.drawable.sssss);
		float w = 2f;
		float h = w * (float)b.getHeight() / (float)b.getWidth();; 
		
		_plane = new Rectangle(w, h, 1,1, new Color4());
		_plane.doubleSidedEnabled(true); // ... so that the back of the plane is visible
		_plane.normalsEnabled(false);
		scene.addChild(_plane);

		Shared.textureManager().addTextureId(b, "mickey", false);
		_plane.textures().addById("mickey");
		
		b.recycle();
		faceObject_2 = faceObject3D.clone();
		faceObject_2.position().x = 1;
		scene.addChild(faceObject_2);
        scene.addChild(faceObject3D);//把模型加入到場景,這樣模型才能顯示,還可以在faceObject3D中加入新模型,同步顯示。
        faceObject3D.colorMaterialEnabled(true); //打開材質支持,默認是關閉的
   }
    
    public void updateScene() {    
 //   	faceObject3D.rotation().x++;//圍繞着X軸旋轉,正數爲順時針,速度由X的數值變化決定  
    	faceObject3D.rotation().y++;//可以單獨圍繞着Y軸(或XZ)旋轉,這個任意,根據外部傳感器同步姿態也可以。  
    	faceObject3D.rotation().z++;//旋轉在飛行器上的參數,對應的是pitch,roll,yaw 
    	_plane.rotation().z += 0.2f;
    	}



}


重點來了。需要在public class MainActivity extends RendererActivity,所繼承的RendererActivity中添加代碼:

protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		_initSceneHander = new Handler();
		_updateSceneHander = new Handler();
		
		//
		// These 4 lines are important.
		//
		Shared.context(this);
		scene = new Scene(this);
		Renderer r = new Renderer(scene);
		Shared.renderer(r);
		
		_glSurfaceView = new GLSurfaceView(this);
		
                 glSurfaceViewConfig();//從這後面開始加
        _glSurfaceView.setZOrderOnTop(true);
        _glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        _glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        //設置透明,在這句前面
		_glSurfaceView.setRenderer(r);
		
		//Set the rendering mode. 
		//When renderMode is RENDERMODE_CONTINUOUSLY, the renderer is called repeatedly to re-render the scene. 
		//When renderMode is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface is created, or when requestRender() is called. 
		//默錕較詫拷錕斤拷爲錕斤拷RENDERMODE_CONTINUOUSLY. 
		_glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
		
        onCreateSetContentView();
	}


編譯運行,  試試看吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章