J2ME 3D programming on mobile phone-simple to create 3D cubes (1)

Source: Internet
Author: User
Tags addchild
Programming | Creating reference documents: JSR-184

Tools: Eclipse3.0+eclipseme

Api:mobile 3D Graphics API

Simulator: Sony Ericsson

Analysis:

In real life, what we see is a 3-dimensional system. Through the eyes, can see is a rich and colorful world. We use the camera, can take a lot of beautiful pictures. When you shoot each picture, you can adjust the content on the screen according to the distance between the shots.








In a virtual world, you can simulate the real world. To show the virtual world, you need to use a virtual camera, the virtual world to shoot out.








In the JSR-184, the virtual world is constructed by the worlds class. The virtual camera is the camera class. The virtual world is displayed on the mobile screen by changing the camera position or angle of view. Camera default is a negative direction toward the z-axis. In the virtual world, each object is created by the mesh class. Different mesh properties correspond to different objects.








When constructing an object, the skeleton (i.e. the shape) of the object is first constructed, and then a material is used to cover the skeleton. The shape of an object is determined by a set of points and by the normal vector of each point. The material is the picture.








Next, build a cube in a virtual world, in the opposite order. The steps are as follows:

1 Structural cubes





Constructing a cube needs to give each vertex of the cube, then specify the normal vector of each vertex, and finally post the picture to a shelf composed of vertices and the normal vectors of each point.








Each vertex is given in conjunction with the normal vector. Since the cube is made up of 6 rectangles, it is necessary to determine the 6 rectangles. Since a single point and a normal vector are able to determine a plane, then 4 vertices and 4 normal vectors determine 4 faces. If these 4 faces overlap, the 4 vertices form a rectangular region. Use this method to determine 6 rectangles, and then use these 6 to form a cube.








To paste the picture into the cube is to correspond the vertices on the cube to the points on the picture. The two can be in different units.


Build a cube below

Give the vertices of the cube:





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



gives the normal vectors of the vertices of the cube:





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








Give the vertices corresponding to the points on the picture (Vert and Tex arrays are one by one corresponding):





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








form a cube According to the array given above:





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 instantiation of the cube












The length of each triangle strip

Int[] Striplen = {4, 4, 4, 4, 4, 4};








Create the index buffer for we object (this tells

Create triangle strips from the contents of the vertex buffer).

Trianglestriparray TSA = new Trianglestriparray (0, Striplen);








Create a polygon pattern.

Polygonmode pm = new Polygonmode ();

Pm.setshading (Polygonmode.shade_smooth);

Pm.setculling (Polygonmode.cull_none);








Generate appearance.

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");

}








Create a grid.

mesh = new Mesh (VB, TSA, APP);

Mesh.setappearance (0, app);















3 Creating the 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 Drawing 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 Rotating cubes












Mesh.postrotate (1.0f, 1.0f, 0f, 0f);

Consciousness rotates the cube 1 degrees in a vector (1,0,0) direction (that is, an X axis).















So far, is to build a virtual world, and put a cube of steps. The code above is intercepted from a complete program and may not be easily understood in some places. The complete code will be attached later.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.