cleaned all trailing white space from source files.
[sdk] / ecere / src / gfx / 3D / meshes / SkyBox.ec
1 namespace gfx3D::meshes;
2
3 /****************************************************************************
4    ECERE Runtime Library
5
6    Copyright (c) 2001 Jerome Jacovella-St-Louis
7    All Rights Reserved.
8
9    skybox.ec - SkyBox 3D Model
10 ****************************************************************************/
11 import "Display"
12
13 static char * faceNames[] = { "up", "fr", "dn", "bk", "rt", "lf" };
14
15 public class SkyBox : Object
16 {
17 public:
18    bool Create(DisplaySystem displaySystem)
19    {
20       bool result = false;
21       if(this)
22       {
23          InitializeMesh(displaySystem);
24
25          if(mesh.Allocate({vertices = true, texCoords1 = true }, 24, displaySystem))
26          {
27             PrimitiveGroup group;
28             Vector3Df vertices[24] =
29             {
30                { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
31                {  (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
32                {  (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
33                { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
34                { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
35                {  (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
36                {  (float)size.x/2, (float)size.y/2, (float)size.z/2 },
37                { -(float)size.x/2, (float)size.y/2, (float)size.z/2 },
38
39                { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
40                {  (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
41                {  (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
42                { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
43                { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
44                {  (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
45                {  (float)size.x/2, (float)size.y/2, (float)size.z/2 },
46                { -(float)size.x/2, (float)size.y/2, (float)size.z/2 },
47
48                { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
49                {  (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
50                {  (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
51                { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
52                { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
53                {  (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
54                {  (float)size.x/2, (float)size.y/2, (float)size.z/2 },
55                { -(float)size.x/2, (float)size.y/2, (float)size.z/2 }
56             };
57             Pointf texCoords[24] =
58             {
59                { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
60                { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
61                { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
62                { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
63                { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
64                { 0, 1 }, { 1, 1 }, { 1, 0 }, { 0, 0 }
65             };
66
67             uint16 indices[6][4] =
68             {
69                // up, front, down, back, right, left
70                /*
71                { 20,21,16,17 },
72                { 7,6,4,5 },
73                { 19,18,23,22 },
74                { 2,3,1,0 },
75                { 14,10,13,9 },
76                { 11,15,8,12 }
77                */
78                { 20,21,17,16 },
79                { 7,6,5,4 },
80                { 19,18,22,23 },
81                { 2,3,0,1 },
82                { 14,10,9,13 },
83                { 11,15,12,8 }
84             };
85
86             int c;
87
88             CopyBytes(mesh.vertices, vertices, sizeof(vertices));
89             CopyBytes(mesh.texCoords, texCoords, sizeof(texCoords));
90
91             for(c = 0; c<6; c++)
92             {
93                Material material;
94                char name[20];
95                sprintf(name, "SKYBOX %s", faceNames[c]);
96                material = displaySystem.AddNamedMaterial(name);
97                if(material)
98                {
99                   char name[256];
100                   sprintf(name, "%s/%s.%s", folder ? folder : ":skycube", faceNames[c], extension ? extension : "pcx");
101                   material.flags = { noFog = true };
102                   material.opacity = 1;
103                   material.emissive.r = material.emissive.g = material.emissive.b = 1;
104                   material.baseMap = Bitmap { };
105                   material.baseMap.LoadMipMaps(name, null, displaySystem);
106                   displaySystem.AddTexture(name, material.baseMap);
107                }
108                group = mesh.AddPrimitiveGroup(triFan, 4);
109                //group = mesh.AddPrimitiveGroup(quads, sizeof(indices[c]) / sizeof(uint16));
110                if(group)
111                {
112                   group.material = material;
113                   CopyBytes(group.indices, indices[c], sizeof(indices[c]));
114                   mesh.UnlockPrimitiveGroup(group);
115                }
116             }
117             result = true;
118             mesh.Unlock(0);
119          }
120          SetMinMaxRadius(true);
121       }
122       return result;
123    }
124
125    void Render(Camera camera, Display display)
126    {
127       if(this)
128       {
129          display.depthTest = false;
130          display.depthWrite = false;
131
132          flags.viewSpace = true;
133          flags.transform = true;
134          flags.root = true;
135
136          // TODO: Not working...
137          // transform.orientation.Inverse(camera.cOrientation);
138          {
139             Quaternion iquat;
140             iquat.Inverse(camera.cOrientation);
141             transform.orientation = iquat;
142          }
143
144          UpdateTransform();
145          display.DrawObject(this);
146
147          display.depthWrite = true;
148          display.depthTest = true;
149       }
150    }
151
152    property Vector3Df size { set { size = value; } };
153    property char * folder { set { folder = value; } };
154    property char * extension { set { extension = value; } };
155
156 private:
157    SkyBox()
158    {
159       size = { 10000,10000,10000 };
160       folder = ":skycube";
161       extension = "pcx";
162    }
163
164    Vector3Df size;
165    char * folder, * extension;
166 }