OpenBarnyard
 
Loading...
Searching...
No Matches
T2FrameBuffer_GL.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "T2FrameBuffer_GL.h"
3#include "T2Render_GL.h"
4
5//-----------------------------------------------------------------------------
6// Enables memory debugging.
7// Note: Should be the last include!
8//-----------------------------------------------------------------------------
10
12
13T2FrameBuffer::T2FrameBuffer()
14{
15 m_uiFBO = 0;
16
17 for ( TINT i = 0; i < TARRAYSIZE( m_aAttachments ); i++ )
18 {
19 m_aAttachments[ i ] = 0;
20 }
21}
22
23T2FrameBuffer::~T2FrameBuffer()
24{
25 Destroy();
26}
27
28void T2FrameBuffer::Create()
29{
30 TASSERT( m_uiFBO == 0 );
31 TASSERT( !IsCreated() );
32
33 glGenFramebuffers( 1, &m_uiFBO );
34}
35
36GLuint g_uiBoundFBO = 0;
37
38void T2FrameBuffer::Bind()
39{
40 TASSERT( IsCreated() );
41
42 if ( g_uiBoundFBO != m_uiFBO )
43 {
44 g_uiBoundFBO = m_uiFBO;
45 glBindFramebuffer( GL_FRAMEBUFFER, m_uiFBO );
46 }
47}
48
49void T2FrameBuffer::Unbind()
50{
51 if ( g_uiBoundFBO != 0 )
52 {
53 g_uiBoundFBO = 0;
54 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
55 }
56}
57
58void T2FrameBuffer::Destroy()
59{
60 if ( m_uiFBO != 0 )
61 {
62 glDeleteFramebuffers( 1, &m_uiFBO );
63 m_uiFBO = 0;
64
65 for ( TINT i = 0; i < TARRAYSIZE( m_aAttachments ); i++ )
66 {
67 if ( m_aAttachments[ i ] != 0 )
68 {
69 T2Render::DestroyTexture( m_aAttachments[ i ] );
70 m_aAttachments[ i ] = 0;
71 }
72 }
73
74 if ( m_uiDepthTexture != 0 )
75 {
76 T2Render::DestroyTexture( m_uiDepthTexture );
77 m_uiDepthTexture = 0;
78 }
79 }
80}
81
82void T2FrameBuffer::CreateDepthTexture( GLsizei a_iWidth, GLsizei a_iHeight )
83{
84 TASSERT( IsCreated() );
85 TASSERT( a_iWidth > 0 );
86 TASSERT( a_iHeight > 0 );
87 TASSERT( m_uiDepthTexture == 0 );
88
89 T2FrameBuffer::Bind();
90 GLuint uiTexture = T2Render::CreateTexture( a_iWidth, a_iHeight, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT, TFALSE, TNULL );
91 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
92 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
93
94 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
95 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
96
97 TFLOAT borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
98 glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );
99
100 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, uiTexture, 0 );
101 m_uiDepthTexture = uiTexture;
102}
103
104void T2FrameBuffer::CreateAttachment( TINT a_iAttachment, GLsizei a_iWidth, GLsizei a_iHeight, GLenum a_eInternalFormat, GLenum a_eFormat, GLenum a_ePixelType )
105{
106 TASSERT( IsCreated() );
107 TASSERT( a_iWidth > 0 );
108 TASSERT( a_iHeight > 0 );
109 TASSERT( m_aAttachments[ a_iAttachment ] == 0 );
110
111 T2FrameBuffer::Bind();
112 GLuint uiTexture = T2Render::CreateTexture( a_iWidth, a_iHeight, a_eInternalFormat, a_eFormat, a_ePixelType, TFALSE, TNULL );
113 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
114 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
115
116 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + a_iAttachment, GL_TEXTURE_2D, uiTexture, 0 );
117 m_aAttachments[ a_iAttachment ] = uiTexture;
118}
119
120void T2FrameBuffer::SetDrawBuffer( GLenum a_eDrawBuffer )
121{
122 T2FrameBuffer::Bind();
123 glDrawBuffer( GL_NONE );
124 glReadBuffer( GL_NONE );
125}
126
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TARRAYSIZE(ARRAY)
Definition Defines.h:70
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
float TFLOAT
Definition Typedefs.h:4
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
GLuint g_uiBoundFBO