opensurgsim
OsgCamera.h
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 
21 #include "SurgSim/Framework/Macros.h"
22 #include "SurgSim/Framework/ObjectFactory.h"
23 #include "SurgSim/Graphics/Camera.h"
24 #include "SurgSim/Graphics/OsgRepresentation.h"
25 #include "SurgSim/Graphics/Texture.h"
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 
64  SURGSIM_CLASSNAME(SurgSim::Graphics::OsgCamera);
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 
133  SurgSim::Math::Matrix44d m_projectionMatrix;
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 
144  void detachCurrentRenderTarget();
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 
156  SurgSim::Math::Vector4d m_ambientColor;
157 
158  bool m_isMainCamera;
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
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
void setViewportSize(std::array< double, 2 > dimensions) override
Sets the width and height of the viewport.
Definition: OsgCamera.cpp:383
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
void setProjectionMatrix(const SurgSim::Math::Matrix44d &matrix) override
Sets the projection matrix of the camera.
Definition: OsgCamera.cpp:240
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
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
Eigen::Matrix< double, 4, 1 > Vector4d
A 4D vector of doubles.
Definition: Vector.h:61
std::shared_ptr< RenderTarget > getRenderTarget() const override
Gets RenderTarget that is currently being used by the camera.
Definition: OsgCamera.cpp:318
void clearMaterial() override
Removes the material from the representation.
Definition: OsgCamera.cpp:342
SurgSim::Math::Matrix44d getInverseProjectionMatrix() const override
Gets the inverse projection matrix of the camera.
Definition: OsgCamera.cpp:251
osg::ref_ptr< osg::Node > getOsgNode() const
Definition: OsgCamera.cpp:489
Eigen::Matrix< double, 4, 4, Eigen::RowMajor > Matrix44d
A 4x4 matrix of doubles.
Definition: Matrix.h:55
osg::ref_ptr< osg::Camera > getOsgCamera() const
Definition: OsgCamera.cpp:484
void setRenderOrder(RenderOrder order, int value) override
Determine when this camera will render.
Definition: OsgCamera.cpp:476
OSG implementation of a graphics camera.
Definition: OsgCamera.h:54
Base graphics camera class, which defines the basic interface for all graphics cameras.
Definition: Camera.h:51
void setViewport(int x, int y, int width, int height) override
Sets the viewport size for this camera.
Definition: OsgCamera.cpp:363
void setGenerateTangents(bool value) override
Enable or disable the generation of tangents.
Definition: OsgCamera.cpp:510
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
OSG implementation of graphics uniform with a value of type T.
Definition: OsgCamera.h:46
std::shared_ptr< Material > getMaterial() const override
Gets the material that defines the visual appearance of the representation.
Definition: OsgCamera.cpp:337
void getViewport(int *x, int *y, int *width, int *height) const override
collect the viewport values
Definition: OsgCamera.cpp:368
void setLocalActive(bool val) override
Set the component&#39;s active state.
Definition: OsgCamera.cpp:229
void update(double dt) override
Updates the representation.
Definition: OsgCamera.cpp:256
std::array< double, 2 > getViewportSize() const override
Gets the dimensions of the viewport.
Definition: OsgCamera.cpp:400
const SurgSim::Math::Matrix44d & getProjectionMatrix() const override
Gets the projection matrix of the camera.
Definition: OsgCamera.cpp:246
bool isMainCamera() override
Definition: OsgCamera.cpp:440
OsgCamera(const std::string &name)
Constructor.
Definition: OsgCamera.cpp:147
virtual SurgSim::Math::Matrix44d getViewMatrix() const
Gets the view matrix of the camera.
Definition: OsgCamera.cpp:235
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
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
SurgSim::Math::Vector4d getAmbientColor() override
Definition: OsgCamera.cpp:505
virtual SurgSim::Math::Matrix44d getInverseViewMatrix() const
Gets the inverse view matrix of the camera.
Definition: OsgCamera.cpp:494
bool setRenderGroup(std::shared_ptr< Group > group) override
Sets the group of representations that will be seen by this camera.
Definition: OsgCamera.cpp:190
Base OSG implementation of a graphics representation.
Definition: OsgRepresentation.h:55
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