2009-12-29 19:08:20 -05:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2009 Joerg Henrichs
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2010-11-19 16:23:06 -05:00
|
|
|
#include "config/user_config.hpp"
|
2010-12-05 15:55:09 -05:00
|
|
|
#include "animations/billboard_animation.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"
|
|
|
|
|
2011-06-08 21:29:25 -04:00
|
|
|
#include <IMeshSceneNode.h>
|
|
|
|
|
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.
|
2009-12-29 19:08:20 -05:00
|
|
|
*/
|
2010-12-05 15:55:09 -05:00
|
|
|
void TrackObjectManager::add(const XMLNode &xml_node)
|
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
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
std::string groupname;
|
|
|
|
xml_node.get("lod_group", &groupname);
|
|
|
|
bool is_lod = !groupname.empty();
|
|
|
|
|
|
|
|
std::string type;
|
|
|
|
xml_node.get("type", &type);
|
|
|
|
if(type=="movable")
|
2011-12-03 20:25:09 -05:00
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
if (is_lod)
|
|
|
|
{
|
|
|
|
m_lod_objects[groupname].push_back(new PhysicalObject(xml_node));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_all_objects.push_back(new PhysicalObject(xml_node));
|
|
|
|
}
|
2011-12-03 20:25:09 -05:00
|
|
|
}
|
2012-02-13 18:40:27 -05:00
|
|
|
else if(type=="animation")
|
2011-12-03 20:25:09 -05:00
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
if (is_lod)
|
|
|
|
{
|
|
|
|
m_lod_objects[groupname].push_back(new ThreeDAnimation(xml_node));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_all_objects.push_back(new ThreeDAnimation(xml_node));
|
|
|
|
}
|
2011-12-03 20:25:09 -05:00
|
|
|
}
|
2012-02-13 18:40:27 -05:00
|
|
|
else if(type=="billboard")
|
|
|
|
{
|
|
|
|
m_all_objects.push_back(new BillboardAnimation(xml_node));
|
|
|
|
}
|
|
|
|
else if(type=="sfx-emitter")
|
|
|
|
{
|
|
|
|
m_all_objects.push_back(new TrackObject(xml_node));
|
|
|
|
}
|
|
|
|
else if(type=="action-trigger")
|
2011-12-03 20:25:09 -05:00
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
m_all_objects.push_back(new TrackObject(xml_node));
|
2011-12-03 20:25:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
fprintf(stderr, "Unknown track object: '%s' - ignored.\n",
|
|
|
|
type.c_str());
|
2011-12-03 20:25:09 -05:00
|
|
|
}
|
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
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
fprintf(stderr, "[TrackObjectManager] WARNING: Could not load track object. Reason : %s\n",
|
|
|
|
e.what());
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // add
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Initialises all track objects.
|
|
|
|
*/
|
|
|
|
void TrackObjectManager::init()
|
|
|
|
{
|
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()
|
|
|
|
{
|
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->reset();
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // reset
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
|
2011-05-23 20:04:43 -04:00
|
|
|
void TrackObjectManager::handleExplosion(const Vec3 &pos, const PhysicalObject *mp)
|
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
|
|
|
{
|
2011-05-23 20:04:43 -04:00
|
|
|
curr->handleExplosion(pos, mp == curr);
|
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
|
|
|
|
|
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)
|
|
|
|
{
|
2011-07-25 22:24:49 -04:00
|
|
|
if (node->getType() == scene::ESNT_MESH ||
|
|
|
|
node->getType() == scene::ESNT_OCTREE ||
|
|
|
|
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();
|
|
|
|
}
|
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);
|
2011-06-08 21:29:25 -04:00
|
|
|
|
|
|
|
} // for j<MATERIAL_MAX_TEXTURES
|
|
|
|
} // for i<getMeshBufferCount()
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->setMaterialFlag(video::EMF_FOG_ENABLE, enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->getType() == scene::ESNT_LOD_NODE)
|
|
|
|
{
|
2011-07-25 22:24:49 -04:00
|
|
|
std::vector<scene::ISceneNode*>&
|
|
|
|
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
|
|
|
{
|
2011-08-06 15:51:12 -04:00
|
|
|
if (curr->getNode() != NULL)
|
|
|
|
{
|
|
|
|
adjustForFog(curr->getNode(), enable);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2011-10-26 21:04:16 -04:00
|
|
|
PhysicalObject* TrackObjectManager::insertObject(const std::string& model,
|
2011-09-01 21:34:08 -04:00
|
|
|
PhysicalObject::bodyTypes shape,
|
|
|
|
float mass, float radius,
|
2011-09-01 16:09:26 -04:00
|
|
|
const core::vector3df& hpr,
|
|
|
|
const core::vector3df& pos,
|
|
|
|
const core::vector3df& scale)
|
|
|
|
{
|
2011-09-01 21:34:08 -04:00
|
|
|
PhysicalObject* object = new PhysicalObject(model, shape, mass, radius,
|
|
|
|
hpr, pos, scale);
|
2011-09-01 16:09:26 -04:00
|
|
|
object->init();
|
|
|
|
m_all_objects.push_back(object);
|
|
|
|
return 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.
|
|
|
|
*/
|
|
|
|
void TrackObjectManager::removeObject(PhysicalObject* 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
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* \brief To be called after all objects are loaded and the LodNodeLoader is done
|
|
|
|
* parsing everything.
|
|
|
|
* This method exists because LOD objects need to be created after others.
|
|
|
|
*
|
|
|
|
* \param lod_nodes the LOD nodes created by the LodNodeLoader.
|
|
|
|
*/
|
|
|
|
void TrackObjectManager::assingLodNodes(const std::vector<LODNode*>& lod_nodes)
|
|
|
|
{
|
|
|
|
for (unsigned int n=0; n<lod_nodes.size(); n++)
|
|
|
|
{
|
|
|
|
std::vector<TrackObject*>& queue = m_lod_objects[ lod_nodes[n]->getGroupName() ];
|
|
|
|
assert( queue.size() > 0 );
|
|
|
|
TrackObject* obj = queue[ queue.size() - 1 ];
|
|
|
|
obj->setNode( lod_nodes[n] );
|
|
|
|
queue.erase( queue.end() - 1 );
|
|
|
|
|
|
|
|
manualInsertObject( obj );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_lod_objects.clear();
|
|
|
|
}
|