forked from graphitemaster/neothyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r_aa.cpp
122 lines (92 loc) · 3.15 KB
/
r_aa.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
#include "r_aa.h"
#include "m_mat.h"
namespace r {
///! aaMethod
bool aaMethod::init(const u::initializer_list<const char *> &defines) {
if (!method::init())
return false;
for (auto &it : defines)
method::define(it);
if (gl::has(gl::ARB_texture_rectangle))
method::define("HAS_TEXTURE_RECTANGLE");
if (!addShader(GL_VERTEX_SHADER, "shaders/fxaa.vs"))
return false;
if (!addShader(GL_FRAGMENT_SHADER, "shaders/fxaa.fs"))
return false;
if (!finalize({ { 0, "position" } }))
return false;
m_WVPLocation = getUniformLocation("gWVP");
m_colorMapLocation = getUniformLocation("gColorMap");
m_screenSizeLocation = getUniformLocation("gScreenSize");
return true;
}
void aaMethod::setWVP(const m::mat4 &wvp) {
gl::UniformMatrix4fv(m_WVPLocation, 1, GL_TRUE, wvp.ptr());
}
void aaMethod::setColorTextureUnit(int unit) {
gl::Uniform1i(m_colorMapLocation, unit);
}
void aaMethod::setPerspective(const m::perspective &p) {
gl::Uniform2f(m_screenSizeLocation, p.width, p.height);
// TODO: frustum in final shader to do other things eventually
//gl::Uniform2f(m_screenFrustumLocation, project.nearp, perspective.farp);
}
///! aa
aa::aa()
: m_fbo(0)
, m_texture(0)
, m_width(0)
, m_height(0)
{
}
aa::~aa() {
destroy();
}
void aa::destroy() {
if (m_fbo)
gl::DeleteFramebuffers(1, &m_fbo);
if (m_texture)
gl::DeleteTextures(1, &m_texture);
}
void aa::update(const m::perspective &p) {
size_t width = p.width;
size_t height = p.height;
if (m_width == width && m_height == height)
return;
GLenum format = gl::has(gl::ARB_texture_rectangle)
? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
m_width = width;
m_height = height;
gl::BindTexture(format, m_texture);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
}
bool aa::init(const m::perspective &p) {
m_width = p.width;
m_height = p.height;
gl::GenFramebuffers(1, &m_fbo);
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
gl::GenTextures(1, &m_texture);
GLenum format = gl::has(gl::ARB_texture_rectangle)
? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
gl::BindTexture(format, m_texture);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
gl::TexParameteri(format, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, format, m_texture, 0);
static GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
gl::DrawBuffers(sizeof(drawBuffers)/sizeof(*drawBuffers), drawBuffers);
GLenum status = gl::CheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
return false;
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
return true;
}
void aa::bindWriting() {
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
}
GLuint aa::texture() const {
return m_texture;
}
}