NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBulletRenderer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
26 // This module
27 #include "tgBulletRenderer.h"
28 // This application
29 #include "abstractMarker.h"
30 #include "tgSpringCable.h"
31 #include "tgSpringCableAnchor.h"
32 #include "tgBulletUtil.h"
33 #include "tgSpringCableActuator.h"
34 #include "tgWorld.h"
36 
37 #include "tgCast.h"
38 
39 #include "LinearMath/btQuickprof.h"
40 
41 // OpenGL_FreeGlut (patched Bullet)
42 #include "tgGLDebugDrawer.h"
43 // The Bullet Physics library
44 #include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
45 // The C++ Standard Library
46 #include <cassert>
47 
48 
49 tgBulletRenderer::tgBulletRenderer(const tgWorld& world) : m_world(world)
50 {
51 }
52 
53 void tgBulletRenderer::render(const tgRod& rod) const
54 {
55 #ifndef BT_NO_PROFILE
56  BT_PROFILE("tgBulletRenderer::renderRod");
57 #endif //BT_NO_PROFILE
58  // render the rod (change color, etc. if we want)
59 }
60 
62 {
63 #ifndef BT_NO_PROFILE
64  BT_PROFILE("tgBulletRenderer::renderString");
65 #endif //BT_NO_PROFILE
66  // Fetch the btDynamicsWorld
67  btDynamicsWorld& dynamicsWorld =
69 
70  btIDebugDraw* const pDrawer = dynamicsWorld.getDebugDrawer();
71 
72  const tgSpringCable* const pSpringCable = mSCA.getSpringCable();
73 
74  if(pDrawer && pSpringCable)
75  {
76  const std::vector<const tgSpringCableAnchor*>& anchors = pSpringCable->getAnchors();
77  std::size_t n = anchors.size() - 1;
78  for (std::size_t i = 0; i < n; i++)
79  {
80  const btVector3 lineFrom =
81  anchors[i]->getWorldPosition();
82  const btVector3 lineTo =
83  anchors[i+1]->getWorldPosition();
84  // Should this be normalized??
85  const double stretch =
86  mSCA.getCurrentLength() - mSCA.getRestLength();
87  const btVector3 color =
88  (stretch < 0.0) ?
89  btVector3(0.0, 0.0, 1.0) :
90  btVector3(0.5 + stretch / 3.0,
91  0.5 - stretch / 2.0,
92  0.0);
93  pDrawer->drawLine(lineFrom, lineTo, color);
94  }
95  }
96 }
97 
98 void tgBulletRenderer::render(const tgModel& model) const
99 {
100 #ifndef BT_NO_PROFILE
101  BT_PROFILE("tgBulletRenderer::renderModel");
102 #endif //BT_NO_PROFILE
103 
107  // Fetch the btDynamicsWorld
108  btDynamicsWorld& dynamicsWorld = tgBulletUtil::worldToDynamicsWorld(m_world);
109  btIDebugDraw* const idraw = dynamicsWorld.getDebugDrawer();
110  for(int j=0;j<model.getMarkers().size() ;j++)
111  {
112  abstractMarker mark = model.getMarkers()[j];
113  idraw->drawSphere(mark.getWorldPosition(),0.6,mark.getColor());
114  }
115 }
116 
Definitions of class tgSpringCable.
Contains the definition of class tgWorldBulletPhysicsImpl.
static btDynamicsWorld & worldToDynamicsWorld(const tgWorld &world)
virtual void render(const tgSpringCableActuator &mSCA) const
Utility class for class casting and filtering collections by type.
Markers for specific places on a tensegrity.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
tgBulletRenderer(const tgWorld &world)
Contains the definition of class tgWorld $Id$.
Contains the definition of class tgBulletUtil.
virtual const std::vector< const tgSpringCableAnchor * > getAnchors() const =0
virtual const double getRestLength() const
Definitions of class tgSpringCableAnchor.
virtual const double getCurrentLength() const
Contains the definition of concrete class tgBulletRenderer.
btVector3 getWorldPosition() const
Definition: tgRod.h:43
const tgSpringCable * getSpringCable() const