1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: Christian Meyer |
---|
15 | */ |
---|
16 | |
---|
17 | #include "world.h" |
---|
18 | #include "world_entity.h" |
---|
19 | #include "collision.h" |
---|
20 | #include "track_manager.h" |
---|
21 | #include "player.h" |
---|
22 | #include "command_node.h" |
---|
23 | #include "camera.h" |
---|
24 | #include "environment.h" |
---|
25 | #include "p_node.h" |
---|
26 | #include "null_parent.h" |
---|
27 | #include "helper_parent.h" |
---|
28 | #include "glmenu_imagescreen.h" |
---|
29 | #include "skysphere.h" |
---|
30 | #include "light.h" |
---|
31 | #include "sfont/fontset.h" |
---|
32 | |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | \brief create a new World |
---|
38 | |
---|
39 | This creates a new empty world! |
---|
40 | */ |
---|
41 | World::World (char* name) |
---|
42 | { |
---|
43 | this->setClassName ("World"); |
---|
44 | this->worldName = name; |
---|
45 | this->debugWorldNr = -1; |
---|
46 | this->entities = new tList<WorldEntity>(); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | \brief creates a new World... |
---|
51 | \param worldID with this ID |
---|
52 | */ |
---|
53 | World::World (int worldID) |
---|
54 | { |
---|
55 | this->debugWorldNr = worldID; |
---|
56 | this->worldName = NULL; |
---|
57 | this->entities = new tList<WorldEntity>(); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | \brief remove the World from memory |
---|
62 | |
---|
63 | delete everything explicitly, that isn't contained in the parenting tree! |
---|
64 | things contained in the tree are deleted automaticaly |
---|
65 | */ |
---|
66 | World::~World () |
---|
67 | { |
---|
68 | printf("World::~World() - deleting current world\n"); |
---|
69 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
70 | cn->unbind(this->localPlayer); |
---|
71 | cn->reset(); |
---|
72 | this->localCamera->destroy(); |
---|
73 | |
---|
74 | this->nullParent->destroy(); |
---|
75 | |
---|
76 | delete this->skySphere; |
---|
77 | |
---|
78 | //delete this->trackManager; |
---|
79 | |
---|
80 | /* |
---|
81 | WorldEntity* entity = entities->enumerate(); |
---|
82 | while( entity != NULL ) |
---|
83 | { |
---|
84 | entity->destroy(); |
---|
85 | entity = entities->nextElement(); |
---|
86 | } |
---|
87 | this->entities->destroy(); |
---|
88 | */ |
---|
89 | |
---|
90 | /* FIX the parent list has to be cleared - not possible if we got the old list also*/ |
---|
91 | |
---|
92 | |
---|
93 | //delete this->entities; |
---|
94 | //delete this->localCamera; |
---|
95 | /* this->localPlayer hasn't to be deleted explicitly, it is |
---|
96 | contained in entities*/ |
---|
97 | } |
---|
98 | |
---|
99 | GLfloat ctrlpoints[4][3] = { |
---|
100 | {20.0, 10.0, 5.0}, {40.0, -10.0, 0.0}, |
---|
101 | {60.0, -10.0, 5.0}, {80.0, 10.0, 5.0}}; |
---|
102 | |
---|
103 | /** |
---|
104 | \brief initializes a new World |
---|
105 | */ |
---|
106 | ErrorMessage World::init() |
---|
107 | { |
---|
108 | this->bPause = false; |
---|
109 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
110 | cn->addToWorld(this); |
---|
111 | cn->enable(true); |
---|
112 | |
---|
113 | //glMap1f (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]); |
---|
114 | //glEnable (GL_MAP1_VERTEX_3); |
---|
115 | |
---|
116 | //theNurb = gluNewNurbsRenderer (); |
---|
117 | //gluNurbsProperty (theNurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR); |
---|
118 | //gluNurbsProperty (theNurb, GLU_NURBS_VERTEX, vertexCallback ); |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | /** |
---|
124 | \brief starts the World |
---|
125 | */ |
---|
126 | ErrorMessage World::start() |
---|
127 | { |
---|
128 | printf("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
129 | this->bQuitOrxonox = false; |
---|
130 | this->bQuitCurrentGame = false; |
---|
131 | this->mainLoop(); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | \brief stops the world. |
---|
136 | |
---|
137 | This happens, when the player decides to end the Level. |
---|
138 | */ |
---|
139 | ErrorMessage World::stop() |
---|
140 | { |
---|
141 | printf("World::stop() - got stop signal\n"); |
---|
142 | this->bQuitCurrentGame = true; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | \brief pauses the Game |
---|
147 | */ |
---|
148 | ErrorMessage World::pause() |
---|
149 | { |
---|
150 | this->isPaused = true; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | \brief ends the pause Phase |
---|
155 | */ |
---|
156 | ErrorMessage World::resume() |
---|
157 | { |
---|
158 | this->isPaused = false; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | \brief destroys the World |
---|
163 | */ |
---|
164 | void World::destroy() |
---|
165 | { |
---|
166 | delete trackManager; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | \brief shows the loading screen |
---|
171 | */ |
---|
172 | void World::displayLoadScreen () |
---|
173 | { |
---|
174 | printf ("World::displayLoadScreen - start\n"); |
---|
175 | |
---|
176 | //GLMenuImageScreen* |
---|
177 | this->glmis = GLMenuImageScreen::getInstance(); |
---|
178 | this->glmis->init(); |
---|
179 | this->glmis->setMaximum(10); |
---|
180 | this->glmis->draw(); |
---|
181 | |
---|
182 | printf ("World::displayLoadScreen - end\n"); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | \brief removes the loadscreen, and changes over to the game |
---|
187 | |
---|
188 | \todo take out the delay |
---|
189 | */ |
---|
190 | void World::releaseLoadScreen () |
---|
191 | { |
---|
192 | printf ("World::releaseLoadScreen - start\n"); |
---|
193 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
194 | SDL_Delay(500); |
---|
195 | printf ("World::releaseLoadScreen - end\n"); |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | \brief loads the World by initializing all resources, and set their default values. |
---|
200 | */ |
---|
201 | void World::load() |
---|
202 | { |
---|
203 | // BezierCurve* tmpCurve = new BezierCurve(); |
---|
204 | if(this->debugWorldNr != -1) |
---|
205 | { |
---|
206 | // initializing Font |
---|
207 | testFont = new FontSet(); |
---|
208 | testFont->buildFont("../data/pictures/font.tga"); |
---|
209 | |
---|
210 | // initializing the TrackManager |
---|
211 | trackManager = TrackManager::getInstance(); |
---|
212 | trackManager->addPoint(Vector(0,-5,0)); |
---|
213 | trackManager->addPoint(Vector(10,0,5)); |
---|
214 | trackManager->addPoint(Vector(20,0,-5)); |
---|
215 | trackManager->addPoint(Vector(30,0,5)); |
---|
216 | trackManager->addPoint(Vector(40,0,5)); |
---|
217 | trackManager->setDuration(2); |
---|
218 | trackManager->setSavePoint(); |
---|
219 | trackManager->addPoint(Vector(50,10,10)); |
---|
220 | trackManager->addPoint(Vector(60,0, 10)); |
---|
221 | trackManager->addPoint(Vector(70,0, 10)); |
---|
222 | trackManager->addPoint(Vector(80,0,-10)); |
---|
223 | trackManager->addPoint(Vector(90,0,-10)); |
---|
224 | trackManager->setDuration(5); |
---|
225 | trackManager->setSavePoint(); |
---|
226 | trackManager->addPoint(Vector(110,0,5)); |
---|
227 | trackManager->addPoint(Vector(120,0, 10)); |
---|
228 | trackManager->addPoint(Vector(130,0, 10)); |
---|
229 | trackManager->addPoint(Vector(140,0,-10)); |
---|
230 | trackManager->addPoint(Vector(150,0,-10)); |
---|
231 | trackManager->setDuration(3); |
---|
232 | int fork11, fork12, fork13, fork14; |
---|
233 | trackManager->fork(4, &fork11, &fork12, &fork13, &fork14); |
---|
234 | trackManager->workOn(fork11); |
---|
235 | trackManager->addPoint(Vector(170, 0, -15)); |
---|
236 | trackManager->addPoint(Vector(180, 0, -15)); |
---|
237 | trackManager->setDuration(3); |
---|
238 | trackManager->workOn(fork12); |
---|
239 | trackManager->addPoint(Vector(170, 0, 10)); |
---|
240 | trackManager->addPoint(Vector(180, 0, 10)); |
---|
241 | trackManager->addPoint(Vector(190,2,5)); |
---|
242 | trackManager->addPoint(Vector(200,2,5)); |
---|
243 | trackManager->setDuration(7); |
---|
244 | int fork21, fork22; |
---|
245 | trackManager->fork(2, &fork21, &fork22); |
---|
246 | trackManager->workOn(fork21); |
---|
247 | trackManager->addPoint(Vector(220, 10,-10)); |
---|
248 | trackManager->addPoint(Vector(230, 0,-10)); |
---|
249 | trackManager->addPoint(Vector(240, 0, 2)); |
---|
250 | trackManager->addPoint(Vector(250, 0, 0)); |
---|
251 | trackManager->addPoint(Vector(260, 0, 5)); |
---|
252 | trackManager->setDuration(3); |
---|
253 | trackManager->join(2, fork12, fork11); |
---|
254 | trackManager->workOn(fork22); |
---|
255 | trackManager->addPoint(Vector(220, -10,10)); |
---|
256 | trackManager->addPoint(Vector(230, 0, 10)); |
---|
257 | trackManager->addPoint(Vector(240, 0, 10)); |
---|
258 | trackManager->addPoint(Vector(250, 0, 5)); |
---|
259 | trackManager->setDuration(6); |
---|
260 | trackManager->workOn(fork13); |
---|
261 | trackManager->addPoint(Vector(200,-10,5)); |
---|
262 | trackManager->addPoint(Vector(250,-10,5)); |
---|
263 | trackManager->setDuration(3); |
---|
264 | trackManager->workOn(fork14); |
---|
265 | trackManager->addPoint(Vector(200,15,0)); |
---|
266 | trackManager->addPoint(Vector(210,0,10)); |
---|
267 | trackManager->setDuration(1); |
---|
268 | trackManager->join(4, fork21, fork22, fork13, fork14); |
---|
269 | trackManager->workOn(10); |
---|
270 | trackManager->addPoint(Vector(250,-10,5)); |
---|
271 | trackManager->addPoint(Vector(260,-10,5)); |
---|
272 | trackManager->finalize(); |
---|
273 | |
---|
274 | /*monitor progress*/ |
---|
275 | this->glmis->step(); |
---|
276 | |
---|
277 | /* |
---|
278 | tmpCurve->addNode(Vector(10, -1, -1)); |
---|
279 | tmpCurve->addNode(Vector(10, -2, 2)); |
---|
280 | tmpCurve->addNode(Vector(10, 3, 3)); |
---|
281 | tmpCurve->addNode(Vector(10, 4, -4), 0); |
---|
282 | tmpCurve->addNode(Vector(10, -1, -1)); |
---|
283 | tmpCurve->addNode(Vector(10, -2, 2)); |
---|
284 | tmpCurve->addNode(Vector(10, 3, 3)); |
---|
285 | tmpCurve->addNode(Vector(10, 4, -4), 0); |
---|
286 | tmpCurve->debug(); |
---|
287 | */ |
---|
288 | switch(this->debugWorldNr) |
---|
289 | { |
---|
290 | /* |
---|
291 | this loads the hard-coded debug world. this only for simplicity and will be |
---|
292 | removed by a reald world-loader, which interprets a world-file. |
---|
293 | if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and |
---|
294 | make whatever you want... |
---|
295 | */ |
---|
296 | case DEBUG_WORLD_0: |
---|
297 | { |
---|
298 | this->nullParent = NullParent::getInstance (); |
---|
299 | this->nullParent->setName ("NullParent"); |
---|
300 | |
---|
301 | // !\todo old track-system has to be removed |
---|
302 | |
---|
303 | //create helper for player |
---|
304 | HelperParent* hp = new HelperParent (); |
---|
305 | /* the player has to be added to this helper */ |
---|
306 | |
---|
307 | // create a player |
---|
308 | WorldEntity* myPlayer = new Player (); |
---|
309 | myPlayer->setName ("player"); |
---|
310 | this->spawn (myPlayer); |
---|
311 | this->localPlayer = myPlayer; |
---|
312 | /*monitor progress*/ |
---|
313 | this->glmis->step(); |
---|
314 | |
---|
315 | // bind input |
---|
316 | Orxonox *orx = Orxonox::getInstance (); |
---|
317 | orx->getLocalInput()->bind (myPlayer); |
---|
318 | |
---|
319 | // bind camera |
---|
320 | this->localCamera = new Camera(this); |
---|
321 | this->localCamera->setName ("camera"); |
---|
322 | this->getCamera()->bind (myPlayer); |
---|
323 | this->localPlayer->addChild (this->localCamera); |
---|
324 | |
---|
325 | // Create SkySphere |
---|
326 | skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
327 | |
---|
328 | /*monitor progress*/ |
---|
329 | this->glmis->step(); |
---|
330 | |
---|
331 | Vector* es = new Vector (50, 2, 0); |
---|
332 | Quaternion* qs = new Quaternion (); |
---|
333 | WorldEntity* env = new Environment(); |
---|
334 | env->setName ("env"); |
---|
335 | this->spawn(env, es, qs); |
---|
336 | |
---|
337 | /*monitor progress*/ |
---|
338 | this->glmis->step(); |
---|
339 | |
---|
340 | trackManager->setBindSlave(env); |
---|
341 | |
---|
342 | break; |
---|
343 | } |
---|
344 | case DEBUG_WORLD_1: |
---|
345 | { |
---|
346 | /* |
---|
347 | this->testCurve = new UPointCurve(); |
---|
348 | this->testCurve->addNode(Vector( 0, 0, 0)); |
---|
349 | this->testCurve->addNode(Vector(10, 0, 5)); |
---|
350 | this->testCurve->addNode(Vector(20, -5,-5)); |
---|
351 | this->testCurve->addNode(Vector(30, 5, 10)); |
---|
352 | this->testCurve->addNode(Vector(40, 0,-10)); |
---|
353 | this->testCurve->addNode(Vector(50, 0,-10)); |
---|
354 | */ |
---|
355 | |
---|
356 | this->nullParent = NullParent::getInstance (); |
---|
357 | this->nullParent->setName ("NullParent"); |
---|
358 | |
---|
359 | |
---|
360 | |
---|
361 | // create a player |
---|
362 | WorldEntity* myPlayer = new Player(); |
---|
363 | myPlayer->setName ("player"); |
---|
364 | this->spawn(myPlayer); |
---|
365 | this->localPlayer = myPlayer; |
---|
366 | |
---|
367 | // bind input |
---|
368 | Orxonox *orx = Orxonox::getInstance(); |
---|
369 | orx->getLocalInput()->bind (myPlayer); |
---|
370 | |
---|
371 | // bind camera |
---|
372 | this->localCamera = new Camera (this); |
---|
373 | this->localCamera->setName ("camera"); |
---|
374 | this->getCamera()->bind (myPlayer); |
---|
375 | this->localPlayer->addChild (this->localCamera); |
---|
376 | |
---|
377 | // Create SkySphere |
---|
378 | skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
379 | |
---|
380 | break; |
---|
381 | |
---|
382 | |
---|
383 | } |
---|
384 | default: |
---|
385 | printf("World::load() - no world with ID %i found", this->debugWorldNr ); |
---|
386 | } |
---|
387 | } |
---|
388 | else if(this->worldName != NULL) |
---|
389 | { |
---|
390 | |
---|
391 | } |
---|
392 | |
---|
393 | // initialize debug coord system |
---|
394 | objectList = glGenLists(1); |
---|
395 | glNewList (objectList, GL_COMPILE); |
---|
396 | glLoadIdentity(); |
---|
397 | glColor3f(1.0,0,0); |
---|
398 | glBegin(GL_QUADS); |
---|
399 | |
---|
400 | int sizeX = 100; |
---|
401 | int sizeZ = 80; |
---|
402 | float length = 1000; |
---|
403 | float width = 200; |
---|
404 | float widthX = float (length /sizeX); |
---|
405 | float widthZ = float (width /sizeZ); |
---|
406 | |
---|
407 | float height [sizeX][sizeZ]; |
---|
408 | Vector normal_vectors[sizeX][sizeZ]; |
---|
409 | |
---|
410 | |
---|
411 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
412 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
413 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
414 | #ifdef __WIN32__ |
---|
415 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
416 | #else |
---|
417 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
418 | #endif |
---|
419 | |
---|
420 | //Die Huegel ein wenig glaetten |
---|
421 | for (int h=1; h<2;h++) |
---|
422 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
423 | for(int j=1;j<sizeZ-2;j+=1) |
---|
424 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
425 | |
---|
426 | //Berechnung von normalen Vektoren |
---|
427 | for(int i=1;i<sizeX-2;i+=1) |
---|
428 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
429 | { |
---|
430 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
431 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
432 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
433 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
434 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
435 | |
---|
436 | Vector c1 = v2 - v1; |
---|
437 | Vector c2 = v3 - v1; |
---|
438 | Vector c3= v4 - v1; |
---|
439 | Vector c4 = v5 - v1; |
---|
440 | Vector zero = Vector (0,0,0); |
---|
441 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
442 | normal_vectors[i][j].normalize(); |
---|
443 | } |
---|
444 | |
---|
445 | int snowheight=3; |
---|
446 | for ( int i = 0; i<sizeX; i+=1) |
---|
447 | for (int j = 0; j<sizeZ;j+=1) |
---|
448 | { |
---|
449 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
450 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
451 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
452 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
453 | float a[3]; |
---|
454 | if(height[i][j]<snowheight){ |
---|
455 | a[0]=0; |
---|
456 | a[1]=1.0-height[i][j]/10-.3; |
---|
457 | a[2]=0; |
---|
458 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
459 | } |
---|
460 | else{ |
---|
461 | a[0]=1.0; |
---|
462 | a[1]=1.0; |
---|
463 | a[2]=1.0; |
---|
464 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
465 | |
---|
466 | } |
---|
467 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
468 | glVertex3f(v1.x, v1.y, v1.z); |
---|
469 | if(height[i+1][j]<snowheight){ |
---|
470 | a[0]=0; |
---|
471 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
472 | a[2]=0; |
---|
473 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
474 | } |
---|
475 | else{ |
---|
476 | a[0]=1.0; |
---|
477 | a[1]=1.0; |
---|
478 | a[2]=1.0; |
---|
479 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
480 | |
---|
481 | } |
---|
482 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
483 | glVertex3f(v2.x, v2.y, v2.z); |
---|
484 | if(height[i+1][j+1]<snowheight){ |
---|
485 | a[0]=0; |
---|
486 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
487 | a[2]=0; |
---|
488 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
489 | } |
---|
490 | else{ |
---|
491 | a[0]=1.0; |
---|
492 | a[1]=1.0; |
---|
493 | a[2]=1.0; |
---|
494 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
495 | |
---|
496 | |
---|
497 | } |
---|
498 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
499 | glVertex3f(v3.x, v3.y, v3.z); |
---|
500 | if(height[i][j+1]<snowheight){ |
---|
501 | a[0]=0; |
---|
502 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
503 | a[2]=0; |
---|
504 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
505 | } |
---|
506 | else{ |
---|
507 | a[0]=1.0; |
---|
508 | a[1]=1.0; |
---|
509 | a[2]=1.0; |
---|
510 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
511 | } |
---|
512 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
513 | glVertex3f(v4.x, v4.y, v4.z); |
---|
514 | |
---|
515 | } |
---|
516 | glEnd(); |
---|
517 | /* |
---|
518 | glBegin(GL_LINES); |
---|
519 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
520 | { |
---|
521 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
522 | { |
---|
523 | glColor3f(1,0,0); |
---|
524 | glVertex3f(x,y,-128.0); |
---|
525 | glVertex3f(x,y,0.0); |
---|
526 | glColor3f(0.5,0,0); |
---|
527 | glVertex3f(x,y,0.0); |
---|
528 | glVertex3f(x,y,128.0); |
---|
529 | } |
---|
530 | } |
---|
531 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
532 | { |
---|
533 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
534 | { |
---|
535 | glColor3f(0,1,0); |
---|
536 | glVertex3f(-128.0,y,z); |
---|
537 | glVertex3f(0.0,y,z); |
---|
538 | glColor3f(0,0.5,0); |
---|
539 | glVertex3f(0.0,y,z); |
---|
540 | glVertex3f(128.0,y,z); |
---|
541 | } |
---|
542 | } |
---|
543 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
544 | { |
---|
545 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
546 | { |
---|
547 | glColor3f(0,0,1); |
---|
548 | glVertex3f(x,-128.0,z); |
---|
549 | glVertex3f(x,0.0,z); |
---|
550 | glColor3f(0,0,0.5); |
---|
551 | glVertex3f(x,0.0,z); |
---|
552 | glVertex3f(x,128.0,z); |
---|
553 | } |
---|
554 | |
---|
555 | } |
---|
556 | */ |
---|
557 | /* |
---|
558 | glBegin(GL_LINE_STRIP); |
---|
559 | glColor3f(1.0, 5.0, 1.0); |
---|
560 | for( int i = 0; i <= 30; i++) |
---|
561 | { |
---|
562 | glEvalCoord1f ((GLfloat) i/30.0); |
---|
563 | } |
---|
564 | glEnd(); |
---|
565 | */ |
---|
566 | |
---|
567 | trackManager->drawGraph(.01); |
---|
568 | trackManager->debug(2); |
---|
569 | /* |
---|
570 | glBegin(GL_LINES); |
---|
571 | float i; |
---|
572 | for(i = 0.0; i<1; i+=.01) |
---|
573 | { |
---|
574 | printf("%f, %f, %f\n",tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z); |
---|
575 | glVertex3f(tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z); |
---|
576 | } |
---|
577 | glEnd(); |
---|
578 | */ |
---|
579 | glEndList(); |
---|
580 | // LIGHT initialisation |
---|
581 | light = Light::getInstance(); |
---|
582 | light->addLight(0); |
---|
583 | light->setAttenuation(QUADRATIC, 1.0); |
---|
584 | light->setAttenuation(CONSTANT, 2.0); |
---|
585 | light->setAttenuation(QUADRATIC, 1.0); |
---|
586 | light->setPosition(10.0, 10.0, 50.0); |
---|
587 | light->setDiffuseColor(1,1,1); |
---|
588 | // light->addLight(1); |
---|
589 | // light->setPosition(20, 10, -20); |
---|
590 | // light->setDiffuseColor(0,0,0); |
---|
591 | light->debug(); |
---|
592 | |
---|
593 | |
---|
594 | } |
---|
595 | |
---|
596 | |
---|
597 | /** |
---|
598 | \brief checks for collisions |
---|
599 | |
---|
600 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
601 | between them. In case of collisions the collide() method of the corresponding entities |
---|
602 | is called. |
---|
603 | */ |
---|
604 | void World::collide () |
---|
605 | { |
---|
606 | /* |
---|
607 | List *a, *b; |
---|
608 | WorldEntity *aobj, *bobj; |
---|
609 | |
---|
610 | a = entities; |
---|
611 | |
---|
612 | while( a != NULL) |
---|
613 | { |
---|
614 | aobj = a->nextElement(); |
---|
615 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
616 | { |
---|
617 | b = a->nextElement(); |
---|
618 | while( b != NULL ) |
---|
619 | { |
---|
620 | bobj = b->nextElement(); |
---|
621 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
622 | { |
---|
623 | unsigned long ahitflg, bhitflg; |
---|
624 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
625 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
626 | &bhitflg) ); |
---|
627 | { |
---|
628 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
629 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
630 | } |
---|
631 | } |
---|
632 | b = b->nextElement(); |
---|
633 | } |
---|
634 | } |
---|
635 | a = a->enumerate(); |
---|
636 | } |
---|
637 | */ |
---|
638 | } |
---|
639 | |
---|
640 | /** |
---|
641 | \brief runs through all entities calling their draw() methods |
---|
642 | */ |
---|
643 | void World::draw () |
---|
644 | { |
---|
645 | // draw entities |
---|
646 | WorldEntity* entity; |
---|
647 | entity = this->entities->enumerate(); |
---|
648 | while( entity != NULL ) |
---|
649 | { |
---|
650 | if( entity->bDraw ) entity->draw(); |
---|
651 | entity = this->entities->nextElement(); |
---|
652 | } |
---|
653 | |
---|
654 | //glmis = new GLMenuImageScreen(); |
---|
655 | ///glmis->init(); |
---|
656 | |
---|
657 | // draw debug coord system |
---|
658 | glCallList (objectList); |
---|
659 | //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list. |
---|
660 | skySphere->draw(); |
---|
661 | |
---|
662 | testFont->printText(0, 0, 1, "orxonox_" PACKAGE_VERSION); |
---|
663 | |
---|
664 | } |
---|
665 | |
---|
666 | /** |
---|
667 | \brief updates Placements and notifies entities when they left the |
---|
668 | world |
---|
669 | |
---|
670 | This runs trough all WorldEntities and maps Locations to Placements |
---|
671 | if they are bound, checks whether they left the level boundaries |
---|
672 | and calls appropriate functions. |
---|
673 | */ |
---|
674 | void World::update () |
---|
675 | { |
---|
676 | /* |
---|
677 | //List<WorldEntity> *l; |
---|
678 | WorldEntity* entity; |
---|
679 | Location* loc; |
---|
680 | Placement* plc; |
---|
681 | Uint32 t; |
---|
682 | |
---|
683 | // l = entities->enumerate(); |
---|
684 | entity = this->entities->enumerate(); |
---|
685 | while( entity != NULL ) |
---|
686 | { |
---|
687 | |
---|
688 | |
---|
689 | if( !entity->isFree() ) |
---|
690 | { |
---|
691 | loc = entity->getLocation(); |
---|
692 | plc = entity->getPlacement(); |
---|
693 | t = loc->part; |
---|
694 | |
---|
695 | if( t >= traclen ) |
---|
696 | { |
---|
697 | printf("An entity is out of the game area\n"); |
---|
698 | entity->leftWorld (); |
---|
699 | } |
---|
700 | else |
---|
701 | { |
---|
702 | while( track[t].mapCoords( loc, plc) ) |
---|
703 | { |
---|
704 | track[t].postLeave (entity); |
---|
705 | if( loc->part >= tracklen ) |
---|
706 | { |
---|
707 | printf("An entity has left the game area\n"); |
---|
708 | entity->leftWorld (); |
---|
709 | break; |
---|
710 | } |
---|
711 | track[loc->part].postEnter (entity); |
---|
712 | } |
---|
713 | } |
---|
714 | } |
---|
715 | else |
---|
716 | { |
---|
717 | } |
---|
718 | |
---|
719 | entity = entities->nextElement(); |
---|
720 | } |
---|
721 | */ |
---|
722 | } |
---|
723 | |
---|
724 | /** |
---|
725 | \brief relays the passed time since the last frame to entities and Track parts |
---|
726 | \param deltaT: the time passed since the last frame in milliseconds |
---|
727 | */ |
---|
728 | void World::timeSlice (Uint32 deltaT) |
---|
729 | { |
---|
730 | //List<WorldEntity> *l; |
---|
731 | WorldEntity* entity; |
---|
732 | float seconds = deltaT / 1000.0; |
---|
733 | |
---|
734 | this->nullParent->update (seconds); |
---|
735 | //this->nullParent->processTick (seconds); |
---|
736 | |
---|
737 | entity = entities->enumerate(); |
---|
738 | while( entity != NULL) |
---|
739 | { |
---|
740 | entity->tick (seconds); |
---|
741 | entity = entities->nextElement(); |
---|
742 | } |
---|
743 | |
---|
744 | skySphere->updatePosition(localCamera->absCoordinate); |
---|
745 | //for( int i = 0; i < tracklen; i++) track[i].tick (seconds); |
---|
746 | } |
---|
747 | |
---|
748 | /** |
---|
749 | \brief removes level data from memory |
---|
750 | */ |
---|
751 | void World::unload() |
---|
752 | { |
---|
753 | |
---|
754 | } |
---|
755 | |
---|
756 | |
---|
757 | /** |
---|
758 | \brief function to put your own debug stuff into it. it can display informations about |
---|
759 | the current class/procedure |
---|
760 | */ |
---|
761 | void World::debug() |
---|
762 | { |
---|
763 | printf ("World::debug() - starting debug\n"); |
---|
764 | PNode* p1 = NullParent::getInstance (); |
---|
765 | PNode* p2 = new PNode (new Vector(2, 2, 2), p1); |
---|
766 | PNode* p3 = new PNode (new Vector(4, 4, 4), p1); |
---|
767 | PNode* p4 = new PNode (new Vector(6, 6, 6), p2); |
---|
768 | |
---|
769 | p1->debug (); |
---|
770 | p2->debug (); |
---|
771 | p3->debug (); |
---|
772 | p4->debug (); |
---|
773 | |
---|
774 | p1->shiftCoor (new Vector(-1, -1, -1)); |
---|
775 | |
---|
776 | printf("World::debug() - shift\n"); |
---|
777 | p1->debug (); |
---|
778 | p2->debug (); |
---|
779 | p3->debug (); |
---|
780 | p4->debug (); |
---|
781 | |
---|
782 | p1->update (1); |
---|
783 | |
---|
784 | printf ("World::debug() - update\n"); |
---|
785 | p1->debug (); |
---|
786 | p2->debug (); |
---|
787 | p3->debug (); |
---|
788 | p4->debug (); |
---|
789 | |
---|
790 | p2->shiftCoor (new Vector(-1, -1, -1)); |
---|
791 | p1->update (2); |
---|
792 | |
---|
793 | p1->debug (); |
---|
794 | p2->debug (); |
---|
795 | p3->debug (); |
---|
796 | p4->debug (); |
---|
797 | |
---|
798 | p2->setAbsCoor (new Vector(1,2,3)); |
---|
799 | |
---|
800 | |
---|
801 | p1->update (2); |
---|
802 | |
---|
803 | p1->debug (); |
---|
804 | p2->debug (); |
---|
805 | p3->debug (); |
---|
806 | p4->debug (); |
---|
807 | |
---|
808 | p1->destroy (); |
---|
809 | |
---|
810 | |
---|
811 | /* |
---|
812 | WorldEntity* entity; |
---|
813 | printf("counting all entities\n"); |
---|
814 | printf("World::debug() - enumerate()\n"); |
---|
815 | entity = entities->enumerate(); |
---|
816 | while( entity != NULL ) |
---|
817 | { |
---|
818 | if( entity->bDraw ) printf("got an entity\n"); |
---|
819 | entity = entities->nextElement(); |
---|
820 | } |
---|
821 | */ |
---|
822 | } |
---|
823 | |
---|
824 | |
---|
825 | /** |
---|
826 | \brief main loop of the world: executing all world relevant function |
---|
827 | |
---|
828 | in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
829 | all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
830 | collisions drawing everything to the screen. |
---|
831 | */ |
---|
832 | void World::mainLoop() |
---|
833 | { |
---|
834 | this->lastFrame = SDL_GetTicks (); |
---|
835 | printf("World::mainLoop() - Entering main loop\n"); |
---|
836 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
837 | { |
---|
838 | // Network |
---|
839 | this->synchronize (); |
---|
840 | // Process input |
---|
841 | this->handleInput (); |
---|
842 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
843 | { |
---|
844 | printf("World::mainLoop() - leaving loop earlier...\n"); |
---|
845 | break; |
---|
846 | } |
---|
847 | // Process time |
---|
848 | this->timeSlice (); |
---|
849 | // Process collision |
---|
850 | this->collision (); |
---|
851 | // Draw |
---|
852 | this->display (); |
---|
853 | |
---|
854 | for( int i = 0; i < 5000000; i++) {} |
---|
855 | /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ |
---|
856 | } |
---|
857 | printf("World::mainLoop() - Exiting the main loop\n"); |
---|
858 | } |
---|
859 | |
---|
860 | /** |
---|
861 | \brief synchronize local data with remote data |
---|
862 | */ |
---|
863 | void World::synchronize () |
---|
864 | { |
---|
865 | // Get remote input |
---|
866 | // Update synchronizables |
---|
867 | } |
---|
868 | |
---|
869 | /** |
---|
870 | \brief run all input processing |
---|
871 | |
---|
872 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
873 | sdl and has its own event-passing-queue. |
---|
874 | */ |
---|
875 | void World::handleInput () |
---|
876 | { |
---|
877 | // localinput |
---|
878 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
879 | cn->process(); |
---|
880 | // remoteinput |
---|
881 | } |
---|
882 | |
---|
883 | /** |
---|
884 | \brief advance the timeline |
---|
885 | |
---|
886 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
887 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
888 | a heart-beat. |
---|
889 | */ |
---|
890 | void World::timeSlice () |
---|
891 | { |
---|
892 | Uint32 currentFrame = SDL_GetTicks(); |
---|
893 | if(!this->bPause) |
---|
894 | { |
---|
895 | Uint32 dt = currentFrame - this->lastFrame; |
---|
896 | |
---|
897 | if(dt > 0) |
---|
898 | { |
---|
899 | float fps = 1000/dt; |
---|
900 | printf("fps = %f\n", fps); |
---|
901 | } |
---|
902 | else |
---|
903 | { |
---|
904 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
905 | nothing. |
---|
906 | */ |
---|
907 | printf("fps = 1000 - frame rate is adjusted\n"); |
---|
908 | SDL_Delay(10); |
---|
909 | dt = 10; |
---|
910 | } |
---|
911 | this->timeSlice (dt); |
---|
912 | this->update (); |
---|
913 | this->localCamera->timeSlice(dt); |
---|
914 | this->trackManager->tick(dt); |
---|
915 | } |
---|
916 | this->lastFrame = currentFrame; |
---|
917 | } |
---|
918 | |
---|
919 | |
---|
920 | /** |
---|
921 | \brief compute collision detection |
---|
922 | */ |
---|
923 | void World::collision () |
---|
924 | { |
---|
925 | this->collide (); |
---|
926 | } |
---|
927 | |
---|
928 | |
---|
929 | /** |
---|
930 | \brief render the current frame |
---|
931 | |
---|
932 | clear all buffers and draw the world |
---|
933 | */ |
---|
934 | void World::display () |
---|
935 | { |
---|
936 | // clear buffer |
---|
937 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
938 | // set camera |
---|
939 | this->localCamera->apply (); |
---|
940 | // draw world |
---|
941 | this->draw(); |
---|
942 | // draw HUD |
---|
943 | /* \todo draw HUD */ |
---|
944 | // flip buffers |
---|
945 | SDL_GL_SwapBuffers(); |
---|
946 | //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); |
---|
947 | //SDL_Flip (screen); |
---|
948 | } |
---|
949 | |
---|
950 | /** |
---|
951 | \brief give back active camera |
---|
952 | |
---|
953 | this passes back the actualy active camera |
---|
954 | \todo ability to define more than one camera or camera-places |
---|
955 | */ |
---|
956 | Camera* World::getCamera() |
---|
957 | { |
---|
958 | return this->localCamera; |
---|
959 | } |
---|
960 | |
---|
961 | |
---|
962 | /** |
---|
963 | \brief add and spawn a new entity to this world |
---|
964 | \param entity to be added |
---|
965 | */ |
---|
966 | void World::spawn(WorldEntity* entity) |
---|
967 | { |
---|
968 | if( this->nullParent != NULL && entity->parent == NULL) |
---|
969 | this->nullParent->addChild (entity); |
---|
970 | |
---|
971 | this->entities->add (entity); |
---|
972 | |
---|
973 | entity->postSpawn (); |
---|
974 | } |
---|
975 | |
---|
976 | |
---|
977 | /** |
---|
978 | \brief add and spawn a new entity to this world |
---|
979 | \param entity to be added |
---|
980 | \param absCoor At what coordinates to add this entity. |
---|
981 | \param absDir In which direction should it look. |
---|
982 | */ |
---|
983 | void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir) |
---|
984 | { |
---|
985 | entity->setAbsCoor (absCoor); |
---|
986 | entity->setAbsDir (absDir); |
---|
987 | |
---|
988 | if( this->nullParent != NULL && entity->parent == NULL) |
---|
989 | this->nullParent->addChild (entity); |
---|
990 | |
---|
991 | this->entities->add (entity); |
---|
992 | |
---|
993 | entity->postSpawn (); |
---|
994 | } |
---|
995 | |
---|
996 | |
---|
997 | |
---|
998 | /** |
---|
999 | \brief commands that the world must catch |
---|
1000 | \returns false if not used by the world |
---|
1001 | */ |
---|
1002 | bool World::command(Command* cmd) |
---|
1003 | { |
---|
1004 | return false; |
---|
1005 | } |
---|
1006 | |
---|
1007 | |
---|
1008 | |
---|
1009 | /** |
---|
1010 | \brief swaps two values |
---|
1011 | \todo make this a global function, and take it out of world |
---|
1012 | */ |
---|
1013 | void World::swap (unsigned char &a, unsigned char &b) |
---|
1014 | { |
---|
1015 | unsigned char temp; |
---|
1016 | temp = a; |
---|
1017 | a = b; |
---|
1018 | b = temp; |
---|
1019 | } |
---|