Home >Backend Development >XML/RSS Tutorial >One of the articles in the J2ME Mobile 3D introductory tutorial series
3D graphics technology has been increasingly used in various fields, of course, this also includes the J2ME field. J2ME provides us with an optional package such as JSR184, which is a set of APIs that implement 3D graphics programming on mobile phones. At the same time, with the development of mobile device hardware, there are now more and more mobile phones that support this optional package, such as Sony
EricCSSon's K series, S series, etc.
It just so happened that I briefly studied 3D graphics some time ago, so I recently started studying Mobile 3D. Here I will share what I have learned with you. I hope it can be helpful to everyone, and I also hope that everyone can learn it. We can learn JSR184 together.
Let’s get down to business. First, let’s imagine how we observe the world in real life. We observe through our eyes, and we live in a world composed of a three-dimensional coordinate system. In Mobile3D, there is also a World class that allows you to construct the world you want as you wish. Of course, it is more professional here. In 3D graphics, we call it "scene"; there is also a Camera class to serve as your eyes. You can Set its position, angle and other parameters to display different images.
In Mobile
How to realize the display of 3D images in 3D? First, you need to create or load a 3D model, then set a series of parameters such as the environment and rendering method in the scene as needed, and then generate and set up a camera and adjust the light you want, and adjust what you need position and angle. OK, what else do you need? Press the shutter, this step is called "rendering" and everything is done. It sounds simple and is actually not difficult. In fact, it is not difficult either.
Let’s explain these steps step by step:
First let’s talk about the establishment of the model. In Mobile
3D is the same as most 3D programming APIs. There are two ways: 1. Real-time operation generation; 2.
External modeling import. Since the external modeling import will import the environment information at the same time, I will give you a detailed introduction later. Here I will focus on introducing the "real-time calculation generation" part, which will help you understand Mobile
How 3D works. Mobile 3D provides us with two classes, VertexArray and VertexBuffer, which are used to save the vertex information of the 3D model.
Where VertexArray
This class has many uses and is more flexible. There are three most common uses of this class, 1 to save vertex coordinate information; 2
Save normal information; 3 Save map information. Someone may ask how this class manages three different things? Let's analyze this class. First, the constructor of this class has 3 parameters: 1
The number of elements to be included in this instance; 2 The number of elements to be included in each element; 3 The number of bytes occupied by each sub-element. This seems to make it clear why this class can be used for 3 things.
In addition, this class also has a more commonly used method set(int index, int length, short[] array0). This method is used to store data in the instance object of this class. The first parameter refers to the number from which Start with elements; the second parameter refers to how many elements are to be set; the third parameter is the actual setting.
The following is a brief introduction to the VertexBuffer class, which is the class that actually saves the frame information of polygons. Modify the class to create graphics by setting vertex positions, discovery, and map information. Among them,
setPositions(VertexBuffer v, float s, float[]b)
is used to set the vertex position. In this method, you will find that there are 3 parameters. Needless to say, the first one is the coordinate information of the vertex. , the last two are used to perform coordinate offset and other operations. The operation is the mathematical formula:
v'=v*s+b
There is also a
setNormals(vertexBuffer norm)
method to set the method Wire. There is also a very important method
setTexCoords(int, VertexArray, float, float[])
In addition to the first one, the last three parameters in this method are
setPositions(VertexBuffer v, float s, float[] b)
is the same, the first parameter is the starting element number. Isn’t this a bit abstract? Let me give you an example so that you can understand. short x = 20;
short fz = (short ) -z;
// Fixed point coordinates
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
, fx,y,z, //A
x,fy,z, fx,fy,z, x,fy,fz, fx,fy,fz}; //E
try{vertArray=new VertexArray(vert .length/3,3,2);
vertArray.set(0,vert.length/3,vert);
}catch(Exception e){System.out.PRintln("vert");}
// hair line
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};
try{normArray=new VertexArray(norm.length/3,3,1);
normArray.set(0,norm.length/3,norm);
}catch(Exception e) {System.out.println("norm");e.printStackTrace();}
//Gives the vertices corresponding to the points on the picture (vert and tex arrays are in one-to-one correspondence)
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, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1 };
try{
texArray=new VertexArray(tex.length/2,2,2);
texArray.set(0,tex.length/2,tex);
}catch(Exception e ){System.out.println("tex");}
//Create a cube
vb=new VertexBuffer();
vb.setPositions(vertArray,1.0f,null);
vb.setNormals(normArray);
vb.setTexCoords(0,texArray,1.0f,null);
In the above code, I create all the vertex and face information needed to create a cube, but everyone should note that the corresponding model is not generated here. The reason is that we have not set up other information to generate the model. Let's take a look at the TriangleStripArray class. This class is the information class of the triangles needed to form the surface. Anyone familiar with 3D graphics knows that constructing 3D graphics is through multiple surfaces. To construct a 3D solid, triangular surfaces are a more commonly used method. I won’t elaborate on the specific content here.
Next we also need to set some environment and material information. The classes used here are Appearance, Texture2D, and Material. Let’s take a look at an example first:
appearance=new Appearance();
//Create texture
Texture2D texture=new Texture2D(image2d);
texture.setBlendColor(Texture2D.FUNC_DECAL);
texture.setWrapping(Texture2D.WRAP_REP EAT ,Texture2D.WRAP_REPEAT);
texture.setFiltering(Texture2D.FILTER_NEAREST,Texture2D.FILTER_NEAREST);
material=new Material();
material.setColor(Material.DIFFUSE, 0xFFFFFFFF);
material.setColor(Material. SPECULAR, 0xFFFFFFFF);
material.setShininess(100.0f);
appearance.setTexture(0,texture);appearnce.setMaterial(material);
mesh=new Mesh(vb,tsa,appearnce);
mesh.setAppearance( 0,appearnce);
I personally feel that the Appearance class is somewhat similar to the VertexBuffer class, and is also the holder of multiple attributes; one thing to emphasize here is that the settings of the Appearance class are far more than what is given above, and there are many more settings (for example, FOG is the fog setting). Texture2D is a texture class. Use it to set the texture information, such as whether the texture is tiled. The name Material means the material. Here you can set the "reflectivity", "color" and other information. In addition, here I will also introduce a method of setting rendering parameters
//Set poly mode settings
PolygonMode polygonMode=new PolygonMode();
polygonMode.setShading(PolygonMode.SHADE_SMOOTH);
polygonMode.setCulling(PolygonMode.CULL_NONE);
//Generate appearance
appearnce=new Appearance();
appearnce.setPolygonMode(polygonMode);
Looking at the code just given, it seems simpler than the one above, right? In fact, PolygonMode has already done a lot of work for us. The setting is very similar to the use of Poly in 3D MAX.
The code just now also gives a Mesh class, which is the final model we want for this type of material. After establishing the model, we need to create the Camera. In Camera, I will only briefly introduce two methods setParallel(float,
float, float, float) and setPerspective(float, float, float,
float). Let's first look at setParallel(float, float, float,
float) This method is to set the Camera's view method to a flat view; the first parameter is to set the height of the angle of view, focus on the height, not the angle, because this is a flat view; the second parameter is the width to height ratio of the Camera, for example we The TV is 4:3, and the widescreen movie is 16:9; the third and fourth parameters are the nearest and farthest rendering range respectively. The same setPerspective sets the Camera to a perspective view, which is closer to the observation angle in our daily life. The last three parameters of this method are the same as the last three parameters of setParallel, and the first parameter is Angle, you should not ignore this angle issue here. This angle is an important parameter for calculating projection in perspective.
It seems that everything is set up, but it is not the case. So far we have only prepared all the materials we need. Let’s take a look at Mobile
The governance mechanism of 3D. Anyone familiar with 3D drawing knows that most 3D software, 3D
The API manages materials through a tree structure. The advantage of this is that each model, model group, camera and other elements can set their own rotation axis and other attributes as nodes, and can follow the animation information they have set. Get some exercise. In Mobile
3D stipulates for us that the root node of the tree structure must be an instance object of the World class. The camera and light are relatively special and do not need to be placed in this tree. Instead, they are set through objects of the Graphics3D class (but they do not need to be
The above is one of the articles in the J2ME Mobile 3D introductory tutorial series. For more related content, please pay attention to the PHP Chinese website (www.php.cn)