opensurgsim
OsgRenderTarget-inl.h
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_GRAPHICS_OSGRENDERTARGET_INL_H
17 #define SURGSIM_GRAPHICS_OSGRENDERTARGET_INL_H
18 
19 namespace SurgSim
20 {
21 
22 namespace Graphics
23 {
24 
25 // Osg Supports 16 Color Attachments plus the depth texture
26 const int OsgSupportedTextureCount = 16 + 1;
27 
28 template <class T>
30  m_width(0),
31  m_height(0),
32  m_colorTargetCount(0),
33  m_textures(OsgSupportedTextureCount)
34 {
35 }
36 
37 template <class T>
39  int width,
40  int height,
41  double scale,
42  int colorCount,
43  bool useDepth,
44  bool useFloat) :
45  m_width(width* scale),
46  m_height(height* scale),
47  m_colorTargetCount(0),
48  m_textures(OsgSupportedTextureCount)
49 {
50  setColorTargetCount(colorCount, useFloat);
51  useDepthTarget(useDepth);
52 }
53 
54 
55 template <class T>
57 {
58 }
59 
60 template <class T>
61 void OsgRenderTarget<T>::getSize(int* width, int* height) const
62 {
63  *width = m_width;
64  *height = m_height;
65 }
66 
67 template <class T>
68 int OsgRenderTarget<T>::setColorTargetCount(int count, bool floatColor)
69 {
70  int result = (count < 16) ? count : 16;
71 
72  // This does not check against graphics card capabilities, the max 16 provided
73  // by OSG might not be supported by the current graphics card
74  // Keep the other texture allocated when the count goes down
75  // Rendertargets are probably not going to change that much once set up
76  // #memory
77  for (int i = m_colorTargetCount; i < result; ++i)
78  {
79  setupTexture(TARGETTYPE_COLORBASE + i, floatColor);
80  }
81  m_colorTargetCount = result;
82  return result;
83 }
84 
85 template <class T>
87 {
88  return m_colorTargetCount;
89 }
90 
91 template <class T>
92 std::shared_ptr<Texture> OsgRenderTarget<T>::getColorTarget(int index) const
93 {
94  std::shared_ptr<Texture> result;
95 
96  if (index < m_colorTargetCount)
97  {
98  result = m_textures[TARGETTYPE_COLORBASE + index];
99  }
100 
101  return result;
102 }
103 
104 template <class T>
105 std::shared_ptr<OsgTexture> OsgRenderTarget<T>::getColorTargetOsg(int index) const
106 {
107  std::shared_ptr<T> result;
108 
109  if (index < m_colorTargetCount)
110  {
111  result = m_textures[TARGETTYPE_COLORBASE + index];
112  }
113 
114  return result;
115 }
116 
117 template <class T>
119 {
120  if (val)
121  {
122  setupTexture(TARGETTYPE_DEPTH, false);
123  }
124  else
125  {
126  m_textures[TARGETTYPE_DEPTH] = nullptr;
127  }
128 }
129 
130 template <class T>
132 {
133  return m_textures.at(TARGETTYPE_DEPTH) != nullptr;
134 }
135 
136 template <class T>
137 std::shared_ptr<Texture> OsgRenderTarget<T>::getDepthTarget() const
138 {
139  return m_textures.at(TARGETTYPE_DEPTH);
140 }
141 
142 template <class T>
143 std::shared_ptr<OsgTexture> OsgRenderTarget<T>::getDepthTargetOsg() const
144 {
145  return m_textures.at(TARGETTYPE_DEPTH);
146 }
147 
148 template <class T>
149 void OsgRenderTarget<T>::setupTexture(int type, bool floatColor)
150 {
151  if (m_textures[type] == nullptr)
152  {
153  m_textures[type] = std::make_shared<T>();
154  m_textures[type]->setSize(m_width, m_height);
155  osg::Texture* osgTexture = m_textures[type]->getOsgTexture();
156  // We are not dealing with mipmaps, fix up the filters to enable rendering to FBO
157  // see http://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture
158  osgTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
159  osgTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
160  osgTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
161  osgTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
162  if (type == TARGETTYPE_DEPTH)
163  {
164  osgTexture->setInternalFormat(GL_DEPTH_COMPONENT32F);
165  osgTexture->setSourceFormat(GL_DEPTH_COMPONENT);
166  osgTexture->setSourceType(GL_FLOAT);
167  osgTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
168  osgTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
169  osgTexture->setBorderColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));
170  }
171  if (type >= TARGETTYPE_COLORBASE)
172  {
173  if (floatColor)
174  {
176  osgTexture->setInternalFormat(GL_RGBA32F_ARB);
177  osgTexture->setSourceFormat(GL_RGBA);
178  osgTexture->setSourceType(GL_FLOAT);
179  }
180  else
181  {
182  osgTexture->setInternalFormat(GL_RGBA);
183  osgTexture->setSourceFormat(GL_RGBA);
184  osgTexture->setSourceType(GL_BYTE);
185  }
186  }
187  }
188 }
189 
190 }; // namespace Graphics
191 
192 }; // namespace SurgSim
193 
194 #endif
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
void getSize(int *width, int *height) const override
Gets a size.
Definition: OsgRenderTarget-inl.h:61
OsgRenderTarget()
Default constructor.
Definition: OsgRenderTarget-inl.h:29
std::shared_ptr< Texture > getColorTarget(int index) const override
Generic accessor for a specific color target texture.
Definition: OsgRenderTarget-inl.h:92
bool doesUseDepthTarget() const override
Determines if RenderTarget does use a depth target.
Definition: OsgRenderTarget-inl.h:131
int getColorTargetCount() const override
Definition: OsgRenderTarget-inl.h:86
std::shared_ptr< OsgTexture > getColorTargetOsg(int index) const
Accessor for the color target as an OsgTexture.
Definition: OsgRenderTarget-inl.h:105
Specific implementation of the render target class.
Definition: OsgRenderTarget.h:52
~OsgRenderTarget()
Destructor.
Definition: OsgRenderTarget-inl.h:56
std::shared_ptr< OsgTexture > getDepthTargetOsg() const
Accessor for the depth target as an OsgTexture.
Definition: OsgRenderTarget-inl.h:143
std::shared_ptr< Texture > getDepthTarget() const override
Generic accessor for the depth Target.
Definition: OsgRenderTarget-inl.h:137