6901fc51de25ec94d10b699db5adc0ef7a83a2c4
[sdk] / ecere / src / gfx / 3D / meshes / Cube.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    cube.ec - Translucent Cube 3D Model
10 ****************************************************************************/
11 import "Display"
12
13 public class Cube : Object
14 {
15 public:
16    bool Create(DisplaySystem displaySystem)
17    {
18       bool result = false;
19       if(this)
20       {
21          InitializeMesh(displaySystem);
22
23          if(mesh)
24          {
25             if(mesh.Allocate({ vertices = true, texCoords1 = true }, 24, displaySystem))
26             {
27                Vector3Df vertices[24] =
28                {
29                   { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
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
38                   { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
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
47                   { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
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                };
56                Pointf texCoords[24] =
57                {
58                   { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
59                   { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
60                   { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
61                   { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
62                   { 0, 1 }, { 1, 1 }, { 1, 1 }, { 0, 1 },
63                   { 0, 0 }, { 1, 0 }, { 1, 0 }, { 0, 0 }
64                };
65                uint16 indices[6][4] =
66                {
67                   // up, front, down, back, right, left
68                   { 17,21,20,16 }, 
69                   { 0,3,2,1 }, 
70                   { 22,18,19,23 }, 
71                   { 5,6,7,4 }, 
72                   { 9,10,14,13 }, 
73                   { 12,15,11,8 }
74                };
75
76                int c;
77
78                CopyBytes(mesh.vertices, vertices, sizeof(vertices));
79                CopyBytes(mesh.texCoords, texCoords, sizeof(texCoords));
80
81                for(c = 0; c<6; c++)
82                {
83                   PrimitiveGroup group;
84                   Material material;
85                   char name[20];
86                   /*
87                   sprintf(name, "tex%d.bmp", c+1);
88                   if(c == 3) strcpy(name, "glass.bmp");
89                   */
90                   sprintf(name, "Cube Face %d", c+1);
91                   material = displaySystem.AddNamedMaterial(name);
92                   if(material)
93                   {
94                      material.flags = { noFog = true, doubleSided = true, translucent = true };
95                      material.opacity = 0.5f;
96                      material.opacity = 1;
97                      material.diffuse.r = material.diffuse.g = material.diffuse.b = 1;
98                      material.ambient = material.diffuse;
99
100                      /*
101                      material.baseMap = display.GetTexture(, name);
102                      if(!material.baseMap)
103                      {
104                         material.baseMap = Bitmap { };
105                         material.baseMap.LoadMipMaps(name, null, displaySystem);
106                         displaySystem.AddTexture(name, material.baseMap);
107                      }*/
108                   }
109                   group = mesh.AddPrimitiveGroup(quads, 4);
110                   // group = mesh.AddPrimitiveGroup(triFan, 4);
111                   if(group)
112                   {
113                      CopyBytes(group.indices, indices[c], sizeof(indices[c]));
114                      mesh.UnlockPrimitiveGroup(group);
115                      group.material = material;
116                   }
117                }
118                mesh.ComputeNormals();
119                result = true;
120                mesh.Unlock(0);
121             }
122             SetMinMaxRadius(true);
123          }
124       }
125       return result;
126    }
127
128    property Vector3Df size { set { size = value; } };
129
130 private:
131    Cube()
132    {
133       size = { 1,1,1 };
134    }
135
136    Vector3Df size;
137 }