7、示例4:Hello States

1 示例4:Hello States

本例介绍了MaterialState, TextureState, LightState, and PointLight等状态的使用。

clip_image002

1.1 源代码
隐藏行号 复制代码 源代码
  1. package jmetest.TutorialGuide;
    
  2. import java.net.URL;
    
  3. import com.jme.app.SimpleGame;
    
  4. import com.jme.bounding.BoundingBox;
    
  5. import com.jme.bounding.BoundingSphere;
    
  6. import com.jme.image.Texture;
    
  7. import com.jme.light.PointLight;
    
  8. import com.jme.math.Vector3f;
    
  9. import com.jme.renderer.ColorRGBA;
    
  10. import com.jme.scene.Node;
    
  11. import com.jme.scene.shape.Box;
    
  12. import com.jme.scene.shape.Sphere;
    
  13. import com.jme.scene.state.LightState;
    
  14. import com.jme.scene.state.MaterialState;
    
  15. import com.jme.scene.state.TextureState;
    
  16. import com.jme.util.TextureManager;
    
  17. /**
    
  18.  * Started Date: Jul 20, 2004

  19.  *
    
  20.  * Demonstrates using RenderStates with jME.
    
  21.  * 
    
  22.  * @author Jack Lindamood
    
  23.  */
    
  24. public class HelloStates extends SimpleGame {
    
  25.     public static void main(String[] args) {
    
  26.         HelloStates app = new HelloStates();
    
  27.         app.setConfigShowMode(ConfigShowMode.AlwaysShow);
    
  28.         app.start();
    
  29.     }
    
  30.     protected void simpleInitGame() {
    
  31.         // Create our objects.  Nothing new here.
    
  32.         Box b=new Box("my box",new Vector3f(1,1,1),new Vector3f(2,2,2));
    
  33.         b.setModelBound(new BoundingBox());
    
  34.         b.updateModelBound();
    
  35.         Sphere s=new Sphere("My sphere",15,15,1);
    
  36.         s.setModelBound(new BoundingSphere());
    
  37.         s.updateModelBound();
    
  38.         Node n=new Node("My root node");
    
  39.         // Get a URL that points to the texture we're going to load
    
  40.         URL monkeyLoc;
    
  41.         monkeyLoc=HelloStates.class.getClassLoader().getResource("jmetest/data/images/Monkey.jpg");
    
  42.         // Get a TextureState
    
  43.         TextureState ts=display.getRenderer().createTextureState();
    
  44.         // Use the TextureManager to load a texture
    
  45.         Texture t=TextureManager.loadTexture(monkeyLoc,Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);
    
  46.         // Assign the texture to the TextureState
    
  47.         ts.setTexture(t);
    
  48.         // Get a MaterialState
    
  49.         MaterialState ms=display.getRenderer().createMaterialState();
    
  50.         // Give the MaterialState an emissive tint
    
  51.         ms.setEmissive(new ColorRGBA(0f,.2f,0f,1));
    
  52.         // Create a point light
    
  53.         PointLight l=new PointLight();
    
  54.         // Give it a location
    
  55.         l.setLocation(new Vector3f(0,10,5));
    
  56.         // Make it a red light
    
  57.         l.setDiffuse(ColorRGBA.red.clone());
    
  58.         // Enable it
    
  59.         l.setEnabled(true);
    
  60.         // Create a LightState to put my light in
    
  61.         LightState ls=display.getRenderer().createLightState();
    
  62.         // Attach the light
    
  63.         ls.attach(l);
    
  64.         // Signal that b should use renderstate ts
    
  65.         b.setRenderState(ts);
    
  66.         // Signal that n should use renderstate ms
    
  67.         n.setRenderState(ms);
    
  68.         // Detach all the default lights made by SimpleGame
    
  69.         lightState.detachAll();
    
  70.         // Make my light effect everything below node n
    
  71.         n.setRenderState(ls);
    
  72.         // Attach b and s to n, and n to rootNode.
    
  73.         n.attachChild(b);
    
  74.         n.attachChild(s);
    
  75.         rootNode.attachChild(n);
    
  76.     }
    
  77. }
    

<script type="text/javascript"> </script>

 

1.2 详细说明

// Get a URL that points to the texture we're going to load

URL monkeyLoc; monkeyLoc=HelloStates.class.getClassLoader().getResource("jmetest/data/images/Monkey.jpg");

获取图片资源的路径。

// Get a TextureState

TextureState ts=display.getRenderer().createTextureState();

// Use the TextureManager to load a texture

Texture t=TextureManager.loadTexture(monkeyLoc,Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);

// Assign the texture to the TextureState

ts.setTexture(t);

// Signal that b should use renderstate ts

b.setRenderState(ts);

把立方体的六个面加载上图片。效果如图:

clip_image004

// Create a point light

PointLight l=new PointLight();

// Give it a location

l.setLocation(new Vector3f(0,10,5));

// Make it a red light

l.setDiffuse(ColorRGBA.red.clone());

// Enable it

l.setEnabled(true);

// Create a LightState to put my light in

LightState ls=display.getRenderer().createLightState();

// Attach the light

ls.attach(l);

这是设置一个点光源(代码rootNode.setLightCombineMode(LightCombineMode.Off);得去掉),效果如图:

clip_image006

将材质设为绿色:

// Get a MaterialState

MaterialState ms=display.getRenderer().createMaterialState();

// Give the MaterialState an emissive tint

ms.setEmissive(new ColorRGBA(0f,.2f,0f,1));

b.setRenderState(ms);

clip_image008

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值