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 
43  {
44  }
45 
46  virtual ~GlutRenderObject();
47 
49  virtual void draw() = 0;
50 };
51 
54 {
60  double halfSize;
63 
74  {
75  }
76 
78  virtual void draw();
79 };
80 
84 {
86  double length;
88  float width;
89 
94  {
95  }
96 
98  virtual void draw();
99 };
100 
103 {
105  double radius;
108 
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
GlutRenderer::display
static void display()
Glut display function which handles the drawing of the scene.
Definition: GlutRenderer.cpp:248
GlutRenderObject::draw
virtual void draw()=0
Pure virtual draw method for subclasses to define how to draw themselves with Glut.
GlutImage::GlutImage
GlutImage(const Eigen::AlignedBox< double, 2 > &bounds)
Constuctor.
Definition: GlutRenderer.h:135
Vector.h
Definitions of small fixed-size vector types.
GlutImage::ImageType
SurgSim::DataStructures::Image< float > ImageType
Definition: GlutRenderer.h:128
GlutCamera::zNear
double zNear
Near clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:182
LockedContainer.h
GlutCamera::up
SurgSim::Math::Vector3d up
Up direction.
Definition: GlutRenderer.h:178
GlutSquare::planeDirectionX
SurgSim::Math::Vector3d planeDirectionX
The unit direction along one of the pairs edges of the square.
Definition: GlutRenderer.h:56
SurgSim::Math::RigidTransform3d
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
GlutCamera::GlutCamera
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
GlutRenderer::run
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
GlutAxes
Axes with center at local origin, red axis along the local X-axis, green axis along the local Y-axis,...
Definition: GlutRenderer.h:84
GlutRenderer::initialize
static void initialize()
Initializes the Glut window.
Definition: GlutRenderer.cpp:223
GlutGroup::draw
virtual void draw()
Draws the group with Glut and iterates through its children to draw them.
Definition: GlutRenderer.cpp:207
GlutGroup::children
std::vector< std::shared_ptr< GlutRenderObject > > children
Children of this group.
Definition: GlutRenderer.h:159
GlutRenderObject
Abstract definition of an object that can render itself with Glut.
Definition: GlutRenderer.h:37
GlutAxes::length
double length
Length of each axis, in meters.
Definition: GlutRenderer.h:86
SurgSim::Math::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
SurgSim
Definition: CompoundShapeToGraphics.cpp:30
GlutImage
An Image drawn to the screen.
Definition: GlutRenderer.h:127
GlutImage::m_bounds
Eigen::AlignedBox< double, 2 > m_bounds
Window coordinates to draw the image.
Definition: GlutRenderer.h:146
GlutSphere::radius
double radius
Radius of the sphere, in meters.
Definition: GlutRenderer.h:105
SurgSim::Framework::LockedContainer
A simple thread-safe data container that can support multiple writers and readers.
Definition: LockedContainer.h:55
GlutGroup::GlutGroup
GlutGroup()
Constructor. The group is initialized with no children.
Definition: GlutRenderer.h:162
GlutSphere::quadratic
GLUquadric * quadratic
GLU quadric object for the quadric operations required to build the sphere.
Definition: GlutRenderer.h:122
GlutSquare::halfSize
double halfSize
One half of the edge length of the square, in meters.
Definition: GlutRenderer.h:60
GlutCamera::fovY
double fovY
Field of view angle (in degrees) in the vertical direction.
Definition: GlutRenderer.h:180
GlutSphere::color
SurgSim::Math::Vector3d color
Color of the sphere.
Definition: GlutRenderer.h:107
GlutAxes::width
float width
Width of each axis, in pixels.
Definition: GlutRenderer.h:88
GlutImage::image
SurgSim::Framework::LockedContainer< ImageType > image
The image to draw.
Definition: GlutRenderer.h:131
GlutRenderer
Simple static class renderer built on Glut.
Definition: GlutRenderer.h:207
GlutSquare::draw
virtual void draw()
Draws the square with Glut.
Definition: GlutRenderer.cpp:36
RigidTransform.h
Definitions of 2x2 and 3x3 rigid (isometric) transforms.
Image.h
GlutRenderObject::pose
SurgSim::Math::RigidTransform3d pose
Pose (rotation and translation) of the object.
Definition: GlutRenderer.h:39
GlutCamera::zFar
double zFar
Far clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:184
GlutRenderer::drawObjects
static void drawObjects()
Iterates through the scene objects to draw them.
Definition: GlutRenderer.h:256
GlutRenderer::m_width
static int m_width
Width of the window.
Definition: GlutRenderer.h:232
GlutImage::m_texture
unsigned int m_texture
Texture used for holding the image.
Definition: GlutRenderer.h:149
GlutSquare::color
SurgSim::Math::Vector3d color
Color of the square.
Definition: GlutRenderer.h:62
GlutSquare::GlutSquare
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
GlutRenderer::m_objects
static std::vector< std::shared_ptr< GlutRenderObject > > m_objects
Objects in the scene.
Definition: GlutRenderer.h:239
GlutSphere::draw
virtual void draw()
Draws the sphere with Glut.
Definition: GlutRenderer.cpp:109
GlutImage::draw
virtual void draw()
Draws the image with Glut.
Definition: GlutRenderer.cpp:127
GlutGroup
Group of objects which provides a transform hierarchy.
Definition: GlutRenderer.h:157
GlutSquare::planeDirectionY
SurgSim::Math::Vector3d planeDirectionY
The unit direction along the other pair of edges of the square.
Definition: GlutRenderer.h:58
GlutRenderObject::~GlutRenderObject
virtual ~GlutRenderObject()
Definition: GlutRenderer.cpp:32
GlutImage::m_firstRun
bool m_firstRun
Is this the fist run of draw.
Definition: GlutRenderer.h:152
GlutCamera
Camera which controls the view of the scene.
Definition: GlutRenderer.h:172
GlutCamera::eye
SurgSim::Math::Vector3d eye
Eye position.
Definition: GlutRenderer.h:174
GlutRenderer::setCamera
static void setCamera(std::shared_ptr< GlutCamera > camera)
Sets the camera used to control the view of the scene.
Definition: GlutRenderer.h:218
GlutAxes::draw
virtual void draw()
Draws the axes with Glut.
Definition: GlutRenderer.cpp:76
GlutRenderer::m_height
static int m_height
Height of the window.
Definition: GlutRenderer.h:234
GlutCamera::center
SurgSim::Math::Vector3d center
Center (look at) position.
Definition: GlutRenderer.h:176
GlutRenderer::m_camera
static std::shared_ptr< GlutCamera > m_camera
Camera which controls the view of the scene.
Definition: GlutRenderer.h:237
GlutSphere
Sphere with center at local origin.
Definition: GlutRenderer.h:103
GlutSphere::GlutSphere
GlutSphere(double radius, const SurgSim::Math::Vector3d &color)
Constructor.
Definition: GlutRenderer.h:112
GlutRenderObject::GlutRenderObject
GlutRenderObject()
Constructor initializes pose as identity (no rotation or translation)
Definition: GlutRenderer.h:42
GlutRenderer::addObject
static void addObject(std::shared_ptr< GlutRenderObject > object)
Adds an object to the scene.
Definition: GlutRenderer.h:225
GlutAxes::GlutAxes
GlutAxes(double length, float width)
Constructor.
Definition: GlutRenderer.h:93
SurgSim::DataStructures::Image
A templated Image class.
Definition: Image.h:34
GlutRenderer::reshape
static void reshape(GLint width, GLint height)
Glut reshape function which handles the resizing of the window.
Definition: GlutRenderer.h:245
GlutSquare
Square with center at local origin.
Definition: GlutRenderer.h:54