Subversion Repositories distributed

Rev

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

Rev Author Line No. Line
44 daniel-mar 1
package de.viathinksoft.marschall.raumplan.fraktal3d;
43 daniel-mar 2
import java.awt.Color;
3
 
22 daniel-mar 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
 
44 daniel-mar 12
public class Kubus {
43 daniel-mar 13
        protected Box centerbox;
26 daniel-mar 14
 
15
        private static final float Size_Faktor = 0.5f;
43 daniel-mar 16
        private static final float Abstand_Faktor = 0.5f;
47 daniel-mar 17
        private static final float Max_Iter = 5; // Ab 7 wird's eklig!
26 daniel-mar 18
 
44 daniel-mar 19
        public Kubus(Node rootNode, float size, float abstand, float x, float y, float z, LockDirectoryEnum e, int iter) {
43 daniel-mar 20
                if (iter > Max_Iter) return;
21
 
47 daniel-mar 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));
43 daniel-mar 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)
44 daniel-mar 30
                        new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x-abstand2, y, z, LockDirectoryEnum.LOCK_X_POS, iter+1);
43 daniel-mar 31
                if (e != LockDirectoryEnum.LOCK_X_POS)
44 daniel-mar 32
                        new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x+abstand2, y, z, LockDirectoryEnum.LOCK_X_NEG, iter+1);
43 daniel-mar 33
                if (e != LockDirectoryEnum.LOCK_Y_NEG)
44 daniel-mar 34
                        new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y-abstand2, z, LockDirectoryEnum.LOCK_Y_POS, iter+1);
43 daniel-mar 35
                if (e != LockDirectoryEnum.LOCK_Y_POS)
44 daniel-mar 36
                        new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y+abstand2, z, LockDirectoryEnum.LOCK_Y_NEG, iter+1);
43 daniel-mar 37
                if (e != LockDirectoryEnum.LOCK_Z_NEG)
44 daniel-mar 38
                        new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y, z-abstand2, LockDirectoryEnum.LOCK_Z_POS, iter+1);
43 daniel-mar 39
                if (e != LockDirectoryEnum.LOCK_Z_POS)
44 daniel-mar 40
                        new Kubus(rn, size*Size_Faktor, abstand*Abstand_Faktor, x, y, z+abstand2, LockDirectoryEnum.LOCK_Z_NEG, iter+1);
26 daniel-mar 41
        }
42
 
43 daniel-mar 43
        protected static Node getRoomNode(Box centerbox, int iter) {
44
                Node roomNode = new Node(); // Unnötiges Zwischennode?
22 daniel-mar 45
 
26 daniel-mar 46
                float opacityAmount = 1.0f;
22 daniel-mar 47
                DisplaySystem display = DisplaySystem.getDisplaySystem();
48
 
49
                MaterialState materialState = display.getRenderer()
50
                .createMaterialState();
43 daniel-mar 51
 
52
        // the sphere material that will be modified to make the sphere
22 daniel-mar 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));
43 daniel-mar 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
 
47 daniel-mar 62
        materialState.setDiffuse(new ColorRGBA(r, g, b, opacityAmount));
22 daniel-mar 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
 
26 daniel-mar 73
        centerbox.setRenderState(materialState);
74
        centerbox.updateRenderState();
43 daniel-mar 75
 
22 daniel-mar 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
 
26 daniel-mar 87
        centerbox.setRenderState(alphaState);
88
        centerbox.updateRenderState();
22 daniel-mar 89
 
43 daniel-mar 90
        roomNode.attachChild(centerbox);
26 daniel-mar 91
        return roomNode;
22 daniel-mar 92
        }
93
 
94
}