forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilters.cpp
121 lines (98 loc) · 2.86 KB
/
Filters.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
#include "Filters.hpp"
#include "projectM-opengl.h"
Filters::Filters(const PresetState& presetState)
: RenderItem()
, m_presetState(presetState)
{
RenderItem::Init();
}
void Filters::InitVertexAttrib()
{
glEnableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Point), reinterpret_cast<void*>(offsetof(Point, x)));
}
void Filters::Draw()
{
UpdateMesh();
if (m_viewportWidth == 0 || m_viewportHeight == 0)
{
return;
}
glEnable(GL_BLEND);
m_presetState.untexturedShader.Bind();
m_presetState.untexturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection);
glBindVertexArray(m_vaoID);
glVertexAttrib4f(1, 1.0, 1.0, 1.0, 1.0);
if (m_presetState.brighten)
{
Brighten();
}
if (m_presetState.darken)
{
Darken();
}
if (m_presetState.solarize)
{
Solarize();
}
if (m_presetState.invert)
{
Invert();
}
glBindVertexArray(0);
Shader::Unbind();
glDisable(GL_BLEND);
}
void Filters::Brighten()
{
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBlendFunc(GL_ZERO, GL_DST_COLOR);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void Filters::Darken()
{
glBlendFunc(GL_ZERO, GL_DST_COLOR);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void Filters::Solarize()
{
glBlendFunc(GL_ZERO, GL_ONE_MINUS_DST_COLOR);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBlendFunc(GL_DST_COLOR, GL_ONE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void Filters::Invert()
{
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void Filters::UpdateMesh()
{
if (m_viewportWidth == m_presetState.renderContext.viewportSizeX &&
m_viewportHeight == m_presetState.renderContext.viewportSizeY)
{
return;
}
m_viewportWidth = m_presetState.renderContext.viewportSizeX;
m_viewportHeight = m_presetState.renderContext.viewportSizeY;
std::array<RenderItem::Point, 4> points;
float const fOnePlusInvWidth = 1.0f + 1.0f / static_cast<float>(m_viewportWidth);
float const fOnePlusInvHeight = 1.0f + 1.0f / static_cast<float>(m_viewportHeight);
points[0].x = -fOnePlusInvWidth;
points[1].x = fOnePlusInvWidth;
points[2].x = -fOnePlusInvWidth;
points[3].x = fOnePlusInvWidth;
points[0].y = fOnePlusInvHeight;
points[1].y = fOnePlusInvHeight;
points[2].y = -fOnePlusInvHeight;
points[3].y = -fOnePlusInvHeight;
glBindVertexArray(m_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}