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