OsgRenderTarget-inl.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_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  m_width(width * scale),
45  m_height(height * scale),
46  m_colorTargetCount(0),
47  m_textures(OsgSupportedTextureCount)
48 {
49  setColorTargetCount(colorCount);
50  useDepthTarget(useDepth);
51 }
52 
53 
54 template <class T>
56 {
57 }
58 
59 template <class T>
60 void OsgRenderTarget<T>::getSize(int* width, int* height) const
61 {
62  *width = m_width;
63  *height = m_height;
64 }
65 
66 template <class T>
68 {
69  int result = (count < 16) ? count : 16;
70 
71  // This does not check against graphics card capabilities, the max 16 provided
72  // by OSG might not be supported by the current graphics card
73  // Keep the other texture allocated when the count goes down
74  // Rendertargets are probably not going to change that much once set up
75  // #memory
76  for (int i = m_colorTargetCount; i<result; ++i)
77  {
78  setupTexture(TARGETTYPE_COLORBASE+i);
79  }
80  m_colorTargetCount = result;
81  return result;
82 }
83 
84 template <class T>
86 {
87  return m_colorTargetCount;
88 }
89 
90 template <class T>
91 std::shared_ptr<Texture> OsgRenderTarget<T>::getColorTarget(int index) const
92 {
93  std::shared_ptr<Texture> result;
94 
95  if (index < m_colorTargetCount)
96  {
97  result = m_textures[TARGETTYPE_COLORBASE + index];
98  }
99 
100  return result;
101 }
102 
103 template <class T>
104 std::shared_ptr<OsgTexture> OsgRenderTarget<T>::getColorTargetOsg(int index) const
105 {
106  std::shared_ptr<T> result;
107 
108  if (index < m_colorTargetCount)
109  {
110  result = m_textures[TARGETTYPE_COLORBASE + index];
111  }
112 
113  return result;
114 }
115 
116 template <class T>
118 {
119  if (val)
120  {
121  setupTexture(TARGETTYPE_DEPTH);
122  }
123  else
124  {
125  m_textures[TARGETTYPE_DEPTH] = nullptr;
126  }
127 }
128 
129 template <class T>
131 {
132  return m_textures.at(TARGETTYPE_DEPTH) != nullptr;
133 }
134 
135 template <class T>
136 std::shared_ptr<Texture> OsgRenderTarget<T>::getDepthTarget() const
137 {
138  return m_textures.at(TARGETTYPE_DEPTH);
139 }
140 
141 template <class T>
142 std::shared_ptr<OsgTexture> OsgRenderTarget<T>::getDepthTargetOsg() const
143 {
144  return m_textures.at(TARGETTYPE_DEPTH);
145 }
146 
147 template <class T>
149 {
150  if (m_textures[type] == nullptr)
151  {
152  m_textures[type] = std::make_shared<T>();
153  m_textures[type]->setSize(m_width, m_height);
154  osg::Texture* osgTexture = m_textures[type]->getOsgTexture();
155  // We are not dealing with mipmaps, fix up the filters to enable rendering to FBO
156  // see http://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture
157  osgTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
158  osgTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
159  osgTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
160  osgTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
161  if (type == TARGETTYPE_DEPTH)
162  {
163  osgTexture->setInternalFormat(GL_DEPTH_COMPONENT32F);
164  osgTexture->setSourceFormat(GL_DEPTH_COMPONENT);
165  osgTexture->setSourceType(GL_FLOAT);
166  osgTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
167  osgTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
168  }
169  if (type >= TARGETTYPE_COLORBASE)
170  {
171  osgTexture->setInternalFormat(GL_RGBA32F_ARB);
172  osgTexture->setSourceFormat(GL_RGBA);
173  osgTexture->setSourceType(GL_FLOAT);
174  }
175  }
176 }
177 
178 }; // namespace Graphics
179 
180 }; // namespace SurgSim
181 
182 #endif
int setColorTargetCount(int count)
Sets color target count.
Definition: OsgRenderTarget-inl.h:67
Definition: DriveElementFromInputBehavior.cpp:27
virtual void getSize(int *width, int *height) const override
Gets a size.
Definition: OsgRenderTarget-inl.h:60
OsgRenderTarget()
Default constructor.
Definition: OsgRenderTarget-inl.h:29
virtual std::shared_ptr< Texture > getColorTarget(int index) const override
Generic accessor for a specific color target texture.
Definition: OsgRenderTarget-inl.h:91
virtual bool doesUseDepthTarget() const override
Determines if RenderTarget does use a depth target.
Definition: OsgRenderTarget-inl.h:130
std::shared_ptr< OsgTexture > getColorTargetOsg(int index) const
Accessor for the color target as an OsgTexture.
Definition: OsgRenderTarget-inl.h:104
virtual int getColorTargetCount() const override
Definition: OsgRenderTarget-inl.h:85
void useDepthTarget(bool val)
Use depth target.
Definition: OsgRenderTarget-inl.h:117
~OsgRenderTarget()
Destructor.
Definition: OsgRenderTarget-inl.h:55
void setupTexture(int type)
Sets up the texture with a given target type (depth or color w/ index).
Definition: OsgRenderTarget-inl.h:148
virtual std::shared_ptr< Texture > getDepthTarget() const override
Generic accessor for the depth Target.
Definition: OsgRenderTarget-inl.h:136
const int OsgSupportedTextureCount
Definition: OsgRenderTarget-inl.h:26
std::shared_ptr< OsgTexture > getDepthTargetOsg() const
Accessor for the depth target as an OsgTexture.
Definition: OsgRenderTarget-inl.h:142