[2036] | 1 | |
---|
| 2 | |
---|
[4570] | 3 | /* |
---|
[2036] | 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
[2190] | 15 | co-programmer: Christian Meyer |
---|
[2036] | 16 | */ |
---|
[5300] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
[2036] | 18 | |
---|
| 19 | #include "world_entity.h" |
---|
[5208] | 20 | #include "shell_command.h" |
---|
[5143] | 21 | |
---|
[5511] | 22 | #include "model.h" |
---|
[5143] | 23 | #include "resource_manager.h" |
---|
| 24 | #include "load_param.h" |
---|
[3608] | 25 | #include "list.h" |
---|
[3803] | 26 | #include "vector.h" |
---|
[4682] | 27 | #include "obb_tree.h" |
---|
[3608] | 28 | |
---|
[6002] | 29 | #include "state.h" |
---|
| 30 | |
---|
[2036] | 31 | using namespace std; |
---|
| 32 | |
---|
[5208] | 33 | SHELL_COMMAND(model, WorldEntity, loadModel) |
---|
| 34 | ->describe("sets the Model of the WorldEntity") |
---|
[5555] | 35 | ->defaultValues(2, "models/ships/fighter.obj", 1.0); |
---|
[5208] | 36 | |
---|
| 37 | |
---|
[2043] | 38 | /** |
---|
[4836] | 39 | * Loads the WordEntity-specific Part of any derived Class |
---|
[5498] | 40 | * |
---|
| 41 | * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves, |
---|
| 42 | * that can calls WorldEntities loadParams for itself. |
---|
| 43 | */ |
---|
[4261] | 44 | WorldEntity::WorldEntity(const TiXmlElement* root) |
---|
[5996] | 45 | : Synchronizeable() |
---|
[2190] | 46 | { |
---|
[4320] | 47 | this->setClassID(CL_WORLD_ENTITY, "WorldEntity"); |
---|
[4597] | 48 | |
---|
[4682] | 49 | this->obbTree = NULL; |
---|
[4261] | 50 | |
---|
[5995] | 51 | if (root != NULL) |
---|
[4261] | 52 | this->loadParams(root); |
---|
| 53 | |
---|
[4885] | 54 | this->setVisibiliy(true); |
---|
[2190] | 55 | } |
---|
[2043] | 56 | |
---|
| 57 | /** |
---|
[4836] | 58 | * standard destructor |
---|
[2043] | 59 | */ |
---|
[2190] | 60 | WorldEntity::~WorldEntity () |
---|
[2036] | 61 | { |
---|
[5498] | 62 | // Delete the obbTree |
---|
[5302] | 63 | if( this->obbTree != NULL) |
---|
[4814] | 64 | delete this->obbTree; |
---|
[5994] | 65 | |
---|
| 66 | // Delete the model (unregister it with the ResourceManager) |
---|
[6005] | 67 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
| 68 | this->setModel(NULL, i); |
---|
[3531] | 69 | } |
---|
| 70 | |
---|
[5498] | 71 | /** |
---|
| 72 | * loads the WorldEntity Specific Parameters. |
---|
| 73 | * @param root: the XML-Element to load the Data From |
---|
| 74 | */ |
---|
[4436] | 75 | void WorldEntity::loadParams(const TiXmlElement* root) |
---|
| 76 | { |
---|
[5498] | 77 | // Do the PNode loading stuff |
---|
[4436] | 78 | static_cast<PNode*>(this)->loadParams(root); |
---|
[5498] | 79 | |
---|
[4436] | 80 | // Model Loading |
---|
[5671] | 81 | LoadParam(root, "model", this, WorldEntity, loadModel) |
---|
[5652] | 82 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
[5995] | 83 | .defaultValues(3, NULL, 1.0f, 0); |
---|
[5465] | 84 | |
---|
[4436] | 85 | } |
---|
| 86 | |
---|
[3531] | 87 | /** |
---|
[4885] | 88 | * loads a Model onto a WorldEntity |
---|
[4836] | 89 | * @param fileName the name of the model to load |
---|
[5057] | 90 | * @param scaling the Scaling of the model |
---|
[5498] | 91 | * |
---|
| 92 | * @todo fix this, so it only has one loadModel-Function. |
---|
[4261] | 93 | */ |
---|
[5995] | 94 | void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber) |
---|
[4261] | 95 | { |
---|
[4732] | 96 | if (fileName != NULL) |
---|
[6005] | 97 | { |
---|
| 98 | // search for the special character # in the LoadParam |
---|
| 99 | if (strchr(fileName, '#') != NULL) |
---|
| 100 | { |
---|
| 101 | PRINTF(4)("Found # in %s... searching for LOD's\n", fileName); |
---|
| 102 | char* lodFile = new char[strlen(fileName)+1]; |
---|
| 103 | strcpy(lodFile, fileName); |
---|
| 104 | char* depth = strchr(lodFile, '#'); |
---|
| 105 | for (unsigned int i = 0; i < 5; i++) |
---|
| 106 | { |
---|
| 107 | *depth = 48+(int)i; |
---|
| 108 | printf("-------%s\n", lodFile); |
---|
| 109 | if (ResourceManager::isInDataDir(lodFile)) |
---|
| 110 | this->loadModel(lodFile, scaling, i); |
---|
| 111 | } |
---|
| 112 | return; |
---|
| 113 | } |
---|
| 114 | |
---|
[5066] | 115 | PRINTF(4)("fetching %s\n", fileName); |
---|
[5057] | 116 | if (scaling == 1.0) |
---|
[6002] | 117 | this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN), modelNumber); |
---|
[5057] | 118 | else |
---|
[6002] | 119 | this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling), modelNumber); |
---|
| 120 | if (modelNumber == 0) |
---|
[5061] | 121 | this->buildObbTree(4); |
---|
[4732] | 122 | } |
---|
| 123 | else |
---|
[5995] | 124 | this->setModel(NULL); |
---|
[4261] | 125 | } |
---|
| 126 | |
---|
[5061] | 127 | /** |
---|
[5994] | 128 | * sets a specific Model for the Object. |
---|
| 129 | * @param model The Model to set |
---|
| 130 | * @param modelNumber the n'th model in the List to get. |
---|
| 131 | */ |
---|
| 132 | void WorldEntity::setModel(Model* model, unsigned int modelNumber) |
---|
| 133 | { |
---|
[5995] | 134 | if (this->models.size() <= modelNumber) |
---|
| 135 | this->models.resize(modelNumber+1, NULL); |
---|
| 136 | |
---|
| 137 | if (this->models[modelNumber] != NULL) |
---|
[6004] | 138 | { |
---|
[5995] | 139 | Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]); |
---|
[5994] | 140 | if (resource != NULL) |
---|
| 141 | ResourceManager::getInstance()->unload(resource, RP_LEVEL); |
---|
| 142 | else |
---|
[5995] | 143 | delete this->models[modelNumber]; |
---|
[5994] | 144 | } |
---|
[5995] | 145 | this->models[modelNumber] = model; |
---|
[5994] | 146 | |
---|
[6004] | 147 | // if (this->model != NULL) |
---|
[5994] | 148 | // this->buildObbTree(4); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | /** |
---|
[5061] | 153 | * builds the obb-tree |
---|
| 154 | * @param depth the depth to calculate |
---|
| 155 | */ |
---|
| 156 | bool WorldEntity::buildObbTree(unsigned int depth) |
---|
| 157 | { |
---|
[5428] | 158 | if (this->obbTree) |
---|
| 159 | delete this->obbTree; |
---|
| 160 | |
---|
[5995] | 161 | if (this->models[0] != NULL) |
---|
[5428] | 162 | { |
---|
| 163 | PRINTF(4)("creating obb tree\n"); |
---|
[5708] | 164 | |
---|
| 165 | |
---|
[5995] | 166 | this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount()); |
---|
[5428] | 167 | return true; |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
| 171 | PRINTF(2)("could not create obb-tree, because no model was loaded yet\n"); |
---|
| 172 | this->obbTree = NULL; |
---|
| 173 | return false; |
---|
| 174 | } |
---|
[5061] | 175 | } |
---|
[5057] | 176 | |
---|
[5061] | 177 | |
---|
[4261] | 178 | /** |
---|
[4885] | 179 | * sets the character attributes of a worldentity |
---|
[4836] | 180 | * @param character attributes |
---|
[4885] | 181 | * |
---|
| 182 | * these attributes don't have to be set, only use them, if you need them |
---|
[2043] | 183 | */ |
---|
[5498] | 184 | //void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
---|
| 185 | //{} |
---|
[2036] | 186 | |
---|
[3583] | 187 | |
---|
[2043] | 188 | /** |
---|
[5029] | 189 | * this function is called, when two entities collide |
---|
| 190 | * @param entity: the world entity with whom it collides |
---|
| 191 | * |
---|
| 192 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 193 | */ |
---|
| 194 | void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 195 | { |
---|
[5498] | 196 | /** |
---|
| 197 | * THIS IS A DEFAULT COLLISION-Effect. |
---|
| 198 | * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT |
---|
| 199 | * USE:: |
---|
| 200 | * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; }; |
---|
| 201 | * |
---|
| 202 | * You can always define a default Action.... don't be affraid just test it :) |
---|
| 203 | */ |
---|
[5257] | 204 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
[5029] | 205 | } |
---|
| 206 | |
---|
[2043] | 207 | |
---|
| 208 | /** |
---|
[5498] | 209 | * this is called immediately after the Entity has been constructed, initialized and then Spawned into the World |
---|
[4885] | 210 | * |
---|
[5498] | 211 | */ |
---|
[3229] | 212 | void WorldEntity::postSpawn () |
---|
[2190] | 213 | { |
---|
| 214 | } |
---|
[2043] | 215 | |
---|
[3583] | 216 | |
---|
[2043] | 217 | /** |
---|
[4836] | 218 | * this method is called by the world if the WorldEntity leaves valid gamespace |
---|
[4885] | 219 | * |
---|
| 220 | * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a |
---|
| 221 | * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy). |
---|
[5498] | 222 | * |
---|
| 223 | * NOT YET IMPLEMENTED |
---|
| 224 | */ |
---|
[3583] | 225 | void WorldEntity::leftWorld () |
---|
[2190] | 226 | { |
---|
| 227 | } |
---|
[2043] | 228 | |
---|
[3583] | 229 | |
---|
[2190] | 230 | /** |
---|
[4836] | 231 | * this method is called every frame |
---|
| 232 | * @param time: the time in seconds that has passed since the last tick |
---|
[4885] | 233 | * |
---|
| 234 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
[2043] | 235 | */ |
---|
[4570] | 236 | void WorldEntity::tick(float time) |
---|
[2190] | 237 | { |
---|
| 238 | } |
---|
[3583] | 239 | |
---|
[5498] | 240 | |
---|
[3583] | 241 | /** |
---|
[4836] | 242 | * the entity is drawn onto the screen with this function |
---|
[4885] | 243 | * |
---|
| 244 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
| 245 | * Just override this function with whatever you want to be drawn. |
---|
[3365] | 246 | */ |
---|
[5500] | 247 | void WorldEntity::draw() const |
---|
[3803] | 248 | { |
---|
[6005] | 249 | this->drawLODsafe(); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | void WorldEntity::drawLODsafe() const |
---|
| 253 | { |
---|
[6002] | 254 | if (!this->models.empty()) |
---|
| 255 | { |
---|
| 256 | glMatrixMode(GL_MODELVIEW); |
---|
| 257 | glPushMatrix(); |
---|
[4570] | 258 | |
---|
[6002] | 259 | /* translate */ |
---|
| 260 | glTranslatef (this->getAbsCoor ().x, |
---|
| 261 | this->getAbsCoor ().y, |
---|
| 262 | this->getAbsCoor ().z); |
---|
[6004] | 263 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 264 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[2043] | 265 | |
---|
[6004] | 266 | |
---|
| 267 | // This Draws the LOD's |
---|
[6002] | 268 | float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len(); |
---|
[6004] | 269 | if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL) |
---|
[6002] | 270 | { |
---|
| 271 | this->models[2]->draw(); |
---|
[6004] | 272 | } |
---|
| 273 | else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL) |
---|
[6002] | 274 | { |
---|
| 275 | this->models[1]->draw(); |
---|
[6004] | 276 | } |
---|
| 277 | else if (this->models.size() >= 1 && this->models[0] != NULL) |
---|
[6002] | 278 | { |
---|
| 279 | this->models[0]->draw(); |
---|
| 280 | } |
---|
| 281 | glPopMatrix(); |
---|
| 282 | } |
---|
[3803] | 283 | } |
---|
[3583] | 284 | |
---|
[5498] | 285 | /** |
---|
| 286 | * DEBUG-DRAW OF THE BV-Tree. |
---|
| 287 | * @param depth What depth to draw |
---|
| 288 | * @param drawMode the mode to draw this entity under |
---|
| 289 | */ |
---|
[5501] | 290 | void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const |
---|
[4684] | 291 | { |
---|
| 292 | glMatrixMode(GL_MODELVIEW); |
---|
| 293 | glPushMatrix(); |
---|
| 294 | /* translate */ |
---|
| 295 | glTranslatef (this->getAbsCoor ().x, |
---|
| 296 | this->getAbsCoor ().y, |
---|
| 297 | this->getAbsCoor ().z); |
---|
| 298 | /* rotate */ |
---|
[4998] | 299 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 300 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[4684] | 301 | |
---|
| 302 | if (this->obbTree) |
---|
| 303 | this->obbTree->drawBV(depth, drawMode); |
---|
| 304 | glPopMatrix(); |
---|
| 305 | } |
---|