1 module dlangui.graphics.scene.skybox;
2 
3 public import dlangui.core.config;
4 static if (ENABLE_OPENGL):
5 static if (BACKEND_GUI):
6 
7 import dlangui.core.math3d;
8 import dlangui.graphics.scene.node;
9 import dlangui.graphics.scene.model;
10 import dlangui.graphics.scene.scene3d;
11 import dlangui.graphics.scene.mesh;
12 import dlangui.graphics.scene.material;
13 import dlangui.graphics.scene.effect;
14 
15 class SkyBox : Node3d {
16     private Node3d[6] _faceNodes;
17 
18     enum Face {
19         Back,
20         Front,
21         Left,
22         Right,
23         Top,
24         Bottom
25     }
26 
27     this(Scene3d scene) {
28         _parent = scene;
29         _scene = scene;
30         _visible = false;
31     }
32 
33     void removeFace(Face face) {
34         if (_faceNodes[face]) {
35             removeChild(_faceNodes[face]);
36             _faceNodes[face] = null;
37         }
38     }
39 
40     void setFaceTexture(Face face, string textureName) {
41         removeFace(face);
42         if (!textureName)
43             return;
44         Material material = new Material(EffectId("textured.vert", "textured.frag", null), textureName);
45         //material.ambientColor = vec3(0.9,0.9,0.9);
46         material.textureLinear = true;
47         Mesh mesh = new Mesh(VertexFormat(VertexElementType.POSITION, VertexElementType.COLOR, VertexElementType.TEXCOORD0));
48         const float d = 1;
49         const vec3 pos = vec3(0,0,0);
50         vec4 color = vec4(1,1,1,1);
51         auto p000 = vec3(pos.x-d, pos.y-d, pos.z-d);
52         auto p100 = vec3(pos.x+d, pos.y-d, pos.z-d);
53         auto p010 = vec3(pos.x-d, pos.y+d, pos.z-d);
54         auto p110 = vec3(pos.x+d, pos.y+d, pos.z-d);
55         auto p001 = vec3(pos.x-d, pos.y-d, pos.z+d);
56         auto p101 = vec3(pos.x+d, pos.y-d, pos.z+d);
57         auto p011 = vec3(pos.x-d, pos.y+d, pos.z+d);
58         auto p111 = vec3(pos.x+d, pos.y+d, pos.z+d);
59 
60         final switch(face) with(Face) {
61             case Back:
62                 mesh.addQuad(p111, p011, p001, p101, color); // back face
63                 break;
64             case Front:
65                 mesh.addQuad(p010, p110, p100, p000, color); // front face
66                 break;
67             case Right:
68                 mesh.addQuad(p110, p111, p101, p100, color); // right face
69                 break;
70             case Left:
71                 mesh.addQuad(p011, p010, p000, p001, color); // left face
72                 break;
73             case Top:
74                 mesh.addQuad(p011, p111, p110, p010, color); // top face
75                 break;
76             case Bottom:
77                 mesh.addQuad(p000, p100, p101, p001, color); // bottom face
78                 break;
79         }
80         Model model = new Model(material, mesh);
81         model.autobindLights = false;
82         Node3d node = new Node3d();
83         node.drawable = model;
84         _faceNodes[face] = node;
85         addChild(node);
86         _visible = true;
87     }
88 }