OpenWalnut  1.5.0dev
WGEOffscreenRenderNode.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 
27 #include <osg/Geode>
28 
29 #include "../../common/WAssert.h"
30 #include "../WGEUtils.h"
31 
32 #include "WGEOffscreenRenderNode.h"
33 
34 bool isPowerOfTwo( size_t x )
35 {
36  return ( ( x != 0 ) && ( ( x & ( ~x + 1 ) ) == x ) );
37 }
38 
39 bool checkTextureSize( size_t size )
40 {
41  return !( ( size > 8192 ) || ( size < 8 ) );
42 }
43 
44 WGEOffscreenRenderNode::WGEOffscreenRenderNode( osg::ref_ptr< WGECamera > reference, size_t width, size_t height, bool noHud ):
45  WGEGroupNode(),
46  m_referenceCamera( reference ),
47  m_hud(),
48  m_textureWidth( width ),
49  m_textureHeight( height ),
50  m_nextPassNum( 0 ),
51  m_forceViewportTextureSize( false )
52 {
53  WAssert( checkTextureSize( width ) && checkTextureSize( height ), "Invalid offscreen texture size. Must be power of two and in [8,4096]." );
54 
55  // initialize members
56  m_hud = new WGETextureHud();
57  if( !noHud )
58  {
60  m_hud->coupleViewportWithTextureViewport();
61  insert( m_hud );
62  }
63 }
64 
66 {
67  // cleanup
68 }
69 
70 osg::ref_ptr< WGEOffscreenRenderPass > WGEOffscreenRenderNode::addGeometryRenderPass( osg::ref_ptr< osg::Node > node, std::string name )
71 {
72  // create a plain render pass and add some geometry
73  osg::ref_ptr< WGEOffscreenRenderPass > pass = addRenderPass< WGEOffscreenRenderPass >( name );
74  pass->addChild( node );
75 
76  // add proxy to ensure proper clipping/culling even when the node changes its size in the shader. The programmer of the node has to take
77  // care, that the getBounds methods of the node returns the right size (use a ComputeBoundingSpereCallback or overwrite computeBounds in
78  // your node)
79  //
80  // It is important to add this proxy into the goup as the passes disable near/far recalculation. They inherit the near/far planes from the
81  // camera of this group node. To ensure that OSG sets it properly, we use these proxies.
83 
84  return pass;
85 }
86 
87 osg::ref_ptr< WGEOffscreenRenderPass > WGEOffscreenRenderNode::addGeometryRenderPass( osg::ref_ptr< osg::Node > node,
88  osg::ref_ptr< WGEShader > shader,
89  std::string name )
90 {
91  // create a plain render pass and add some geometry
92  osg::ref_ptr< WGEOffscreenRenderPass > pass = addRenderPass< WGEOffscreenRenderPass >( name );
93  pass->addChild( node );
94  shader->apply( pass );
95 
96  // add proxy to ensure proper clipping/culling even when the node changes its size in the shader. The programmer of the node has to take
97  // care, that the getBounds methods of the node returns the right size (use a ComputeBoundingSpereCallback or overwrite computeBounds in
98  // your node)
99  //
100  // It is important to add this proxy into the goup as the passes disable near/far recalculation. They inherit the near/far planes from the
101  // camera of this group node. To ensure that OSG sets it properly, we use these proxies. insert( wge::generateDynamicCullProxy( node ) );
102 
103  return pass;
104 }
105 
106 osg::ref_ptr< WGEOffscreenTexturePass > WGEOffscreenRenderNode::addTextureProcessingPass( std::string name )
107 {
108  osg::ref_ptr< WGEOffscreenTexturePass > pass = addRenderPass< WGEOffscreenTexturePass >( name );
109  return pass;
110 }
111 
112 osg::ref_ptr< WGEOffscreenTexturePass > WGEOffscreenRenderNode::addTextureProcessingPass( osg::ref_ptr< WGEShader > shader, std::string name )
113 {
114  osg::ref_ptr< WGEOffscreenTexturePass > pass = addRenderPass< WGEOffscreenTexturePass >( name );
115  shader->apply( pass );
116  return pass;
117 }
118 
119 osg::ref_ptr< WGEOffscreenFinalPass > WGEOffscreenRenderNode::addFinalOnScreenPass( std::string name )
120 {
121  osg::ref_ptr< WGEOffscreenFinalPass > pass = addRenderPass< WGEOffscreenFinalPass >( name );
122  return pass;
123 }
124 
125 osg::ref_ptr< WGEOffscreenFinalPass > WGEOffscreenRenderNode::addFinalOnScreenPass( osg::ref_ptr< WGEShader > shader, std::string name )
126 {
127  osg::ref_ptr< WGEOffscreenFinalPass > pass = addRenderPass< WGEOffscreenFinalPass >( name );
128  shader->apply( pass );
129  return pass;
130 }
131 
132 osg::ref_ptr< WGETextureHud > WGEOffscreenRenderNode::getTextureHUD() const
133 {
134  return m_hud;
135 }
136 
138 {
139  return m_textureWidth;
140 }
141 
143 {
144  return m_textureHeight;
145 }
146 
148 {
150 }
151 
153 {
155 }
156 
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Definition: WGEGroupNode.h:48
void insert(osg::ref_ptr< osg::Node > node)
Adds the specified node to the child list of this node in a safe manner.
osg::ref_ptr< WGETextureHud > getTextureHUD() const
Returns the instance of the texture HUD.
size_t getTextureWidth() const
Get FBO texture width.
virtual ~WGEOffscreenRenderNode()
Destructor.
size_t m_textureHeight
The height of each texture in this offscreen rendering.
void setLinkViewportToTextureSize(bool vp=true)
If true, the viewport has the size of the resulting texture.
virtual osg::ref_ptr< WGEOffscreenFinalPass > addFinalOnScreenPass(std::string name="Unnamed")
Creates a new render pass which can be seen as put-textures-back-on-screen-pass.
WGEOffscreenRenderNode(osg::ref_ptr< WGECamera > reference, size_t width=2048, size_t height=2048, bool noHud=false)
Create a new managing instance.
osg::ref_ptr< WGECamera > m_referenceCamera
The camera to which is used for setting this camera up.
size_t m_textureWidth
The width of each texture in this offscreen rendering.
osg::ref_ptr< WGETextureHud > m_hud
The pointer to the hud used to render all used texture buffers.
bool m_forceViewportTextureSize
Flag denotes whether the viewport is coupled to the reference camera or the texture size.
virtual osg::ref_ptr< WGEOffscreenRenderPass > addGeometryRenderPass(osg::ref_ptr< osg::Node > node, std::string name="Unnamed")
Creates a new offscreen-render-pass coupled with the reference camera which renders a specified OSG g...
bool getLinkViewportToTextureSize() const
The flag denotes whether the viewport is linked to the texture size or the reference camera.
virtual osg::ref_ptr< WGEOffscreenTexturePass > addTextureProcessingPass(std::string name="Unnamed")
Creates a new offscreen-render-pass coupled with the reference camera which simply processes textures...
size_t getTextureHeight() const
Get FBO texture height.
This class implements a HUD showing several textures on screen.
Definition: WGETextureHud.h:47
This callback is useful to update viewport information on several nodes supporting it.
osg::ref_ptr< osg::Node > generateDynamicCullProxy(osg::ref_ptr< osg::Node > node)
Generate a proxy cube, which ensures OSG does proper near-far plane calculation and culling.
Definition: WGEUtils.cpp:295