-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathCopyTexture.cpp
232 lines (179 loc) · 5.93 KB
/
CopyTexture.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "CopyTexture.hpp"
#include <array>
#include <iostream>
namespace libprojectM {
namespace Renderer {
#ifdef USE_GLES
static constexpr char ShaderVersion[] = "#version 300 es\n\n";
#else
static constexpr char ShaderVersion[] = "#version 330\n\n";
#endif
static constexpr char CopyTextureVertexShader[] = R"(
precision mediump float;
layout(location = 0) in vec2 position;
layout(location = 1) in vec2 tex_coord;
out vec2 fragment_tex_coord;
uniform ivec2 flip;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
fragment_tex_coord = tex_coord;
if (flip.x > 0)
{
fragment_tex_coord.s = 1.0 - fragment_tex_coord.s;
}
if (flip.y > 0)
{
fragment_tex_coord.t = 1.0 - fragment_tex_coord.t;
}
}
)";
static constexpr char CopyTextureFragmentShader[] = R"(
precision mediump float;
in vec2 fragment_tex_coord;
uniform sampler2D texture_sampler;
out vec4 color;
void main(){
color = texture(texture_sampler, fragment_tex_coord);
}
)";
CopyTexture::CopyTexture()
{
RenderItem::Init();
m_framebuffer.CreateColorAttachment(0, 0);
std::string vertexShader(static_cast<const char*>(ShaderVersion));
std::string fragmentShader(static_cast<const char*>(ShaderVersion));
vertexShader.append(static_cast<const char*>(CopyTextureVertexShader));
fragmentShader.append(static_cast<const char*>(CopyTextureFragmentShader));
m_shader.CompileProgram(vertexShader, fragmentShader);
}
void CopyTexture::InitVertexAttrib()
{
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast<void*>(offsetof(TexturedPoint, x))); // Position
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast<void*>(offsetof(TexturedPoint, u))); // Texture coordinate
std::array<RenderItem::TexturedPoint, 4> points;
points[0].x = -1.0;
points[0].y = 1.0;
points[1].x = 1.0;
points[1].y = 1.0;
points[2].x = -1.0;
points[2].y = -1.0;
points[3].x = 1.0;
points[3].y = -1.0;
points[0].u = 0.0;
points[0].v = 1.0;
points[1].u = 1.0;
points[1].v = 1.0;
points[2].u = 0.0;
points[2].v = 0.0;
points[3].u = 1.0;
points[3].v = 0.0;
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points.data(), GL_STATIC_DRAW);
}
void CopyTexture::Draw(const std::shared_ptr<class Texture>& originalTexture, bool flipVertical, bool flipHorizontal)
{
if (originalTexture == nullptr)
{
return;
}
// Just bind the texture and draw it to the currently bound buffer.
originalTexture->Bind(0);
Copy(flipVertical, flipHorizontal);
}
void CopyTexture::Draw(const std::shared_ptr<class Texture>& originalTexture, const std::shared_ptr<class Texture>& targetTexture,
bool flipVertical, bool flipHorizontal)
{
if (originalTexture == nullptr ||
originalTexture->Empty() ||
(targetTexture != nullptr && targetTexture->Empty()) ||
originalTexture == targetTexture)
{
return;
}
if (targetTexture == nullptr)
{
UpdateTextureSize(originalTexture->Width(), originalTexture->Height());
}
else
{
UpdateTextureSize(targetTexture->Width(), targetTexture->Height());
}
if (m_width == 0 || m_height == 0)
{
return;
}
std::shared_ptr<class Texture> internalTexture;
m_framebuffer.Bind(0);
// Draw from unflipped texture
originalTexture->Bind(0);
if (targetTexture)
{
internalTexture = m_framebuffer.GetColorAttachmentTexture(0, 0);
m_framebuffer.GetAttachment(0, TextureAttachment::AttachmentType::Color, 0)->Texture(targetTexture);
}
Copy(flipVertical, flipHorizontal);
// Rebind our internal texture.
if (targetTexture)
{
m_framebuffer.GetAttachment(0, TextureAttachment::AttachmentType::Color, 0)->Texture(internalTexture);
}
Framebuffer::Unbind();
}
void CopyTexture::Draw(const std::shared_ptr<class Texture>& originalTexture, Framebuffer& framebuffer, int framebufferIndex,
bool flipVertical, bool flipHorizontal)
{
if (originalTexture == nullptr ||
originalTexture->Empty() ||
framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr ||
framebuffer.GetColorAttachmentTexture(framebufferIndex, 0)->Empty())
{
return;
}
UpdateTextureSize(framebuffer.Width(), framebuffer.Height());
if (m_width == 0 || m_height == 0)
{
return;
}
m_framebuffer.Bind(0);
// Draw from unflipped texture
originalTexture->Bind(0);
Copy(flipVertical, flipHorizontal);
// Swap texture attachments
auto tempAttachment = framebuffer.GetAttachment(framebufferIndex, TextureAttachment::AttachmentType::Color, 0);
framebuffer.RemoveColorAttachment(framebufferIndex, 0);
framebuffer.SetAttachment(framebufferIndex, 0, m_framebuffer.GetAttachment(0, TextureAttachment::AttachmentType::Color, 0));
m_framebuffer.RemoveColorAttachment(0, 0);
m_framebuffer.SetAttachment(0, 0, tempAttachment);
Framebuffer::Unbind();
}
auto CopyTexture::Texture() -> std::shared_ptr<class Texture>
{
return m_framebuffer.GetColorAttachmentTexture(0, 0);
}
void CopyTexture::UpdateTextureSize(int width, int height)
{
if (m_width == width &&
m_height == height)
{
return;
}
m_width = width;
m_height = height;
m_framebuffer.SetSize(m_width, m_height);
}
void CopyTexture::Copy(bool flipVertical, bool flipHorizontal) const
{
m_shader.Bind();
m_shader.SetUniformInt("texture_sampler", 0);
m_shader.SetUniformInt2("flip", {flipHorizontal ? 1 : 0, flipVertical ? 1 : 0});
m_sampler.Bind(0);
glBindVertexArray(m_vaoID);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
Sampler::Unbind(0);
Shader::Unbind();
}
} // namespace Renderer
} // namespace libprojectM