手機上J2ME的3D編程-簡單創建3D立方體(1)

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 手機上J2ME的3D編程-簡單創建3D立方體(1) (http://www.webasp.net/article/18/17272.htm)
-- 作者:未知
-- 發佈日期: 2005-03-31
參考文檔:JSR-184

工具:Eclipse3.0+Eclipseme

API:Mobile 3D Graphics API

模擬器:Sony Ericsson

分析:

在現實生活中,我們所看到的是一個3維繫統。透過雙眼,可以看到的是一個豐富多彩的世界。我們所用的照相機,能夠拍出很多漂亮的圖片。拍攝每張圖片時,可以根據拍攝距離的遠近可以調整所拍攝畫面上的內容。








在虛擬的世界裡,可以模擬真實世界。欲將虛擬世界展示出來,就要利用一個虛擬的照相機,將虛擬的世界拍攝出來。








在JSR-184中,虛擬的世界是由World類構造的。虛擬的照相機是Camera類。通過Camera的位置或視角的改面,將虛擬世界顯示到手機屏幕上。Camera默認是朝向Z軸的負方向。在虛擬世界中,各個物體都是由Mesh類創建的。不同的Mesh屬性對應不同的物體。








構造物體時,先要構造物體的骨架(即形狀),然後用一種材料蒙到骨架上。物體的形狀,是由一組點和每個點的法向量決定的。材料則是圖片。








下面按照與上面相反的順序再一個虛擬的世界中構建一個立方體。步驟如下:

1 構造立方體





構造一個立方體需要給出立方體的各個頂點,然後規定各個頂點的法向量,最後將圖片帖到由頂點和各點法向量構成的架子上。








各個頂點的給出,是和法向量配合的。由於立方體是由6個矩形組成的,所以就要確定這6個矩形。因為由一個點和一個法向量就能夠確定一個面,那麼4個頂點和4個法向量就確定了4個面。如果這4個面重疊,4個頂點就構成了一個矩形的區域。用這個方法確定6個矩形,再用這6個舉行組成一個立方體。








將圖片帖到立方體是,就是將立方體上的頂點和圖片上的點進行對應。兩者可以用不同的單位。


下面構建一個立方體

給出立方體的頂點們:





short x = 20;

short y = 20;

short z = 20;

short fx = (short) -x;

short fy = (short) -y;

short fz = (short) -z;



short[] vert = {x,y,z, fx,y,z, x,fy,z, fx,fy,z, //D

fx,y,fz, x,y,fz, fx,fy,fz, x,fy,fz, //C

fx,y,z, fx,y,fz, fx,fy,z, fx,fy,fz, //B

x,y,fz, x,y,z, x,fy,fz, x,fy,z, //F

x,y,fz, fx,y,fz, x,y,z, fx,y,z, //A

x,fy,z, fx,fy,z, x,fy,fz, fx,fy,fz}; //E



VertexArray vertArray = new VertexArray(vert.length/3, 3, 2);

vertArray.set(0, vert.length/3, vert);



給出立方體頂點們的法向量:





byte[] norm = { 0,0,127, 0,0,127, 0,0,127, 0,0,127,

0,0,-127, 0,0,-127, 0,0,-127, 0,0,-127,

-127,0,0, -127,0,0, -127,0,0, -127,0,0,

127,0,0, 127,0,0, 127,0,0, 127,0,0,

0,127,0, 0,127,0, 0,127,0, 0,127,0,

0,-127,0, 0,-127,0, 0,-127,0, 0,-127,0};








VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);

normArray.set(0, norm.length / 3, norm);








給出頂點們對應圖片上的點(vert和tex數組是一一對應的):





short[] tex = { 1, 0, 0, 0, 1, 1, 0, 1,

1, 0, 0, 0, 1, 1, 0, 1,

1, 0, 0, 0, 1, 1, 0, 1,

1, 0, 0, 0, 1, 1, 0, 1,

1, 0, 0, 0, 1, 1, 0, 1,

1, 0, 0, 0, 1, 1, 0, 1 };








VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);

texArray.set(0, tex.length / 2, tex);








根據上面給出的數組,組成立方體:





// create the VertexBuffer for our object

VertexBuffer vb = boxVB = new VertexBuffer();

vb.setPositions(vertArray, 1.0f, null);

vb.setNormals(normArray);

vb.setTexCoords(0, texArray, 1.0f, null);















2 將立方體實例化












// the length of each triangle strip

int[] stripLen = { 4, 4, 4, 4, 4, 4 };








// create the index buffer for our object (this tells how to

// create triangle strips from the contents of the vertex buffer).

TriangleStripArray tsa = new TriangleStripArray(0, stripLen);








//創建多邊形模式。

PolygonMode pm = new PolygonMode();

pm.setShading(PolygonMode.SHADE_SMOOTH);

pm.setCulling(PolygonMode.CULL_NONE);








//生成外貌。

Appearance app = new Appearance();

app.setPolygonMode(pm);








Image texImg= null;

try{

texImg = Image.createImage(this.imageUrl);

}catch(Exception e){

String strErr="Load Image (path=";

strErr += this.imageUrl;

strErr += ") Fail!";

System.err.println(strErr);

}





Texture2D texture=null;

try{

// create texture from loaded image.

texture = new Texture2D(new Image2D(Image2D.RGB, texImg));

}catch(Exception e){

e.printStackTrace();

String strErr = "Failed to create texture Use Format --- Image2D.RGB";

System.out.println(strErr);

}



try{

// repeat texture on surface

texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT);



}catch(Exception e){

e.printStackTrace();

System.out.println("Failed to create texture3");

}



try{

// Blend mode to use.

texture.setBlending(Texture2D.FUNC_DECAL);

}catch(Exception e){

System.out.println("Failed to create texture4");

}



try{

// Use nearest for performance, linear for quality

texture.setFiltering(Texture2D.FILTER_NEAREST,Texture2D.FILTER_NEAREST);

}catch(Exception e){

System.out.println("Failed to create texture5");

}



try{

app.setTexture(0, texture);

}catch(Exception e){

System.out.println("Failed to create texture6");

}








//創建網格。

mesh = new Mesh(vb, tsa, app);

mesh.setAppearance(0, app);















3 創建World





//create a camera

camera = new Camera();

camera.setPerspective(60.0f, // field of view

(float) getWidth() / (float) getHeight(), // aspectRatio

1.0f, // near clipping plane

1000.0f); // far clipping plane

camera.setTranslation(0,0, 50);








world = new World();

world.addChild(camera);



world.addChild(mesh);

world.setActiveCamera(camera);















4 繪製World





//private Graphics3D g3d = null;








protected void paint(Graphics g) {

// TODO Auto-generated method stub

Draw(g);

}

private void Draw(Graphics g){

try{

g3d.bindTarget(g);

g3d.render(world);

}catch(Exception e){



}finally{

g3d.releaseTarget();

}

}















5 旋轉立方體












mesh.postRotate(1.0f, 1.0f, 0f, 0f);

意識是以向量為(1,0,0)的方向(就是x軸)為軸將立方體旋轉1度。















至此,就是建造一個虛擬的世界,並放置一個立方體的步驟。上面的代碼是從一個完整的程序中截取的,有的地方可能不方便理解。後面將附上完整的代碼。





webasp.net