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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值