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

來源:互聯網
上載者:User

     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修改為:

@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {// TODO Auto-generated method stubworld = 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();}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.