From 02d1141bb7904919e403991e8bf21ca347bad4bc Mon Sep 17 00:00:00 2001 From: hikerstk Date: Mon, 14 May 2012 22:26:41 +0000 Subject: [PATCH] Added class to show an arbitrary 3d curve in a race, and used this class to display the curve of a cannon (when using --track-debug). git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@11235 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- sources.cmake | 28 +++---- src/graphics/show_curve.cpp | 125 ++++++++++++++++++++++++++++++++ src/graphics/show_curve.hpp | 66 +++++++++++++++++ src/ide/vc9/supertuxkart.vcproj | 12 +++ src/tracks/check_cannon.cpp | 14 ++++ src/tracks/check_cannon.hpp | 7 ++ 6 files changed, 239 insertions(+), 13 deletions(-) create mode 100644 src/graphics/show_curve.cpp create mode 100644 src/graphics/show_curve.hpp diff --git a/sources.cmake b/sources.cmake index d104ee938..494082265 100644 --- a/sources.cmake +++ b/sources.cmake @@ -43,6 +43,7 @@ src/graphics/post_processing.cpp src/graphics/rain.cpp src/graphics/referee.cpp src/graphics/shadow.cpp +src/graphics/show_curve.cpp src/graphics/skid_marks.cpp src/graphics/slip_stream.cpp src/graphics/stars.cpp @@ -93,8 +94,8 @@ src/items/projectile_manager.cpp src/items/rubber_ball.cpp src/items/rubber_band.cpp src/items/swatter.cpp -src/karts/abstract_kart_animation.cpp src/karts/abstract_kart.cpp +src/karts/abstract_kart_animation.cpp src/karts/cannon_animation.cpp src/karts/controller/ai_base_controller.cpp src/karts/controller/controller.cpp @@ -182,13 +183,13 @@ src/states_screens/kart_selection.cpp src/states_screens/main_menu_screen.cpp src/states_screens/minimal_race_gui.cpp src/states_screens/options_screen_audio.cpp -src/states_screens/options_screen_input2.cpp src/states_screens/options_screen_input.cpp +src/states_screens/options_screen_input2.cpp src/states_screens/options_screen_players.cpp src/states_screens/options_screen_ui.cpp src/states_screens/options_screen_video.cpp -src/states_screens/race_gui_base.cpp src/states_screens/race_gui.cpp +src/states_screens/race_gui_base.cpp src/states_screens/race_gui_overworld.cpp src/states_screens/race_result_gui.cpp src/states_screens/race_setup_screen.cpp @@ -249,8 +250,8 @@ src/animations/billboard_animation.hpp src/animations/ipo.hpp src/animations/three_d_animation.hpp src/audio/dummy_sfx.hpp -src/audio/music_dummy.hpp src/audio/music.hpp +src/audio/music_dummy.hpp src/audio/music_information.hpp src/audio/music_manager.hpp src/audio/music_ogg.hpp @@ -258,8 +259,8 @@ src/audio/sfx_base.hpp src/audio/sfx_buffer.hpp src/audio/sfx_manager.hpp src/audio/sfx_openal.hpp -src/challenges/challenge_data.hpp src/challenges/challenge.hpp +src/challenges/challenge_data.hpp src/challenges/game_slot.hpp src/challenges/unlock_manager.hpp src/config/device_config.hpp @@ -286,6 +287,7 @@ src/graphics/post_processing.hpp src/graphics/rain.hpp src/graphics/referee.hpp src/graphics/shadow.hpp +src/graphics/show_curve.hpp src/graphics/skid_marks.hpp src/graphics/slip_stream.hpp src/graphics/stars.hpp @@ -299,11 +301,11 @@ src/guiengine/scalable_font.hpp src/guiengine/screen.hpp src/guiengine/skin.hpp src/guiengine/widget.hpp +src/guiengine/widgets.hpp src/guiengine/widgets/bubble_widget.hpp src/guiengine/widgets/button_widget.hpp src/guiengine/widgets/check_box_widget.hpp src/guiengine/widgets/dynamic_ribbon_widget.hpp -src/guiengine/widgets.hpp src/guiengine/widgets/icon_button_widget.hpp src/guiengine/widgets/label_widget.hpp src/guiengine/widgets/list_widget.hpp @@ -314,8 +316,8 @@ src/guiengine/widgets/spinner_widget.hpp src/guiengine/widgets/text_box_widget.hpp src/input/binding.hpp src/input/device_manager.hpp -src/input/input_device.hpp src/input/input.hpp +src/input/input_device.hpp src/input/input_manager.hpp src/input/wiimote_manager.hpp src/io/file_manager.hpp @@ -336,8 +338,8 @@ src/items/projectile_manager.hpp src/items/rubber_ball.hpp src/items/rubber_band.hpp src/items/swatter.hpp -src/karts/abstract_kart_animation.hpp src/karts/abstract_kart.hpp +src/karts/abstract_kart_animation.hpp src/karts/cannon_animation.hpp src/karts/controller/ai_base_controller.hpp src/karts/controller/controller.hpp @@ -348,8 +350,8 @@ src/karts/controller/new_ai_controller.hpp src/karts/controller/player_controller.hpp src/karts/explosion_animation.hpp src/karts/ghost_kart.hpp -src/karts/kart_gfx.hpp src/karts/kart.hpp +src/karts/kart_gfx.hpp src/karts/kart_model.hpp src/karts/kart_properties.hpp src/karts/kart_properties_manager.hpp @@ -438,13 +440,13 @@ src/states_screens/kart_selection.hpp src/states_screens/main_menu_screen.hpp src/states_screens/minimal_race_gui.hpp src/states_screens/options_screen_audio.hpp -src/states_screens/options_screen_input2.hpp src/states_screens/options_screen_input.hpp +src/states_screens/options_screen_input2.hpp src/states_screens/options_screen_players.hpp src/states_screens/options_screen_ui.hpp src/states_screens/options_screen_video.hpp -src/states_screens/race_gui_base.hpp src/states_screens/race_gui.hpp +src/states_screens/race_gui_base.hpp src/states_screens/race_gui_overworld.hpp src/states_screens/race_result_gui.hpp src/states_screens/race_setup_screen.hpp @@ -473,8 +475,8 @@ src/tracks/check_sphere.hpp src/tracks/check_structure.hpp src/tracks/graph_node.hpp src/tracks/lod_node_loader.hpp -src/tracks/quad_graph.hpp src/tracks/quad.hpp +src/tracks/quad_graph.hpp src/tracks/quad_set.hpp src/tracks/terrain_info.hpp src/tracks/track.hpp @@ -482,8 +484,8 @@ src/tracks/track_manager.hpp src/tracks/track_object.hpp src/tracks/track_object_manager.hpp src/tracks/track_sector.hpp -src/tutorial/tutorial_data.hpp src/tutorial/tutorial.hpp +src/tutorial/tutorial_data.hpp src/tutorial/tutorial_manager.hpp src/utils/aligned_array.hpp src/utils/constants.hpp diff --git a/src/graphics/show_curve.cpp b/src/graphics/show_curve.cpp new file mode 100644 index 000000000..e8ee791d9 --- /dev/null +++ b/src/graphics/show_curve.cpp @@ -0,0 +1,125 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2012 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 "graphics/show_curve.hpp" + +#include "graphics/irr_driver.hpp" +#include "utils/vec3.hpp" + +#include +#include + + +/** ShowCurve constructor. It just creates an empty scene node. + */ +ShowCurve::ShowCurve(float width, float height) + : m_width(width), m_height(height) +{ + video::SColor color(77, 0, 179, 0); + video::SMaterial m; + m.AmbientColor = color; + m.DiffuseColor = color; + m.EmissiveColor = color; + m.BackfaceCulling = false; + m_mesh = irr_driver->createQuadMesh(&m, + /*create_one_quad*/ false); + m_buffer = m_mesh->getMeshBuffer(0); + assert(m_buffer->getVertexType()==video::EVT_STANDARD); + m_scene_node = irr_driver->addMesh(m_mesh); + // After addMesh ref count is 1 (for the attachment to the + // scene). We keep a copy here, so increase the ref count. + m_scene_node->grab(); +#ifdef DEBUG + std::string debug_name = " (show-curve)"; + m_scene_node->setName(debug_name.c_str()); +#endif + +} // ShowCurve + +// ---------------------------------------------------------------------------- +/** The destructor removes this scene node and frees the mesh. */ +ShowCurve::~ShowCurve() +{ + m_mesh->drop(); + m_scene_node->drop(); + irr_driver->removeNode(m_scene_node); +} // ShowCurve + +// ---------------------------------------------------------------------------- +void ShowCurve::addPoint(const Vec3 &pnt) +{ + // Check (again) that buffer is indeed the right type, otherwise the + // cast to vertices would be incorrect. + assert(m_buffer->getVertexType()==video::EVT_STANDARD); + video::S3DVertex vertices[4]; + + // 1. Append the four new vertices + // ------------------------------- + const float w2 = 0.5f*m_width, h2 = 0.5f*m_height; + vertices[0].Pos = Vec3(pnt + Vec3(-w2, -h2, 0)).toIrrVector(); + vertices[1].Pos = Vec3(pnt + Vec3( w2, -h2, 0)).toIrrVector(); + vertices[2].Pos = Vec3(pnt + Vec3( w2, h2, 0)).toIrrVector(); + vertices[3].Pos = Vec3(pnt + Vec3(-w2, h2, 0)).toIrrVector(); + vertices[0].Normal = core::vector3df(-w2, -h2, 0).normalize(); + vertices[1].Normal = core::vector3df( w2, -h2, 0).normalize(); + vertices[2].Normal = core::vector3df( w2, h2, 0).normalize(); + vertices[3].Normal = core::vector3df(-w2, h2, 0).normalize(); + + // 2. Handle the first point, which does not define any triangles. + // ---------------------------------------------------------------- + + // The first point does not create any triangles. + unsigned int n = m_buffer->getVertexCount(); + if(n==0) + { + m_buffer->append(vertices, 4, NULL, 0); + return; + } + + // 3. Connect to previous indices + // ------------------------------ + // Appending the vertices to an existing mesh buffer is complicated by the + // fact that irrlicht adjust the indices by the number of current vertices + // (which means that the data added can not use any previous vertex). + // So we first add empty indices (so that irrlicht reallocates the + // index data), then we set the actual index data. + + // Create space for the indices: we need 2 triangles for each of the + // 4 sides of the 'tunnel', so all in all 24 indices. + irr::u16 *indices = new irr::u16[24]; + m_buffer->append(vertices, 4, indices, 24); + delete indices; + indices = m_buffer->getIndices(); + + // index = first newly added index + unsigned int index = m_buffer->getIndexCount()-24; + for(unsigned int i=0; i<4; i++) + { + indices[index ] = n-4 + i; + indices[index+1] = n + i; + indices[index+2] = n + (i+1)%4; + indices[index+3] = n-4 + i; + indices[index+4] = n + (i+1)%4; + indices[index+5] = n-4 + (i+1)%4; + index += 6; + } + m_buffer->recalculateBoundingBox(); + m_mesh->setBoundingBox(m_buffer->getBoundingBox()); +} // addPoint + +// ---------------------------------------------------------------------------- diff --git a/src/graphics/show_curve.hpp b/src/graphics/show_curve.hpp new file mode 100644 index 000000000..2f12e83a7 --- /dev/null +++ b/src/graphics/show_curve.hpp @@ -0,0 +1,66 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2012 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. + +#ifndef HEADER_SHOW_CURVE_HPP +#define HEADER_SHOW_CURVE_HPP + +#include "utils/leak_check.hpp" +#include "utils/no_copy.hpp" + +class Vec3; + +namespace irr +{ + namespace scene { class IMesh; class IMeshSceneNode; class IMeshBuffer; } +} +using namespace irr; + +/** This class is used for debugging. It allows to show an arbitrary curve + * in the race. The curve is shown as a 'tunnel', i.e. in the constructor + * you can specify the width and height of this tunnel. Each point then + * adds 4 vertices: point + (+- width/2, +- height2, 0). That's not + * exact (if the curve is not parallel to the z axis), but good enough for + * debugging. + * \ingroup graphics + */ +class ShowCurve : public NoCopy +{ + LEAK_CHECK(); +private: + + /** The actual scene node. */ + scene::IMeshSceneNode *m_scene_node; + + /** The mesh of the curve. */ + scene::IMesh *m_mesh; + + /** The mesh buffer containing the actual vertices of the curve. */ + scene::IMeshBuffer *m_buffer; + + /** The width of the graph when it is displayed. */ + float m_width; + + /** The height of the graph when it is displayed. */ + float m_height; +public: + ShowCurve(float width, float height); + ~ShowCurve(); + void addPoint(const Vec3 &pnt); + void update(float dt); +}; // ShowCurve +#endif diff --git a/src/ide/vc9/supertuxkart.vcproj b/src/ide/vc9/supertuxkart.vcproj index d370fd4ab..0097ef9d2 100644 --- a/src/ide/vc9/supertuxkart.vcproj +++ b/src/ide/vc9/supertuxkart.vcproj @@ -1004,6 +1004,10 @@ RelativePath="..\..\graphics\shadow.cpp" > + + @@ -1746,6 +1750,10 @@ RelativePath="..\..\utils\constants.hpp" > + + @@ -2186,6 +2194,10 @@ RelativePath="..\..\graphics\shadow.hpp" > + + diff --git a/src/tracks/check_cannon.cpp b/src/tracks/check_cannon.cpp index 8b13624de..29c1a856e 100644 --- a/src/tracks/check_cannon.cpp +++ b/src/tracks/check_cannon.cpp @@ -20,6 +20,7 @@ #include "animations/animation_base.hpp" #include "animations/ipo.hpp" +#include "graphics/show_curve.hpp" #include "io/xml_node.hpp" #include "karts/abstract_kart.hpp" #include "karts/cannon_animation.hpp" @@ -44,6 +45,15 @@ CheckCannon::CheckCannon(const XMLNode &node, unsigned int index) m_curve = new Ipo(*(node.getNode("curve")), /*fps*/25, /*reverse*/race_manager->getReverseTrack()); +#ifdef DEBUG + if(UserConfigParams::m_track_debug) + { + m_show_curve = new ShowCurve(0.5f, 0.5f); + const std::vector &p = m_curve->getPoints(); + for(unsigned int i=0; iaddPoint(p[i]); + } +#endif } // CheckCannon // ---------------------------------------------------------------------------- @@ -53,6 +63,10 @@ CheckCannon::CheckCannon(const XMLNode &node, unsigned int index) CheckCannon::~CheckCannon() { delete m_curve; +#ifdef DEBUG + if(UserConfigParams::m_track_debug) + delete m_show_curve; +#endif } // ~CheckCannon // ---------------------------------------------------------------------------- diff --git a/src/tracks/check_cannon.hpp b/src/tracks/check_cannon.hpp index ee6e2845a..7726763a1 100644 --- a/src/tracks/check_cannon.hpp +++ b/src/tracks/check_cannon.hpp @@ -24,6 +24,7 @@ class CheckManager; class Ipo; +class ShowCurve; class XMLNode; /** @@ -41,6 +42,12 @@ private: /** Stores the cannon curve data. */ Ipo *m_curve; +#ifdef DEBUG + /** If track debugging is enabled, this will show the the curve of + * the cannon in the race. */ + ShowCurve * m_show_curve; +#endif + public: CheckCannon(const XMLNode &node, unsigned int index); virtual ~CheckCannon();