[J2me3d series study Article 2] (instant mode) constructs the first cube in our 3D world!

Source: Internet
Author: User
Tags try catch


 

Source: http://blog.csdn.net/xiaominghimi/archive/2010/12/07/6059650.aspx

Himi original, reprinted please note! Thank you.

 

 

 

Why is it necessary to construct a cube first? In fact, cubes are an excellent example when demonstrating concepts. Of course they are not a milestone in complex 3D design.

 

First, we will introduce the steps required to construct a 3D Cube: (general steps)

1. Construct the spatial vertex of a cube

Second: the triangle band is used to construct each plane of a cube. For details about the triangle band, see @ Note 1.

Third: Construct a camera

Fourth: bind a paint brush

Fifth: rendering.

 

The following code has been annotated and I believe it can be understood! Some remarks are explained below!

 

 

 

 

 

Import javax. microedition. lcdui. graphics; <br/> Import javax. microedition. lcdui. game. gamecanvas; <br/> Import javax. microedition. m3G. appearance; <br/> Import javax. microedition. m3G. camera; <br/> Import javax. microedition. m3G. graphics3d; <br/> Import javax. microedition. m3G. transform; <br/> Import javax. microedition. m3G. trianglestriparray; <br/> Import javax. microedition. m3G. vertexarray; <br/> Import javax. m Icroedition. m3G. vertexbuffer; <br/>/** <br/> * @ author himi <br/> */<br/> public class my3dworld extends gamecanvas implements runnable {<br/> private thread th; <br/>/** <br/> * @ author himi <br/> * @ vertex_positions defines an array of all vertices loaded into the cube in the form of triangles <br/> * @ vertex_colors defines color arrays in the form of triangles <br/> * @ triangle_indices loads cubes in the form of triangles all faces <br/> * @ vertexarray: Vertex_positions defines vertex arrays but not space points, <br/> * this class stores vertex arrays as space vertex coordinates, normal information, <br/> * saves post image information, and color information. <br/> * <br/> * @ vertexbuffer, to create a graph <br/> * (this class is the class for saving the frame information of a polygon) set the vertex attributes <br/> *, including the position, normal, color, texture coordinates <br/> * @ transform perform a series of operations on the cube, for example, invert, pan, zoom <br/> * @ camera 3D camera projection, viewing angle, etc. <br/> * @ trianglestriparray this class concatenates a surface into a cube by triangle band <br/> */<br/> Private Static fin Al byte [] vertex_positions = {-1,-1, 1, 1,-1, 1,-1, <br/> 1, 1, 1, 1, 1, 1, -1,-1,-1, 1,-1,-1,-1, 1,-1, 1, 1,-1 }; <br/> // @ Note 1 <br/> Private Static final byte [] vertex_colors = {0, (byte) 255, 0, 0, <br/> (byte) 255, (byte) 255, (byte) 255, 0, 0, (byte) 255, 0, <br/> (byte) 255, (byte) 255, (byte) 255, 0, (byte) 255, (byte) 255, <br/> (byte) 255, 0, 0, (byte) 128, 0, 0, (byte) 255 ,}; <br/> PR Ivate static int [] triangle_indices = {0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, <br/> 4, 0, 1 }; <br/> private graphics3d g3d; <br/> private vertexarray va_vertex; <br/> private vertexbuffer Vb; <br/> private vertexarray va_color; <br/> private transform TF; <br/> private camera; <br/> private trianglestriparray TSA; <br/> Public my3dworld (Boolean suppresskeyevents) {<br/> super (suppresskeyevents); <br/> th = New thread (this); <br/> g3d = graphics3d. getinstance (); // get a g3d instance <br/> VB = new vertexbuffer (); <br/> camera = new camera (); <br/> TF = new transform (); <br/> TSA = new trianglestriparray (triangle_indices, <br/> New int [] {triangle_indices.length }); <br/> // parameter 1 is used to input a surface array in the triangle format, the second parameter indicates copying the array of parameter 1 to the new array <br/> // process the triangle into a normal 3.1-plane format <br/> va_vertex = new vertexarray (vertex_positions.length/3, 3, 1); <br/> // The Space vertex array is defined here. The first parameter represents the number of vertices <br/> // The second parameter represents the number of vertices into a space point; the third parameter indicates the number of bytes occupied by each vertex <br/> va_vertex.set (0, vertex_positions.length/3, vertex_positions ); <br/> // This method sets vertex_positions, the first defined vertex array, to a space vertex (one-to-one correspondence with the vertex array) <br/> // The first parameter can be understood as starting from the subscript of the first vertex array to 0. <br/> // The number of space vertices encapsulated by the second parameter has points. length/3, <br/> // input the third parameter to be converted into an array of Space vertices <br/> va_color = new vertexarray (vertex_colors.length/3, 3, 1 ); <br/> // defines the color vertex array of the space. The first parameter represents the color vertex. Number (one-to-one correspondence with the vertex array) <br/> // The second parameter indicates that the vertex is a space point; the third parameter indicates the number of bytes occupied by each vertex <br/> va_color.set (0, vertex_colors.length/3, vertex_colors ); <br/> // This method sets vertex_colors, the first defined vertex array, to a space vertex color array. <br/> // The first parameter can be understood as a subscript from the first vertex array. start from 0, <br/> // The number of color numbers of Space vertices encapsulated by the second parameter is points. length/3, <br/> // input the third parameter to be converted into an array of Space vertex colors <br/> float PC [] = {0, 0, 1 }; <br/> VB. setpositions (va_vertex, 1.0f, PC ); // @ Note 2 <br/> // set the first parameter of the vertex position to input the vertex space array <br/> // The second parameter ID pair Zoom 1.0 without zoom <br/> // identify deviation of the third parameter. For more information, see @ Note 2. <br/> VB. setcolors (va_color); <br/> // sets the space color array <br/> camera. setperspective (30, (float) This. getwidth () <br/>/(float) This. getheight (), 1, 1000 ); // @ Note 3 <br/> // set Perspective Projection <br/> // For detailed explanation, see @ Note 3 <br/> transform = new transform (); // @ Note 4 <br/> // For detailed explanation, see @ Note 4 <br/> transform. posttranslate (0, 0, 10); <br/> // set the camera projection position <br/> g3d. setcamera (camera, transform); <br // Use a 3D paint brush to set the 3D space camera <br/> th. start (); // start the thread <br/>}< br/> Public void draw (Graphics g) {<br/> try {<br/> g3d. bindtarget (g); // @ Note 5 <br/> // bind the paint brush to a 3D space paint brush <br/> g3d. clear (null); <br/> // The default fl mode of the parameter null <br/> g3d. render (VB, TSA, new appearance (), TF); // @ Note 6 <br/> // The first parameter for rendering passes in the information required to create a graph, includes the normal and vertex information, color and so on <br/> // The second parameter identifies the triangle band information required by the stereo image <br/> // The third information indicates the default appearance, subsequent articles will learn <br/>}catch (exception e) {<br/> S Ystem. Out. println ("Draw-> Error !! "); <Br/>}finally {<br/> g3d. releasetarget (); // @ Note 7 <br/>}< br/> Public void keypressed (INT key) {<br/> If (Key =-1 | key =-3) <br/> TF. postrotate (10, 1, 0, 0); // @ Note 8 <br/> else if (Key =-2 | key =-4) <br/> TF. postrotate (10, 0, 1, 0); // @ Note 8 <br/>}< br/> protected void keyrepeated (INT key) {<br/> keypressed (key); <br/>}< br/> Public void run () {<br/> while (true) {<br/> try {<br/> draw (this. getgraphics (); <br/> flushgraphics (); // midp2.0 refresh paint brush <br/> thread. sleep (100); // sleep thread <br/>} catch (interruptedexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> 

Simulator:

 

 

 

Note 1: here we will explain in detail the triangle band. Next we will refer to figure A. You can see this vertex array as shown in the figure!

 

 

 

Let's take a look at the vertex array defined in the triangle form at the beginning.

 

Vertex_positions = {-1,-1, 1, 1,-1, 1,-1, 1, 1, 1,-1,-1,-1,-1, 1, -1,-1,-1, 1,-1, 1, 1,-1 };

We take the center of the cube as {0, 0, 0}. If the vertex 0 coordinate is (-1,-1, 1), then is vertex 2 {-, 1, yes!

Here, the three coordinates represent the X axis and Y axis.

That is to say, the two vertices 0 and 2 both share the same-1, 1, and the triangle pattern identifies the vertices of all cubes by repeating the vertices!

 

 

NOTE 2: here we define a deviation array float PC [] = {0, 0, 1}. Why is it in array format? In fact, you can look at it in another form. It is actually a coordinate point,

After all, this is a 3D world, Wahaha. It actually means that the X axis is + = 0, the Y axis is + = 0, and the Z axis is + = 1. When we pass this offset point to the setpositions () method,

This means that the center point of the cube is changed from {0, 0} to {0, 0, 1}. If you run this project and press the Left or Right button to rotate the cube,

It is found that the cube is not rotated around the center point, but is rotated by {0, 0, 1}. So setpositions () is actually a deviation of the center point.

 

Some people should say that the central point is {0, 0}. In fact, no one sets this point. Instead, when we first define the cube array vertex, we set a rule in our hearts,

The center is {0, 0. If vertex_positions is not defined as the center point {0, 0} at the beginning, but the vertex 0 coordinate is the center point,

Then vertex 2 becomes {0, 2, 0} at this time, and vertex 3 becomes {0, 2}, so it can also be written as a triangle band, however, pay attention to the color array and

When defining a triangle with an array, you should also think that vertex 0 is the center point to write Oh, don't forget, we need to correspond one by one!

 

NOTE 3: Camera. setperspective (30, (float) This. getwidth ()/(float) This. getheight (), 1, 1000 );

The first parameter indicates the perspective angle! Not the height! The second is the screen width proportion, and the third is the visible range min and Max.

In fact, there is also a common method here: Camera. setparallel () and note 5 in subsequent articles

I will explain this article to you later in "Deep buffering and Projection.

 

Note 4: Transform transform = new transform (); here we define a transformation object. In fact, we also have a member variable defined at the beginning.

Private transform TF; however, it should be noted that transform is defined to set the camera position while setting the camera, while TF is for rendering the cube

A variable object defined by such operations as inversion, scaling, and translation, which will be described in subsequent articles during rendering. Be sure to note the differences between the two transform!

 

Note 5: Note 3 has already been explained. I will explain it to you later when I study "Deep buffering and Projection" in subsequent articles.

 

Note 6: This is to emphasize the difference between the transform object and the transform object in note 4!

 

Note 7: Calling releasetarget () here means terminating rendering. You will find that g3d. releasetarget (); this sentence is written in the try catch block, because

Many graphics3d Methods throw uncontrollable exceptions, but most errors are irrecoverable. Therefore, make sure that rendering can be terminated no matter whether an exception occurs or not!

 

Note 8: Here is the button operation. The button processing and cube operations (scaling, translation, and rotation) will be explained in detail in subsequent learning articles.

 

Finally, we will release the source code for you and hope you can communicate with each other. All articles are original, and errors may exist in many places. Thank you!

 

Source code: http://download.csdn.net/source/2887681

 

 

I hope you can share the source code with us in the middle of the night ~

 

(We recommend that you subscribe to this blog, because our update speed is very fast ~ Wahaha)

 

2010/12/7

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.