3D technology is already very familiar to us, the most commonly used 3D API has OpenGL and Microsoft Direct, in the desktop game has long been widely used. For J2ME programs, the advent of the mobile 3D Graphics API (JSR184) makes it possible to add 3D functionality to mobile applications.
The JSR184 standard (M3g:mobile 3D Graphics) defines a concise 3D API interface for Java Mobile applications, and J2ME programs can easily use M3G to implement 3D applications such as games and so on. The m3g is designed to be very lightweight, and the full implementation of the API does not exceed 150KB.
M3G is an optional package for J2ME, a lite version based on OpenGL, a total of 30 classes, running on cldc1.1/cldc2.0 (which must support floating-point operations) and can be used in MIDP1.0 and MIDP2.0. At present, the handset that supports m3g has Nokia 6230/3650/7650/6600, Siemens s65/cx65/s55/m55, Sony-ericsson k700i/p800/p900, Moto 220/t720 and so on. M3G is just a Java interface, the specific bottom 3D engine is generally implemented by C code, such as many mobile phone manufacturers 3D engine is the Superscape company's swerve engine, this is a specially designed for mobile devices high-performance 3D engine.
Microsoft-like D3D,M3G supports two 3D modes: Immediate Mode (immediate mode) and retention mode (retained mode). In immediate mode, developers must manually render each frame to achieve faster speed, but the code is cumbersome; in the retention mode, the developer only needs to set the key frame, the rest of the animation by m3g completion, the code is simpler, but slower. M3G also allows mixed use of both modes.
The 3D model can be created in a program, but it is cumbersome. Therefore, M3G provides a loader class that allows you to read all 3D scenes directly from a single. m3g file. m3g files can be created by software such as 3D Studio max.
If you are familiar with OpenGL, then m3g is very easy to understand. In m3g, Graphics3d is a 3D rendering of the screen interface, world represents the entire 3D scene, including camera (for the viewer perspective), Light (lighting), Background (background) and tree structure of any number of 3D objects. 3D objects are described by dots (point, Pixel), lines (line, polyline, Spline) and surface (Mesh) in the computer, and the specific storage and operation (such as rotation and projection) are matrix operations and transformations.
Sun's WTK2.2 already has a built-in M3G implementation package, and if WTK2.2 is installed, you can run the 3D MIDP program on the emulator. You can refer to the WTK2.2 example Demo3d.
Here is the simplest m3g program, the sample code from Sony-ericsson, which creates a spinning pyramid where you can download the complete code and run it in WTK2.2.
First, we want to get a unique graphics3d instance for rendering the 3D scene. Graphics3d is a singleton implementation that can be obtained anywhere:
G3d = Graphics3d.getinstance ();
Then, render in canvas:
public class MyCanvas extends Canvas
{
public void Paint (Graphics g) {
try {
G3d.bindtarget (g);
... update the scene ...
... render the scene ...
finally {
G3d.releasetarget ();
}
}
Next, create a world and set camera:
World = New World ();
Camera = new camera ();
World.addchild (camera);
The width and height of the canvas.
float w = getwidth ();
Float h = getheight ();
Constructs a perspective projection matrix and sets as the current projection matrix.
Camera.setperspective (60.0f, w/h, 0.1f, 50f);
World.setactivecamera (camera);
Next, create a mesh in the Createpyramid () method that represents the pyramid and adds it to the world:
Private Mesh Pyramidmesh; The pyramid in the scene
Pyramidmesh = Createpyramid (); Create our pyramid.
Pyramidmesh.settranslation (0.0f, 0.0f, -3.0f); Move the pyramid 3 units to the screen.
World.addchild (Pyramidmesh); Add the pyramid to the world
Finally, let the pyramid rotate around the y-axis in a thread:
public void Run () {
Graphics g = getgraphics ();
while (true) {
Rotate the Pyramid 1 degree around the y-axis.
Pyramidmesh.postrotate (3.0f, 0.0f, 1.0f, 0.0f);
Draw3d (g);
Flushgraphics ();
}
}
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.