FixedOrder_Camera.cpp
Go to the documentation of this file.
1 #include "FixedOrder_Camera.hpp"
2 #include "../Defs.hpp"
3 
4 namespace iv
5 {
6 
7 bool FixedOrder_Camera::RenderIdComparator::operator()( RenderId const & left, RenderId const & right ) const
8 {
9  return std::tie( left.shader_id, left.primary_texture_id, left.renderable ) < std::tie( right.shader_id, right.primary_texture_id, right.renderable );
10 }
11 
13  Camera( inst, geometry ),
14  cm( inst, this, "FixedOrder_Camera" ),
15  _depth_next( 0 )
16 {
17  this->cm.inherits( this->Camera::cm );
18 }
19 
21 {
22  this->translucents.clear();
23  this->_depth_next = 0;
24 }
25 
26 void FixedOrder_Camera::AddRenderable_Solid( Renderable * renderable, GLuint shader_id, GLuint primary_texture_id )
27 {
28  this->cm.log( SRC_INFO, Defs::Log::FixedOrderCamera, "Add solid (depth ",this->_depth_next++,")." );
29 
30  RenderId id;
31  id.renderable = renderable;
32  id.shader_id = shader_id;
33  id.primary_texture_id = primary_texture_id;
34 
35  SolidInfo info;
36  info.frame_id = this->instance()->frame_id();
37  info.depth = this->_depth_next++;
38 
39  this->solids[ id ] = info;
40 }
41 
42 void FixedOrder_Camera::AddRenderable_Translucent( Renderable * renderable, GLuint shader_id, GLuint primary_texture_id )
43 {
44  this->cm.log( SRC_INFO, Defs::Log::FixedOrderCamera, "Add translucent (depth ",this->_depth_next++,")." );
45 
46  RenderId id;
47  id.renderable = renderable;
48  id.shader_id = shader_id;
49  id.primary_texture_id = primary_texture_id;
50 
51  TranslucentInfo info;
52  info.id = id;
53  info.depth = this->_depth_next++;
54 
55  this->translucents.push_back( info );
56 }
57 
59 {
60  //
61  auto frame_id = this->instance()->frame_id();
62  RenderTarget * target = GlInfo( this->instance() ).render_target();
63 
64  // stats
65  int solid_throws = 0;
66  int solid_renders = 0;
67  int translucent_renders = 0;
68  int shader_switches = 0;
69  int texture_switches = 0;
70  RenderId previous_id;
71 
72  // render solid targets
73  {
74  // enable depth test
75  target->DepthBufferMode( GL_GEQUAL, true );
76  target->DepthBufferClear( 0.0f );
77  target->Blending( false );
78 
79  //
80  this->cm.log( SRC_INFO, Defs::Log::FixedOrderCamera, "Render ", this->solids.size(), " solids." );
81 
82  // render
83  auto it = this->solids.begin();
84  while( it != this->solids.end() )
85  {
86  RenderId const & id = it->first;
87  SolidInfo const & info = it->second;
88 
89  // throw away obsolete target
90  if( info.frame_id != frame_id )
91  {
92  //
93  solid_throws++;
94 
95  //
96  auto todel = it;
97  ++it;
98  this->solids.erase( todel );
99  continue;
100  }
101 
102  // check context switches
103  if( id.shader_id != previous_id.shader_id )
104  shader_switches++;
105  if( id.primary_texture_id != previous_id.primary_texture_id )
106  texture_switches++;
107  previous_id = id;
108 
109  // render
110  solid_renders++;
111  id.renderable->render( camera, float( info.depth + 1 ) / float( this->_depth_next + 2 ) );
112 
113  //
114  ++it;
115  }
116  }
117 
118  // render translucent targets
119  {
120  // disable depth test
121  target->DepthBufferMode( GL_GEQUAL, false );
122  target->Blending( true );
123 
124  //
125  this->cm.log( SRC_INFO, Defs::Log::FixedOrderCamera, "Render ", this->translucents.size(), " translucents." );
126 
127  // render
128  for( TranslucentInfo const & info : this->translucents )
129  {
130  RenderId const & id = info.id;
131 
132  // check context switches
133  if( id.shader_id != previous_id.shader_id )
134  shader_switches++;
135  if( id.primary_texture_id != previous_id.primary_texture_id )
136  texture_switches++;
137  previous_id = id;
138 
139  // render
140  id.renderable->render( camera, float( info.depth + 1 ) / float( this->_depth_next + 2 ) );
141  translucent_renders++;
142  }
143  }
144 
145  // print stats
146  this->cm.log( SRC_INFO, Defs::Log::ElementRenderSummary, solid_throws, " solids removed, ", solid_renders, " solid renders, ", translucent_renders, " translucent renders, ", shader_switches, " shader switches, ", texture_switches, " texture switches" );
147 }
148 
149 }