Skip to content

Commit 0ad258d

Browse files
committed
Use Mesh class in CopyTexture class
1 parent 3e510ad commit 0ad258d

File tree

2 files changed

+39
-59
lines changed

2 files changed

+39
-59
lines changed

src/libprojectM/Renderer/CopyTexture.cpp

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "CopyTexture.hpp"
22

3-
#include <array>
4-
#include <iostream>
5-
63
namespace libprojectM {
74
namespace Renderer {
85

@@ -16,7 +13,7 @@ static constexpr char CopyTextureVertexShader[] = R"(
1613
precision mediump float;
1714
1815
layout(location = 0) in vec2 position;
19-
layout(location = 1) in vec2 tex_coord;
16+
layout(location = 2) in vec2 tex_coord;
2017
2118
out vec2 fragment_tex_coord;
2219
@@ -52,48 +49,33 @@ void main(){
5249
)";
5350

5451
CopyTexture::CopyTexture()
52+
: m_mesh(VertexBufferUsage::StaticDraw, false, true)
5553
{
56-
RenderItem::Init();
57-
5854
m_framebuffer.CreateColorAttachment(0, 0);
5955

60-
std::string vertexShader(static_cast<const char*>(ShaderVersion));
61-
std::string fragmentShader(static_cast<const char*>(ShaderVersion));
62-
vertexShader.append(static_cast<const char*>(CopyTextureVertexShader));
63-
fragmentShader.append(static_cast<const char*>(CopyTextureFragmentShader));
56+
std::string vertexShader(ShaderVersion);
57+
std::string fragmentShader(ShaderVersion);
58+
vertexShader.append(CopyTextureVertexShader);
59+
fragmentShader.append(CopyTextureFragmentShader);
6460

6561
m_shader.CompileProgram(vertexShader, fragmentShader);
66-
}
6762

68-
void CopyTexture::InitVertexAttrib()
69-
{
70-
glEnableVertexAttribArray(0);
71-
glEnableVertexAttribArray(1);
72-
73-
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast<void*>(offsetof(TexturedPoint, x))); // Position
74-
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast<void*>(offsetof(TexturedPoint, u))); // Texture coordinate
75-
76-
std::array<RenderItem::TexturedPoint, 4> points;
77-
78-
points[0].x = -1.0;
79-
points[0].y = 1.0;
80-
points[1].x = 1.0;
81-
points[1].y = 1.0;
82-
points[2].x = -1.0;
83-
points[2].y = -1.0;
84-
points[3].x = 1.0;
85-
points[3].y = -1.0;
86-
87-
points[0].u = 0.0;
88-
points[0].v = 1.0;
89-
points[1].u = 1.0;
90-
points[1].v = 1.0;
91-
points[2].u = 0.0;
92-
points[2].v = 0.0;
93-
points[3].u = 1.0;
94-
points[3].v = 0.0;
95-
96-
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points.data(), GL_STATIC_DRAW);
63+
m_mesh.SetRenderPrimitiveType(Mesh::PrimitiveType::TriangleStrip);
64+
65+
m_mesh.SetVertexCount(4);
66+
m_mesh.Vertices().Set({{-1.0, 1.0},
67+
{1.0, 1.0},
68+
{-1.0, -1.0},
69+
{1.0, -1.0}});
70+
71+
m_mesh.UVs().Set({{0.0, 1.0},
72+
{1.0, 1.0},
73+
{0.0, 0.0},
74+
{1.0, 0.0}});
75+
76+
m_mesh.Indices().Set({0, 1, 2, 3});
77+
78+
m_mesh.Update();
9779
}
9880

