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