Use native data to create 3D model_rotation on the imode Platform

Source: Internet
Author: User
Tags mul
On the imode platform, there are two ways to create a 3D model: 1. Import the model from an external. mbac file; 2. Create a 3D model using native data. The following is a program that uses native data to create a triangle cone and can rotate around X and Y axes. Import com. nttdocomo. UI .*;
Import com. nttdocomo. Opt. UI. j3d .*;//
/**
* @ Author ppby
* Rotate 3D
*/
Public class rotate extends iapplication {
Private canvas3d canv; Public void start (){
Try {
Canv = new canvas3d ();
Display. setcurrent (canv );
Thread thread = new thread (canv );
Thread. Start ();
} Catch (illegalthreadstateexception ITSE ){
ITSE. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}/**
* Canvas class
*/
Class canvas3d extends canvas implements runnable {
// Command list Array
Private int [] Using listfunc _;
Private int [] Using listprim _;
// 3D affine transformation matrix
Private affinetrans viewt = new affinetrans ();
Private affinetrans affinet = new affinetrans ();
Private affinetrans rotatet = new affinetrans (); Private int rotx, roty; // variable for object rotation public canvas3d (){
// Set a camera :( eye position, look at position, UP direction)
Viewt. lookat (
New vector3d (0, 0,800 ),
New vector3d (0, 0, 0 ),
New vector3d (0, 4096, 0)
);
// Set a command list
Setupcmdlistfunc ();
Setupcmdlistprim ();
} Public void run (){
While (true ){
Updateaffinetrans ();
Repaint ();
Try {
Thread. Sleep (100); // wait 100 ms
} Catch (interruptedexception IE ){
}
}
} Public void updateaffinetrans ()
{
Affinet. setidentity ();

// Rotate Y axis
Rotatet. setidentity ();
Rotatet. setrotatey (roty );
Affinet. Mul (rotatet );

// Rotate X axis
Rotatet. setidentity ();
Rotatet. setrotatex (rotx );
Affinet. Mul (rotatet, affinet );
}
/**
* Rendering method.
*/
Public void paint (Graphics g ){
If (G = NULL)
Return;
Try {
G. Lock ();
// Background clear
G. setcolor (graphics. getcolorofrgb (0, 0, 0 ));
G. fillrect (0, 0, getwidth (), getheight (); G. setcolor (graphics. getcolorofrgb (150,150,120 ));
Graphics3d g3d = (graphics3d) g;

// Method 1
G3d. setcliprect3d (0, 0, getwidth (), getheight ());
G3d. setscreencenter (getwidth ()> 1, getheight ()> 1 );
G3d. setperspective (200,100 00, 4096*50/360 );

// Method 2: The effect is the same s method 1.
// G3d.exe cutecommandlist (optional listfunc _); // excute the command list

// Set viewport
Affinet. Mul (viewt, affinet); g3d. setviewtrans (affinet); // set view coordinate Matrix
Setupcmdlistprim ();
G3d.exe cutecommandlist (optional listprim _); // excute a command list
//
G. setcolor (graphics. getcolorofrgb (0,255, 0xff ));
G. drawstring ("Use: Up, down, left, right key.", 0, Font. getdefaultfont ()
. Getheight (); G. Unlock (true );
} Catch (exception e ){
E. printstacktrace ();
}
}/**
* Key event process.
*/
Public void processevent (INT type, int PARAM ){
Try {
If (type = display. key_pressed_event ){
Switch (PARAM ){
Case display. key_up:
Rotx-= 128;
Rotx % = 4096;
Break;
Case display. key_down:
Rotx ++ = 128;
Rotx % = 4096;
Break;
Case display. key_left:
Roty-= 128;
Roty %= 4096;
Break;
Case display. key_right:
Roty + = 128;
Roty %= 4096;
Break;
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}/**
* Set the command list for methods.
* @ Return true-success. False-fail.
*/
Private Boolean setupcmdlistfunc (){
Try {
Int [] Listen listfunc = {
Graphics3d. command_list_version_1,
Graphics3d. command_clip_rect, 0, 0, getwidth (),
Getheight (), graphics3d. command_screen_center,
(Getwidth ()> 1), (getheight ()> 1 ),
Graphics3d. command_perspective1, 200,100 00,
4096*50/360, graphics3d. command_end };
Required listfunc _ = required listfunc;
Return true;
} Catch (exception e ){
E. printstacktrace ();
Return false;
}
}/**
* Set the command list for primitives.
* @ Return true-setting succeeded. False-setting failed.
*/
Private Boolean setupcmdlistprim (){
Try {
// Draw four trangles.
Int [] bytes listprim = {
Graphics3d. command_list_version_1,
// Triangles
(Graphics3d. command_render_triangles
| Graphics3d. color_per_face | (4 <16), // trangle num <16 // each trangle's three vertexes. vertex :( x, y, z)
0,120, 0,-120,-120,120,120,-120,120, 0,120, 0,120,-120,120,120,-120,-120,-0,120, 0,120,-120,-120,-120, -120,-120, 0,120, 0,-120,-120,-120,-120,-120,120, // each trangle's color
0xff <16, // red
0xff <8, // green
0xff <4, // blue
0xff <8 | 0xff <16, // yellow
// Flush
Graphics3d. command_flush, graphics3d. command_end };
Required listprim _ = required listprim;
Return true;
} Catch (exception e ){
E. printstacktrace ();
Return false;
}
}
}

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.