GlutRenderer.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_TESTING_VISUALTESTCOMMON_GLUTRENDERER_H
#define SURGSIM_TESTING_VISUALTESTCOMMON_GLUTRENDERER_H
#include <Eigen/Geometry>
#include <memory>
#include <vector>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#define GLUT_NO_LIB_PRAGMA 1
#include <GL/glut.h>
#endif
#include "SurgSim/DataStructures/Image.h"
#include "SurgSim/Framework/LockedContainer.h"
#include "SurgSim/Math/Vector.h"
#include "SurgSim/Math/RigidTransform.h"
struct GlutRenderObject
{
SurgSim::Math::RigidTransform3d pose;
GlutRenderObject() : pose(SurgSim::Math::RigidTransform3d::Identity())
{
}
virtual ~GlutRenderObject();
virtual void draw() = 0;
};
struct GlutSquare : public GlutRenderObject
{
SurgSim::Math::Vector3d planeDirectionX;
SurgSim::Math::Vector3d planeDirectionY;
double halfSize;
SurgSim::Math::Vector3d color;
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)) :
GlutRenderObject(), planeDirectionX(planeDirectionX), planeDirectionY(planeDirectionY),
halfSize(halfSize), color(color)
{
}
virtual void draw();
};
struct GlutAxes : GlutRenderObject
{
double length;
float width;
GlutAxes(double length, float width) : GlutRenderObject(), length(length), width(width)
{
}
virtual void draw();
};
struct GlutSphere : GlutRenderObject
{
double radius;
SurgSim::Math::Vector3d color;
GlutSphere(double radius, const SurgSim::Math::Vector3d& color) :
radius(radius), color(color), quadratic(gluNewQuadric())
{
}
virtual void draw();
private:
GLUquadric* quadratic;
};
struct GlutImage : GlutRenderObject
{
typedef SurgSim::DataStructures::Image<float> ImageType;
SurgSim::Framework::LockedContainer<ImageType> image;
explicit GlutImage(const Eigen::AlignedBox<double, 2>& bounds) :
m_bounds(bounds),
m_firstRun(true)
{
}
virtual void draw();
private:
Eigen::AlignedBox<double, 2> m_bounds;
unsigned int m_texture;
bool m_firstRun;
};
struct GlutGroup : public GlutRenderObject
{
std::vector< std::shared_ptr<GlutRenderObject> > children;
GlutGroup() : GlutRenderObject()
{
}
virtual void draw();
};
struct GlutCamera
{
SurgSim::Math::Vector3d eye;
SurgSim::Math::Vector3d center;
SurgSim::Math::Vector3d up;
double fovY;
double zNear;
double zFar;
GlutCamera(const SurgSim::Math::Vector3d& eye_, const SurgSim::Math::Vector3d& center_,
const SurgSim::Math::Vector3d& up_, const double fovY_, double zNear_, double zFar_) :
eye(eye_),
center(center_),
up(up_),
fovY(fovY_),
zNear(zNear_),
zFar(zFar_)
{
}
};
class GlutRenderer
{
public:
static void run()
{
initialize();
glutMainLoop();
}
static void setCamera(std::shared_ptr<GlutCamera> camera)
{
m_camera = camera;
}
static void addObject(std::shared_ptr<GlutRenderObject> object)
{
m_objects.push_back(object);
}
private:
static int m_width;
static int m_height;
static std::shared_ptr<GlutCamera> m_camera;
static std::vector< std::shared_ptr<GlutRenderObject> > m_objects;
static void initialize();
static void reshape(GLint width, GLint height)
{
m_width = width;
m_height = height;
glViewport(0, 0, m_width, m_height);
}
static void display();
static void drawObjects()
{
for (auto it = m_objects.cbegin(); it != m_objects.cend(); ++it)
{
(*it)->draw();
}
}
};
#endif // SURGSIM_TESTING_VISUALTESTCOMMON_GLUTRENDERER_H
GlutAxes::width
float width
Width of each axis, in pixels.
Definition: GlutRenderer.h:88
GlutRenderObject::draw
virtual void draw()=0
Pure virtual draw method for subclasses to define how to draw themselves with Glut.
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
GlutAxes::length
double length
Length of each axis, in meters.
Definition: GlutRenderer.h:86
SurgSim::DataStructures::Image
A templated Image class.
Definition: Image.h:33
GlutImage::m_firstRun
bool m_firstRun
Is this the fist run of draw.
Definition: GlutRenderer.h:152
GlutAxes
Axes with center at local origin, red axis along the local X-axis, green axis along the local Y-axis...
Definition: GlutRenderer.h:83
GlutImage::m_bounds
Eigen::AlignedBox< double, 2 > m_bounds
Window coordinates to draw the image.
Definition: GlutRenderer.h:146
GlutGroup::GlutGroup
GlutGroup()
Constructor. The group is initialized with no children.
Definition: GlutRenderer.h:162
GlutRenderer::m_objects
static std::vector< std::shared_ptr< GlutRenderObject > > m_objects
Objects in the scene.
Definition: GlutRenderer.h:239
GlutSquare::halfSize
double halfSize
One half of the edge length of the square, in meters.
Definition: GlutRenderer.h:60
GlutCamera::zNear
double zNear
Near clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:182
LockedContainer.h
GlutSphere::quadratic
GLUquadric * quadratic
GLU quadric object for the quadric operations required to build the sphere.
Definition: GlutRenderer.h:122
GlutCamera::up
SurgSim::Math::Vector3d up
Up direction.
Definition: GlutRenderer.h:178
GlutSphere::radius
double radius
Radius of the sphere, in meters.
Definition: GlutRenderer.h:105
GlutRenderer::reshape
static void reshape(GLint width, GLint height)
Glut reshape function which handles the resizing of the window.
Definition: GlutRenderer.h:245
GlutSquare::planeDirectionX
SurgSim::Math::Vector3d planeDirectionX
The unit direction along one of the pairs edges of the square.
Definition: GlutRenderer.h:56
Image.h
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::m_height
static int m_height
Height of the window.
Definition: GlutRenderer.h:234
GlutCamera::zFar
double zFar
Far clipping plane distance from camera, in meters.
Definition: GlutRenderer.h:184
GlutImage::GlutImage
GlutImage(const Eigen::AlignedBox< double, 2 > &bounds)
Constuctor.
Definition: GlutRenderer.h:135
GlutSquare
Square with center at local origin.
Definition: GlutRenderer.h:53
GlutImage
An Image drawn to the screen.
Definition: GlutRenderer.h:126
GlutRenderer::drawObjects
static void drawObjects()
Iterates through the scene objects to draw them.
Definition: GlutRenderer.h:256
GlutImage::m_texture
unsigned int m_texture
Texture used for holding the image.
Definition: GlutRenderer.h:149
GlutCamera::fovY
double fovY
Field of view angle (in degrees) in the vertical direction.
Definition: GlutRenderer.h:180
GlutRenderObject
Abstract definition of an object that can render itself with Glut.
Definition: GlutRenderer.h:36
RigidTransform.h
Definitions of 2x2 and 3x3 rigid (isometric) transforms.
GlutRenderer
Simple static class renderer built on Glut.
Definition: GlutRenderer.h:206
GlutSphere::color
SurgSim::Math::Vector3d color
Color of the sphere.
Definition: GlutRenderer.h:107
SurgSim::Math::RigidTransform3d
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
GlutImage::image
SurgSim::Framework::LockedContainer< ImageType > image
The image to draw.
Definition: GlutRenderer.h:131
GlutCamera
Camera which controls the view of the scene.
Definition: GlutRenderer.h:171
GlutCamera::eye
SurgSim::Math::Vector3d eye
Eye position.
Definition: GlutRenderer.h:174
GlutRenderer::addObject
static void addObject(std::shared_ptr< GlutRenderObject > object)
Adds an object to the scene.
Definition: GlutRenderer.h:225
GlutSquare::color
SurgSim::Math::Vector3d color
Color of the square.
Definition: GlutRenderer.h:62
Vector.h
Definitions of small fixed-size vector types.
GlutRenderObject::~GlutRenderObject
virtual ~GlutRenderObject()
Definition: GlutRenderer.cpp:32
GlutRenderObject::pose
SurgSim::Math::RigidTransform3d pose
Pose (rotation and translation) of the object.
Definition: GlutRenderer.h:39
SurgSim::Framework::LockedContainer
A simple thread-safe data container that can support multiple writers and readers.
Definition: LockedContainer.h:54
GlutSphere
Sphere with center at local origin.
Definition: GlutRenderer.h:102
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
GlutCamera::center
SurgSim::Math::Vector3d center
Center (look at) position.
Definition: GlutRenderer.h:176
GlutSquare::planeDirectionY
SurgSim::Math::Vector3d planeDirectionY
The unit direction along the other pair of edges of the square.
Definition: GlutRenderer.h:58
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
GlutAxes::GlutAxes
GlutAxes(double length, float width)
Constructor.
Definition: GlutRenderer.h:93
GlutRenderer::m_camera
static std::shared_ptr< GlutCamera > m_camera
Camera which controls the view of the scene.
Definition: GlutRenderer.h:237
GlutGroup::children
std::vector< std::shared_ptr< GlutRenderObject > > children
Children of this group.
Definition: GlutRenderer.h:159
GlutGroup
Group of objects which provides a transform hierarchy.
Definition: GlutRenderer.h:156
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
SurgSim::Math::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
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
GlutRenderer::m_width
static int m_width
Width of the window.
Definition: GlutRenderer.h:232
GlutImage::ImageType
SurgSim::DataStructures::Image< float > ImageType
Definition: GlutRenderer.h:128