2009-12-29 19:08:20 -05:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2013-11-15 06:43:21 -05:00
|
|
|
// Copyright (C) 2009-2013 Joerg Henrichs
|
2009-12-29 19:08:20 -05:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "tracks/track_object_manager.hpp"
|
|
|
|
|
2012-04-18 09:14:18 -04:00
|
|
|
#include "animations/ipo.hpp"
|
2009-12-29 19:08:20 -05:00
|
|
|
#include "animations/three_d_animation.hpp"
|
2011-05-17 21:00:26 -04:00
|
|
|
#include "graphics/lod_node.hpp"
|
2011-06-08 21:29:25 -04:00
|
|
|
#include "graphics/material_manager.hpp"
|
2009-12-29 19:08:20 -05:00
|
|
|
#include "io/xml_node.hpp"
|
|
|
|
#include "physics/physical_object.hpp"
|
|
|
|
#include "tracks/track_object.hpp"
|
2014-04-11 14:14:20 -04:00
|
|
|
#include "utils/log.hpp"
|
2009-12-29 19:08:20 -05:00
|
|
|
|
2011-06-08 21:29:25 -04:00
|
|
|
#include <IMeshSceneNode.h>
|
2012-07-25 20:33:25 -04:00
|
|
|
#include <ISceneManager.h>
|
2011-06-08 21:29:25 -04:00
|
|
|
|
2009-12-29 19:08:20 -05:00
|
|
|
TrackObjectManager::TrackObjectManager()
|
|
|
|
{
|
|
|
|
} // TrackObjectManager
|
|
|
|
|
2010-01-06 20:14:46 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
TrackObjectManager::~TrackObjectManager()
|
|
|
|
{
|
|
|
|
} // ~TrackObjectManager
|
|
|
|
|
2009-12-29 19:08:20 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds an object to the track object manager. The type to add is specified
|
|
|
|
* in the xml_node.
|
2011-12-10 16:49:24 -05:00
|
|
|
* \note If you add add any objects with LOD, don't forget to call
|
|
|
|
* TrackObjectManager::assingLodNodes after everything is loaded
|
|
|
|
* to finalize their creation.
|
2013-12-31 13:45:18 -05:00
|
|
|
*
|
|
|
|
* FIXME: all of this is horrible, just make the exporter write LOD definitions
|
|
|
|
* in a separate section that's read before everything and remove all this
|
|
|
|
* crap
|
2009-12-29 19:08:20 -05:00
|
|
|
*/
|
2014-09-11 07:48:03 -04:00
|
|
|
void TrackObjectManager::add(const XMLNode &xml_node, scene::ISceneNode* parent,
|
|
|
|
ModelDefinitionLoader& model_def_loader)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
try
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2014-09-11 07:48:03 -04:00
|
|
|
TrackObject *obj = new TrackObject(xml_node, parent, model_def_loader);
|
|
|
|
m_all_objects.push_back(obj);
|
|
|
|
if(obj->isDriveable())
|
|
|
|
m_driveable_objects.push_back(obj);
|
2010-12-05 15:55:09 -05:00
|
|
|
}
|
2012-02-13 18:40:27 -05:00
|
|
|
catch (std::exception& e)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2014-01-01 16:18:06 -05:00
|
|
|
Log::warn("TrackObjectManager", "Could not load track object. Reason : %s",
|
|
|
|
e.what());
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // add
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Initialises all track objects.
|
|
|
|
*/
|
|
|
|
void TrackObjectManager::init()
|
|
|
|
{
|
2014-08-01 23:45:48 -04:00
|
|
|
|
2011-05-23 20:04:43 -04:00
|
|
|
TrackObject* curr;
|
|
|
|
for_in (curr, m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2011-05-23 20:04:43 -04:00
|
|
|
curr->init();
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // reset
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Initialises all track objects.
|
|
|
|
*/
|
|
|
|
void TrackObjectManager::reset()
|
|
|
|
{
|
2014-11-15 22:40:19 -05:00
|
|
|
for (TrackObject* curr : m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2011-05-23 20:04:43 -04:00
|
|
|
curr->reset();
|
2014-09-10 09:39:31 -04:00
|
|
|
if (!curr->isEnabled())
|
|
|
|
{
|
|
|
|
//PhysicalObjects may need to be added
|
|
|
|
if (curr->getType() == "mesh")
|
|
|
|
{
|
|
|
|
if (curr->getPhysicalObject() != NULL)
|
|
|
|
curr->getPhysicalObject()->addBody();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr->setEnable(true);
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // reset
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2014-08-05 10:58:40 -04:00
|
|
|
/** disables all track objects with a particular ID
|
|
|
|
* \param name Name or ID for disabling
|
|
|
|
*/
|
2014-06-18 03:42:51 -04:00
|
|
|
void TrackObjectManager::disable(std::string name)
|
2014-06-11 01:22:04 -04:00
|
|
|
{
|
2014-11-15 22:40:19 -05:00
|
|
|
for (TrackObject* curr : m_all_objects)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
if (curr->getName() == (name) || curr->getID() == (name))
|
|
|
|
{
|
2014-05-22 03:09:46 -04:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
curr->setEnable(false);
|
|
|
|
if (curr->getType() == "mesh")
|
|
|
|
{
|
|
|
|
if (curr->getPhysicalObject()!=NULL)
|
|
|
|
curr->getPhysicalObject()->removeBody();
|
2014-03-16 23:56:24 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
2014-03-16 23:56:24 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 10:58:40 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** enables all track objects with a particular ID
|
|
|
|
* \param name Name or ID for enabling
|
|
|
|
*/
|
2014-06-18 03:42:51 -04:00
|
|
|
void TrackObjectManager::enable(std::string name)
|
2014-06-11 01:22:04 -04:00
|
|
|
{
|
2014-11-15 22:40:19 -05:00
|
|
|
for (TrackObject* curr : m_all_objects)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
if (curr->getName() == (name) || curr->getID() == (name))
|
|
|
|
{
|
|
|
|
curr->reset();
|
|
|
|
curr->setEnable(true);
|
|
|
|
if (curr->getType() == "mesh")
|
2014-05-29 22:45:34 -04:00
|
|
|
{
|
2014-10-27 18:43:09 -04:00
|
|
|
if (curr->getPhysicalObject() != NULL)
|
|
|
|
curr->getPhysicalObject()->addBody();
|
2014-03-16 23:56:24 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 23:56:24 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2014-08-05 10:58:40 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** returns activation status for all track objects
|
|
|
|
* with a particular ID
|
|
|
|
* \param name Name or ID of track object
|
|
|
|
*/
|
2014-06-11 01:22:04 -04:00
|
|
|
bool TrackObjectManager::getStatus(std::string name)
|
|
|
|
{
|
2014-11-15 22:40:19 -05:00
|
|
|
for (TrackObject* curr : m_all_objects){
|
2014-06-02 03:37:51 -04:00
|
|
|
if (curr->getName() == (name)||curr->getID()==(name))
|
2014-05-29 22:45:34 -04:00
|
|
|
{
|
2014-05-23 05:09:03 -04:00
|
|
|
|
|
|
|
return curr->isEnabled();
|
2014-03-16 23:56:24 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-05-29 22:45:34 -04:00
|
|
|
//object not found
|
|
|
|
return false;
|
2014-03-16 23:56:24 -04:00
|
|
|
}
|
2014-08-05 10:58:40 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** returns a reference to the track object
|
|
|
|
* with a particular ID
|
|
|
|
* \param name Name or ID of track object
|
|
|
|
*/
|
2014-06-11 01:22:04 -04:00
|
|
|
TrackObject* TrackObjectManager::getTrackObject(std::string name)
|
|
|
|
{
|
2014-11-15 22:40:19 -05:00
|
|
|
for (TrackObject* curr : m_all_objects)
|
2014-06-18 03:42:51 -04:00
|
|
|
{
|
2014-06-11 01:22:04 -04:00
|
|
|
if (curr->getName() == (name) || curr->getID() == (name))
|
|
|
|
{
|
|
|
|
|
|
|
|
return curr;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//object not found
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-29 19:08:20 -05:00
|
|
|
/** Handles an explosion, i.e. it makes sure that all physical objects are
|
|
|
|
* affected accordingly.
|
|
|
|
* \param pos Position of the explosion.
|
|
|
|
* \param obj If the hit was a physical object, this object will be affected
|
|
|
|
* more. Otherwise this is NULL.
|
2012-11-04 07:57:03 -05:00
|
|
|
* \param secondary_hits True if items that are not directly hit should
|
|
|
|
* also be affected.
|
2009-12-29 19:08:20 -05:00
|
|
|
*/
|
|
|
|
|
2012-11-04 07:57:03 -05:00
|
|
|
void TrackObjectManager::handleExplosion(const Vec3 &pos, const PhysicalObject *mp,
|
|
|
|
bool secondary_hits)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2011-05-23 20:04:43 -04:00
|
|
|
TrackObject* curr;
|
|
|
|
for_in (curr, m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2014-01-24 06:25:55 -05:00
|
|
|
if(secondary_hits || mp == curr->getPhysicalObject())
|
|
|
|
curr->handleExplosion(pos, mp == curr->getPhysicalObject());
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // handleExplosion
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2011-07-25 22:24:49 -04:00
|
|
|
/** Updates all track objects.
|
|
|
|
* \param dt Time step size.
|
|
|
|
*/
|
2009-12-29 19:08:20 -05:00
|
|
|
void TrackObjectManager::update(float dt)
|
|
|
|
{
|
2011-09-18 13:50:20 -04:00
|
|
|
TrackObject* curr;
|
|
|
|
for_in (curr, m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2011-09-18 13:50:20 -04:00
|
|
|
curr->update(dt);
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // update
|
|
|
|
|
2014-09-11 07:48:03 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2014-09-11 18:37:03 -04:00
|
|
|
/** Does a raycast against all driveable objects. This way part of the track
|
|
|
|
* can be a physical object, and can e.g. be animated. A separate list of all
|
|
|
|
* driveable objects is maintained (in one case there were over 2000 bodies,
|
|
|
|
* but only one is driveable). The result of the raycast against the track
|
|
|
|
* mesh are the input parameter. It is then tested if the raycast against
|
|
|
|
* a track object gives a 'closer' result. If so, the parameters hit_point,
|
|
|
|
* normal, and material will be updated.
|
|
|
|
* \param from/to The from and to position for the raycast.
|
|
|
|
* \param xyz The position in world where the ray hit.
|
|
|
|
* \param material The material of the mesh that was hit.
|
|
|
|
* \param normal The intrapolated normal at that position.
|
|
|
|
* \param interpolate_normal If true, the returned normal is the interpolated
|
|
|
|
* based on the three normals of the triangle and the location of the
|
|
|
|
* hit point (which is more compute intensive, but results in much
|
|
|
|
* smoother results).
|
|
|
|
* \return True if a triangle was hit, false otherwise (and no output
|
|
|
|
* variable will be set.
|
|
|
|
|
|
|
|
*/
|
2014-09-11 07:48:03 -04:00
|
|
|
void TrackObjectManager::castRay(const btVector3 &from,
|
|
|
|
const btVector3 &to, btVector3 *hit_point,
|
|
|
|
const Material **material,
|
|
|
|
btVector3 *normal,
|
|
|
|
bool interpolate_normal) const
|
|
|
|
{
|
|
|
|
float distance = 9999.9f;
|
|
|
|
// If there was a hit already, compute the current distance
|
|
|
|
if(*material)
|
|
|
|
{
|
|
|
|
distance = hit_point->distance(from);
|
|
|
|
}
|
2014-11-16 16:01:44 -05:00
|
|
|
const TrackObject* curr;
|
|
|
|
for_in(curr, m_driveable_objects)
|
2014-09-11 07:48:03 -04:00
|
|
|
{
|
|
|
|
btVector3 new_hit_point;
|
|
|
|
const Material *new_material;
|
|
|
|
btVector3 new_normal;
|
|
|
|
if(curr->castRay(from, to, &new_hit_point, &new_material, &new_normal,
|
|
|
|
interpolate_normal))
|
|
|
|
{
|
|
|
|
float new_distance = new_hit_point.distance(from);
|
2014-09-11 18:37:03 -04:00
|
|
|
// If the new hit is closer than the current hit, save
|
|
|
|
// the data.
|
2014-09-11 07:48:03 -04:00
|
|
|
if (new_distance < distance)
|
|
|
|
{
|
|
|
|
*material = new_material;
|
|
|
|
*hit_point = new_hit_point;
|
|
|
|
*normal = new_normal;
|
|
|
|
distance = new_distance;
|
|
|
|
} // if new_distance < distance
|
|
|
|
} // if hit
|
|
|
|
} // for all track objects.
|
|
|
|
} // castRay
|
|
|
|
|
2011-07-25 22:24:49 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Enables or disables fog for a given scene node.
|
|
|
|
* \param node The node to adjust.
|
|
|
|
* \param enable True if fog is enabled, otherwise fog is disabled.
|
|
|
|
*/
|
2011-06-08 21:29:25 -04:00
|
|
|
void adjustForFog(scene::ISceneNode *node, bool enable)
|
|
|
|
{
|
2013-05-29 18:04:35 -04:00
|
|
|
if (node->getType() == scene::ESNT_MESH ||
|
|
|
|
node->getType() == scene::ESNT_OCTREE ||
|
2011-07-25 22:24:49 -04:00
|
|
|
node->getType() == scene::ESNT_ANIMATED_MESH)
|
2011-06-08 21:29:25 -04:00
|
|
|
{
|
2011-07-25 22:24:49 -04:00
|
|
|
scene::IMesh* mesh;
|
|
|
|
if (node->getType() == scene::ESNT_ANIMATED_MESH) {
|
|
|
|
mesh = ((scene::IAnimatedMeshSceneNode*)node)->getMesh();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mesh = ((scene::IMeshSceneNode*)node)->getMesh();
|
|
|
|
}
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2011-06-08 21:29:25 -04:00
|
|
|
unsigned int n = mesh->getMeshBufferCount();
|
|
|
|
for (unsigned int i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
scene::IMeshBuffer *mb = mesh->getMeshBuffer(i);
|
|
|
|
video::SMaterial &irr_material=mb->getMaterial();
|
|
|
|
for (unsigned int j=0; j<video::MATERIAL_MAX_TEXTURES; j++)
|
|
|
|
{
|
|
|
|
video::ITexture* t = irr_material.getTexture(j);
|
2011-07-25 22:24:49 -04:00
|
|
|
if (t) material_manager->adjustForFog(t, mb, node, enable);
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2011-06-08 21:29:25 -04:00
|
|
|
} // for j<MATERIAL_MAX_TEXTURES
|
|
|
|
} // for i<getMeshBufferCount()
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->setMaterialFlag(video::EMF_FOG_ENABLE, enable);
|
|
|
|
}
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2011-06-08 21:29:25 -04:00
|
|
|
if (node->getType() == scene::ESNT_LOD_NODE)
|
|
|
|
{
|
2013-05-29 18:04:35 -04:00
|
|
|
std::vector<scene::ISceneNode*>&
|
2011-07-25 22:24:49 -04:00
|
|
|
subnodes = ((LODNode*)node)->getAllNodes();
|
2011-06-08 21:29:25 -04:00
|
|
|
for (unsigned int n=0; n<subnodes.size(); n++)
|
|
|
|
{
|
|
|
|
adjustForFog(subnodes[n], enable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // adjustForFog
|
|
|
|
|
|
|
|
|
2009-12-29 19:08:20 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2011-07-25 22:24:49 -04:00
|
|
|
/** Enable or disable fog on objects.
|
|
|
|
*/
|
2011-05-17 21:00:26 -04:00
|
|
|
void TrackObjectManager::enableFog(bool enable)
|
|
|
|
{
|
2011-05-23 20:04:43 -04:00
|
|
|
TrackObject* curr;
|
|
|
|
for_in (curr, m_all_objects)
|
2011-05-17 21:00:26 -04:00
|
|
|
{
|
2013-04-03 21:15:37 -04:00
|
|
|
TrackObjectPresentationMesh* meshPresentation =
|
|
|
|
curr->getPresentation<TrackObjectPresentationMesh>();
|
|
|
|
if (meshPresentation!= NULL)
|
2011-08-06 15:51:12 -04:00
|
|
|
{
|
2013-04-03 21:15:37 -04:00
|
|
|
adjustForFog(meshPresentation->getNode(), enable);
|
2011-08-06 15:51:12 -04:00
|
|
|
}
|
2011-05-17 21:00:26 -04:00
|
|
|
}
|
2011-07-25 22:24:49 -04:00
|
|
|
} // enableFog
|
2011-09-01 16:09:26 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2013-04-04 21:30:34 -04:00
|
|
|
void TrackObjectManager::insertObject(TrackObject* object)
|
2011-09-01 16:09:26 -04:00
|
|
|
{
|
|
|
|
m_all_objects.push_back(object);
|
|
|
|
}
|
|
|
|
|
2011-09-01 19:47:15 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2011-09-15 08:46:03 -04:00
|
|
|
/** Removes the object from the scene graph, bullet, and the list of
|
|
|
|
* track objects, and then frees the object.
|
|
|
|
* \param obj The physical object to remove.
|
|
|
|
*/
|
2013-04-03 21:15:37 -04:00
|
|
|
void TrackObjectManager::removeObject(TrackObject* obj)
|
2011-09-01 19:47:15 -04:00
|
|
|
{
|
2011-09-15 08:46:03 -04:00
|
|
|
m_all_objects.remove(obj);
|
|
|
|
delete obj;
|
|
|
|
} // removeObject
|
2011-09-01 16:09:26 -04:00
|
|
|
|
2011-12-10 16:49:24 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2014-01-01 16:18:06 -05:00
|
|
|
|
|
|
|
/*
|
2013-12-31 13:09:13 -05:00
|
|
|
void TrackObjectManager::assingLodNodes(const std::vector<LODNode*>& lod_nodes)
|
2011-12-10 16:49:24 -05:00
|
|
|
{
|
|
|
|
for (unsigned int n=0; n<lod_nodes.size(); n++)
|
|
|
|
{
|
2013-04-04 20:20:14 -04:00
|
|
|
std::vector<const XMLNode*>& queue = m_lod_objects[ lod_nodes[n]->getGroupName() ];
|
2011-12-10 16:49:24 -05:00
|
|
|
assert( queue.size() > 0 );
|
2013-04-04 20:20:14 -04:00
|
|
|
const XMLNode* xml = queue[ queue.size() - 1 ];
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2013-12-31 13:09:13 -05:00
|
|
|
TrackObject* obj = new TrackObject(*xml, lod_nodes[n]->getParent(), lod_nodes[n]);
|
2011-12-10 16:49:24 -05:00
|
|
|
queue.erase( queue.end() - 1 );
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2013-01-19 16:43:21 -05:00
|
|
|
m_all_objects.push_back(obj);
|
2011-12-10 16:49:24 -05:00
|
|
|
}
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2011-12-10 16:49:24 -05:00
|
|
|
m_lod_objects.clear();
|
|
|
|
}
|
2014-03-29 06:33:43 -04:00
|
|
|
*/
|