Initial Import
[glAntsMech.git] / glants_mech / win32 / glAntsV05 / cube.cpp
blob6128faeddabaf7f2409b9d42513d24fe99b3aa55
1 //
2 // Berlin Brown
3 // bigbinc@hotmail.com
4 //
5 // cube.cpp
6 //
7 #include <windows.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include <gl\gl.h> // Header File For The OpenGL32 Library
12 #include <gl\glu.h> // Header File For The GLu32 Library
13 #include <gl\glaux.h> // Header File For The Glaux Library
15 #include "objects.h"
17 static void compilecube(void);
18 static void drawcube(void);
19 static void init_cube(int list_id);
20 static void rendercube(void);
22 #undef CURRENT_OBJECT
23 #define CURRENT_OBJECT colorcube
26 // simple objects library
28 DriverObjects colorcube =
30 init_cube, // init, must be called first
31 compilecube, // compile
32 drawcube, // draw
33 rendercube, // render to scene
34 0 // loaded by INIT
38 // init cube
39 // - load anything special about the cube
40 // one important function
42 static void init_cube(int list_id)
45 CURRENT_OBJECT.visible = 0;
47 // store the id through the function
48 // there is probably a better way to do this
49 CURRENT_OBJECT.call_id = list_id;
51 } // end of the functino
53 //=========================================================
54 // draw cube with normals turned on
55 // Note: have to use triangles, (dope!)
56 // - also no particular order when drawing triangles
57 //=========================================================
58 static void drawcube(void)
60 float v[3][3] = { 0 };
61 float size = 1.0f;
63 // change the size here
64 // Note: starts from ground
66 glBegin(GL_TRIANGLES);
68 // left bottom front
69 v[0][0] = -size;
70 v[0][1] = 0.0f;
71 v[0][2] = size;
73 v[1][0] = size;
74 v[1][1] = 0.0f;
75 v[1][2] = size;
77 v[2][0] = size;
78 v[2][1] = size;
79 v[2][2] = size;
81 glColor3f(1.0f,0.0f,0.0f);
82 // Calc normal and draw
83 glVertex3fv(v[0]);
84 glVertex3fv(v[1]);
85 glVertex3fv(v[2]); // triangle left bottom front
87 // Finish the front
88 v[0][0] = size;
89 v[0][1] = size;
90 v[0][2] = size;
92 v[1][0] = -size;
93 v[1][1] = size;
94 v[1][2] = size;
96 v[2][0] = -size;
97 v[2][1] = 0.0f;
98 v[2][2] = size;
100 // Calc normal and draw
101 glVertex3fv(v[0]);
102 glVertex3fv(v[1]);
103 glVertex3fv(v[2]); // triangle left bottom front
105 // Draw the back triangle
106 //-----------------------------
107 v[0][0] = -size;
108 v[0][1] = 0.0f;
109 v[0][2] = -size;
111 v[1][0] = size;
112 v[1][1] = 0.0f;
113 v[1][2] = -size;
115 v[2][0] = size;
116 v[2][1] = size;
117 v[2][2] = -size;
120 glColor3f(1.0f,1.0f,0.0f);
121 // Calc normal and draw
122 glVertex3fv(v[0]);
123 glVertex3fv(v[1]);
124 glVertex3fv(v[2]); // triangle left bottom bac
126 // Finish the back
127 v[0][0] = size;
128 v[0][1] = size;
129 v[0][2] = -size;
131 v[1][0] = -size;
132 v[1][1] = size;
133 v[1][2] = -size;
135 v[2][0] = -size;
136 v[2][1] = 0.0f;
137 v[2][2] = -size;
139 // Calc normal and draw
140 glVertex3fv(v[0]);
141 glVertex3fv(v[1]);
142 glVertex3fv(v[2]); // triangle left bottom front
144 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?
145 // Draw the right side
146 // Triangle
147 v[0][0] = size;
148 v[0][1] = 0.0f;
149 v[0][2] = size;
151 v[1][0] = size;
152 v[1][1] = 0.0f;
153 v[1][2] = -size;
155 v[2][0] = size;
156 v[2][1] = size;
157 v[2][2] = size;
161 glColor3f(0.0f,0.5f,1.0f);
162 // Calc normal and draw
163 glVertex3fv(v[0]);
164 glVertex3fv(v[1]);
165 glVertex3fv(v[2]); // triangle left bottom bac
167 // FINISh the right side of the box
168 v[0][0] = size;
169 v[0][1] = 0.0f;
170 v[0][2] = -size;
172 v[1][0] = size;
173 v[1][1] = size;
174 v[1][2] = -size;
176 v[2][0] = size;
177 v[2][1] = size;
178 v[2][2] = size;
180 // Calc normal and draw
181 glVertex3fv(v[0]);
182 glVertex3fv(v[1]);
183 glVertex3fv(v[2]); // triangle left bottom bac
185 // FINISh the left side of the box
186 v[0][0] = -size;
187 v[0][1] = 0.0f;
188 v[0][2] = -size;
190 v[1][0] = -size;
191 v[1][1] = size;
192 v[1][2] = -size;
194 v[2][0] = -size;
195 v[2][1] = size;
196 v[2][2] = size;
198 glColor3f(1.0f,0.5f,1.0f);
199 // Calc normal and draw
200 glVertex3fv(v[0]);
201 glVertex3fv(v[1]);
202 glVertex3fv(v[2]); // triangle left bottom bac
204 // Draw the left side
205 // Triangle
206 v[0][0] = -size;
207 v[0][1] = 0.0f;
208 v[0][2] = size;
210 v[1][0] = -size;
211 v[1][1] = 0.0f;
212 v[1][2] = -size;
214 v[2][0] = -size;
215 v[2][1] = size;
216 v[2][2] = size;
218 // Calc normal and draw
219 glVertex3fv(v[0]);
220 glVertex3fv(v[1]);
221 glVertex3fv(v[2]); // triangle left side
223 // Draw the top and bottom
224 v[0][0] = size;
225 v[0][1] = size;
226 v[0][2] = size;
228 v[1][0] = size;
229 v[1][1] = size;
230 v[1][2] = -size;
232 v[2][0] = -size;
233 v[2][1] = size;
234 v[2][2] = -size;
236 glColor3f(0.6f,0.6f,0.6f);
237 // Calc normal and dra
238 glVertex3fv(v[0]);
239 glVertex3fv(v[1]);
240 glVertex3fv(v[2]); // triangle left side
242 // Draw one of the bottom triangles
243 v[0][0] = size;
244 v[0][1] = 0.0f;
245 v[0][2] = size;
247 v[1][0] = size;
248 v[1][1] = 0.0f;
249 v[1][2] = -size;
251 v[2][0] = -size;
252 v[2][1] = 0.0f;
253 v[2][2] = -size;
255 // Calc normal and draw
256 glVertex3fv(v[0]);
257 glVertex3fv(v[1]);
258 glVertex3fv(v[2]); // triangle left side
260 // Lets finish the bottom with the second triangle
261 v[0][0] = -size;
262 v[0][1] = 0.0f;
263 v[0][2] = size;
265 v[1][0] = size;
266 v[1][1] = 0.0f;
267 v[1][2] = size;
269 v[2][0] = -size;
270 v[2][1] = 0.0f;
271 v[2][2] = -size;
274 glColor3f(0.03f,0.3f,0.3f);
275 // Calc normal and dra
276 glVertex3fv(v[0]);
277 glVertex3fv(v[1]);
278 glVertex3fv(v[2]); // triangle left side
280 // Go back and finish the top
281 v[0][0] = -size;
282 v[0][1] = size;
283 v[0][2] = size;
285 v[1][0] = size;
286 v[1][1] = size;
287 v[1][2] = size;
289 v[2][0] = -size;
290 v[2][1] = size;
291 v[2][2] = -size;
293 // Calc normal and dra
294 glVertex3fv(v[0]);
295 glVertex3fv(v[1]);
296 glVertex3fv(v[2]); // triangle left side
298 glEnd();
300 } // end of the function
303 //=========================================================
304 // Now the function to actually draw it
305 //=========================================================
306 static void rendercube(void)
308 static float x=0.0f;
310 if (CURRENT_OBJECT.visible)
312 glPushMatrix();
314 glRotatef(x, 0.0f, 1.0f, 0.0f);
315 glCallList(colorcube.call_id);
316 x+=2.1f;
317 glPopMatrix();
318 } // end of the if
320 } // end of the function
323 //=========================================================
324 // compile cube
325 //=========================================================
326 static void compilecube(void)
328 int cube_id;
329 // setup a spot for display list for background
330 //cubeobject = getcurrentobject();
331 cube_id = colorcube.call_id;
333 // apply list
334 glNewList(cube_id, GL_COMPILE);
336 // call drawing function
337 // we could use (drawcube)
338 // but this may method make it a little better
339 colorcube.draw();
341 glEndList();
343 } // end of the function