RenderTarget.cpp
Go to the documentation of this file.
1 #include "RenderTarget.hpp"
2 
3 #include <ivorium_config.hpp>
4 #include "gl.h"
5 
6 namespace iv
7 {
8 
10  size( 0, 0 ),
11  density( 1.0 )
12 {
13 }
14 
15 RenderTarget::Geometry::Geometry( float2 size, float density ) :
16  size( size ),
17  density( density )
18 {
19 }
20 
22  _geometry(),
23  _texture_unit( 0 ),
24  _texture_ids(),
25  _program_id( 0 )
26 {
27  this->_texture_ids.fill( 0 );
28 }
29 
31 {
32  // depth buffer
33  this->ClearDepthBuffer();
34  this->DepthBufferMode( GL_ALWAYS, false );
35 
36  // state control
37  glActiveTexture( GL_TEXTURE0 );
38  this->_texture_unit = 0;
39  this->_texture_ids.fill( 0 );
40  this->_program_id = 0;
41 }
42 
44 {
45 }
46 
48 {
49  this->_geometry = geometry;
50 }
51 
53 {
54  return this->_geometry;
55 }
56 
57 void RenderTarget::bind_texture( int texture_unit, GLuint texture_id )
58 {
59  if( texture_unit >= MaxTexturingUnits )
60  return;
61 
62  if( this->_texture_unit != texture_unit )
63  {
64  this->_texture_unit = texture_unit;
65  glActiveTexture( GL_TEXTURE0 + texture_unit );
66  }
67 
68  if( this->_texture_ids[ texture_unit ] != texture_id )
69  {
70  this->_texture_ids[ texture_unit ] = texture_id;
71  glBindTexture( GL_TEXTURE_2D, texture_id );
72  }
73 }
74 
75 void RenderTarget::bind_shader( GLuint program_id )
76 {
77  if( this->_program_id != program_id )
78  this->_program_id = program_id,
79  glUseProgram( program_id );
80 }
81 
82 void RenderTarget::DepthBufferClear( float value )
83 {
84  #if IV_GLPLATFORM_GLFW
85  glClearDepth( value );
86  #elif IV_GLPLATFORM_GLFM
87  glClearDepthf( value );
88  #else
89  #error "Unknown OpenGL platform."
90  #endif
91  glClear( GL_DEPTH_BUFFER_BIT );
92 }
93 
94 void RenderTarget::DepthBufferMode( GLenum test_condition, bool write_enabled )
95 {
96  if( test_condition == GL_ALWAYS && !write_enabled )
97  {
98  glDisable( GL_DEPTH_TEST );
99  }
100  else
101  {
102  glEnable( GL_DEPTH_TEST );
103 
104  glDepthFunc( test_condition );
105 
106  if( write_enabled )
107  glDepthMask( GL_TRUE );
108  else
109  glDepthMask( GL_FALSE );
110  }
111 }
112 
113 void RenderTarget::Blending( bool enabled )
114 {
115  if( enabled )
116  glEnable( GL_BLEND );
117  else
118  glDisable( GL_BLEND );
119 }
120 
121 void RenderTarget::ClearDepthBuffer()
122 {
123  glClear( GL_DEPTH_BUFFER_BIT );
124 }
125 
126 }