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