5、示例2:Hello Node

1 示例2:Hello Node

这个例子将介绍Nodes, Bounding Volumes, Sphere, Colors, Translation(平移) and Scaling(缩放)。

1.1 源代码
隐藏行号 复制代码 源代码
  1. package jmetest.TutorialGuide;
    
  2.  
  3. import com.jme.app.SimpleGame;
    
  4. import com.jme.bounding.BoundingBox;
    
  5. import com.jme.bounding.BoundingSphere;
    
  6. import com.jme.math.Vector3f;
    
  7. import com.jme.renderer.ColorRGBA;
    
  8. import com.jme.scene.Node;
    
  9. import com.jme.scene.Spatial;
    
  10. import com.jme.scene.shape.Box;
    
  11. import com.jme.scene.shape.Sphere;
    
  12.  
  13. /**
    
  14.  * Started Date: Jul 20, 2004<br><br>
    
  15.  *
    
  16.  * Simple Node object with a few Geometry manipulators.
    
  17.  * 
    
  18.  * @author Jack Lindamood
    
  19.  */
    
  20. public class HelloNode extends SimpleGame {
    
  21.     public static void main(String[] args) {
    
  22.         HelloNode app = new HelloNode();
    
  23.         app.setConfigShowMode(ConfigShowMode.AlwaysShow);
    
  24.         app.start();
    
  25.     }
    
  26.  
  27.     protected void simpleInitGame() {
    
  28.         Box b=new Box("My Box",new Vector3f(0,0,0),new Vector3f(1,1,1));
    
  29.         // Give the box a bounds object to allow it to be culled
    
  30.         b.setModelBound(new BoundingSphere());
    
  31.         // Calculate the best bounds for the object you gave it
    
  32.         b.updateModelBound();
    
  33.         // Move the box 2 in the y direction up
    
  34.         b.setLocalTranslation(new Vector3f(0,2,0));
    
  35.         // Give the box a solid color of blue.
    
  36.         b.setDefaultColor(ColorRGBA.blue.clone());
    
  37.  
  38.         Sphere s=new Sphere("My sphere",5,5,1f);
    
  39.         // Do bounds for the sphere, but we'll use a BoundingBox this time
    
  40.         s.setModelBound(new BoundingBox());
    
  41.         s.updateModelBound();
    
  42.         // Give the sphere random colors
    
  43.         s.setRandomColors();
    
  44.  
  45.         // Make a node and give it children
    
  46.         Node n=new Node("My Node");
    
  47.         n.attachChild(b);
    
  48.         n.attachChild(s);
    
  49.         // Make the node and all its children 5 times larger.
    
  50.         n.setLocalScale(5);
    
  51.  
  52.         // Remove  lighting for rootNode so that it will use our basic colors.
    
  53.         rootNode.setLightCombineMode(Spatial.LightCombineMode.Off);
    
  54.         rootNode.attachChild(n);
    
  55.     }
    
  56. }
    
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){var content=codeElement.innerText;if(window.clipboardData==null){window.alert("您的浏览器不支持脚本复制,请尝试手动复制。")}else{window.clipboardData.setData("Text",content);window.alert("源代码已经复制到剪贴板上。")}}}function LineNumberVisible(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i  

1.2 详细说明

首先来预览一下结果

clip_image002

// Give the box a bounds object to allow it to be culled

b.setModelBound(new BoundingSphere());

用一个球把这个box的边界包围起来。包围盒(Bounding Volumes)例如BoundingSphere、BoundingBox、BoundingCapsule是jME中用来加速的关键。通常一个物体是不规则的。对一个不规则的物体,计算起来自然要麻烦一些。如果把这个物体用一个规则的简单的形状包围起来,那么就可以提高计算速度,也可以很简单地测试这个物体是否可见。比如我们可以把一个非常复杂的人用一个球包起来。如果这个球不可见,咱们也用不着花那么大的力气去画一个非常复杂的人。本例中我们用一个球来包一个立方体。

这里只是一个空的边界,需要调用

// Calculate the best bounds for the object you gave it

b.updateModelBound();

以使球正确无缝地包起立方体。用一个半径1米的球做宽只有1厘米的立方体显然是没什么意义的。updateModelBound()会计算最佳的边界。

// Move the box 2 in the y direction up

b.setLocalTranslation(new Vector3f(0,2,0));

是将这个box向上平移2个单元格。因为它的初始坐标是(0,0,0),现在设成了(0,2,0)。

// Give the box a solid color of blue.

b.setDefaultColor(ColorRGBA.blue.clone());

把球的颜色着为蓝色。ColorRGBA的四元素为Red/Green/Blue/Alpha(透明度)。取值范围是[0-1],0为完全透明的,0.5意味着半透明。

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

这里我们新构造了一个球。jME只会画三角形,不会画曲线。用三角形来画圆形是非常困难的,所以只能是无限接近圆。中间两个参数表示了近似度,值越大越近似圆。但是值越大的话所画的三角形也越多,太多的三角形也会降低帧率(FPS)。最后一个参数是球的半径,float型。

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);

把box和球加到节点中。再把这个节点加到场景的根节点中:rootNode.attachChild(n);

要将节点n的所有子对象同步放大5倍怎么做呢:

// Make the node and all its children 5 times larger.

n.setLocalScale(5);

同理,想平移的话用setLocalTranslation,旋转用setLocalRotation。

// Remove lighting for rootNode so that it will use our basic colors.

rootNode.setLightCombineMode(Spatial.LightCombineMode.Off);

最后加这行代码使我们设置的颜色生效。否则就是灰色的。

场景图如下:

rootNode

“My Node” made 5x bigger

“My box” moved 2 up.

“My sphere”

运行时可以按“B”看下边界效果:

clip_image004

按“T”数一下有多少三角形:

clip_image006

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值