OsgCamera.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, 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_GRAPHICS_OSGCAMERA_H
17 #define SURGSIM_GRAPHICS_OSGCAMERA_H
18 
19 #include <unordered_map>
20 
26 
27 
28 #include <osg/Camera>
29 #include <osg/Switch>
30 
31 #if defined(_MSC_VER)
32 #pragma warning(push)
33 #pragma warning(disable:4250)
34 #endif
35 
36 namespace SurgSim
37 {
38 namespace Graphics
39 {
40 
41 class Material;
42 class Texture;
43 class RenderTarget;
44 
45 template <class T>
46 class OsgUniform;
47 
48 SURGSIM_STATIC_REGISTRATION(OsgCamera);
49 
54 class OsgCamera : public OsgRepresentation, public Camera
55 {
56 public:
62  explicit OsgCamera(const std::string& name);
63 
65 
66  bool setRenderGroup(std::shared_ptr<Group> group) override;
67 
68  bool setRenderGroups(const std::vector<std::shared_ptr<Group>>& groups) override;
69 
70  void setLocalActive(bool val) override;
71 
73 
75 
76  void setProjectionMatrix(const SurgSim::Math::Matrix44d& matrix) override;
77 
78  const SurgSim::Math::Matrix44d& getProjectionMatrix() const override;
79 
81 
82  void update(double dt) override;
83 
85  osg::ref_ptr<osg::Camera> getOsgCamera() const;
86 
88  osg::ref_ptr<osg::Node> getOsgNode() const;
89 
90  bool setRenderTarget(std::shared_ptr<RenderTarget> renderTarget) override;
91 
92  std::shared_ptr<RenderTarget> getRenderTarget() const override;
93 
94  bool setMaterial(std::shared_ptr<SurgSim::Framework::Component> material) override;
95 
96  std::shared_ptr<Material> getMaterial() const override;
97 
98  void clearMaterial() override;
99 
100  void setRenderOrder(RenderOrder order, int value) override;
101 
102  void setAmbientColor(const SurgSim::Math::Vector4d& color) override;
103 
105 
106  void setGenerateTangents(bool value) override;
107 
108  void setPerspectiveProjection(double fovy, double aspect, double near, double far) override;
109 
111  double left, double right,
112  double bottom, double top,
113  double near, double far) override;
114 
115 
116  void setViewport(int x, int y, int width, int height) override;
117 
118  void getViewport(int* x, int* y, int* width, int* height) const override;
119 
120  void setViewportSize(std::array<double, 2> dimensions) override;
121 
122  std::array<double, 2> getViewportSize() const override;
123 
124  void setMainCamera(bool val) override;
125 
126  bool isMainCamera() override;
127 
128 private:
129 
130  osg::ref_ptr<osg::Camera> m_camera;
131 
134 
135  std::unordered_map<int, std::shared_ptr<Texture>> m_textureMap;
136  std::shared_ptr<RenderTarget> m_renderTarget;
137 
141  void attachRenderTargetTexture(osg::Camera::BufferComponent buffer, std::shared_ptr<Texture> texture);
142 
145 
147  std::shared_ptr<OsgUniform<SurgSim::Math::Matrix44f>> m_viewMatrixUniform;
148 
150  std::shared_ptr<OsgUniform<SurgSim::Math::Matrix44f>> m_inverseViewMatrixUniform;
151 
153  std::shared_ptr<OsgUniform<SurgSim::Math::Vector4f>> m_ambientColorUniform;
154 
157 
159 };
160 
161 }; // namespace Graphics
162 }; // namespace SurgSim
163 
164 #if defined(_MSC_VER)
165 #pragma warning(pop)
166 #endif
167 
168 #endif // SURGSIM_GRAPHICS_OSGCAMERA_H
Texture.h
SurgSim::Graphics::OsgCamera::setGenerateTangents
void setGenerateTangents(bool value) override
Enable or disable the generation of tangents.
Definition: OsgCamera.cpp:510
SurgSim::Graphics::OsgCamera::attachRenderTargetTexture
void attachRenderTargetTexture(osg::Camera::BufferComponent buffer, std::shared_ptr< Texture > texture)
Attach a specific texture to a specific BufferComponent, works for Depth and all the Colors.
Definition: OsgCamera.cpp:458
SurgSim::Graphics::OsgCamera::m_ambientColor
SurgSim::Math::Vector4d m_ambientColor
Value for ambient color.
Definition: OsgCamera.h:156
SurgSim::Graphics::OsgCamera::setOrthogonalProjection
void setOrthogonalProjection(double left, double right, double bottom, double top, double near, double far) override
Set the projection matrix with the appropriate orthogonal projection parameters.
Definition: OsgCamera.cpp:452
Macros.h
SurgSim::Graphics::OsgCamera::getInverseViewMatrix
virtual SurgSim::Math::Matrix44d getInverseViewMatrix() const
Gets the inverse view matrix of the camera.
Definition: OsgCamera.cpp:494
SurgSim::Graphics::OsgCamera::setRenderGroup
bool setRenderGroup(std::shared_ptr< Group > group) override
Sets the group of representations that will be seen by this camera.
Definition: OsgCamera.cpp:190
SurgSim::Graphics::OsgCamera::getRenderTarget
std::shared_ptr< RenderTarget > getRenderTarget() const override
Gets RenderTarget that is currently being used by the camera.
Definition: OsgCamera.cpp:318
SurgSim::Graphics::OsgCamera::setMaterial
bool setMaterial(std::shared_ptr< SurgSim::Framework::Component > material) override
Sets the material that defines the visual appearance of the representation.
Definition: OsgCamera.cpp:324
SurgSim::Graphics::Camera
Base graphics camera class, which defines the basic interface for all graphics cameras.
Definition: Camera.h:52
SurgSim::Graphics::OsgCamera::getViewportSize
std::array< double, 2 > getViewportSize() const override
Gets the dimensions of the viewport.
Definition: OsgCamera.cpp:400
SurgSim::Graphics::OsgCamera::getAmbientColor
SurgSim::Math::Vector4d getAmbientColor() override
Definition: OsgCamera.cpp:505
SurgSim::Graphics::OsgCamera::m_textureMap
std::unordered_map< int, std::shared_ptr< Texture > > m_textureMap
Definition: OsgCamera.h:135
SurgSim::Graphics::OsgCamera::m_viewMatrixUniform
std::shared_ptr< OsgUniform< SurgSim::Math::Matrix44f > > m_viewMatrixUniform
Uniform to carry the view matrix.
Definition: OsgCamera.h:147
SurgSim::Graphics::OsgCamera::setLocalActive
void setLocalActive(bool val) override
Set the component's active state.
Definition: OsgCamera.cpp:229
SurgSim::Graphics::OsgCamera::setPerspectiveProjection
void setPerspectiveProjection(double fovy, double aspect, double near, double far) override
Set the projection matrix with the appropriate perspective projection parameters.
Definition: OsgCamera.cpp:445
ObjectFactory.h
SurgSim::Graphics::OsgCamera::setMainCamera
void setMainCamera(bool val) override
Marks the camera as a main view camera, this means that view dependent passes should follow this came...
Definition: OsgCamera.cpp:411
SurgSim
Definition: CompoundShapeToGraphics.cpp:30
SurgSim::Graphics::OsgCamera::update
void update(double dt) override
Updates the representation.
Definition: OsgCamera.cpp:256
SurgSim::Graphics::OsgCamera::m_renderTarget
std::shared_ptr< RenderTarget > m_renderTarget
Definition: OsgCamera.h:136
SurgSim::Graphics::OsgCamera::m_camera
osg::ref_ptr< osg::Camera > m_camera
Definition: OsgCamera.h:130
SurgSim::Graphics::OsgCamera::setAmbientColor
void setAmbientColor(const SurgSim::Math::Vector4d &color) override
Sets a value for the ambient lighting term, this can add light to the scene when there is no lighting...
Definition: OsgCamera.cpp:499
SurgSim::Graphics::OsgCamera::getViewMatrix
virtual SurgSim::Math::Matrix44d getViewMatrix() const
Gets the view matrix of the camera.
Definition: OsgCamera.cpp:235
SurgSim::Graphics::OsgCamera::clearMaterial
void clearMaterial() override
Removes the material from the representation.
Definition: OsgCamera.cpp:342
SurgSim::Math::Matrix44d
Eigen::Matrix< double, 4, 4, Eigen::RowMajor > Matrix44d
A 4x4 matrix of doubles.
Definition: Matrix.h:55
SurgSim::Graphics::OsgCamera::OsgCamera
OsgCamera(const std::string &name)
Constructor.
Definition: OsgCamera.cpp:147
SurgSim::Graphics::Camera::getViewport
std::array< int, 4 > getViewport() const
Definition: Camera.cpp:170
SurgSim::Math::Vector4d
Eigen::Matrix< double, 4, 1 > Vector4d
A 4D vector of doubles.
Definition: Vector.h:61
SurgSim::Graphics::OsgCamera::getOsgNode
osg::ref_ptr< osg::Node > getOsgNode() const
Definition: OsgCamera.cpp:489
SurgSim::Graphics::OsgCamera::getOsgCamera
osg::ref_ptr< osg::Camera > getOsgCamera() const
Definition: OsgCamera.cpp:484
SurgSim::Graphics::OsgCamera::m_ambientColorUniform
std::shared_ptr< OsgUniform< SurgSim::Math::Vector4f > > m_ambientColorUniform
Uniform to carry the ambient color.
Definition: OsgCamera.h:153
SurgSim::Graphics::OsgCamera::setRenderOrder
void setRenderOrder(RenderOrder order, int value) override
Determine when this camera will render.
Definition: OsgCamera.cpp:476
SurgSim::Graphics::OsgCamera::getProjectionMatrix
const SurgSim::Math::Matrix44d & getProjectionMatrix() const override
Gets the projection matrix of the camera.
Definition: OsgCamera.cpp:246
SurgSim::Graphics::OsgCamera::SURGSIM_CLASSNAME
SURGSIM_CLASSNAME(SurgSim::Graphics::OsgCamera)
SurgSim::Graphics::Camera::RenderOrder
RenderOrder
Definition: Camera.h:56
SurgSim::Graphics::OsgCamera::setProjectionMatrix
void setProjectionMatrix(const SurgSim::Math::Matrix44d &matrix) override
Sets the projection matrix of the camera.
Definition: OsgCamera.cpp:240
SurgSim::Graphics::OsgCamera
OSG implementation of a graphics camera.
Definition: OsgCamera.h:55
SurgSim::Graphics::OsgCamera::setRenderTarget
bool setRenderTarget(std::shared_ptr< RenderTarget > renderTarget) override
Sets RenderTarget for the current camera, enables the camera to render to off-screen textures.
Definition: OsgCamera.cpp:276
Camera.h
SurgSim::Graphics::OsgCamera::setRenderGroups
bool setRenderGroups(const std::vector< std::shared_ptr< Group >> &groups) override
Sets the representation groups that will be seen by this camera.
Definition: OsgCamera.cpp:196
SurgSim::Graphics::OsgCamera::isMainCamera
bool isMainCamera() override
Definition: OsgCamera.cpp:440
SurgSim::Graphics::OsgCamera::m_inverseViewMatrixUniform
std::shared_ptr< OsgUniform< SurgSim::Math::Matrix44f > > m_inverseViewMatrixUniform
Uniform to carry the inverse view matrix.
Definition: OsgCamera.h:150
SurgSim::Graphics::OsgCamera::setViewportSize
void setViewportSize(std::array< double, 2 > dimensions) override
Sets the width and height of the viewport.
Definition: OsgCamera.cpp:383
SurgSim::Graphics::OsgCamera::getMaterial
std::shared_ptr< Material > getMaterial() const override
Gets the material that defines the visual appearance of the representation.
Definition: OsgCamera.cpp:337
SurgSim::Graphics::OsgCamera::m_projectionMatrix
SurgSim::Math::Matrix44d m_projectionMatrix
Projection matrix of the camera.
Definition: OsgCamera.h:133
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
SurgSim::Graphics::OsgCamera::setViewport
void setViewport(int x, int y, int width, int height) override
Sets the viewport size for this camera.
Definition: OsgCamera.cpp:363
SurgSim::Graphics::OsgCamera::getInverseProjectionMatrix
SurgSim::Math::Matrix44d getInverseProjectionMatrix() const override
Gets the inverse projection matrix of the camera.
Definition: OsgCamera.cpp:251
SurgSim::Graphics::OsgCamera::detachCurrentRenderTarget
void detachCurrentRenderTarget()
Detach the current render target from the camera.
Definition: OsgCamera.cpp:347
OsgRepresentation.h
SurgSim::Graphics::OsgCamera::m_isMainCamera
bool m_isMainCamera
Definition: OsgCamera.h:158
SurgSim::Graphics::OsgRepresentation
Base OSG implementation of a graphics representation.
Definition: OsgRepresentation.h:56