J2ME Mobile 3D Introductory Tutorial Series II

Source: Internet
Author: User
Here I first thank you to the previous article J2ME Mobile 3D Introductory Tutorial series of the support of one of the articles, it is also with the support of everyone, I decided to take my mobile3d learning process to share with you, I hope we can together to discuss Mobile3d.
  
In the last tutorial I gave you a specific introduction, in the Moble3d through the timely operation of the creation of 3D graphics, and the import of models from the *.m3g file made a simple introduction, this time I would like to use the *.m3g file here to briefly introduce the mobile3d of the control of the animation, And some operations on the model.
  
First of all, I would simply say the establishment of m3g file, this is actually very simple, you only need to choose a familiar 3D graphics production software, and install the corresponding plug-in, where I use the software is Maya and 3DS MAX, plug-in using the H3T EXPort Plugin, The plug-in has Maya and 3ds Max, and you need to download a software m3g Tools kit.
  
These can all be found on the Sony Ericsson website. The plug-in installation should be all right. After the plug-in installation is complete, you can build the model, set up the material, set the camera, set the key frame and so on, and finally as long as the output to h3t file. Then open the M3G Tools kit to output the h3t file as a m3g file. It is important to use M3G Tools kit to navigate through the file before using the m3g file in order to record the corresponding information, such as the UserID and the tree structure of the scene. If you do this, you will find that the camera in the exported m3g file is not under the world Tree, but is a peer to the world tree.
  
In fact, as I said in the previous article, camera and rendering information can not be placed under the world Tree, but all the model information must be placed under the tree. When we look at the m3g file, it's important to see where the world node is located and the userid of the model that needs our operation.
  
The prep work is almost over, now look at Mobile3d, in the previous article I said that the m3g file was loaded using Javax.microedition.m3g.Loader.load (String URL), And the method returns an array of Javax.microedition.m3g.Object3D, and maybe someone wants to ask, since world is the root node, why not just return to a world?
  
Just let us see the structure of the m3g file has been explained that the camera and animation settings and so on is not put into the world as a node of the book, but with the world node peer. The world node is the root node of the scene. So how do we properly take out the world node? There are two ways of doing this:
  
1. Iterate over the Object3d array and compare the UserID of each element if the Useid of the Official World node takes the element out.
  
2, traversing the Object3d array, and comparing each element is not an instance of the world class, then since the World node is the root node of the scene, then in the Object3d array there should be only one instance object of the world class.
  
The first method is simple, and I'll just give a snippet of the second method here. (In fact, the same)
  
PRivate void Loadworld () {
System.out.println ("Now Loading ...");
try{
Buffer=loader.load ("/img/tmpmicrofile.m3g");
for (int i=0;i if (Buffer[i] instanceof World) {
World= (World) buffer[i];
return;
}
}
}catch (Exception e) {
Buffer=null;
System.out.println ("Thorw a exception when loading");
E.printstacktrace ();
}
}
  
Then we set up the camera, the difference is, this time we get the camera information from the world, we can get some basic settings for it here. Let's focus on the parts of the animation below.
  
When we import this m3g file we actually have the animated information, and these animation information can be very complex, I tested the linkage two axis of motion can be no problem, from some information on the IK inverse movement is not a problem. Perhaps you have been worried, how not to say how to control the animation play it?
Simply say World.animate () to update the animation information, this method needs to pass in an int type parameter, when you first call the method, the system will record this value, each time after each call will be compared with this value, The system then calculates where to update the animation, and the method returns a parameter of type int, which represents a suggested value for the next update (in milliseconds).
  
We can then let the thread calling this method hibernate this time so that the animation data is updated next time. Here you may have doubts, my animation is clearly only dozens of frames, but here is repeated playback, although this is good news, but in a lot of situations play the length, when playing, we need to control what to do?
  
At this time we need to use the Javax.microedition.m3g.AnimationController class, in fact, each animated model has its own Animationcontroller object, and model animation as we can through the World.find (int controllerid), in which we can set the start and focus time of the animation playing in the system through Setactiveinterval (int activetime,int unactivetime), In addition, the setposition (int starttime,int endTime) method controls which paragraph the animation needs to play.
  
I'm sorry about the two methods I didn't use in this tutorial instance, but if you want to know about the use of these two methods, I suggest you look at the example of the kangaroo in WTK2.2 's demo3d, which is quite specific. Here, I'll give you an animated and drawn code snippet.
  
