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, 19 years ago

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

File size: 6.2 KB
RevLine 
[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
22#include "resource_manager.h"
23#include "load_param.h"
[3608]24#include "list.h"
[3803]25#include "vector.h"
[4682]26#include "obb_tree.h"
[3608]27
[2036]28using namespace std;
29
[5208]30SHELL_COMMAND(model, WorldEntity, loadModel)
31    ->describe("sets the Model of the WorldEntity")
32    ->defaultValues(1, "models/ships/reaplow.obj");
33
34
[2043]35/**
[4836]36 *  Loads the WordEntity-specific Part of any derived Class
[5498]37 *
38 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
39 *              that can calls WorldEntities loadParams for itself.
40 */
[4261]41WorldEntity::WorldEntity(const TiXmlElement* root)
[2190]42{
[4320]43  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
[4597]44
[4261]45  this->model = NULL;
[4682]46  this->obbTree = NULL;
[4261]47
48  if (root)
49    this->loadParams(root);
50
[4885]51  this->setVisibiliy(true);
[2190]52}
[2043]53
54/**
[4836]55 *  standard destructor
[2043]56*/
[2190]57WorldEntity::~WorldEntity ()
[2036]58{
[5498]59  // Delete the model (unregister it with the ResourceManager)
[5302]60  if (likely(this->model != NULL))
[3672]61    ResourceManager::getInstance()->unload(this->model);
[5498]62  // Delete the obbTree
[5302]63  if( this->obbTree != NULL)
[4814]64    delete this->obbTree;
[3531]65}
66
[5498]67/**
68 * loads the WorldEntity Specific Parameters.
69 * @param root: the XML-Element to load the Data From
70 */
[4436]71void WorldEntity::loadParams(const TiXmlElement* root)
72{
[5498]73  // Do the PNode loading stuff
[4436]74  static_cast<PNode*>(this)->loadParams(root);
[5498]75
[4436]76  // Model Loading
[5499]77  LoadParam<WorldEntity>(root, "model", this, &WorldEntity::loadModel, false, NULL, 1.0f)
[5465]78      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") ;
79
[4436]80}
81
[3531]82/**
[4885]83 * loads a Model onto a WorldEntity
[4836]84 * @param fileName the name of the model to load
[5057]85 * @param scaling the Scaling of the model
[5498]86 *
87 * @todo fix this, so it only has one loadModel-Function.
[4261]88*/
[5499]89void WorldEntity::loadModel(const char* fileName, float scaling)
[4261]90{
91  if (this->model)
92    ResourceManager::getInstance()->unload(this->model, RP_LEVEL);
[4732]93  if (fileName != NULL)
94  {
[5066]95    PRINTF(4)("fetching %s\n", fileName);
[5057]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
[5061]101    this->buildObbTree(4);
[4732]102  }
103  else
104    this->model = NULL;
[4261]105}
106
[5061]107/**
108 * builds the obb-tree
109 * @param depth the depth to calculate
110 */
111bool WorldEntity::buildObbTree(unsigned int depth)
112{
[5428]113  if (this->obbTree)
114    delete this->obbTree;
115
[5482]116  if (this->model != NULL)
[5428]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  }
[5061]128}
[5057]129
[5061]130
[4261]131/**
[4885]132 * sets the character attributes of a worldentity
[4836]133 * @param character attributes
[4885]134 *
135 * these attributes don't have to be set, only use them, if you need them
[2043]136*/
[5498]137//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
138//{}
[2036]139
[3583]140
[2043]141/**
[5029]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{
[5498]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   */
[5257]157//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[5029]158}
159
[2043]160
161/**
[5498]162 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
[4885]163 *
[5498]164 */
[3229]165void WorldEntity::postSpawn ()
[2190]166{
167}
[2043]168
[3583]169
[2043]170/**
[4836]171 *  this method is called by the world if the WorldEntity leaves valid gamespace
[4885]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).
[5498]175 *
176 * NOT YET IMPLEMENTED
177 */
[3583]178void WorldEntity::leftWorld ()
[2190]179{
180}
[2043]181
[3583]182
[2190]183/**
[4836]184 *  this method is called every frame
185 * @param time: the time in seconds that has passed since the last tick
[4885]186 *
187 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
[2043]188*/
[4570]189void WorldEntity::tick(float time)
[2190]190{
191}
[3583]192
[5498]193
[3583]194/**
[4836]195 *  the entity is drawn onto the screen with this function
[4885]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.
[3365]199*/
[4570]200void WorldEntity::draw()
[3803]201{
202  glMatrixMode(GL_MODELVIEW);
203  glPushMatrix();
204  float matrix[4][4];
[4570]205
[3803]206  /* translate */
[4570]207  glTranslatef (this->getAbsCoor ().x,
208                this->getAbsCoor ().y,
209                this->getAbsCoor ().z);
[5435]210  /* rotate */ // FIXME: devise a new Way to rotate this
[3803]211  this->getAbsDir ().matrix (matrix);
212  glMultMatrixf((float*)matrix);
[2043]213
[3803]214  if (this->model)
215    this->model->draw();
216  glPopMatrix();
217}
[3583]218
[5498]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 */
[5431]224void WorldEntity::drawBVTree(unsigned int depth, int drawMode)
[4684]225{
226  glMatrixMode(GL_MODELVIEW);
227  glPushMatrix();
228  /* translate */
229  glTranslatef (this->getAbsCoor ().x,
230                this->getAbsCoor ().y,
231                this->getAbsCoor ().z);
232  /* rotate */
[4998]233  Vector tmpRot = this->getAbsDir().getSpacialAxis();
234  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[4684]235
236  if (this->obbTree)
237    this->obbTree->drawBV(depth, drawMode);
238  glPopMatrix();
239}
Note: See TracBrowser for help on using the repository browser.