⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compliments of redikod 3d programming tutorial for mobile devices using m3g (jsr 184).htm

📁 英文版的,详细讲解jsr184的开发,有demo,虽然是英文版但也比较易懂,是我转3D java时看的第一篇文章,得益十分多,建议初学者看看.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
                  <DIV>Now, I've created a simple M3G file, called map. M3G and 
                  I want to render it. To load this file we'll use the Loader. 
                  load method, however as you've just seen it returns an array 
                  of Object3D. We can't use the Object3D array for rendering as 
                  it is so we need to convert it to something that we can render 
                  to screen later on. In this tutorial we'll load the World 
                  node. The World node is the top node of the JSR 184 scene 
                  graph. It holds all kinds of information such as camera, 
                  lighting, background and a lot of meshes. I'll go through 
                  scene graphs and JSR 184's implementation of scene graphs in a 
                  later part of this series, for now you just need to know that 
                  the World class can hold a whole scene, and that's exactly 
                  what we want! Check out this method, it loads the World node 
                  from a M3G file.</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV><FONT face="Courier New" color=#228b22>/** Loads our 
                  world */<BR>&nbsp;&nbsp;&nbsp; private void 
                  loadWorld()<BR>&nbsp;&nbsp;&nbsp; 
                  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  try<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  // Loading the world is very simple. Note that I like to use 
                  a<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  // res-folder that I keep all files in. If you normally just 
                  put 
                  your<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  // resources in the project root, then load it from the 
                  root.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  Object3D[] buffer = 
                  Loader.load("/res/map.M3G");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  // Find the world node, best to do it the "safe" 
                  way<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  for(int i = 0; i &lt; buffer.length; 
                  i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  if(buffer[i] instanceof 
                  World)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  world = 
                  (World)buffer[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  // Clean 
                  objects<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  buffer = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  catch(Exception 
                  e)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  // 
                  ERROR!<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  System.out.println("Loading 
                  error!");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  reportException(e);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  }<BR>&nbsp;&nbsp;&nbsp; }</FONT><BR></DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>As you can see, after we load the Object3D array with the 
                  Loader class, we just simply go through the entire array and 
                  look for the World note. This is the safest way of finding a 
                  World node. After we find our world node we just break out of 
                  the loop and clean our buffer (not that it's needed, as they'd 
                  automatically get cleared upon leaving the method. Good 
                  practice though.)</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>OK, now we have loaded our World node, which I've already 
                  told you is the top node of a scene graph and holds all scene 
                  information. Before I show you how easy it is to render, let's 
                  first extract a camera so that we can move around the world we 
                  just loaded. </DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV><STRONG>Camera handling<BR></STRONG>We have our World 
                  node ready for rendering and now all we need is a camera, that 
                  we can move around in the world. If you remember, I've already 
                  told you that the World node holds Camera information, so we 
                  should be able to extract the camera from the World and 
                  manipulate it.</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>A camera in JSR 184 is described by the Camera class. 
                  This class makes it very easy to manipulate the camera in our 
                  3D application with simple translation and orientation 
                  methods. The only two methods we'll be using in this example 
                  are the translate(float, float, float) and the 
                  setOrientation(float, float, float, float). The first one 
                  simply moves the camera in 3D space by an offset in x, y and 
                  z. So, for instance, if you wanted to move the camera 3 units 
                  on the X axis and 3 units on the Z axis, you'd do this:</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV><FONT face="Courier New" color=#228b22>Camera cam = new 
                  Camera();&nbsp; // This is our camera</FONT></DIV>
                  <DIV><FONT face="Courier New" color=#228b22>//Move 
                  camera&nbsp;&nbsp; X&nbsp; Y&nbsp; Z<BR>cam.translate(3.0f, 
                  0.0f, 3.0f);</FONT></DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>Piece of cake! Each method call translates the camera 
                  further, so two of the above calls would actually translate 
                  the camera 6 units along the x axis and 6 units along the z 
                  axis. Rotating is just as easy, but I have to explain the 
                  method first. It works like almost all 3D API rotation 
                  methods. You have four parameters, the first one is the actual 
                  rotation in degrees and the last three compose an orientation 
                  vector (xAxis, yAxis, zAxis) to rotate around. Orientation and 
                  orientation vectors will come later in the series, for now, 
                  just know this:</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV><FONT face="Courier New" color=#228b22>//Rotate camera 30 
                  degrees around the X axis<BR>cam.setOrientation(30.0f, 1.0f, 
                  0.0f, 0.0f);</FONT></DIV>
                  <DIV><FONT face="Courier New" color=#228b22>//Rotate camera 30 
                  degrees around the Y axis<BR>cam.setOrientation(30.0f, 0.0f, 
                  1.0f, 0.0f);</FONT></DIV>
                  <DIV><FONT face="Courier New" color=#228b22>//Rotate camera 30 
                  degrees around the Z axis<BR>cam.setOrientation(30.0f, 0.0f, 
                  0.0f, 1.0f);</FONT></DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>Note that the method is named setOrientation, which means 
                  that it actually clears any previous rotation you might have 
                  done. I'll assume that you already know what rotation around 
                  an axis means and won't go into detail on that topic 
                  here.</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>Now that you know everything to make the camera move and 
                  rotate as you wish I'll show you how to extract a Camera from 
                  a World.</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV><FONT face="Courier New" color=#228b22>/** Loads our 
                  camera */<BR>&nbsp;&nbsp;&nbsp; private void 
                  loadCamera()<BR>&nbsp;&nbsp;&nbsp; 
                  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 
                  BAD!<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(world == 
                  null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  return;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Get the 
                  active camera from the 
                  world<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cam = 
                  world.getActiveCamera();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a 
                  light<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Light l = 
                  new Light();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Make sure 
                  it's AMBIENT<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  l.setMode(Light.AMBIENT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // We want a 
                  little higher 
                  intensity<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  l.setIntensity(3.0f);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Add it to 
                  our world<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  world.addChild(l);<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>It's that simple? Yes, it's that simple. We just extract 
                  the active camera from the World by using the getActiveCamera. 
                  This gives us the camera that the world was exported with. 
                  With the method above we have a camera that we can move around 
                  as much as we want to. However, the method is doing something 
                  else as well, it adds a light! We will delve deeper into 
                  lights in later parts, but here you see how easy it is to add 
                  a light to your world. I create an Ambient light (for you who 
                  don't know, an ambient light lights all surfaces from all 
                  directions) and add it to the world. This way we get a 
                  very-well lit world. As I told you earlier, the World node can 
                  hold all kinds of information, including light, so we only 
                  need to add the light once to our world and JSR 184 will do 
                  the rest for us. Isn't that handy? Before we get to the last 
                  part; rendering, let's make the camera move. I've already told 
                  you that the array of booleans, key, holds our key information 
                  so all we have to do is query the array and make our camera 
                  behave. First of all, we'll need some variables to make our 
                  camera obey.</DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV><FONT face="Courier New" color=#228b22>// Camera 
                  rotation<BR>float camRot = 0.0f;<BR>double camSine = 
                  0.0f;<BR>double camCosine = 0.0f;<BR>&nbsp;&nbsp;&nbsp; <BR>// 
                  Head bobbing<BR>float headDeg = 0.0f;</FONT><BR></DIV>
                  <DIV>&nbsp;</DIV>
                  <DIV>The variables above will help us keep track of the 
                  camera's rotation, trigonometry and the head bobbing. The 
                  trigonometry is used for movement later on. Head bobbing is 
                  really quite simple, it's just a cheap effect we'll insert to 
                  make the camera bob up and down as we walk around the world, 
                  for a more natural feeling. All right, all we need to do is 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -