NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgSimViewGraphics.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 
25 // This module
26 #include "tgSimViewGraphics.h"
27 // This application
28 #include "tgBulletUtil.h"
29 #include "tgSimulation.h"
30 // Bullet OpenGL_FreeGlut (patched files)
31 #include "tgGLDebugDrawer.h"
32 // The Bullet Physics library
33 #include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
34 
36  double stepSize,
37  double renderRate) :
38  tgSimView(world, stepSize, renderRate)
39 {
41  gDebugDrawer = new tgGLDebugDrawer();
42 }
43 
44 tgSimViewGraphics::~tgSimViewGraphics()
45 {
46 #ifndef BT_NO_PROFILE
47  CProfileManager::Release_Iterator(m_profileIterator);
48 #endif //BT_NO_PROFILE
49  delete m_shootBoxShape;
50  delete m_shapeDrawer;
51 }
52 
54 {
55  // Just set tgSimView::m_initialized to true
56  tgSimView::setup();
57 
58  // Cache a pointer to the btSoftRigidDynamicsWorld
60  btSoftRigidDynamicsWorld& dynamicsWorld =
62  // Store a pointer to the btSoftRigidDynamicsWorld
63  // This class is not taking ownership of it
65  m_dynamicsWorld = &dynamicsWorld;
66 
67  // Give the pointer to demoapplication for rendering
68  dynamicsWorld.setDebugDrawer(gDebugDrawer);
69 
70  // @todo Valgrind thinks this is a leak. Perhaps its a GLUT issue?
71  m_pModelVisitor = new tgBulletRenderer(world);
72  std::cout << "setup graphics" << std::endl;
73 }
74 
75 void tgSimViewGraphics::teardown()
76 {
77  //tgWorld owns this pointer, so we shouldn't delete it
78  m_dynamicsWorld = 0;
79  tgSimView::teardown();
80 }
81 
82 void tgSimViewGraphics::render()
83 {
85  {
86 
87  glClear(GL_COLOR_BUFFER_BIT |
88  GL_DEPTH_BUFFER_BIT |
89  GL_STENCIL_BUFFER_BIT);
90 
91  m_pSimulation->onVisit(*m_pModelVisitor);
92 
93  //Freeglut code
94 #if (0)
95  clientMoveAndDisplay();
96  tgGlutMainEventLoop();
97 #endif
98  }
99 }
100 
101 void tgSimViewGraphics::run(int steps)
102 {
103  if (isInitialzed())
104  {
105  tgglutmain(1024, 600, "Tensegrity Demo", this);
106  const double dt = 1.0f/120.f;
107  glutMainLoop();
108 
109  /* Free glut code
110  // This would normally run forever, but this is just for testing
111  for(int i = 0; i < steps; i++) {
112  m_pSimulation->step(dt);
113 
114  if(i % 5 == 0) {
115  render();
116  }
117  if(getExitFlag())
118  {
119  break;
120  }
121  }
122  std::cout << "leaving loop" << std::endl;
123  glutLeaveMainLoop();
124  */
125  }
126 }
127 
128 // tgSimulation handles calling teardown and setup on this,
129 // since it knows when the new world is available
130 void tgSimViewGraphics::reset()
131 {
132  assert(isInitialzed());
133  m_pSimulation->reset();
134  assert(isInitialzed());
135 }
136 
137 void tgSimViewGraphics::clientMoveAndDisplay()
138 {
139  if (isInitialzed()){
142  if (m_renderTime >= m_renderRate)
143  {
144  render();
145  // Doesn't appear to do anything yet...
146  m_dynamicsWorld->debugDrawWorld();
147  renderme();
148  // Camera is updated in renderme
149  glFlush();
150  swapBuffers();
151  m_renderTime = 0;
152  }
153  }
154 }
155 
156 void tgSimViewGraphics::displayCallback()
157 {
158  if (isInitialzed())
159  {
160  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
161  renderme();
162  // optional but useful: debug drawing to detect problems
163  if (m_dynamicsWorld)
164  {
165  m_dynamicsWorld->debugDrawWorld();
166  }
167  glFlush();
168  swapBuffers();
169  }
170 }
171 
172 void tgSimViewGraphics::clientResetScene()
173 {
174  reset();
175  assert(isInitialzed());
176 
178  tgBulletUtil::worldToDynamicsWorld(world).setDebugDrawer(gDebugDrawer);
179 }
bool isInitialzed() const
Definition: tgSimView.h:145
double m_stepSize
Definition: tgSimView.h:169
double m_renderTime
Definition: tgSimView.h:181
tgModelVisitor * m_pModelVisitor
Definition: tgSimView.h:162
static btSoftRigidDynamicsWorld & worldToDynamicsWorld(tgWorld &world)
tgSimViewGraphics(tgWorld &world, double stepSize=1.0/120.0, double renderRate=1.0/60.0)
tgWorld & getWorld() const
Contains the definition of class tgSimulation $Id$.
Contains the definition of class tgSimViewGraphics $Id$.
double m_renderRate
Definition: tgSimView.h:175
Contains the definition of class tgBulletUtil.
tgSimulation * m_pSimulation
Definition: tgSimView.h:155
tgWorld & world()
Definition: tgSimView.h:64
void step(double dt) const