Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Load_Param2 changed, to load Models with, or without scale.

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