OpenWalnut  1.3.1
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 
31 #include "WGEOffscreenRenderNode.h"
32 
33 bool isPowerOfTwo( size_t x )
34 {
35  return ( (x != 0 ) && ( (x & ( ~x + 1 ) ) == x ) );
36 }
37 
38 bool checkTextureSize( size_t size )
39 {
40  return !( ( size > 4096 ) || ( size < 8 ) || !isPowerOfTwo( size ) );
41 }
42 
43 WGEOffscreenRenderNode::WGEOffscreenRenderNode( osg::ref_ptr< osg::Camera > reference, size_t width, size_t height, bool noHud ):
44  WGEGroupNode(),
45  m_referenceCamera( reference ),
46  m_hud(),
47  m_textureWidth( width ),
48  m_textureHeight( height ),
49  m_nextPassNum( 0 )
50 {
51  WAssert( checkTextureSize( width ) && checkTextureSize( height ), "Invalid offscreen texture size. Must be power of two and in [8,4096]." );
52 
53  // initialize members
54  m_hud = new WGETextureHud();
55  if( !noHud )
56  {
58  m_hud->coupleViewportWithTextureViewport();
59  insert( m_hud );
60  }
61 }
62 
64 {
65  // cleanup
66 }
67 
68 osg::ref_ptr< WGEOffscreenRenderPass > WGEOffscreenRenderNode::addGeometryRenderPass( osg::ref_ptr< osg::Node > node, std::string name )
69 {
70  // create a plain render pass and add some geometry
71  osg::ref_ptr< WGEOffscreenRenderPass > pass = addRenderPass< WGEOffscreenRenderPass >( name );
72  pass->addChild( node );
73  return pass;
74 }
75 
76 osg::ref_ptr< WGEOffscreenRenderPass > WGEOffscreenRenderNode::addGeometryRenderPass( osg::ref_ptr< osg::Node > node,
77  osg::ref_ptr< WGEShader > shader,
78  std::string name )
79 {
80  // create a plain render pass and add some geometry
81  osg::ref_ptr< WGEOffscreenRenderPass > pass = addRenderPass< WGEOffscreenRenderPass >( name );
82  pass->addChild( node );
83  shader->apply( pass );
84  return pass;
85 }
86 
87 osg::ref_ptr< WGEOffscreenTexturePass > WGEOffscreenRenderNode::addTextureProcessingPass( std::string name )
88 {
89  osg::ref_ptr< WGEOffscreenTexturePass > pass = addRenderPass< WGEOffscreenTexturePass >( name );
90  return pass;
91 }
92 
93 osg::ref_ptr< WGEOffscreenTexturePass > WGEOffscreenRenderNode::addTextureProcessingPass( osg::ref_ptr< WGEShader > shader, std::string name )
94 {
95  osg::ref_ptr< WGEOffscreenTexturePass > pass = addRenderPass< WGEOffscreenTexturePass >( name );
96  shader->apply( pass );
97  return pass;
98 }
99 
100 osg::ref_ptr< WGEOffscreenFinalPass > WGEOffscreenRenderNode::addFinalOnScreenPass( std::string name )
101 {
102  osg::ref_ptr< WGEOffscreenFinalPass > pass = addRenderPass< WGEOffscreenFinalPass >( name );
103  return pass;
104 }
105 
106 osg::ref_ptr< WGEOffscreenFinalPass > WGEOffscreenRenderNode::addFinalOnScreenPass( osg::ref_ptr< WGEShader > shader, std::string name )
107 {
108  osg::ref_ptr< WGEOffscreenFinalPass > pass = addRenderPass< WGEOffscreenFinalPass >( name );
109  shader->apply( pass );
110  return pass;
111 }
112 
113 osg::ref_ptr< WGETextureHud > WGEOffscreenRenderNode::getTextureHUD() const
114 {
115  return m_hud;
116 }
117