外出旅行、冬季保暖得常备户外袜、速干袜、加厚袜子哦。

猛戳乐途驿站http://zhoupa1188.taobao.com抢购品牌男女式加厚户外袜子,coolmax、全棉、保暖、吸汗、速干、登山、徒步袜子。满10包邮


谢炜的cnblogs

CSDN上比较完整:http://hi.csdn.net/xiefeifeihu

导航

7、示例4:Hello States

1 示例4:Hello States

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

clip_image002

1.1 源代码

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

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

 

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

posted on 2009-09-25 12:26  飞飞狐  阅读(221)  评论(0编辑  收藏  举报

外出旅行、冬季保暖得常备户外袜、速干袜、加厚袜子哦。

猛戳乐途驿站http://zhoupa1188.taobao.com抢购品牌男女式加厚户外袜子,coolmax、全棉、保暖、吸汗、速干、登山、徒步袜子。满10包邮