97ff3c12ae1fe0c58d7cb9efa44debc939d12074
[sdk] / samples / 3D / VertexColorTest / VertexColorTest.ec
1 import "ecere"
2
3 class MyApp : GuiApplication
4 {
5    driver = "OpenGL";
6    // driver = "Direct3D";
7 };
8
9 Camera camera
10 {
11    fixed,
12    position = Vector3D { 0, 0, -200 },
13    orientation = Euler { 0, 0, 0 },
14    fov = 53;
15 };
16
17 Light light
18 {
19    //diffuse = white;
20    specular = white;
21    orientation = Euler { pitch = 10, yaw = 30 };
22 };
23
24 Light light2
25 {
26    diffuse = white;
27    //specular = white;
28    orientation = Euler { pitch = 20, yaw = -30 };
29 };
30
31 class FunkaColorCube : Object
32 {
33 public:
34    bool Create(DisplaySystem displaySystem)
35    {
36       bool result = false;
37       if(this)
38       {
39          InitializeMesh(displaySystem);
40
41          if(mesh)
42          {
43             if(mesh.Allocate({ vertices = true, texCoords1 = true, colors = true }, 24, displaySystem))
44             {
45                Vector3Df vertices[24] =
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                   { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
57                   {  (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
58                   {  (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
59                   { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
60                   { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
61                   {  (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
62                   {  (float)size.x/2, (float)size.y/2, (float)size.z/2 },
63                   { -(float)size.x/2, (float)size.y/2, (float)size.z/2 },
64
65                   { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
66                   {  (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
67                   {  (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
68                   { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
69                   { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
70                   {  (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
71                   {  (float)size.x/2, (float)size.y/2, (float)size.z/2 },
72                   { -(float)size.x/2, (float)size.y/2, (float)size.z/2 }
73                };
74                Pointf texCoords[24] =
75                {
76                   { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
77                   { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
78                   { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
79                   { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
80                   { 0, 1 }, { 1, 1 }, { 1, 1 }, { 0, 1 },
81                   { 0, 0 }, { 1, 0 }, { 1, 0 }, { 0, 0 }
82                };
83                ColorRGBAf colors[24] =
84                {
85                   red, magenta, teal, white,
86                   blue, cyan, black, pink,
87                   orange, powderBlue, lightGreen, violet,
88                   green, gray, lightBlue, blanchedAlmond,
89                   purple, forestGreen, slateGray, lavender,
90                   yellow, lime, salmon, tomato
91                };
92                uint16 indices[6][4] =
93                {
94                   // up, front, down, back, right, left
95                   { 17,21,20,16 },
96                   { 0,3,2,1 },
97                   { 22,18,19,23 },
98                   { 5,6,7,4 },
99                   { 9,10,14,13 },
100                   { 12,15,11,8 }
101                };
102
103                int c;
104
105                CopyBytes(mesh.vertices, vertices, sizeof(vertices));
106                CopyBytes(mesh.texCoords, texCoords, sizeof(texCoords));
107                CopyBytes(mesh.colors, colors, sizeof(colors));
108
109                for(c = 0; c<6; c++)
110                {
111                   PrimitiveGroup group;
112                   group = mesh.AddPrimitiveGroup(quads, 4);
113                   if(group)
114                   {
115                      CopyBytes(group.indices, indices[c], sizeof(indices[c]));
116                      mesh.UnlockPrimitiveGroup(group);
117                   }
118                }
119                mesh.ComputeNormals();
120                result = true;
121                mesh.Unlock(0);
122             }
123             SetMinMaxRadius(true);
124          }
125       }
126       return result;
127    }
128
129    property Vector3Df size { set { size = value; } };
130
131 private:
132    FunkaColorCube()
133    {
134       size = { 1,1,1 };
135    }
136
137    Vector3Df size;
138 }
139
140 class Test3D : Window
141 {
142    text = "Form1";
143    background = black;
144    borderStyle = sizable;
145    hasMaximize = true;
146    hasMinimize = true;
147    hasClose = true;
148    size = { 480, 480 };
149
150    //BitmapResource texture { "http://www.ecere.com/images/knot.png", window = this };
151    FunkaColorCube cube { };
152    Material cubeMat { opacity = 1.0f, diffuse = white, ambient = white, flags = { doubleSided = true, translucent = true } };
153
154    bool OnLoadGraphics()
155    {
156       cube.Create(displaySystem);
157       //cubeMat.baseMap = texture.bitmap;
158       cube.mesh.ApplyMaterial(cubeMat);
159       cube.mesh.ApplyTranslucency(cube);
160       cube.transform.scaling = { 100, 100, 100 };
161       cube.transform.position = { 0, 0, 0 };
162       cube.transform.orientation = Euler { 50, 50 };
163       cube.UpdateTransform();
164       return true;
165    }
166
167    void OnResize(int w, int h)
168    {
169       camera.Setup(w, h, null);
170    }
171
172    void OnRedraw(Surface surface)
173    {
174       surface.Clear(depthBuffer);
175       camera.Update();
176       display.SetLight(0, light);
177       display.SetLight(1, light2);
178       display.fogDensity = 0;
179       display.SetCamera(surface, camera);
180       display.DrawObject(cube);
181       display.SetCamera(surface, null);
182    }
183 }
184
185 Test3D test3D {};