9981
void CopyTexture::Draw(const std::shared_ptr<class Texture>& originalTexture, bool flipVertical, bool flipHorizontal)
@@ -160,9 +142,9 @@ void CopyTexture::Draw(const std::shared_ptr<class Texture>& originalTexture, co
160142
void CopyTexture::Draw(const std::shared_ptr<class Texture>& originalTexture, Framebuffer& framebuffer, int framebufferIndex,
161143
bool flipVertical, bool flipHorizontal)
162144
{
163-
if (originalTexture == nullptr
164-
|| originalTexture->Empty()
165-
|| framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr
145+
if (originalTexture == nullptr //
146+
|| originalTexture->Empty() //
147+
|| framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr //
166148
|| framebuffer.GetColorAttachmentTexture(framebufferIndex, 0)->Empty())
167149
{
168150
return;
@@ -211,19 +193,18 @@ void CopyTexture::UpdateTextureSize(int width, int height)
211193
m_framebuffer.SetSize(m_width, m_height);
212194
}
213195

214-
void CopyTexture::Copy(bool flipVertical, bool flipHorizontal) const
196+
void CopyTexture::Copy(bool flipVertical, bool flipHorizontal)
215197
{
216198
m_shader.Bind();
217199
m_shader.SetUniformInt("texture_sampler", 0);
218200
m_shader.SetUniformInt2("flip", {flipHorizontal ? 1 : 0, flipVertical ? 1 : 0});
219201

220202
m_sampler.Bind(0);
221203

222-
glBindVertexArray(m_vaoID);
223-
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
224-
glBindVertexArray(0);
204+
m_mesh.Draw();
225205

226206
glBindTexture(GL_TEXTURE_2D, 0);
207+
Mesh::Unbind();
227208
Sampler::Unbind(0);
228209
Shader::Unbind();
229210
}

src/libprojectM/Renderer/CopyTexture.hpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "Renderer/Framebuffer.hpp"
4-
#include "Renderer/RenderItem.hpp"
4+
#include "Renderer/Mesh.hpp"
55
#include "Renderer/Shader.hpp"
66

77
namespace libprojectM {
@@ -13,18 +13,16 @@ namespace Renderer {
1313
*
1414
* Optionally flips the image horizontally or vertically during the copy operation.
1515
*/
16-
class CopyTexture : public RenderItem
16+
class CopyTexture
1717
{
1818
public:
1919
CopyTexture();
2020

21-
void InitVertexAttrib() override;
22-
2321
/**
2422
* @brief Copies the original texture into the currently bound framebuffer.
2523
* @param originalTexture The texture to be copied.
26-
* @param flipVertical Flip image on the y axis when copying.
27-
* @param flipHorizontal Flip image on the x axis when copying.
24+
* @param flipVertical Flip image on the y-axis when copying.
25+
* @param flipHorizontal Flip image on the x-axis when copying.
2826
*/
2927
void Draw(const std::shared_ptr<class Texture>& originalTexture,
3028
bool flipVertical = false, bool flipHorizontal = false);
@@ -34,8 +32,8 @@ class CopyTexture : public RenderItem
3432
* The original and target textures must not be the same.
3533
* @param originalTexture The texture to be copied.
3634
* @param targetTexture Optional target texture to draw onto.
37-
* @param flipVertical Flip image on the y axis when copying.
38-
* @param flipHorizontal Flip image on the x axis when copying.
35+
* @param flipVertical Flip image on the y-axis when copying.
36+
* @param flipHorizontal Flip image on the x-axis when copying.
3937
*/
4038
void Draw(const std::shared_ptr<class Texture>& originalTexture, const std::shared_ptr<class Texture>& targetTexture = {},
4139
bool flipVertical = false, bool flipHorizontal = false);
@@ -47,8 +45,8 @@ class CopyTexture : public RenderItem
4745
* @param originalTexture The texture to be copied.
4846
* @param targetFramebuffer Optional target texture to draw onto.
4947
* @param framebufferIndex The index of the framebuffer to use.
50-
* @param flipVertical Flip image on the y axis when copying.
51-
* @param flipHorizontal Flip image on the x axis when copying.
48+
* @param flipVertical Flip image on the y-axis when copying.
49+
* @param flipHorizontal Flip image on the x-axis when copying.
5250
*/
5351
void Draw(const std::shared_ptr<class Texture>& originalTexture, Framebuffer& framebuffer, int framebufferIndex,
5452
bool flipVertical = false, bool flipHorizontal = false);
@@ -58,16 +56,17 @@ class CopyTexture : public RenderItem
5856
*
5957
* @return The flipped texture.
6058
*/
61-
auto Texture() -> std::shared_ptr<class Texture>;
59+
auto Texture() -> std::shared_ptr<Texture>;
6260

6361
private:
6462
/**
6563
* Updates the mesh
6664
*/
6765
void UpdateTextureSize(int width, int height);
6866

69-
void Copy(bool flipVertical, bool flipHorizontal) const;
67+
void Copy(bool flipVertical, bool flipHorizontal);
7068

69+
Mesh m_mesh;
7170
Shader m_shader; //!< Simple textured shader
7271
Framebuffer m_framebuffer{1}; //!< Framebuffer for drawing the flipped texture
7372
Sampler m_sampler{GL_CLAMP_TO_EDGE, GL_NEAREST}; //!< Texture sampler settings

0 commit comments

Comments
 (0)