11#include " CopyTexture.hpp"
22
3- #include < array>
4- #include < iostream>
5-
63namespace libprojectM {
74namespace Renderer {
85
@@ -16,7 +13,7 @@ static constexpr char CopyTextureVertexShader[] = R"(
1613precision mediump float;
1714
1815layout(location = 0) in vec2 position;
19- layout(location = 1 ) in vec2 tex_coord;
16+ layout(location = 2 ) in vec2 tex_coord;
2017
2118out vec2 fragment_tex_coord;
2219
@@ -52,48 +49,33 @@ void main(){
5249)" ;
5350
5451CopyTexture::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
9981void 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
160142void 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}
0 commit comments