Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new algorithm to rotate entities for drawing works (shifcoor ONLY takes normalized Vectors…)

File size: 7.4 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
[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]31using namespace std;
32
[5208]33SHELL_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]44WorldEntity::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]60WorldEntity::~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)
67  this->setModel(NULL);
[3531]68}
69
[5498]70/**
71 * loads the WorldEntity Specific Parameters.
72 * @param root: the XML-Element to load the Data From
73 */
[4436]74void WorldEntity::loadParams(const TiXmlElement* root)
75{
[5498]76  // Do the PNode loading stuff
[4436]77  static_cast<PNode*>(this)->loadParams(root);
[5498]78
[4436]79  // Model Loading
[5671]80  LoadParam(root, "model", this, WorldEntity, loadModel)
[5652]81      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
[5995]82      .defaultValues(3, NULL, 1.0f, 0);
[5465]83
[4436]84}
85
[3531]86/**
[4885]87 * loads a Model onto a WorldEntity
[4836]88 * @param fileName the name of the model to load
[5057]89 * @param scaling the Scaling of the model
[5498]90 *
91 * @todo fix this, so it only has one loadModel-Function.
[4261]92*/
[5995]93void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
[4261]94{
[4732]95  if (fileName != NULL)
96  {
[5066]97    PRINTF(4)("fetching %s\n", fileName);
[5057]98    if (scaling == 1.0)
[6002]99      this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN), modelNumber);
[5057]100    else
[6002]101      this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling), modelNumber);
102    if (modelNumber == 0)
[5061]103    this->buildObbTree(4);
[4732]104  }
105  else
[5995]106    this->setModel(NULL);
[4261]107}
108
[5061]109/**
[5994]110 * sets a specific Model for the Object.
111 * @param model The Model to set
112 * @param modelNumber the n'th model in the List to get.
113 */
114void WorldEntity::setModel(Model* model, unsigned int modelNumber)
115{
[5995]116  if (this->models.size() <= modelNumber)
117    this->models.resize(modelNumber+1, NULL);
118
119  if (this->models[modelNumber] != NULL)
[6004]120  {
[5995]121    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
[5994]122    if (resource != NULL)
123      ResourceManager::getInstance()->unload(resource, RP_LEVEL);
124    else
[5995]125      delete this->models[modelNumber];
[5994]126  }
[5995]127  this->models[modelNumber] = model;
[5994]128
[6004]129//   if (this->model != NULL)
[5994]130//     this->buildObbTree(4);
131}
132
133
134/**
[5061]135 * builds the obb-tree
136 * @param depth the depth to calculate
137 */
138bool WorldEntity::buildObbTree(unsigned int depth)
139{
[5428]140  if (this->obbTree)
141    delete this->obbTree;
142
[5995]143  if (this->models[0] != NULL)
[5428]144  {
145    PRINTF(4)("creating obb tree\n");
[5708]146
147
[5995]148    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
[5428]149    return true;
150  }
151  else
152  {
153    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
154    this->obbTree = NULL;
155    return false;
156  }
[5061]157}
[5057]158
[5061]159
[4261]160/**
[4885]161 * sets the character attributes of a worldentity
[4836]162 * @param character attributes
[4885]163 *
164 * these attributes don't have to be set, only use them, if you need them
[2043]165*/
[5498]166//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
167//{}
[2036]168
[3583]169
[2043]170/**
[5029]171 *  this function is called, when two entities collide
172 * @param entity: the world entity with whom it collides
173 *
174 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
175 */
176void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
177{
[5498]178  /**
179   * THIS IS A DEFAULT COLLISION-Effect.
180   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
181   * USE::
182   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
183   *
184   * You can always define a default Action.... don't be affraid just test it :)
185   */
[5257]186//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[5029]187}
188
[2043]189
190/**
[5498]191 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
[4885]192 *
[5498]193 */
[3229]194void WorldEntity::postSpawn ()
[2190]195{
196}
[2043]197
[3583]198
[2043]199/**
[4836]200 *  this method is called by the world if the WorldEntity leaves valid gamespace
[4885]201 *
202 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
203 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
[5498]204 *
205 * NOT YET IMPLEMENTED
206 */
[3583]207void WorldEntity::leftWorld ()
[2190]208{
209}
[2043]210
[3583]211
[2190]212/**
[4836]213 *  this method is called every frame
214 * @param time: the time in seconds that has passed since the last tick
[4885]215 *
216 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
[2043]217*/
[4570]218void WorldEntity::tick(float time)
[2190]219{
220}
[3583]221
[5498]222
[3583]223/**
[4836]224 *  the entity is drawn onto the screen with this function
[4885]225 *
226 * This is a central function of an entity: call it to let the entity painted to the screen.
227 * Just override this function with whatever you want to be drawn.
[3365]228*/
[5500]229void WorldEntity::draw() const
[3803]230{
[6002]231  if (!this->models.empty())
232  {
233    glMatrixMode(GL_MODELVIEW);
234    glPushMatrix();
[4570]235
[6002]236    /* translate */
237    glTranslatef (this->getAbsCoor ().x,
238                  this->getAbsCoor ().y,
239                  this->getAbsCoor ().z);
[6004]240    Vector tmpRot = this->getAbsDir().getSpacialAxis();
241    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[2043]242
[6004]243
244    // This Draws the LOD's
[6002]245    float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len();
[6004]246    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
[6002]247    {
248       this->models[2]->draw();
[6004]249    }
250    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
[6002]251    {
252      this->models[1]->draw();
[6004]253    }
254    else if (this->models.size() >= 1 && this->models[0] != NULL)
[6002]255    {
256      this->models[0]->draw();
257    }
258    glPopMatrix();
259  }
[3803]260}
[3583]261
[5498]262/**
263 * DEBUG-DRAW OF THE BV-Tree.
264 * @param depth What depth to draw
265 * @param drawMode the mode to draw this entity under
266 */
[5501]267void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
[4684]268{
269  glMatrixMode(GL_MODELVIEW);
270  glPushMatrix();
271  /* translate */
272  glTranslatef (this->getAbsCoor ().x,
273                this->getAbsCoor ().y,
274                this->getAbsCoor ().z);
275  /* rotate */
[4998]276  Vector tmpRot = this->getAbsDir().getSpacialAxis();
277  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[4684]278
279  if (this->obbTree)
280    this->obbTree->drawBV(depth, drawMode);
281  glPopMatrix();
282}
Note: See TracBrowser for help on using the repository browser.