-
Notifications
You must be signed in to change notification settings - Fork 1
/
FinalBuffer.cpp
125 lines (104 loc) · 3.51 KB
/
FinalBuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "FinalBuffer.h"
#include "DrawFunc.h"
#include <assert.h>
#include "Logger.h"
#include "Profiler.h"
#include "GameState.h"
#include "MatrixManager.h"
#include "ShaderManager.h"
FinalBuffer::FinalBuffer(int nWidth, int nHeight)
{
this->m_nWidth=nWidth;
this->m_nHeight=nHeight;
glEnable(GL_TEXTURE_2D);
glGenFramebuffersEXT(1,&m_nFrameBuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_nFrameBuffer);
glGenTextures(1, &m_nFinalTex);
glBindTexture(GL_TEXTURE_2D, m_nFinalTex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_nWidth, m_nHeight, 0, GL_RGBA, GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_nFinalTex, 0);
// check FbO status
GLenum FBOstatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(FBOstatus != GL_FRAMEBUFFER_COMPLETE_EXT)
{
Logging::GRAPHICS->error("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO. " + FBOstatus);
}
else
{
Logging::GRAPHICS->info("Final Buffer Done");
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
FinalBuffer::~FinalBuffer()
{
glDeleteFramebuffers(1,&m_nFrameBuffer);
}
void FinalBuffer::drawToBuffer(GLuint nColorTex, GLuint nLightTex, GLuint nGlowTex, GLuint indirectTex, GLuint reflectionTex, View *view)
{
Profiler::getInstance()->startProfile("Draw Final");
GLSLProgram *glslProgram = ShaderManager::getInstance()->getShader("Final");
glslProgram->use();
bind();
GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT};
glDrawBuffers(1, mrt);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib( GL_VIEWPORT_BIT );
glViewport( 0, 0, getWidth(), getHeight());
WorldState *worldState = (WorldState *) GameState::GAMESTATE;
MatrixManager::getInstance()->putMatrix4(MODELVIEW, glm::mat4(1.0f));
MatrixManager::getInstance()->putMatrix4(PROJECTION, glm::mat4(1.0f));
view->use3D(false);
glBindFragDataLocation(glslProgram->getHandle(), 0, "finalBuffer");
glBindAttribLocation(glslProgram->getHandle(), 0, "v_vertex");
glBindAttribLocation(glslProgram->getHandle(), 1, "v_texture");
glslProgram->sendUniform("projectionMatrix", &MatrixManager::getInstance()->getMatrix4(PROJECTION)[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, nColorTex);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, nLightTex);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, nGlowTex);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, indirectTex);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, reflectionTex);
glslProgram->sendUniform("colorTex",0);
glslProgram->sendUniform("lightTex",1);
glslProgram->sendUniform("glowTex",2);
glslProgram->sendUniform("indirectTex",3);
glslProgram->sendUniform("reflectionTex",4);
drawScreenShader(0,0,1.0f,1.0f);
glFinish();
glslProgram->disable();
unbind();
Profiler::getInstance()->endProfile();
}
void FinalBuffer::bind()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_nFrameBuffer);
}
void FinalBuffer::unbind()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void FinalBuffer::bindFinalTex()
{
glBindTexture(GL_TEXTURE_2D, m_nFinalTex);
}
GLuint FinalBuffer::getFinalTex()
{
return m_nFinalTex;
}
int FinalBuffer::getWidth()
{
return m_nWidth;
}
int FinalBuffer::getHeight()
{
return m_nHeight;
}