protected void Paint (Graphics g) {
  
Starttime= System.currenttimemillis ()-worldstarttime;
validity= world.animate ((int) startTime);
perframetime= (int) system.currenttimemillis ();
  
G.setcolor (0x00);
G.fillrect (0,0,getwidth (), getheight ());
  
G.setclip (0,0,getwidth (), getheight ());
G3d.bindtarget (G,true,graphics3d.dithergraphics3d.true_color);
G3d.setviewport (0,0,getwidth (), getheight ());
  
G3d.render (World);
  
G3d.releasetarget ();
  
framepor= (int) 1000/((int) system.currenttimemillis ()-perframetime);
  
System.out.println ("3D demo frame/sn:" +framepor);
  
if (Validity < 1)
{//the validity too small; allow a minimum of 1ms.
validity = 1;
}
  
if (validity = = 0x7fffffff)
{//the validity is infinite; scehdule A and refresh in 1 second.
validity=1000;
}
  
}
  
public void Run () {
while (Isrun) {
Repaint ();
try{
Thread.Sleep (validity);
}catch (Exception e) {}
}
}
  
If you don't like to use threads here, you can switch to a timer. Personal preferences, I'm familiar with threading, so here I'm using threads.
  
The animation seems to me to understand this now, and all share it with you. I would like to say a few methods in the Transformable class. First, let's introduce the Transformable class. The Transformable class is a very important class, and node is its subclass, knowing its importance.
  
There are four methods in transformable. The postrotate (float, float, float, float), which is related to the rotation of the object, is the rotation axis of all objects in the Mobile3d in the center of their own, Therefore, the rotation of the object, it must be the rotation, rather than around a point, or an axis of the revolution, this must be clear. Maybe it's a little abstract, so I'll change the point that the rotation here only changes the orientation of the object and does not change the position of the object.
  
Now that's clear, maybe someone will ask, "Isn't that what we need?" Why should we emphasize it? "The reason is that, in some 3D engines, rotation is based on the origin. In other words, if you need to do rotation, you have to do the move, then rotate, and then move such operations, in fact, the advantage of doing so is to facilitate the operation of points, because the point does not exist the concept of rotation. And our mobile3d to some extent can not operate on the point, so its smallest unit is mesh, then how does it achieve rotation? Look at a matrix, you'll see.
  
90,0,0,0
0,0,0,0
0,0,1,0
0,0,0,0
  
The current model represented by this matrix has a rotation of 90 degrees on the y-axis. Now let's look back at the postrotate (float A, float x, float y, float z) method, which has 4 parameters, the first one is the angle that needs to be rotated, the last three actually a rotation of the axis, is represented by a vector, The Postrotate method is to continue the new rotation on the original orientation, and if it is represented by a mathematical formula, it is multiplied by the original matrix.
  
a,0,0,0
0,x,0,0
0,0,y,0
0,0,0,z
  
This matrix is the new direction matrix. If this method sounds difficult to understand mathematically, you just have to remember that the first four parameters are the angle of the rotation, and the second three determine the axis of revolution.
Similar to the Postrotate method is the setorientation (float A, float x, float y, float z). The difference is that this time it is no longer rotated but directly set to this direction.
  
It seems to be a lot easier when you say rotation and then look at the move. Translate (float x, float y, float z) This means panning by the specified vector; settranslation (float x, float y, float z) This is much simpler, Moves directly to the specified location.
  
In fact, there is a way scale (float SX, float sy, float sz) is used for scaling, and there are also Setscale (float sx, float sy, float sz) directly set the scale bar, this six method is the direct operation of the object , very useful, especially the first four, in the writing of the game will be used frequently, and then the two used to use less frequency, because most of the time we are directly to the camera's distance operation (unless there is an extraordinary need). Let me give you an example of using the first four methods.
  
public void keypressed (int keycode) {
Float[] Camertra;
float x;
float Z;
Switch (keycode) {
Case Gamecanvas.down:
Break
Case GAMECANVAS.UP:
Break
Case 52:
Dir=dir-2;
System.out.println (dir);
x= (float) (3*math.sin (dir * 3.14159f)

The above is the J2ME Mobile 3D Introductory Tutorial Series Two of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.