GlutRenderer.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_TESTING_VISUALTESTCOMMON_GLUTRENDERER_H
17 #define SURGSIM_TESTING_VISUALTESTCOMMON_GLUTRENDERER_H
18 
19 #include <Eigen/Geometry>
20 #include <memory>
21 #include <vector>
22 
23 #ifdef __APPLE__
24 #include <GLUT/glut.h>
25 #else
26 #define GLUT_NO_LIB_PRAGMA 1
27 #include <GL/glut.h>
28 #endif
29 
32 #include "SurgSim/Math/Vector.h"
34 
37 {
40 
42  GlutRenderObject() : pose(SurgSim::Math::RigidTransform3d::Identity())
43  {
44  }
45 
46  virtual ~GlutRenderObject();
47 
49  virtual void draw() = 0;
50 };
51 
54 {
60  double halfSize;
63 
69  GlutSquare(double halfSize, const SurgSim::Math::Vector3d& color,
70  const SurgSim::Math::Vector3d& planeDirectionX = SurgSim::Math::Vector3d(1.0, 0.0, 0.0),
71  const SurgSim::Math::Vector3d& planeDirectionY = SurgSim::Math::Vector3d(0.0, 1.0, 0.0)) :
72  GlutRenderObject(), planeDirectionX(planeDirectionX), planeDirectionY(planeDirectionY),
73  halfSize(halfSize), color(color)
74  {
75  }
76 
78  virtual void draw();
79 };
80 
84 {
86  double length;
88  float width;
89 
93  GlutAxes(double length, float width) : GlutRenderObject(), length(length), width(width)
94  {
95  }
96 
98  virtual void draw();
99 };
100 
103 {
105  double radius;
108 
112  GlutSphere(double radius, const SurgSim::Math::Vector3d& color) :
113  radius(radius), color(color), quadratic(gluNewQuadric())
114  {
115  }
116 
118  virtual void draw();
119 
120 private:
122  GLUquadric* quadratic;
123 };
124 
127 {
129 
132 
135  explicit GlutImage(const Eigen::AlignedBox<double, 2>& bounds) :
136  m_bounds(bounds),
137  m_firstRun(true)
138  {
139  }
140 
142  virtual void draw();
143 
144 private:
146  Eigen::AlignedBox<double, 2> m_bounds;
147 
149  unsigned int m_texture;
150 
153 };
154 
157 {
159  std::vector< std::shared_ptr<GlutRenderObject> > children;
160 
163  {
164  }
165 
167  virtual void draw();
168 };
169 
172 {
180  double fovY;
182  double zNear;
184  double zFar;
185 
194  const SurgSim::Math::Vector3d& up_, const double fovY_, double zNear_, double zFar_) :
195  eye(eye_),
196  center(center_),
197  up(up_),
198  fovY(fovY_),
199  zNear(zNear_),
200  zFar(zFar_)
201  {
202  }
203 };
204 
207 {
208 public:
210  static void run()
211  {
212  initialize();
213  glutMainLoop();
214  }
215 
218  static void setCamera(std::shared_ptr<GlutCamera> camera)
219  {
220  m_camera = camera;
221  }
222 
225  static void addObject(std::shared_ptr<GlutRenderObject> object)
226  {
227  m_objects.push_back(object);
228  }
229 
230 private:
232  static int m_width;
234  static int m_height;
235 
237  static std::shared_ptr<GlutCamera> m_camera;
239  static std::vector< std::shared_ptr<GlutRenderObject> > m_objects;
240 
242  static void initialize();
243 
245  static void reshape(GLint width, GLint height)
246  {
247  m_width = width;
248  m_height = height;
249  glViewport(0, 0, m_width, m_height);
250  }
251 
253  static void display();
254 
256  static void drawObjects()
257  {
258  for (auto it = m_objects.cbegin(); it != m_objects.cend(); ++it)
259  {
260  (*it)->draw();
261  }
262  }
263 };
264 
265 
266 #endif // SURGSIM_TESTING_VISUALTESTCOMMON_GLUTRENDERER_H
float width
Width of each axis, in pixels.
Definition: GlutRenderer.h:88
virtual void draw()=0
Pure virtual draw method for subclasses to define how to draw themselves with Glut.
Definition: CompoundShapeToGraphics.cpp:29
double length
Length of each axis, in meters.
Definition: GlutRenderer.h:86
A templated Image class.
Definition: Image.h:33
bool m_firstRun
Is this the fist run of draw.
Definition: GlutRenderer.h:152
Axes with center at local origin, red axis along the local X-axis, green axis along the local Y-axis...
Definition: GlutRenderer.h:83
Eigen::AlignedBox< double, 2 > m_bounds
Window coordinates to draw the image.
Definition: GlutRenderer.h:146
GlutGroup()
Constructor. The group is initialized with no children.
Definition: GlutRenderer.h:162
static std::vector< std::shared_ptr< GlutRenderObject > > m_objects
Objects in the scene.
Definition: GlutRenderer.h:239
double halfSize
One half of the edge length of the square, in meters.
Definition: GlutRenderer.h:60
double zNear
Near clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:182
GLUquadric * quadratic
GLU quadric object for the quadric operations required to build the sphere.
Definition: GlutRenderer.h:122
SurgSim::Math::Vector3d up
Up direction.
Definition: GlutRenderer.h:178
double radius
Radius of the sphere, in meters.
Definition: GlutRenderer.h:105
static void reshape(GLint width, GLint height)
Glut reshape function which handles the resizing of the window.
Definition: GlutRenderer.h:245
SurgSim::Math::Vector3d planeDirectionX
The unit direction along one of the pairs edges of the square.
Definition: GlutRenderer.h:56
GlutCamera(const SurgSim::Math::Vector3d &eye_, const SurgSim::Math::Vector3d &center_, const SurgSim::Math::Vector3d &up_, const double fovY_, double zNear_, double zFar_)
Constructor.
Definition: GlutRenderer.h:193
static int m_height
Height of the window.
Definition: GlutRenderer.h:234
double zFar
Far clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:184
GlutImage(const Eigen::AlignedBox< double, 2 > &bounds)
Constuctor.
Definition: GlutRenderer.h:135
Square with center at local origin.
Definition: GlutRenderer.h:53
An Image drawn to the screen.
Definition: GlutRenderer.h:126
static void drawObjects()
Iterates through the scene objects to draw them.
Definition: GlutRenderer.h:256
unsigned int m_texture
Texture used for holding the image.
Definition: GlutRenderer.h:149
double fovY
Field of view angle (in degrees) in the vertical direction.
Definition: GlutRenderer.h:180
Abstract definition of an object that can render itself with Glut.
Definition: GlutRenderer.h:36
Definitions of 2x2 and 3x3 rigid (isometric) transforms.
Simple static class renderer built on Glut.
Definition: GlutRenderer.h:206
SurgSim::Math::Vector3d color
Color of the sphere.
Definition: GlutRenderer.h:107
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
SurgSim::Framework::LockedContainer< ImageType > image
The image to draw.
Definition: GlutRenderer.h:131
Camera which controls the view of the scene.
Definition: GlutRenderer.h:171
SurgSim::Math::Vector3d eye
Eye position.
Definition: GlutRenderer.h:174
static void addObject(std::shared_ptr< GlutRenderObject > object)
Adds an object to the scene.
Definition: GlutRenderer.h:225
SurgSim::Math::Vector3d color
Color of the square.
Definition: GlutRenderer.h:62
Definitions of small fixed-size vector types.
virtual ~GlutRenderObject()
Definition: GlutRenderer.cpp:32
SurgSim::Math::RigidTransform3d pose
Pose (rotation and translation) of the object.
Definition: GlutRenderer.h:39
A simple thread-safe data container that can support multiple writers and readers.
Definition: LockedContainer.h:54
Sphere with center at local origin.
Definition: GlutRenderer.h:102
GlutSphere(double radius, const SurgSim::Math::Vector3d &color)
Constructor.
Definition: GlutRenderer.h:112
GlutRenderObject()
Constructor initializes pose as identity (no rotation or translation)
Definition: GlutRenderer.h:42
SurgSim::Math::Vector3d center
Center (look at) position.
Definition: GlutRenderer.h:176
SurgSim::Math::Vector3d planeDirectionY
The unit direction along the other pair of edges of the square.
Definition: GlutRenderer.h:58
GlutSquare(double halfSize, const SurgSim::Math::Vector3d &color, const SurgSim::Math::Vector3d &planeDirectionX=SurgSim::Math::Vector3d(1.0, 0.0, 0.0), const SurgSim::Math::Vector3d &planeDirectionY=SurgSim::Math::Vector3d(0.0, 1.0, 0.0))
Constructor.
Definition: GlutRenderer.h:69
GlutAxes(double length, float width)
Constructor.
Definition: GlutRenderer.h:93
static std::shared_ptr< GlutCamera > m_camera
Camera which controls the view of the scene.
Definition: GlutRenderer.h:237
std::vector< std::shared_ptr< GlutRenderObject > > children
Children of this group.
Definition: GlutRenderer.h:159
Group of objects which provides a transform hierarchy.
Definition: GlutRenderer.h:156
static void setCamera(std::shared_ptr< GlutCamera > camera)
Sets the camera used to control the view of the scene.
Definition: GlutRenderer.h:218
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
static void run()
Initializes and runs the Glut main loop. This function will block until the Glut graphics window is c...
Definition: GlutRenderer.h:210
static int m_width
Width of the window.
Definition: GlutRenderer.h:232
SurgSim::DataStructures::Image< float > ImageType
Definition: GlutRenderer.h:128