Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5995 was 5995, checked in by bensch, 18 years ago

orxonox/trunk: preparations for LOD's

File size: 6.8 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 "resource_manager.h"
24#include "load_param.h"
25#include "list.h"
26#include "vector.h"
27#include "obb_tree.h"
28
29using namespace std;
30
31SHELL_COMMAND(model, WorldEntity, loadModel)
32    ->describe("sets the Model of the WorldEntity")
33    ->defaultValues(2, "models/ships/fighter.obj", 1.0);
34
35
36/**
37 *  Loads the WordEntity-specific Part of any derived Class
38 *
39 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
40 *              that can calls WorldEntities loadParams for itself.
41 */
42WorldEntity::WorldEntity(const TiXmlElement* root)
43{
44  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
45
46  this->obbTree = NULL;
47
48  if (root != NULL)
49    this->loadParams(root);
50
51  this->setVisibiliy(true);
52}
53
54/**
55 *  standard destructor
56*/
57WorldEntity::~WorldEntity ()
58{
59  // Delete the obbTree
60  if( this->obbTree != NULL)
61    delete this->obbTree;
62
63  // Delete the model (unregister it with the ResourceManager)
64  this->setModel(NULL);
65}
66
67/**
68 * loads the WorldEntity Specific Parameters.
69 * @param root: the XML-Element to load the Data From
70 */
71void WorldEntity::loadParams(const TiXmlElement* root)
72{
73  // Do the PNode loading stuff
74  static_cast<PNode*>(this)->loadParams(root);
75
76  // Model Loading
77  LoadParam(root, "model", this, WorldEntity, loadModel)
78      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
79      .defaultValues(3, NULL, 1.0f, 0);
80
81}
82
83/**
84 * loads a Model onto a WorldEntity
85 * @param fileName the name of the model to load
86 * @param scaling the Scaling of the model
87 *
88 * @todo fix this, so it only has one loadModel-Function.
89*/
90void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
91{
92  if (fileName != NULL)
93  {
94    PRINTF(4)("fetching %s\n", fileName);
95    if (scaling == 1.0)
96      this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN));
97    else
98      this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling));
99
100    this->buildObbTree(4);
101  }
102  else
103    this->setModel(NULL);
104}
105
106/**
107 * sets a specific Model for the Object.
108 * @param model The Model to set
109 * @param modelNumber the n'th model in the List to get.
110 */
111void WorldEntity::setModel(Model* model, unsigned int modelNumber)
112{
113  if (this->models.size() <= modelNumber)
114    this->models.resize(modelNumber+1, NULL);
115
116  if (this->models[modelNumber] != NULL)
117  { 
118    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
119    if (resource != NULL)
120      ResourceManager::getInstance()->unload(resource, RP_LEVEL);
121    else
122      delete this->models[modelNumber];
123  }
124  this->models[modelNumber] = model;
125
126//   if (this->model != NULL) 
127//     this->buildObbTree(4);
128}
129
130
131/**
132 * builds the obb-tree
133 * @param depth the depth to calculate
134 */
135bool WorldEntity::buildObbTree(unsigned int depth)
136{
137  if (this->obbTree)
138    delete this->obbTree;
139
140  if (this->models[0] != NULL)
141  {
142    PRINTF(4)("creating obb tree\n");
143
144
145    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
146    return true;
147  }
148  else
149  {
150    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
151    this->obbTree = NULL;
152    return false;
153  }
154}
155
156
157/**
158 * sets the character attributes of a worldentity
159 * @param character attributes
160 *
161 * these attributes don't have to be set, only use them, if you need them
162*/
163//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
164//{}
165
166
167/**
168 *  this function is called, when two entities collide
169 * @param entity: the world entity with whom it collides
170 *
171 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
172 */
173void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
174{
175  /**
176   * THIS IS A DEFAULT COLLISION-Effect.
177   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
178   * USE::
179   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
180   *
181   * You can always define a default Action.... don't be affraid just test it :)
182   */
183//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
184}
185
186
187/**
188 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
189 *
190 */
191void WorldEntity::postSpawn ()
192{
193}
194
195
196/**
197 *  this method is called by the world if the WorldEntity leaves valid gamespace
198 *
199 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
200 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
201 *
202 * NOT YET IMPLEMENTED
203 */
204void WorldEntity::leftWorld ()
205{
206}
207
208
209/**
210 *  this method is called every frame
211 * @param time: the time in seconds that has passed since the last tick
212 *
213 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
214*/
215void WorldEntity::tick(float time)
216{
217}
218
219
220/**
221 *  the entity is drawn onto the screen with this function
222 *
223 * This is a central function of an entity: call it to let the entity painted to the screen.
224 * Just override this function with whatever you want to be drawn.
225*/
226void WorldEntity::draw() const
227{
228  glMatrixMode(GL_MODELVIEW);
229  glPushMatrix();
230  float matrix[4][4];
231
232  /* translate */
233  glTranslatef (this->getAbsCoor ().x,
234                this->getAbsCoor ().y,
235                this->getAbsCoor ().z);
236  /* rotate */ // FIXME: devise a new Way to rotate this
237  this->getAbsDir ().matrix (matrix);
238  glMultMatrixf((float*)matrix);
239
240  if (this->models[0])
241    this->models[0]->draw();
242  glPopMatrix();
243}
244
245/**
246 * DEBUG-DRAW OF THE BV-Tree.
247 * @param depth What depth to draw
248 * @param drawMode the mode to draw this entity under
249 */
250void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
251{
252  glMatrixMode(GL_MODELVIEW);
253  glPushMatrix();
254  /* translate */
255  glTranslatef (this->getAbsCoor ().x,
256                this->getAbsCoor ().y,
257                this->getAbsCoor ().z);
258  /* rotate */
259  Vector tmpRot = this->getAbsDir().getSpacialAxis();
260  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
261
262  if (this->obbTree)
263    this->obbTree->drawBV(depth, drawMode);
264  glPopMatrix();
265}
Note: See TracBrowser for help on using the repository browser.