Guest User

Markus Mayer

a guest
May 29th, 2010
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. //##########################################################################################################################
  2. // OrientationHelperSceneNode.h file
  3. //##########################################################################################################################
  4.  
  5. /**
  6.  * Orientation Helper Scene Node for Irrlicht
  7.  * (c) 2010, Markus Mayer <https://launchpad.net/~sunside>
  8.  */
  9.  
  10. /* This program is free software. It comes without any warranty, to
  11.  * the extent permitted by applicable law. You can redistribute it
  12.  * and/or modify it under the terms of the Creative Commons
  13.  * Attribution 3.0 Unported license.
  14.  * See http://creativecommons.org/licenses/by/3.0/ for more details. */
  15.  
  16. #include <Irrlicht.h>
  17. using namespace irr;
  18.  
  19. class OrientationHelperSceneNode : public scene::ISceneNode
  20. {
  21. public:
  22.     OrientationHelperSceneNode(f32 size, scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id);
  23.     virtual ~OrientationHelperSceneNode(void) {}
  24.     virtual void render();
  25.     inline virtual const core::aabbox3d<f32>& getBoundingBox() const { return boundingBox; }
  26.        
  27.     inline virtual void OnRegisterSceneNode() {
  28.         if (!isVisible()) return;
  29.  
  30.         SceneManager->registerNodeForRendering(this);
  31.         ISceneNode::OnRegisterSceneNode();
  32.     }
  33.  
  34.     //! Rotates the node, so that it's Z direction matches the given vector
  35.     /**
  36.     * @direction        The direction vector
  37.     * @worldUp      The world up vector
  38.     */
  39.     void rotateZToDirection(const core::vector3df &direction, const core::vector3df &worldUp);
  40.  
  41.     //! Rotates the node, so that it's Z direction matches the given vector
  42.     /**
  43.     * @direction        The camera to take the target direction vector from
  44.     * @worldUp      The world up vector
  45.     */
  46.     inline void rotateZToDirection(const scene::ICameraSceneNode *camera, const core::vector3df &worldUp) {
  47.         core::vector3df direction = (camera->getTarget() - camera->getPosition()).normalize();
  48.         rotateZToDirection(direction, worldUp);
  49.     }
  50.  
  51.     //! Rotates the node, so that it's Z direction matches the given vector
  52.     /**
  53.     * @direction        The camera to take the target direction and world up vector from
  54.     */
  55.     inline void rotateZToDirection(const scene::ICameraSceneNode *camera) {
  56.         core::vector3df worldUp = camera->getUpVector();
  57.         rotateZToDirection(camera, worldUp);
  58.     }
  59.  
  60. private:
  61.  
  62.     core::aabbox3d<f32> boundingBox;
  63.     f32 size;
  64.     video::SMaterial boxMaterial;
  65. };
  66.  
  67. //##########################################################################################################################
  68. // OrientationHelperSceneNode.c file
  69. //##########################################################################################################################
  70.  
  71.  
  72. /**
  73.  * Orientation Helper Scene Node for Irrlicht
  74.  * (c) 2010, Markus Mayer <https://launchpad.net/~sunside>
  75.  */
  76.  
  77. /* This program is free software. It comes without any warranty, to
  78.  * the extent permitted by applicable law. You can redistribute it
  79.  * and/or modify it under the terms of the Creative Commons
  80.  * Attribution 3.0 Unported license.
  81.  * See http://creativecommons.org/licenses/by/3.0/ for more details. */
  82.  
  83. #include "OrientationHelperSceneNode.h"
  84.  
  85. OrientationHelperSceneNode::OrientationHelperSceneNode(f32 helperSize, scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id)
  86.     : scene::ISceneNode(parent, mgr, id), size(helperSize)
  87. {
  88.     boundingBox = core::aabbox3d<f32>(-size*0.5f, -size*0.5f, -size*0.5f, size*1.5f, size*1.5f, size*1.5f);
  89.  
  90.     // Materialien erzeugen
  91.     boxMaterial.Lighting = false;
  92.     boxMaterial.Thickness = 2.0f;
  93.     boxMaterial.Wireframe = true;
  94.     boxMaterial.FrontfaceCulling = false;
  95.     boxMaterial.BackfaceCulling = false;
  96. }
  97.  
  98. //! Renders the node
  99. void OrientationHelperSceneNode::render() {
  100.        
  101.     // Prepare rendering
  102.     video::IVideoDriver* driver = SceneManager->getVideoDriver();
  103.     driver->setMaterial(boxMaterial);
  104.     driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  105.        
  106.     // Calculate lengths
  107.     f32 halfSize = size*0.5f;
  108.     f32 lineLength = 1.5f*size;
  109.     f32 arrowOffset = -0.125f*size;
  110.     f32 offsetLine = lineLength - 0.25f*size;
  111.  
  112.     // Create and draw box
  113.     core::vector3df zero = vector3df(0, 0, 0);
  114.     core::aabbox3d<f32> box(-halfSize, -halfSize, -halfSize, halfSize, halfSize, halfSize);
  115.     driver->draw3DBox(box);
  116.  
  117.     // X arrow (red)
  118.     video::SColor color = video::SColor(255, 255, 0, 0);
  119.     driver->draw3DLine(zero, vector3df(lineLength, 0, 0), color);
  120.  
  121.     core::triangle3df triangle(
  122.         core::vector3df(offsetLine, -arrowOffset, 0),
  123.         core::vector3df(offsetLine, arrowOffset, 0),
  124.         core::vector3df(lineLength, 0, 0));
  125.     driver->draw3DTriangle(triangle, color);
  126.  
  127.     triangle = core::triangle3df(
  128.         core::vector3df(offsetLine, 0, -arrowOffset),
  129.         core::vector3df(offsetLine, 0, arrowOffset),
  130.         core::vector3df(lineLength, 0, 0));
  131.     driver->draw3DTriangle(triangle, color);
  132.  
  133.     // Y arrow (green)
  134.     color = video::SColor(255, 0, 255, 0);
  135.     driver->draw3DLine(zero, vector3df(0, lineLength, 0), color);
  136.  
  137.     triangle = core::triangle3df(
  138.         core::vector3df(-arrowOffset, offsetLine, 0),
  139.         core::vector3df(arrowOffset, offsetLine, 0),
  140.         core::vector3df(0, lineLength, 0));
  141.     driver->draw3DTriangle(triangle, color);
  142.  
  143.     triangle = core::triangle3df(
  144.         core::vector3df(0, offsetLine, -arrowOffset),
  145.         core::vector3df(0, offsetLine, arrowOffset),
  146.         core::vector3df(0, lineLength, 0));
  147.     driver->draw3DTriangle(triangle, color);
  148.  
  149.     // Z arrow (blue)
  150.     color = video::SColor(255, 0, 0, 255);
  151.     driver->draw3DLine(zero, vector3df(0, 0, lineLength), color);
  152.  
  153.     triangle = core::triangle3df(
  154.         core::vector3df(-arrowOffset, 0, offsetLine),
  155.         core::vector3df(arrowOffset, 0, offsetLine),
  156.         core::vector3df(0, 0, lineLength));
  157.     driver->draw3DTriangle(triangle, color);
  158.        
  159.     triangle = core::triangle3df(
  160.         core::vector3df(0, -arrowOffset, offsetLine),
  161.         core::vector3df(0, arrowOffset, offsetLine),
  162.         core::vector3df(0, 0, lineLength));
  163.     driver->draw3DTriangle(triangle, color);
  164. }
  165.  
  166. //! Rotates the element
  167. void OrientationHelperSceneNode::rotateZToDirection(const core::vector3df &direction, const core::vector3df &worldUp) {
  168.     core::vector3df right = worldUp.crossProduct(direction).normalize();
  169.     core::vector3df up = direction.crossProduct(right).normalize();
  170.  
  171.     // Create aim-at matrix from look-at matrix
  172.     core::matrix4 mLookAt, m;
  173.     mLookAt.buildCameraLookAtMatrixLH(core::vector3df(), direction, worldUp);
  174.     mLookAt.getInverse(m);
  175.  
  176.     // Set the rotation
  177.     core::vector3df rotation = m.getRotationDegrees();
  178.     setRotation(rotation);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment