-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFbo.cpp
140 lines (115 loc) · 3.42 KB
/
Fbo.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Fbo.cpp
// CSE167-Final
//
// Created by Freddy Sauceda on 12/5/16.
// Copyright © 2016 Alfredo Sauceda. All rights reserved.
//
#include "Fbo.h"
FBO::FBO(){
fboRefl = initRefl();
checkErrors(fboRefl);
fboRefr = initRefr();
checkErrors(fboRefr);
}
void FBO::checkErrors(GLuint fbo){
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
//Check for erros
GLenum e = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
if (e != GL_FRAMEBUFFER_COMPLETE)
printf("%x\n",e);
else
printf("Gucci\n");
glBindFramebuffer(GL_FRAMEBUFFER, 0); //Return to default fbo
}
GLuint FBO::initRefl(){
GLuint fbo;
//Create a fbo
glGenFramebuffers(1, &fbo);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
//Create attachments
reflTex = createTexture();
reflDepthBuffer = createDepthBuff();
return fbo;
}
GLuint FBO::initRefr(){
GLuint fbo;
//Create a fbo
glGenFramebuffers(1, &fbo);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
//Create attachments
refrTex = createTexture();
refrDepthTexture = createDepth();
return fbo;
}
GLuint FBO::createTexture(){
GLuint tex;
//Create texture
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Window::width, Window::height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
//Filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Attach
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0);
return tex;
}
GLuint FBO::createDepth(){
GLuint depth;
//Create texture
glGenTextures(1, &depth);
glBindTexture(GL_TEXTURE_2D, depth);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, Window::width, Window::height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
//Filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Attach
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth, 0);
return depth;
}
GLuint FBO::createDepthBuff(){
GLuint depthBuff;
//Create buffer
glGenRenderbuffers(1, &depthBuff);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, Window::width, Window::height);
//Attach with
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuff);
return depthBuff;
}
void FBO::bind(GLuint framebuffer){
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glViewport(0, 0, Window::width, Window::height);
GLenum drawBuffs = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, &drawBuffs);
//Enable alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void FBO::unbind(){
// glDisable(GL_BLEND);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, Window::width, Window::height);
}
GLuint FBO::getReflectionFBO(){
return fboRefl;
}
GLuint FBO::getRefractionFBO(){
return fboRefr;
}
GLuint FBO::getReflTex(){
return reflTex;
}
GLuint FBO::getRefrTex(){
return refrTex;
}
GLuint FBO::getRefrDepth(){
return refrDepthTexture;
}
GLuint FBO::getReflDepth(){
return reflDepthBuffer;
}