From be6b0f80857b8fa5222df4cc852921fac3ef0047 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Tue, 10 Feb 2009 11:11:39 +0000 Subject: [PATCH] Fixed compilation problem on non-irrlicht compilation (missing files, missing ifdef - sorry for that). In irrlicht karts have now (non-rotating) wheels, and the camera points correctly at the kart. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@3116 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/graphics/camera.cpp | 4 +-- src/graphics/irr_driver.cpp | 11 ++++++++ src/graphics/irr_driver.hpp | 1 + src/graphics/mesh_tools.cpp | 47 ++++++++++++++++++++++++++++++++++ src/graphics/mesh_tools.hpp | 35 ++++++++++++++++++++++++++ src/items/attachment.cpp | 2 ++ src/items/item_manager.cpp | 2 ++ src/karts/kart.cpp | 3 +++ src/karts/kart_model.cpp | 50 +++++++++++++++++++++++++++++++++---- src/karts/kart_model.hpp | 5 +++- src/karts/moveable.cpp | 3 +++ 11 files changed, 155 insertions(+), 8 deletions(-) create mode 100755 src/graphics/mesh_tools.cpp create mode 100755 src/graphics/mesh_tools.hpp diff --git a/src/graphics/camera.cpp b/src/graphics/camera.cpp index 8a2617870..ef29713e0 100644 --- a/src/graphics/camera.cpp +++ b/src/graphics/camera.cpp @@ -195,7 +195,7 @@ void Camera::setInitialTransform() } // updateKartPosition //----------------------------------------------------------------------------- -void Camera::update (float dt) +void Camera::update(float dt) { if(m_mode==CM_FINAL) return finalCamera(dt); @@ -262,7 +262,7 @@ void Camera::update (float dt) m_hpr = c.getHPR(); #ifdef HAVE_IRRLICHT m_camera->setPosition(m_xyz.toIrrVector()); - //m_camera->setTarget(kart_xyz.toIrrVector()); + m_camera->setTarget(kart_xyz.toIrrVector()); #else m_context -> setCamera(&c.toSgCoord()); #endif diff --git a/src/graphics/irr_driver.cpp b/src/graphics/irr_driver.cpp index 7da5a3853..f901ea313 100755 --- a/src/graphics/irr_driver.cpp +++ b/src/graphics/irr_driver.cpp @@ -80,6 +80,17 @@ scene::IAnimatedMesh *IrrDriver::getAnimatedMesh(const std::string &filename) return m_scene_manager->getMesh(filename.c_str()); } // getAnimatedMesh +// ---------------------------------------------------------------------------- +/** Loads a non-animated mesh and returns a pointer to it. + * \param filename File to load. + */ +scene::IMesh *IrrDriver::getMesh(const std::string &filename) +{ + scene::IAnimatedMesh *m = m_scene_manager->getMesh(filename.c_str()); + if(!m) return NULL; + return m->getMesh(0); +} // getMesh + // ---------------------------------------------------------------------------- /** Adds a mesh that will be optimised using an oct tree. * \param mesh Mesh to add. diff --git a/src/graphics/irr_driver.hpp b/src/graphics/irr_driver.hpp index abbcfc425..45fccb247 100755 --- a/src/graphics/irr_driver.hpp +++ b/src/graphics/irr_driver.hpp @@ -40,6 +40,7 @@ public: IrrlichtDevice *getDevice() const { return m_device; } scene::ISceneManager *getSceneManager() const { return m_scene_manager; } scene::IAnimatedMesh *getAnimatedMesh(const std::string &name); + scene::IMesh *getMesh(const std::string &name); bool OnEvent(const irr::SEvent &event); scene::ISceneNode *addOctTree(scene::IMesh *mesh); scene::ISceneNode *addMesh(scene::IMesh *mesh); diff --git a/src/graphics/mesh_tools.cpp b/src/graphics/mesh_tools.cpp new file mode 100755 index 000000000..c96c42712 --- /dev/null +++ b/src/graphics/mesh_tools.cpp @@ -0,0 +1,47 @@ +// $Id: mesh_tools.hpp 3001 2009-01-20 13:51:08Z hikerstk $ +// +// 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. +#ifdef HAVE_IRRLICHT +#include "graphics/mesh_tools.hpp" + +void MeshTools::minMax3D(scene::IMesh* mesh, Vec3 *min, Vec3 *max) { + + Vec3 extend; + *min = Vec3( 999999.9f); + *max = Vec3(-999999.9f); + for(unsigned int i=0; igetMeshBufferCount(); i++) { + scene::IMeshBuffer *mb = mesh->getMeshBuffer(i); + if(mb->getVertexType()!=video::EVT_STANDARD) { + fprintf(stderr, "Tools::minMax3D: Ignoring type '%d'!", + mb->getVertexType()); + continue; + } + u16 *mbIndices = mb->getIndices(); + irr::video::S3DVertex* mbVertices=(irr::video::S3DVertex*)mb->getVertices(); + for(unsigned int j=0; jgetIndexCount(); j+=1) { + int indx=mbIndices[j]; + Vec3 c(mbVertices[indx].Pos.X, + mbVertices[indx].Pos.Y, + mbVertices[indx].Pos.Z ); + min->min(c); + max->max(c); + } // for j + } // for iaddMesh(m_mesh); for(unsigned int i=0; i<4; i++) { - // *node->addChild(m_wh + m_wheel_node[i] = irr_driver->addMesh(m_wheel_model[i]); + m_wheel_node[i]->setPosition(m_wheel_graphics_position[i].toIrrVector()); + (*node)->addChild(m_wheel_node[i]); } } // attachModel - +#endif // ---------------------------------------------------------------------------- /** Loads the 3d model and all wheels. @@ -111,7 +114,7 @@ void KartModel::loadModels(const std::string &kart_ident) { #ifdef HAVE_IRRLICHT std::string full_path = file_manager->getKartFile(m_model_filename); - m_mesh = irr_driver->getAnimatedMesh(full_path)->getMesh(0); + m_mesh = irr_driver->getMesh(full_path); Vec3 min, max; MeshTools::minMax3D(m_mesh, &min, &max); Vec3 size = max-min; @@ -170,7 +173,7 @@ void KartModel::loadModels(const std::string &kart_ident) #ifdef HAVE_IRRLICHT std::string full_wheel = file_manager->getKartFile(m_wheel_filename[i], kart_ident); - m_wheel_model[i] = irr_driver->getAnimatedMesh(full_wheel)->getMesh(0); + m_wheel_model[i] = irr_driver->getMesh(full_wheel); // FIXME: wheel handling still missing. #else m_wheel_model[i] = loader->load(m_wheel_filename[i], CB_KART); @@ -273,7 +276,44 @@ void KartModel::adjustWheels(float rotation, float steer, const float suspension[4]) { #ifdef HAVE_IRRLICHT - // FIXME: missing + float clamped_suspension[4]; + // Clamp suspension to minimum and maximum suspension length, so that + // the graphical wheel models don't look too wrong. + for(unsigned int i=0; i<4; i++) + { + const float suspension_length = (m_max_suspension[i]-m_min_suspension[i])/2; + + // limit amplitude between set limits, first dividing it by a + // somewhat arbitrary constant to reduce visible wheel movement + clamped_suspension[i] = std::min(std::max(suspension[i]/m_dampen_suspension_amplitude[i], + m_min_suspension[i]), + m_max_suspension[i]); + float ratio = clamped_suspension[i] / suspension_length; + const int sign = ratio < 0 ? -1 : 1; + ratio = sign * fabsf(ratio*(2-ratio)); // expanded form of 1 - (1 - x)^2, i.e. making suspension display quadratic and not linear + clamped_suspension[i] = ratio*suspension_length; + } // for i<4 + + core::vector3df wheel_rot (RAD_TO_DEGREE(-rotation), 0, 0); + //core::vector3df wheel_steer(0, wheel_steer, 0); + //core::vector3df wheel_front = wheel_rot+wheel_steer; +#ifdef FIXME + sgCopyVec3(wheel_front[3], m_wheel_graphics_position[0].toFloat()); + wheel_front[3][2] += clamped_suspension[0]; + m_wheel_transform[0]->setTransform(wheel_front); + + sgCopyVec3(wheel_front[3], m_wheel_graphics_position[1].toFloat()); + wheel_front[3][2] += clamped_suspension[1]; + m_wheel_transform[1]->setTransform(wheel_front); + + sgCopyVec3(wheel_rot[3], m_wheel_graphics_position[2].toFloat()); + wheel_rot[3][2] += clamped_suspension[2]; + m_wheel_transform[2]->setTransform(wheel_rot); + + sgCopyVec3(wheel_rot[3], m_wheel_graphics_position[3].toFloat()); + wheel_rot[3][2] += clamped_suspension[3]; + m_wheel_transform[3]->setTransform(wheel_rot); +#endif #else sgMat4 wheel_front; sgMat4 wheel_steer; diff --git a/src/karts/kart_model.hpp b/src/karts/kart_model.hpp index e7a2a12d4..caebd8aef 100755 --- a/src/karts/kart_model.hpp +++ b/src/karts/kart_model.hpp @@ -58,7 +58,10 @@ private: #ifdef HAVE_IRRLICHT /** The four wheel models. */ - scene::IMesh *m_wheel_model[4]; + scene::IMesh *m_wheel_model[4]; + + /** The four scene nodes the wheels are attached to */ + scene::ISceneNode *m_wheel_node[4]; #else /** The four wheel models. */ ssgEntity *m_wheel_model[4]; diff --git a/src/karts/moveable.cpp b/src/karts/moveable.cpp index f5e75f040..49ad70472 100644 --- a/src/karts/moveable.cpp +++ b/src/karts/moveable.cpp @@ -57,6 +57,9 @@ void Moveable::updateGraphics(const Vec3& off_xyz, const Vec3& off_hpr) sgCoord c=Coord(xyz, hpr).toSgCoord(); #ifdef HAVE_IRRLICHT m_root->setPosition(xyz.toIrrVector()); + hpr*=180.0f/3.14159f; + core::vector3df f(hpr.getZ(), -hpr.getX(), hpr.getY()); + m_root->setRotation(f); #else m_model_transform->setTransform(&c); #endif