Subversion Repositories distributed

Rev

Rev 44 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. package de.viathinksoft.marschall.raumplan.fraktal3d;
  2. import java.awt.Color;
  3.  
  4. import com.jme.math.Vector3f;
  5. import com.jme.renderer.ColorRGBA;
  6. import com.jme.scene.Node;
  7. import com.jme.scene.shape.Box;
  8. import com.jme.scene.state.BlendState;
  9. import com.jme.scene.state.MaterialState;
  10. import com.jme.system.DisplaySystem;
  11.  
  12. public class Kubus {
  13.         protected Box centerbox;
  14.        
  15.         private static final float Size_Faktor = 0.5f;
  16.         private static final float Abstand_Faktor = 0.5f;
  17.         private static final float Max_Iter = 5; // Ab 7 wird's eklig!
  18.        
  19.         public Kubus(Node rootNode, float size, float abstand, float x, float y, float z, LockDirectoryEnum e, int iter) {
  20.                 if (iter > Max_Iter) return;
  21.                
  22.                 Box centerbox = new Box("Center-Box", new Vector3f(x-size/2, y-size/2, z-size/2), new Vector3f(x+size/2, y+size/2, z+size/2));
  23.                
  24.                 Node rn = getRoomNode(centerbox, iter);
  25.                 rootNode.attachChild(rn);
  26.                
  27.                 float abstand2 = 0.5f*size + abstand + 0.5f*size*Size_Faktor;
  28.                
  29.                 if (e != LockDirectoryEnum.LOCK_X_NEG)
  30.                         new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x-abstand2, y, z, LockDirectoryEnum.LOCK_X_POS, iter+1);
  31.                 if (e != LockDirectoryEnum.LOCK_X_POS)
  32.                         new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x+abstand2, y, z, LockDirectoryEnum.LOCK_X_NEG, iter+1);
  33.                 if (e != LockDirectoryEnum.LOCK_Y_NEG)
  34.                         new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y-abstand2, z, LockDirectoryEnum.LOCK_Y_POS, iter+1);
  35.                 if (e != LockDirectoryEnum.LOCK_Y_POS)
  36.                         new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y+abstand2, z, LockDirectoryEnum.LOCK_Y_NEG, iter+1);
  37.                 if (e != LockDirectoryEnum.LOCK_Z_NEG)
  38.                         new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y, z-abstand2, LockDirectoryEnum.LOCK_Z_POS, iter+1);
  39.                 if (e != LockDirectoryEnum.LOCK_Z_POS)
  40.                         new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y, z+abstand2, LockDirectoryEnum.LOCK_Z_NEG, iter+1);
  41.         }
  42.        
  43.         protected static Node getRoomNode(Box centerbox, int iter) {
  44.                 Node roomNode = new Node(); // Unnötiges Zwischennode?
  45.                
  46.                 float opacityAmount = 1.0f;
  47.                 DisplaySystem display = DisplaySystem.getDisplaySystem();
  48.                
  49.                 MaterialState materialState = display.getRenderer()
  50.                 .createMaterialState();
  51.  
  52.         // the sphere material that will be modified to make the sphere
  53.         // look opaque then transparent then opaque and so on
  54.         materialState = display.getRenderer().createMaterialState();
  55.         materialState.setAmbient(new ColorRGBA(0.0f, 0.0f, 0.0f, opacityAmount));
  56.        
  57.         Color x = ColorUtilities.HSLtoRGB(1.0f/iter, 1.0f, 0.5f);
  58.         float r = (float)x.getRed()/255;
  59.         float g = (float)x.getGreen()/255;
  60.         float b = (float)x.getBlue()/255;
  61.        
  62.         materialState.setDiffuse(new ColorRGBA(r, g, b, opacityAmount));
  63.         materialState.setSpecular(new ColorRGBA(1.0f, 1.0f, 1.0f, opacityAmount));
  64.         materialState.setShininess(128.0f);
  65.         materialState.setEmissive(new ColorRGBA(0.0f, 0.0f, 0.0f, opacityAmount));
  66.         materialState.setEnabled(true);
  67.  
  68.         // IMPORTANT: this is used to handle the internal sphere faces when
  69.         // setting them to transparent, try commenting this line to see what
  70.         // happens
  71.         materialState.setMaterialFace(MaterialState.MaterialFace.FrontAndBack);
  72.  
  73.         centerbox.setRenderState(materialState);
  74.         centerbox.updateRenderState();
  75.  
  76.         // to handle transparency: a BlendState
  77.         // an other tutorial will be made to deal with the possibilities of this
  78.         // RenderState
  79.         final BlendState alphaState = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
  80.         alphaState.setBlendEnabled(true);
  81.         alphaState.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
  82.         alphaState.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
  83.         alphaState.setTestEnabled(true);
  84.         alphaState.setTestFunction(BlendState.TestFunction.GreaterThan);
  85.         alphaState.setEnabled(true);
  86.  
  87.         centerbox.setRenderState(alphaState);
  88.         centerbox.updateRenderState();
  89.                
  90.         roomNode.attachChild(centerbox);
  91.         return roomNode;
  92.         }
  93.  
  94. }
  95.