`
peigen
  • 浏览: 76143 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Learning-Jme2-tutorial_2

    博客分类:
  • JME2
阅读更多

请浏览:http://peigen.info/2010/1/29/Learning-Jme2-tutorial_2/
以后在http://peigen.info/category/JME/更新

2. Hello Node

( Note: the starter tutorials in this wiki are not always up to date with the latest development version of jME. You can find up to date source files for the tutorials here: https://jme.dev.java.net/source/browse/jme/src/jmetest/TutorialGuide/ )

This program introduces Nodes, Bounding Volumes, Sphere, Colors, Translation and Scaling.

 

(注解:这份指南在wiki上并不一定是跟最新的jME开发版本一样.你可以在http://code.google.com/p/jmonkeyengine/source/browse/#svn/trunk/src/jmetest/TutorialGuide 找到最新的源码文件)

这里我们将介绍Nodes, Bounding Volumes, Sphere, Colors, Translation and Scaling.
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.LightState;
/**
 * Started Date: Jul 20, 2004<br><br>
 *
 * Simple Node object with a few Geometry manipulators.
 *
 * @author Jack Lindamood
 */
public class HelloNode extends SimpleGame {
 public static void main(String[] args) {
 HelloNode app = new HelloNode();
 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
 app.start();
 }
 protected void simpleInitGame() {
 Box b=new Box("My Box",new Vector3f(0,0,0),new Vector3f(1,1,1));
 // Give the box a bounds object to allow it to be culled
 b.setModelBound(new BoundingSphere());
 // Calculate the best bounds for the object you gave it
 b.updateModelBound();
 // Move the box 2 in the y direction up
 b.setLocalTranslation(new Vector3f(0,2,0));
 // Give the box a solid color of blue.
 b.setDefaultColor(ColorRGBA.blue.clone());
 Sphere s=new Sphere("My sphere",10,10,1f);
 // Do bounds for the sphere, but we'll use a BoundingBox this time
 s.setModelBound(new BoundingBox());
 s.updateModelBound();
 // Give the sphere random colors
 s.setRandomColors();
 // Make a node and give it children
 Node n=new Node("My Node");
 n.attachChild(b);
 n.attachChild(s);
 // Make the node and all its children 5 times larger.
 n.setLocalScale(5);
 // Remove lighting for rootNode so that it will use our
 //basic colors.
 rootNode.setLightCombineMode(Spatial.LightCombineMode.Off);
 rootNode.attachChild(n);
 }
}
 

The first new thing you see is:

首先你会看到的是

// Give the box a bounds object to allow it to be culled
b.setModelBound(new BoundingSphere());
 

Bounding Volumes (such as BoundingSphere and BoundingBox) are the key to speed in jME. What happens is your object is surrounded by a bounds, and this bounds allows jME to do a quick, easy test to see if your object is even viewable. So in your game, you’ll make this really complex person and surround him with a sphere, for instance. A sphere is a really easy object mathematically, so jME can tell very fast if the sphere around your object is visible. If the sphere surrounding your guy isn’t visible (which is an easy test), then jME doesn’t even bother trying to draw your really large, very complex person. In this example, I surround my box with a sphere, but it makes more sense to surround it with a BoundingBox. Why? Well trying to draw a sphere around a box isn’t as tight a fit as a Box around a Box (obviously). I'd just use a sphere as an example.

边界设定(类似BoundingSphere(球形边界) 和 BoundingBox(盒子边界))以加速jMe.对象被边界包裹,这个边界使jME快捷,简单的测试对象是否可见.在游戏中创建更加复杂的人物和被边界包裹的sphere,这里就是一个例子.

TODO,这段不太会翻...汗啊

就是边界,碰撞检测之类的东西.程序运行起来的时候按"B"可以看的到包裹的边界的.

// Calculate the best bounds for the object you gave it
b.updateModelBound();
 

 

When I start, I give my Box object bounds, but it’s an empty bounds. I now have to “update” the bounds so that it surrounds the object correctly. Now we’ll move it up:

我刚开始给了Box对象一个范围,并不是空的范围.我现在不得不"更新"范伟以便对象可以正确.我现在移动它

// Move the box 2 in the y direction up
b.setLocalTranslation(new Vector3f(0,2,0));
 

Now we start moving our object. Vector3f has the format x, y, z, so this moves the object 2 units up. Remember how our Box looks above the Sphere? That’s because I moved it up some. If I had used Vector3f(0,-2,0) then it would have moved down two. Moving objects in jME is extremely simple. Now color it:

 

现 在我们开始移动我们的对象.Vector3f有x,y,z这样的格式,所以这就把对象向上移动了2个单位.记住我们怎么把对象移动的看起来像是向上(向屏 幕外)了,如果是用Vector3f(0,-2,0)就是把对象向下(向屏幕里)移动了.在jMe移动对象是非常简单的.现在给对象上色:

// Give the box a solid color of blue.
b.setDefaultColor(ColorRGBA.blue.clone());
 

Just looking at the function, you can tell I give my box a solid color. As you can guess by the color type, the color is blue. ColorRGBA.red would make it… red! You can also use new ColorRGBA(0,1,0,1). These four numbers are: Red/Green/Blue/Alpha and are percentages from 0 to 1. Alpha is lingo for how opaque it is. (The opposite of opaque is transparent. Opaque would be a wall; not opaque would be a window.) So, an alpha of 0 would mean we couldn’t see it at all, while .5 would mean we see half way through it. New ColorRGBA(0,1,1,1) would create a color that is green and blue. Now let’s make a sphere:

 

就 像方法写的那样,可以给box一个统一的颜色.你可以猜猜看颜色的种类.这里我们用的是蓝色.ColorRGBA.red就是上红色!也可以用 ColorRGBA(0,1,0,1).这四个数字的意思是:红/绿/蓝/Alpha和百分比0到1.Alpha是表示是不是透明的.(相反不透明是透明 的,不透明的将是一个墙;没有不透明的将是一个窗口).因此,一个字母的0将意味着它是透明的,而0.5意味着我们看到一半。新ColorRGBA ( 0,1,1,1 )将建立一个颜色,绿色和蓝色。现在让我们做一个sphere:

Sphere s=new Sphere("My sphere",10,10,1f);
 

jME can’t draw curves. It draws triangles. It’s very hard to draw a circle with triangles, isn’t it? Because of this, you have to get as close to a circle as you can. The first two numbers represent that closeness. If you change 10, 10 to 5,5 you’ll see a pretty bad looking sphere. If you make them 30, 30 you’ll see a very round looking sphere but the problem is that it’s made of a lot of triangles. More triangles mean slower FPS. The last number, 1, is the radius of the sphere. Just like my Node and my Box, Sphere needs a name which I give “My sphere”. For our sphere, let’s be a bit prettier with colors:

jME 不能画曲线.可以画三角形.用三角形画圆是很困难的,是吗?正因为如此,你必须得到接近圆形的.头两个数字是控制接近圆形的.如果你把10改成5那么你会 看到一个五角星,如果你改成30你会看到一个很圆的圆形,但是这样会画很多个三角形,三角形多了意味着拖慢FPS.最后一个数字,1,是圆形的半径,就像 Node和BOx那样,Sphere需要一个名字,像"My sphere".下来为我们的圆球上点颜色:

// Give the sphere random colors
s.setRandomColors();
 

This gives each vertex of the sphere a random color, which creates that psychodelic effect you saw. Now on to the node:

 

随机的给顶点上颜色,弄出了一个迷幻的效果.下来附到Node上去:

// Make a node and give it children
Node n=new Node("My Node");
n.attachChild(b);
n.attachChild(s);
 

Instead of attaching our box and sphere to the rootNode, we create a new node n to attach them too. I also wanted n (and all its children) to be five times bigger, so I did this:

 

我们创建了一个Node用来承载我们的box和sphere,然后把node附到rootNode上去.我还希望node(其上所有的元素)都变成5倍大,看下面:

// Make the node and all its children 5 times larger.
n.setLocalScale(5);
 

You’ll notice it’s similar to the function call setLocalTranslation in name. There’s also a setLocalRotation, which we’ll get into later. As the name suggests, this makes everything five times larger. Finally, you’ll notice a strange line at the end of my code:

 

你会注意到这很像setLocalTranslation()方法.这儿还有一个叫setLocalRotation()的方法,之后我们会讲到.看名字就知道这个方法是的所有的元素都放大五倍了.最后,会注意到最后一行很气怪的代码:

// Remove lighting for rootNode so that it will use our
//basic colors.
rootNode.setLightCombineMode(Spatial.LightCombineMode.Off);
 

You see, per vertex colors (which is what I’m doing here) isn’t really lighting. It’s a cheap, easy way to create colors. You’ll notice there’s no shading for these colors at all. If you don’t use this line, jME won’t try to use per vertex colors. The scene graph looks like this:

 

你看,每个顶点的颜色并没有点亮.这是着色的简单方法.你会注意到这些颜色都没有底纹.如果你不写这一行,jME就不会为每一个顶点着色.现在场景的结构像下面这样:

 

rootNode
“My Node” made 5x bigger
“My box” moved 2 up. “My sphere”

You’ll notice that because “My box” and “My sphere” are children of “My Node”, they are made five times bigger as well. If I move “My Node” down 10, then “My Box” and “My sphere” would move down 10, as well. This is the ease of making a game with a scene graph.

 

你会注意到 “My box” 和 “My sphere” 是依附于 “My Node”的,他们都放大了5倍了.如果我向下移动"My Node"10个单位,那么“My Box”和“My sphere”都向下移动10个单位.

 

While you’re running the program, press “B” to see the bounding in action. It is a neat way to visualize the parent/child scene graph relationship. You can see the BoundingSphere around your box, the BoundingBox around your sphere, and the automatic BoundingSphere around “My node”:

 

当你运行程序的时候,按"B"就会看见元素的边界.用这个方法可以很好的看到场景内元素的依附关系.可以看到BoundingSphere(球形边界)包裹着你box,BoundingBox(盒子边界)包裹着sphere,

 

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics