Skip to content

Commit 7df5dca

Browse files
committed
Use Mesh class in Final Composite effect
1 parent e40b7d6 commit 7df5dca

File tree

2 files changed

+61
-68
lines changed

2 files changed

+61
-68
lines changed

src/libprojectM/MilkdropPreset/FinalComposite.cpp

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,23 @@ namespace MilkdropPreset {
1414
static std::string const defaultCompositeShader = "shader_body\n{\nret = tex2D(sampler_main, uv).xyz;\n}";
1515

1616
FinalComposite::FinalComposite()
17+
: m_compositeMesh(Renderer::VertexBufferUsage::StreamDraw, true, true)
1718
{
18-
RenderItem::Init();
19-
}
20-
21-
void FinalComposite::InitVertexAttrib()
22-
{
23-
glGenBuffers(1, &m_elementBuffer);
24-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
19+
m_compositeMesh.SetRenderPrimitiveType(Renderer::Mesh::PrimitiveType::Triangles);
2520

26-
glEnableVertexAttribArray(0);
27-
glEnableVertexAttribArray(1);
28-
glEnableVertexAttribArray(2);
29-
glEnableVertexAttribArray(3);
30-
31-
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), nullptr); // Positions
32-
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), reinterpret_cast<void*>(offsetof(MeshVertex, r))); // Colors
33-
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), reinterpret_cast<void*>(offsetof(MeshVertex, u))); // Textures
34-
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), reinterpret_cast<void*>(offsetof(MeshVertex, radius))); // Radius/Angle
21+
// Add attribute array for radius and angle information to the mesh.
22+
m_compositeMesh.Bind();
23+
m_radiusAngle.Bind();
24+
m_radiusAngle.Resize(vertexCount);
25+
m_radiusAngle.InitializeAttributePointer(3);
26+
Renderer::VertexBuffer<Renderer::Point>::SetEnableAttributeArray(3, true);
3527

3628
// Pre-allocate vertex and index buffers
37-
glBufferData(GL_ARRAY_BUFFER, sizeof(MeshVertex) * vertexCount, m_vertices.data(), GL_STREAM_DRAW);
38-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * m_indices.size(), m_indices.data(), GL_STREAM_DRAW);
29+
m_compositeMesh.SetVertexCount(vertexCount);
30+
m_compositeMesh.Indices().Resize(indexCount);
31+
m_compositeMesh.Update();
32+
33+
Renderer::Mesh::Unbind();
3934
}
4035

4136
void FinalComposite::LoadCompositeShader(const PresetState& presetState)
@@ -58,7 +53,7 @@ void FinalComposite::LoadCompositeShader(const PresetState& presetState)
5853
std::cerr << "[Composite Shader] Error loading composite warp shader code:" << ex.message() << std::endl;
5954
std::cerr << "[Composite Shader] Using fallback shader." << std::endl;
6055
#else
61-
(void)ex; // silence unused parameter warning
56+
(void) ex; // silence unused parameter warning
6257
#endif
6358
// Fall back to default shader
6459
m_compositeShader = std::make_unique<MilkdropShader>(MilkdropShader::ShaderType::CompositeShader);
@@ -104,7 +99,7 @@ void FinalComposite::CompileCompositeShader(PresetState& presetState)
10499
std::cerr << "[Composite Shader] Error compiling composite warp shader code:" << ex.message() << std::endl;
105100
std::cerr << "[Composite Shader] Using fallback shader." << std::endl;
106101
#else
107-
(void)ex; // silence unused parameter warning
102+
(void) ex; // silence unused parameter warning
108103
#endif
109104
// Fall back to default shader
110105
m_compositeShader = std::make_unique<MilkdropShader>(MilkdropShader::ShaderType::CompositeShader);
@@ -123,14 +118,10 @@ void FinalComposite::Draw(const PresetState& presetState, const PerFrameContext&
123118

124119
// Render the grid
125120
glDisable(GL_BLEND);
126-
glBindVertexArray(m_vaoID);
127-
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
128-
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(MeshVertex) * vertexCount, m_vertices.data());
129-
glBindBuffer(GL_ARRAY_BUFFER, 0);
130-
131121
m_compositeShader->LoadVariables(presetState, perFrameContext);
132122

133-
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr);
123+
m_compositeMesh.Draw();
124+
Renderer::Mesh::Unbind();
134125
}
135126
else
136127
{
@@ -142,7 +133,6 @@ void FinalComposite::Draw(const PresetState& presetState, const PerFrameContext&
142133
}
143134
}
144135

145-
glBindVertexArray(0);
146136
Renderer::Shader::Unbind();
147137
}
148138

@@ -167,6 +157,9 @@ void FinalComposite::InitializeMesh(const PresetState& presetState)
167157

168158
constexpr float PI = 3.1415926535898f;
169159

160+
auto& vertices = m_compositeMesh.Vertices();
161+
auto& uvs = m_compositeMesh.UVs();
162+
170163
for (int gridY = 0; gridY < compositeGridHeight; gridY++)
171164
{
172165
int const gridY2 = gridY - gridY / (compositeGridHeight / 2);
@@ -179,13 +172,12 @@ void FinalComposite::InitializeMesh(const PresetState& presetState)
179172
float const u = SquishToCenter(gridX2 * dividedByX, 3.0f);
180173
float const sx = u * 2.0f - 1.0f;
181174

182-
auto& vertex = m_vertices.at(gridX + gridY * compositeGridWidth);
175+
const size_t vertexIndex = gridX + gridY * compositeGridWidth;
183176

184-
vertex.x = sx;
185-
vertex.y = sy;
177+
vertices[vertexIndex] = {sx, sy};
186178

187-
float rad;
188-
float ang;
179+
float rad{};
180+
float ang{};
189181
UvToMathSpace(presetState.renderContext.aspectX,
190182
presetState.renderContext.aspectY,
191183
u, v, rad, ang);
@@ -251,16 +243,16 @@ void FinalComposite::InitializeMesh(const PresetState& presetState)
251243
ang = PI * 0.0f;
252244
}
253245
}
254-
vertex.u = u + halfTexelWidth;
255-
vertex.v = v + halfTexelHeight;
246+
uvs[vertexIndex] = {u + halfTexelWidth,
247+
v + halfTexelHeight};
256248

257-
vertex.radius = rad;
258-
vertex.angle = ang;
249+
m_radiusAngle[vertexIndex] = {rad, ang};
259250
}
260251
}
261252

262253
// build index list for final composite blit -
263254
// order should be friendly for interpolation of 'ang' value!
255+
auto& indices = m_compositeMesh.Indices();
264256
int currentIndex = 0;
265257
for (int gridY = 0; gridY < compositeGridHeight - 1; gridY++)
266258
{
@@ -283,32 +275,30 @@ void FinalComposite::InitializeMesh(const PresetState& presetState)
283275

284276
if ((static_cast<int>(leftHalf) + static_cast<int>(topHalf) + static_cast<int>(center4)) % 2 == 1)
285277
{
286-
m_indices[currentIndex + 0] = gridY * compositeGridWidth + gridX;
287-
m_indices[currentIndex + 1] = gridY * compositeGridWidth + gridX + 1;
288-
m_indices[currentIndex + 2] = (gridY + 1) * compositeGridWidth + gridX + 1;
289-
m_indices[currentIndex + 3] = (gridY + 1) * compositeGridWidth + gridX + 1;
290-
m_indices[currentIndex + 4] = (gridY + 1) * compositeGridWidth + gridX;
291-
m_indices[currentIndex + 5] = gridY * compositeGridWidth + gridX;
278+
indices[currentIndex + 0] = gridY * compositeGridWidth + gridX;
279+
indices[currentIndex + 1] = gridY * compositeGridWidth + gridX + 1;
280+
indices[currentIndex + 2] = (gridY + 1) * compositeGridWidth + gridX + 1;
281+
indices[currentIndex + 3] = (gridY + 1) * compositeGridWidth + gridX + 1;
282+
indices[currentIndex + 4] = (gridY + 1) * compositeGridWidth + gridX;
283+
indices[currentIndex + 5] = gridY * compositeGridWidth + gridX;
292284
}
293285
else
294286
{
295-
m_indices[currentIndex + 0] = (gridY + 1) * compositeGridWidth + (gridX);
296-
m_indices[currentIndex + 1] = (gridY) *compositeGridWidth + (gridX);
297-
m_indices[currentIndex + 2] = (gridY) *compositeGridWidth + (gridX + 1);
298-
m_indices[currentIndex + 3] = (gridY) *compositeGridWidth + (gridX + 1);
299-
m_indices[currentIndex + 4] = (gridY + 1) * compositeGridWidth + (gridX + 1);
300-
m_indices[currentIndex + 5] = (gridY + 1) * compositeGridWidth + (gridX);
287+
indices[currentIndex + 0] = (gridY + 1) * compositeGridWidth + (gridX);
288+
indices[currentIndex + 1] = (gridY) *compositeGridWidth + (gridX);
289+
indices[currentIndex + 2] = (gridY) *compositeGridWidth + (gridX + 1);
290+
indices[currentIndex + 3] = (gridY) *compositeGridWidth + (gridX + 1);
291+
indices[currentIndex + 4] = (gridY + 1) * compositeGridWidth + (gridX + 1);
292+
indices[currentIndex + 5] = (gridY + 1) * compositeGridWidth + (gridX);
301293
}
302294

303295
currentIndex += 6;
304296
}
305297
}
306298

