Subversion Repositories distributed

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
22 daniel-mar 1
package test.fraktal3d;
2
import com.jme.math.Vector3f;
3
import com.jme.renderer.ColorRGBA;
4
import com.jme.scene.Node;
5
import com.jme.scene.shape.Box;
6
import com.jme.scene.state.BlendState;
7
import com.jme.scene.state.MaterialState;
8
import com.jme.system.DisplaySystem;
9
 
10
public class Room {
11
        private Box bottom;
12
        private Box top;
13
        private Box left;
14
        private Box right;
15
        private Box front;
16
        private Box back;
17
 
18
        private float thickness;
19
 
20
        public Room(float length, float width, float depth, float thickness) {
21
                this.thickness = thickness;
22
 
23
                this.bottom = new Box("Bottom", new Vector3f(), width + thickness,
24
                                thickness, depth);
25
 
26
                this.top = new Box("Top", new Vector3f(), width + thickness, thickness,
27
                                depth);
28
                this.top.setLocalTranslation(new Vector3f(0, length * 2, 0));
29
 
30
                this.left = new Box("Left Border", new Vector3f(), thickness, length,
31
                                depth);
32
                this.left.setLocalTranslation(new Vector3f(-(width), length, 0));
33
 
34
                this.right = new Box("Right Border", new Vector3f(), thickness, length,
35
                                depth);
36
                this.right.setLocalTranslation(new Vector3f(width, length, 0));
37
 
38
                this.back = new Box("Back Border", new Vector3f(), width, length,
39
                                thickness);
40
                this.back.setLocalTranslation(new Vector3f(0, length, -depth));
41
 
42
                this.front = new Box("Front Border", new Vector3f(), width, length,
43
                                thickness);
44
                this.front.setLocalTranslation(new Vector3f(0, length, depth));
45
        }
46
 
47
        public Node getRoomNode() {
48
                Node roomNode = new Node();
49
 
50
                float opacityAmount = 0.1f;
51
                DisplaySystem display = DisplaySystem.getDisplaySystem();
52
 
53
                MaterialState materialState = display.getRenderer()
54
                .createMaterialState();
55
 
56
        // the sphere material taht will be modified to make the sphere
57
        // look opaque then transparent then opaque and so on
58
        materialState = display.getRenderer().createMaterialState();
59
        materialState.setAmbient(new ColorRGBA(0.0f, 0.0f, 0.0f, opacityAmount));
60
        materialState.setDiffuse(new ColorRGBA(0.1f, 0.5f, 0.8f, opacityAmount));
61
        materialState.setSpecular(new ColorRGBA(1.0f, 1.0f, 1.0f, opacityAmount));
62
        materialState.setShininess(128.0f);
63
        materialState.setEmissive(new ColorRGBA(0.0f, 0.0f, 0.0f, opacityAmount));
64
        materialState.setEnabled(true);
65
 
66
        // IMPORTANT: this is used to handle the internal sphere faces when
67
        // setting them to transparent, try commenting this line to see what
68
        // happens
69
        materialState.setMaterialFace(MaterialState.MaterialFace.FrontAndBack);
70
 
71
        back.setRenderState(materialState);
72
        back.updateRenderState();
73
 
74
        roomNode.attachChild(back);
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
        back.setRenderState(alphaState);
88
        back.updateRenderState();
89
 
90
 
91
 
92
                roomNode.attachChild(bottom);
93
                roomNode.attachChild(top);
94
                roomNode.attachChild(right);
95
                roomNode.attachChild(left);
96
                roomNode.attachChild(back);
97
 
98
                return roomNode;
99
        }
100
 
101
        public Vector3f getBorderPosition(RoomBorderEnum border) {
102
                Vector3f vec = new Vector3f();
103
 
104
                if (border == RoomBorderEnum.UP) {
105
                        vec = new Vector3f(this.top.getLocalTranslation());
106
                        vec.y -= this.thickness;
107
 
108
                } else if (border == RoomBorderEnum.DOWN) {
109
                        vec = new Vector3f(this.bottom.getLocalTranslation());
110
                        vec.y += this.thickness;
111
                }
112
 
113
                else if (border == RoomBorderEnum.LEFT) {
114
                        vec = new Vector3f(this.left.getLocalTranslation());
115
                        vec.x += this.thickness;
116
                }
117
 
118
                else if (border == RoomBorderEnum.RIGHT) {
119
                        vec = new Vector3f(this.right.getLocalTranslation());
120
                        vec.x -= this.thickness;
121
                }
122
 
123
                else if (border == RoomBorderEnum.FRONT) {
124
                        vec = new Vector3f(this.front.getLocalTranslation());
125
                        vec.z -= this.thickness;
126
                }
127
 
128
                else if (border == RoomBorderEnum.BACK) {
129
                        vec = new Vector3f(this.back.getLocalTranslation());
130
                        vec.z += this.thickness;
131
                }
132
 
133
                return vec;
134
        }
135
}