Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/world_entity.cc @ 6230

Last change on this file since 6230 was 6222, checked in by bensch, 19 years ago

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

File size: 9.2 KB
Line 
1
2
3/*
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
15   co-programmer: Christian Meyer
16*/
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "world_entity.h"
20#include "shell_command.h"
21
22#include "model.h"
23#include "md2Model.h"
24#include "resource_manager.h"
25#include "load_param.h"
26#include "vector.h"
27#include "obb_tree.h"
28
29#include "state.h"
30
31using namespace std;
32
33SHELL_COMMAND(model, WorldEntity, loadModel)
34    ->describe("sets the Model of the WorldEntity")
35    ->defaultValues(2, "models/ships/fighter.obj", 1.0);
36
37
38/**
39 *  Loads the WordEntity-specific Part of any derived Class
40 *
41 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
42 *              that can calls WorldEntities loadParams for itself.
43 */
44WorldEntity::WorldEntity(const TiXmlElement* root)
45  : Synchronizeable()
46{
47  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
48
49  this->obbTree = NULL;
50
51  if (root != NULL)
52    this->loadParams(root);
53
54  this->setVisibiliy(true);
55
56  this->objectListNumber = OM_INIT;
57  this->objectListIterator = NULL;
58
59  this->toList(OM_NULL);
60}
61
62/**
63 *  standard destructor
64*/
65WorldEntity::~WorldEntity ()
66{
67  // Delete the obbTree
68  if( this->obbTree != NULL)
69    delete this->obbTree;
70
71  // Delete the model (unregister it with the ResourceManager)
72  for (unsigned int i = 0; i < this->models.size(); i++)
73    this->setModel(NULL, i);
74
75  State::getObjectManager()->toList(this, OM_INIT);
76}
77
78/**
79 * loads the WorldEntity Specific Parameters.
80 * @param root: the XML-Element to load the Data From
81 */
82void WorldEntity::loadParams(const TiXmlElement* root)
83{
84  // Do the PNode loading stuff
85  static_cast<PNode*>(this)->loadParams(root);
86
87  LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture)
88      .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)")
89      .defaultValues(1, NULL);
90
91  // Model Loading
92  LoadParam(root, "model", this, WorldEntity, loadModel)
93      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
94      .defaultValues(3, NULL, 1.0f, 0);
95}
96
97
98/**
99 * loads a Model onto a WorldEntity
100 * @param fileName the name of the model to load
101 * @param scaling the Scaling of the model
102 *
103 * @todo fix this, so it only has one loadModel-Function.
104*/
105void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
106{
107  if (fileName != NULL)
108  {
109   // search for the special character # in the LoadParam
110    if (strchr(fileName, '#') != NULL)
111    {
112      PRINTF(4)("Found # in %s... searching for LOD's\n", fileName);
113      char* lodFile = new char[strlen(fileName)+1];
114      strcpy(lodFile, fileName);
115      char* depth = strchr(lodFile, '#');
116      for (unsigned int i = 0; i < 5; i++)
117      {
118        *depth = 48+(int)i;
119        printf("-------%s\n", lodFile);
120        if (ResourceManager::isInDataDir(lodFile))
121          this->loadModel(lodFile, scaling, i);
122      }
123      return;
124    }
125
126    if(strstr(fileName, ".obj"))
127    {
128      PRINTF(4)("fetching OBJ file: %s\n", fileName);
129      if (scaling == 1.0)
130        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN), modelNumber);
131      else
132        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling), modelNumber);
133
134      if( modelNumber == 0)
135        this->buildObbTree(4);
136    }
137    else if(strstr(fileName, ".md2"))
138    {
139      PRINTF(4)("fetching MD2 file: %s\n", fileName);
140      Model* m = new MD2Model(fileName, this->md2TextureFileName);
141        //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0);
142      this->setModel(m, 0);
143    }
144  }
145  else
146    this->setModel(NULL);
147}
148
149/**
150 * sets a specific Model for the Object.
151 * @param model The Model to set
152 * @param modelNumber the n'th model in the List to get.
153 */
154void WorldEntity::setModel(Model* model, unsigned int modelNumber)
155{
156  if (this->models.size() <= modelNumber)
157    this->models.resize(modelNumber+1, NULL);
158
159  if (this->models[modelNumber] != NULL)
160  {
161    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
162//     if (resource != NULL)
163    ResourceManager::getInstance()->unload(resource, RP_LEVEL);
164  }
165  else
166    delete this->models[modelNumber];
167
168  this->models[modelNumber] = model;
169
170
171//   if (this->model != NULL)
172//     this->buildObbTree(4);
173}
174
175
176/**
177 * builds the obb-tree
178 * @param depth the depth to calculate
179 */
180bool WorldEntity::buildObbTree(unsigned int depth)
181{
182  if (this->obbTree)
183    delete this->obbTree;
184
185  if (this->models[0] != NULL)
186  {
187    PRINTF(4)("creating obb tree\n");
188
189
190    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
191    return true;
192  }
193  else
194  {
195    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
196    this->obbTree = NULL;
197    return false;
198  }
199}
200
201/**
202 * @brief moves this entity to the List OM_List
203 * @param list the list to set this Entity to.
204 *
205 * this is the same as a call to State::getObjectManager()->toList(entity , list);
206 * directly, but with an easier interface.
207 *
208 * @todo inline this (peut etre)
209 */
210void WorldEntity::toList(OM_LIST list)
211{
212  State::getObjectManager()->toList(this, list);
213}
214
215
216
217/**
218 * sets the character attributes of a worldentity
219 * @param character attributes
220 *
221 * these attributes don't have to be set, only use them, if you need them
222*/
223//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
224//{}
225
226
227/**
228 *  this function is called, when two entities collide
229 * @param entity: the world entity with whom it collides
230 *
231 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
232 */
233void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
234{
235  /**
236   * THIS IS A DEFAULT COLLISION-Effect.
237   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
238   * USE::
239   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
240   *
241   * You can always define a default Action.... don't be affraid just test it :)
242   */
243//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
244}
245
246
247/**
248 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
249 *
250 */
251void WorldEntity::postSpawn ()
252{
253}
254
255
256/**
257 *  this method is called by the world if the WorldEntity leaves valid gamespace
258 *
259 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
260 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
261 *
262 * NOT YET IMPLEMENTED
263 */
264void WorldEntity::leftWorld ()
265{
266}
267
268
269/**
270 *  this method is called every frame
271 * @param time: the time in seconds that has passed since the last tick
272 *
273 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
274*/
275void WorldEntity::tick(float time)
276{
277}
278
279
280/**
281 *  the entity is drawn onto the screen with this function
282 *
283 * This is a central function of an entity: call it to let the entity painted to the screen.
284 * Just override this function with whatever you want to be drawn.
285*/
286void WorldEntity::draw() const
287{
288  this->drawLODsafe();
289}
290
291
292/**
293 *  this functions draws the model automaticaly in multiple LOD
294 */
295void WorldEntity::drawLODsafe() const
296{
297  if (!unlikely(this->models.empty()))
298  {
299    glMatrixMode(GL_MODELVIEW);
300    glPushMatrix();
301
302    /* translate */
303    glTranslatef (this->getAbsCoor ().x,
304                  this->getAbsCoor ().y,
305                  this->getAbsCoor ().z);
306    Vector tmpRot = this->getAbsDir().getSpacialAxis();
307    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
308
309
310    // This Draws the LOD's
311    float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len();
312    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
313    {
314      this->models[2]->draw();
315    }
316    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
317    {
318      this->models[1]->draw();
319    }
320    else if (this->models.size() >= 1 && this->models[0] != NULL)
321    {
322      this->models[0]->draw();
323    }
324    glPopMatrix();
325  }
326}
327
328/**
329 * DEBUG-DRAW OF THE BV-Tree.
330 * @param depth What depth to draw
331 * @param drawMode the mode to draw this entity under
332 */
333void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
334{
335  glMatrixMode(GL_MODELVIEW);
336  glPushMatrix();
337  /* translate */
338  glTranslatef (this->getAbsCoor ().x,
339                this->getAbsCoor ().y,
340                this->getAbsCoor ().z);
341  /* rotate */
342  Vector tmpRot = this->getAbsDir().getSpacialAxis();
343  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
344
345  if (this->obbTree)
346    this->obbTree->drawBV(depth, drawMode);
347  glPopMatrix();
348}
Note: See TracBrowser for help on using the repository browser.