307-
// Store indices.
308-
// ToDo: Probably don't need to store m_indices
309-
glBindVertexArray(m_vaoID);
310-
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int) * m_indices.size(), m_indices.data());
311-
glBindVertexArray(0);
299+
// Update mesh geometry and indices.
300+
m_compositeMesh.Update();
301+
m_radiusAngle.Update();
312302
}
313303

314304
float FinalComposite::SquishToCenter(float x, float exponent)
@@ -366,13 +356,16 @@ void FinalComposite::ApplyHueShaderColors(const PresetState& presetState)
366356
}
367357

368358
// Interpolate and apply to all grid vertices.
359+
auto& vertices = m_compositeMesh.Vertices();
360+
auto& colors = m_compositeMesh.Colors();
361+
369362
for (int gridY = 0; gridY < compositeGridHeight; gridY++)
370363
{
371364
for (int gridX = 0; gridX < compositeGridWidth; gridX++)
372365
{
373-
auto& vertex = m_vertices[gridX + gridY * compositeGridWidth];
374-
float x = vertex.x * 0.5f + 0.5f;
375-
float y = vertex.y * 0.5f + 0.5f;
366+
auto vertexIndex = gridX + gridY * compositeGridWidth;
367+
float x = vertices[vertexIndex].X() * 0.5f + 0.5f;
368+
float y = vertices[vertexIndex].Y() * 0.5f + 0.5f;
376369

377370
std::array<float, 3> color{{1.0f, 1.0f, 1.0f}};
378371
for (int col = 0; col < 3; col++)
@@ -383,12 +376,16 @@ void FinalComposite::ApplyHueShaderColors(const PresetState& presetState)
383376
shade[3][col] * (1 - x) * (1 - y);
384377
}
385378

386-
vertex.r = color[0];
387-
vertex.g = color[1];
388-
vertex.b = color[2];
389-
vertex.a = 1.0f;
379+
colors[vertexIndex] = {color[0],
380+
color[1],
381+
color[2],
382+
1.0f};
390383
}
391384
}
385+
386+
// Only update color buffer.
387+
m_compositeMesh.Bind();
388+
m_compositeMesh.Colors().Update();
392389
}
393390

394391
} // namespace MilkdropPreset

src/libprojectM/MilkdropPreset/FinalComposite.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
#include "MilkdropShader.hpp"
55
#include "VideoEcho.hpp"
66

7-
#include <Renderer/RenderItem.hpp>
7+
#include <Renderer/Mesh.hpp>
88

9-
#include <array>
109
#include <memory>
1110

1211
namespace libprojectM {
@@ -15,13 +14,11 @@ namespace MilkdropPreset {
1514
/**
1615
* @brief Draws the final composite effect, either a shader or Milkdrop 1 effects.
1716
*/
18-
class FinalComposite : public Renderer::RenderItem
17+
class FinalComposite
1918
{
2019
public:
2120
FinalComposite();
2221

23-
void InitVertexAttrib() override;
24-
2522
/**
2623
* @brief Loads the composite shader, if the preset uses one.
2724
* @param presetState The preset state to retrieve the shader from.
@@ -90,9 +87,8 @@ class FinalComposite : public Renderer::RenderItem
9087
static constexpr int vertexCount{compositeGridWidth * compositeGridHeight};
9188
static constexpr int indexCount{(compositeGridWidth - 2) * (compositeGridHeight - 2) * 6};
9289

93-
GLuint m_elementBuffer{}; //!< Element buffer holding the draw indices.
94-
std::array<MeshVertex, vertexCount> m_vertices{}; //!< Composite grid vertices
95-
std::array<int, indexCount> m_indices{}; //!< Composite grid draw indices
90+
Renderer::Mesh m_compositeMesh; //!< The composite shader mesh.
91+
Renderer::VertexBuffer<Renderer::Point> m_radiusAngle; //!< Additional vertex attribute array for radius and angle.
9692

9793
int m_viewportWidth{}; //!< Last known viewport width.
9894
int m_viewportHeight{}; //!< Last known viewport height.

0 commit comments

Comments
 (0)