📄 compliments of redikod 3d programming tutorial for mobile devices using m3g (jsr 184).htm
字号:
and funding for games development, including mobile,
that, pending final political decision this fall, will
come into effect in 2006. But 3D and multiplayer mobile
games development are their day job.</P></TD>
<TD>
<P> <IMG
src="Compliments of Redikod 3D programming tutorial for mobile devices using M3G (JSR 184).files/mikaelbarosweb.gif"
border=0></P>
<P><EM>Mikael Baros, senior programmer at
Redikod.</EM></P></TD></TR></TBODY></TABLE></P></TD></TR>
<TR>
<TD class=text vAlign=top colSpan=2>
<P>Below you can download the source code and application
package zip files, and continue with the tutorial.</P>
<UL>
<LI><A
href="http://developer.sonyericsson.com/getDocument.do?docId=73849">Source
Code (Java classes and resources)>></A>
<LI><A
href="http://developer.sonyericsson.com/getDocument.do?docId=73850">Application
Package (JAR/JAD) >></A>
<LI>More information about <A href="http://www.redikod.com/"
target=_blank>Redikod>></A> </LI></UL>
<P>So, over to Mikael for the first installment. </P>
<HR class=hLine1>
<P><STRONG>Part One: Quick jump into the world of Mobile Java
3D programming</STRONG> </P>
<P><STRONG>Introduction<BR></STRONG>To begin with I'd like you
to know of a few links on the net that will be very helpful on
your journey towards M3G land.</P>
<P>First of all, and probably most importantly, is the
dedicated Mobile Java 3D web section on <A
href="http://developer.sonyericsson.com/site/global/newsandevents/campaigns/java_3d/p_java3d.jsp">Sony
Ericsson Developer World</A>. Second, if you ever get stuck
visit to the <A
href="http://developer.sonyericsson.com/show_forum.do?searchBy=MSGTHREAD_IS_STICKY_FL&searchValue=N&searchBy4=MSGTHREAD_IS_ARCHIVED_FL&searchValue4=N&searchBy=MSGTHREAD_IS_STICKY_FL&searchValue=N&searchBy4=MSGTHREAD_IS_ARCHIVED_FL&searchValue4=N&forumId=110&ps=50&pn=1&searchBy2=MSG_PARENT_ID&searchValue2=-1&sortCol=MSG_AUDIT_MOD_DT&sortDirection=0&searchBy3=MSGFORUM_FORUMID&searchValue3=110&searchBy=MSGTHREAD_IS_STICKY_FL&searchValue=N&searchBy4=MSGTHREAD_IS_ARCHIVED_FL&searchValue4=N">Sony
Ericsson Mobile Java 3D forum</A>. For everything else, use
the <A
href="http://developer.sonyericsson.com/site/global/home/p_home.jsp">Sony
Ericsson Developer World portal</A>, where you will find the
answers to your questions and more.</P>
<P>Now that you know where to go if you get in trouble, let's
proceed with the tutorial. The goal of this tutorial is to
teach you how to set up your own 3D Canvas and make it render
stuff on screen. To render models, I'll first show you how to
load them and tell you about the tools that are available to
create M3G models. Then we'll finish by manipulating the
camera some so that we can walk around in our scene. I just
want you to get warm in your seat and see how fast one can
develop a 3D application with M3G, so this tutorial will be
pretty fast and straight-forward with little in-depth
explanation. The other parts of this series will explore the
various M3G topics in detail. </P>
<P>Since the code is meant for educational purposes it isn't
optimal nor does it cover all the errors the might occur.
These are more advanced topics that will be addressed later
on. </P>
<P><STRONG>What you should know</STRONG><BR>Before you start
reading this, you should know the basics of a MIDlet class and
a Canvas class. This isn't a hard topic and if you feel lost,
consult the source code (distributed with this tutorial) and
check out the M3GMIDlet and M3GCanvas classes. It's also very
good if you have some background in 3D programming/math, but
it's not required.</P>
<P><STRONG>The Canvas</STRONG><BR>When we develop in JSR 184
we will be using the MIDP 2.0 profile, which means we get a
few great functions for free. Let's begin with setting up our
Canvas. It is the same procedure as with a normal 2D Java
game, you set up your MIDlet class, you start your Canvas and
draw in your paint method. This is a fairly easy process and
since you already should know this I'll just quickly skim
through it. Let's first take a look at the header of the
Canvas class, the import and variable declarations.</P>
<DIV><FONT face="Courier New" color=#228b22>import
javax.microedition.lcdui.Graphics;<BR>import
javax.microedition.lcdui.game.GameCanvas;<BR>import
javax.microedition.M3G.Camera;<BR>import
javax.microedition.M3G.Graphics3D;<BR>import
javax.microedition.M3G.Light;<BR>import
javax.microedition.M3G.Loader;<BR>import
javax.microedition.M3G.Object3D;<BR>import
javax.microedition.M3G.Transform;<BR>import
javax.microedition.M3G.World;</FONT></DIV>
<DIV><FONT face="Courier New"
color=#228b22>/**<BR> *<BR> * @author
Biovenger <BR> * @version<BR> */<BR>public class
M3GCanvas<BR>extends GameCanvas<BR>implements Runnable
{<BR> //
Thread-control<BR> boolean running =
false;<BR> boolean done =
true;<BR> <BR> // If the
game should end<BR> public static boolean
gameOver = false;<BR> <BR>
// Rendering hints<BR> public static final
int STRONG_RENDERING_HINTS = Graphics3D.ANTIALIAS |
Graphics3D.TRUE_COLOR |
Graphics3D.DITHER;<BR> public static final
int WEAK_RENDERING_HINTS = 0;<BR> public
static int RENDERING_HINTS =
STRONG_RENDERING_HINTS;<BR>
<BR> // Key array<BR>
boolean[] key = new boolean[5];<BR>
<BR> // Key constants<BR>
public static final int FIRE = 0;<BR> public
static final int UP = FIRE + 1;<BR> public
static final int DOWN = UP + 1;<BR> public
static final int LEFT = DOWN + 1;<BR> public
static final int RIGHT = LEFT + 1;<BR></DIV></FONT>
<DIV> </DIV>
<DIV>That was pretty basic stuff but let's quickly see what's
going on. First of all we have a lot of imports, we're just
importing all the classes that we're going to use in this
tutorial and you can find their documentation in the general
JSR 184 API javadoc. We also have some thread variables such
as running and done, but those should be pretty
self-explanatory.</DIV>
<DIV> </DIV>
<DIV>Now, let's check out the rendering hints. These "hints"
are ways of telling the mobile device just what kind of
quality you want while rendering. However, since they are
hints it's not guaranteed that the mobile device will act upon
them. Here I define two different hints. Weak and Strong. As
you see, the Strong rendering hints hold both anti-aliasing,
true color and dithering. The weak holds no hints at all,
which is basically the ugliest and fastest rendering you can
get. As you see from the code, the hints can be combined by a
simple logical OR. I'll talk more about hints in a later part
of this tutorial series. </DIV>
<DIV> </DIV>
<DIV>Next we have the key array, that's just a very simple
array that holds which key was pressed. If you are curious as
to how keys are processed, check out the source code of this
example. Suffice to say: by querying if(key[UP]) you will find
out if the UP key is pressed right now.</DIV>
<DIV> </DIV>
<DIV><STRONG>The M3G File Format</STRONG><BR>The JSR 184
standard has its own format, called M3G. This very versatile
3D format can hold tons of data such as models, lights,
cameras, textures and even animation. Nifty! Not even is the
format really good, it's also real easy to load into your
application, more on that later though. Anyhow, I bet you're
thinking "M3G? Never heard of it. How do I create an M3G
file?" and even if you weren't thinking it, I'll explain.
There are numerous ways to create M3G files:</DIV>
<DIV><BR>1. First of all, the latest iteration of Discreet's
3D Studio Max has a built-in M3G exporter. Just hit the Export
button and you can export your entire scene, animation, bones,
materials and all, into an M3G file. However, many find
Discreet's exporter a bit cumbersome and has some bugs, so for
best results, use method 2. </DIV>
<DIV> </DIV>
<DIV>2. HiCorp, who are also the providers of Sony Ericsson's
JSR 184 implementation, have created very powerful exporters
available to the 3 most popular 3D modeling programs. They're
available for 3D Studio Max, LightWave and Maya. You can find
them <A
href="http://developer.sonyericsson.com/site/global/docstools/java/p_java.jsp">here>></A></DIV>
<DIV> </DIV>
<DIV>3. Blender, a powerful and free 3D modeling tool also has
an M3G exporter available. However, it's still early in
development and a bit buggy. Check out <A
href="http://www.blender3d.org/cms/Home.2.0.html"
target=_blank>Blender here>></A> <BR></DIV>
<DIV> </DIV>
<DIV>So, how do we load these very powerful files into our
program? Very easy. The JSR 184 contains a class called
Loader, and it does exactly that, loads files. With one simple
method call one can load all references from an M3G file. The
method is called Loader.load and has two different argument
lists. One takes an URL as a String and the other takes a raw
byte array. Here's an example of how it is used.</DIV>
<DIV> </DIV>
<DIV><FONT face="Courier New" color=#228b22>Object3D[] objects
= Loader.load("file.M3G");</FONT></DIV>
<DIV><FONT face="Courier New" color=#228b22>Object3D[]
objects2 = Loader.load(byteArray, offset);</FONT><BR></DIV>
<DIV> </DIV>
<DIV>The load method always returns an array of Object3Ds and
there's a very good reason for it. The best one is that the
Loader class can load much more than M3G files, it can
basically deserialize any class that inherits from Object3D.
However, you'll mostly use it for loading M3G files.</DIV>
<DIV> </DIV>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -