JPCT-AE for Android 3D (二)----------爲立方體各面貼上不同的紋理

     Primitives.getCube(10)雖然能夠很方便的創建立方體,但是其支持的紋理映射方式還比較單一。下面將介紹一下如何自己創建立方體併爲立方體的各個面都貼上不同的問題了。

這裏我們使用上一篇文章中的代碼,在CBoxRander中加入如下兩個函數:

// 創建紋理
	private void CreateTextures() {
		Texture texture = new Texture(BitmapHelper.rescale(
				BitmapHelper.convert(mActivity.getResources().getDrawable(
						R.drawable.yi)), 64, 64));
		TextureManager.getInstance().addTexture("yi", texture);

		texture = new Texture(BitmapHelper.rescale(
				BitmapHelper.convert(mActivity.getResources().getDrawable(
						R.drawable.shi)), 64, 64));
		TextureManager.getInstance().addTexture("shi", texture);

		texture = new Texture(BitmapHelper.rescale(
				BitmapHelper.convert(mActivity.getResources().getDrawable(
						R.drawable.zhu)), 64, 64));
		TextureManager.getInstance().addTexture("zhu", texture);

		texture = new Texture(BitmapHelper.rescale(
				BitmapHelper.convert(mActivity.getResources().getDrawable(
						R.drawable.xing)), 64, 64));
		TextureManager.getInstance().addTexture("xing", texture);

		texture = new Texture(BitmapHelper.rescale(
				BitmapHelper.convert(mActivity.getResources().getDrawable(
						R.drawable.wan)), 64, 64));
		TextureManager.getInstance().addTexture("wan", texture);

		texture = new Texture(BitmapHelper.rescale(
				BitmapHelper.convert(mActivity.getResources().getDrawable(
						R.drawable.wen)), 64, 64));
		TextureManager.getInstance().addTexture("wen", texture);
	}


private void CreateBox() {
cube = new Object3D(12);
		
		// 前
		
		cube.addTriangle(GetPoint(-30, -30, 30), 0.0f, 0.0f,
				GetPoint(30, -30, 30), 1.0f, 0.0f, GetPoint(-30, 30, 30), 0.0f,
				1.0f, TextureManager.getInstance().getTextureID("yi"));

		cube.addTriangle(GetPoint(30, -30, 30), 1.0f, 0.0f,
				GetPoint(30, 30, 30), 1.0f, 1.0f, GetPoint(-30, 30, 30), 0.0f,
				1.0f, TextureManager.getInstance().getTextureID("yi"));
	
		// 上
	
		cube.addTriangle(GetPoint(-30, 30, 30), 0.0f, 0.0f,
				GetPoint(30, 30, 30), 1.0f, 0.0f, GetPoint(-30, 30, -30), 0.0f,
				1.0f, TextureManager.getInstance().getTextureID("shi"));

		cube.addTriangle(GetPoint(30, 30, 30), 1.0f, 0.0f,
				GetPoint(30, 30, -30), 1.0f, 1.0f, GetPoint(-30, 30, -30),
				0.0f, 1.0f, TextureManager.getInstance().getTextureID("shi"));
		

		// 後
		
		cube.addTriangle(GetPoint( -30, 30, -30), 0.0f, 0.0f,
				GetPoint(30, 30, -30), 1.0f, 0.0f, GetPoint(-30, -30, -30),
				0.0f, 1.0f, TextureManager.getInstance().getTextureID("zhu"));

		cube.addTriangle(GetPoint(30, 30, -30), 1.0f, 0.0f,
				GetPoint(30, -30, -30), 1.0f, 1.0f, GetPoint(-30, -30, -30),
				0.0f, 1.0f, TextureManager.getInstance().getTextureID("zhu"));
		
		
		// 下
		
		cube.addTriangle(GetPoint(-30, -30, -30), 0.0f, 0.0f,
				GetPoint(30, -30, -30), 1.0f, 0.0f, GetPoint(-30, -30, 30),
				0.0f, 1.0f, TextureManager.getInstance().getTextureID("xing"));

		cube.addTriangle(GetPoint(30, -30, -30), 1.0f, 0.0f,
				GetPoint(30, -30, 30), 1.0f, 1.0f, GetPoint( -30, -30, 30), 0.0f,
				1.0f, TextureManager.getInstance().getTextureID("xing"));
		
		
		
		// 左
		
		cube.addTriangle(GetPoint( -30, -30, -30), 0.0f, 0.0f,
				GetPoint(-30, -30, 30), 1.0f, 0.0f, GetPoint(-30, 30, -30),
				0.0f, 1.0f, TextureManager.getInstance().getTextureID("wan"));

		cube.addTriangle(GetPoint( -30, -30, 30), 1.0f, 0.0f,
				GetPoint(-30, 30, 30), 1.0f, 1.0f, GetPoint(-30, 30, -30),
				0.0f, 1.0f, TextureManager.getInstance().getTextureID("wan"));
		
		// 右
		
		cube.addTriangle(GetPoint(30, -30, 30), 0.0f, 0.0f,
				GetPoint(30, -30, -30), 1.0f, 0.0f, GetPoint(30, 30, 30), 0.0f,
				1.0f, TextureManager.getInstance().getTextureID("wen"));

		cube.addTriangle(GetPoint(30, -30, -30), 1.0f, 0.0f,
				GetPoint(30, 30, -30), 1.0f, 1.0f, GetPoint(30, 30, 30), 0.0f,
				1.0f, TextureManager.getInstance().getTextureID("wen"));
		

		cube.strip();
		cube.build();
		world.addObject(cube);
		cube.setCulling(false);
		cube.scale( 0.4f);
		cube.rotateZ( 180);
		}

然後將onSurfaceCreated修改爲:

@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		world = new World();
		world.setAmbientLight(100, 100, 100);

		sun = new Light(world);
		sun.setIntensity(250, 250, 250);
		sun.setPosition( GetPoint( 0, 0, -150));
		sun.setDiscardDistance( 500);

		// 紋理
		CreateTextures();

		// 立方體
		CreateBox();

		// 攝像機
		cam = world.getCamera();
		cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
		cam.lookAt(cube.getTransformedCenter());

		SimpleVector sv = new SimpleVector();
		sv.set(cube.getTransformedCenter());
		sv.y -= 100;
		sv.z -= 100;
		sun.setPosition(sv);
		MemoryHelper.compact();
	}


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