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 <vector>
20 #include <memory>
21 
22 #ifdef __APPLE__
23 #include <GLUT/glut.h>
24 #else
25 #include <GL/glut.h>
26 #endif
27 
28 #include "SurgSim/Math/Vector.h"
30 
33 {
36 
38  GlutRenderObject() : pose(SurgSim::Math::RigidTransform3d::Identity())
39  {
40  }
41 
42  virtual ~GlutRenderObject();
43 
45  virtual void draw() const = 0;
46 };
47 
50 {
56  double halfSize;
59 
65  GlutSquare(double halfSize, const SurgSim::Math::Vector3d& color,
66  const SurgSim::Math::Vector3d& planeDirectionX = SurgSim::Math::Vector3d(1.0, 0.0, 0.0),
67  const SurgSim::Math::Vector3d& planeDirectionY = SurgSim::Math::Vector3d(0.0, 1.0, 0.0)) :
68  GlutRenderObject(), planeDirectionX(planeDirectionX), planeDirectionY(planeDirectionY),
69  halfSize(halfSize), color(color)
70  {
71  }
72 
74  virtual void draw() const;
75 };
76 
80 {
82  double length;
84  float width;
85 
89  GlutAxes(double length, float width) : GlutRenderObject(), length(length), width(width)
90  {
91  }
92 
94  virtual void draw() const;
95 };
96 
99 {
101  double radius;
104 
108  GlutSphere(double radius, const SurgSim::Math::Vector3d& color) :
109  radius(radius), color(color), quadratic(gluNewQuadric())
110  {
111  }
112 
114  virtual void draw() const;
115 
116 private:
118  GLUquadric* quadratic;
119 };
120 
123 {
125  std::vector< std::shared_ptr<GlutRenderObject> > children;
126 
129  {
130  }
131 
133  virtual void draw() const;
134 };
135 
138 {
146  double fovY;
148  double zNear;
150  double zFar;
151 
160  const SurgSim::Math::Vector3d& up_, const double fovY_, double zNear_, double zFar_) :
161  eye(eye_),
162  center(center_),
163  up(up_),
164  fovY(fovY_),
165  zNear(zNear_),
166  zFar(zFar_)
167  {
168  }
169 };
170 
173 {
174 public:
176  static void run()
177  {
178  initialize();
179  glutMainLoop();
180  }
181 
184  static void setCamera(std::shared_ptr<GlutCamera> camera)
185  {
186  m_camera = camera;
187  }
188 
191  static void addObject(std::shared_ptr<GlutRenderObject> object)
192  {
193  m_objects.push_back(object);
194  }
195 
196 private:
198  static int m_width;
200  static int m_height;
201 
203  static std::shared_ptr<GlutCamera> m_camera;
205  static std::vector< std::shared_ptr<GlutRenderObject> > m_objects;
206 
208  static void initialize();
209 
211  static void reshape(GLint width, GLint height)
212  {
213  m_width = width;
214  m_height = height;
215  glViewport(0, 0, m_width, m_height);
216  }
217 
219  static void display();
220 
222  static void drawObjects()
223  {
224  for (auto it = m_objects.cbegin(); it != m_objects.cend(); ++it)
225  {
226  (*it)->draw();
227  }
228  }
229 };
230 
231 
232 #endif // SURGSIM_TESTING_VISUALTESTCOMMON_GLUTRENDERER_H
float width
Width of each axis, in pixels.
Definition: GlutRenderer.h:84
Definition: DriveElementFromInputBehavior.cpp:27
double length
Length of each axis, in meters.
Definition: GlutRenderer.h:82
Axes with center at local origin, red axis along the local X-axis, green axis along the local Y-axis...
Definition: GlutRenderer.h:79
virtual void draw() const =0
Pure virtual draw method for subclasses to define how to draw themselves with Glut.
GlutGroup()
Constructor. The group is initialized with no children.
Definition: GlutRenderer.h:128
static std::vector< std::shared_ptr< GlutRenderObject > > m_objects
Objects in the scene.
Definition: GlutRenderer.h:205
double halfSize
One half of the edge length of the square, in meters.
Definition: GlutRenderer.h:56
double zNear
Near clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:148
GLUquadric * quadratic
GLU quadric object for the quadric operations required to build the sphere.
Definition: GlutRenderer.h:118
SurgSim::Math::Vector3d up
Up direction.
Definition: GlutRenderer.h:144
double radius
Radius of the sphere, in meters.
Definition: GlutRenderer.h:101
static void reshape(GLint width, GLint height)
Glut reshape function which handles the resizing of the window.
Definition: GlutRenderer.h:211
SurgSim::Math::Vector3d planeDirectionX
The unit direction along one of the pairs edges of the square.
Definition: GlutRenderer.h:52
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:159
static int m_height
Height of the window.
Definition: GlutRenderer.h:200
double zFar
Far clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:150
Square with center at local origin.
Definition: GlutRenderer.h:49
static void drawObjects()
Iterates through the scene objects to draw them.
Definition: GlutRenderer.h:222
double fovY
Field of view angle (in degrees) in the vertical direction.
Definition: GlutRenderer.h:146
Abstract definition of an object that can render itself with Glut.
Definition: GlutRenderer.h:32
Definitions of 2x2 and 3x3 rigid (isometric) transforms.
Simple static class renderer built on Glut.
Definition: GlutRenderer.h:172
SurgSim::Math::Vector3d color
Color of the sphere.
Definition: GlutRenderer.h:103
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
Camera which controls the view of the scene.
Definition: GlutRenderer.h:137
SurgSim::Math::Vector3d eye
Eye position.
Definition: GlutRenderer.h:140
static void addObject(std::shared_ptr< GlutRenderObject > object)
Adds an object to the scene.
Definition: GlutRenderer.h:191
SurgSim::Math::Vector3d color
Color of the square.
Definition: GlutRenderer.h:58
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:35
Sphere with center at local origin.
Definition: GlutRenderer.h:98
GlutSphere(double radius, const SurgSim::Math::Vector3d &color)
Constructor.
Definition: GlutRenderer.h:108
GlutRenderObject()
Constructor initializes pose as identity (no rotation or translation)
Definition: GlutRenderer.h:38
SurgSim::Math::Vector3d center
Center (look at) position.
Definition: GlutRenderer.h:142
SurgSim::Math::Vector3d planeDirectionY
The unit direction along the other pair of edges of the square.
Definition: GlutRenderer.h:54
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:65
GlutAxes(double length, float width)
Constructor.
Definition: GlutRenderer.h:89
static std::shared_ptr< GlutCamera > m_camera
Camera which controls the view of the scene.
Definition: GlutRenderer.h:203
std::vector< std::shared_ptr< GlutRenderObject > > children
Children of this group.
Definition: GlutRenderer.h:125
Group of objects which provides a transform hierarchy.
Definition: GlutRenderer.h:122
static void setCamera(std::shared_ptr< GlutCamera > camera)
Sets the camera used to control the view of the scene.
Definition: GlutRenderer.h:184
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:56
static void run()
Initializes and runs the Glut main loop. This function will block until the Glut graphics window is c...
Definition: GlutRenderer.h:176
static int m_width
Width of the window.
Definition: GlutRenderer.h:198