55#include < Renderer/ShaderCache.hpp>
66#include < Renderer/TextureManager.hpp>
77
8+ #include < algorithm>
9+
810namespace libprojectM {
911namespace MilkdropPreset {
1012
1113MotionVectors::MotionVectors (PresetState& presetState)
12- : RenderItem( )
13- , m_presetState(presetState )
14+ : m_presetState(presetState )
15+ , m_motionVectorMesh(Renderer::VertexBufferUsage::StreamDraw )
1416{
15- RenderItem::Init ();
16- }
17-
18- void MotionVectors::InitVertexAttrib ()
19- {
20- glEnableVertexAttribArray (0 );
21- glDisableVertexAttribArray (1 );
22- glEnableVertexAttribArray (2 );
23-
24- glVertexAttribPointer (0 , 2 , GL_FLOAT, GL_FALSE, sizeof (MotionVectorVertex), reinterpret_cast <void *>(offsetof (MotionVectorVertex, x)));
25- glVertexAttribIPointer (2 , 1 , GL_INT, sizeof (MotionVectorVertex), reinterpret_cast <void *>(offsetof (MotionVectorVertex, index)));
17+ m_motionVectorMesh.SetRenderPrimitiveType (Renderer::Mesh::PrimitiveType::Lines);
2618}
2719
2820void MotionVectors::Draw (const PerFrameContext& presetPerFrameContext, std::shared_ptr<Renderer::Texture> motionTexture)
@@ -59,30 +51,16 @@ void MotionVectors::Draw(const PerFrameContext& presetPerFrameContext, std::shar
5951 auto const divertY2 = static_cast <float >(*presetPerFrameContext.mv_dy );
6052
6153 // Clamp X/Y diversions to 0..1
62- if (divertX < 0 .0f )
63- {
64- divertX = 0 .0f ;
65- }
66- if (divertX > 1 .0f )
67- {
68- divertX = 1 .0f ;
69- }
70- if (divertY < 0 .0f )
71- {
72- divertY = 0 .0f ;
73- }
74- if (divertY > 1 .0f )
75- {
76- divertY = 1 .0f ;
77- }
54+ divertX = std::min (1 .0f , std::max (0 .0f , divertX));
55+ divertY = std::min (1 .0f , std::max (0 .0f , divertY));
7856
7957 // Tweaked this a bit to ensure lines are always at least a bit more than 1px long.
8058 // Line smoothing makes some of them disappear otherwise.
8159 float const inverseWidth = 1 .25f / static_cast <float >(m_presetState.renderContext .viewportSizeX );
8260 float const inverseHeight = 1 .25f / static_cast <float >(m_presetState.renderContext .viewportSizeY );
8361 float const minimumLength = sqrtf (inverseWidth * inverseWidth + inverseHeight * inverseHeight);
8462
85- std::vector<MotionVectorVertex> lineVertices (static_cast <std::size_t >(countX + 1 ) * 2 ); // countX + 1 lines for each grid row, 2 vertices each.
63+ m_motionVectorMesh. SetVertexCount (static_cast <std::size_t >(countX + 1 ) * 2 ); // countX + 1 lines for each grid row, 2 vertices each.
8664
8765 glEnable (GL_BLEND);
8866 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -103,14 +81,13 @@ void MotionVectors::Draw(const PerFrameContext& presetPerFrameContext, std::shar
10381 static_cast <float >(*presetPerFrameContext.mv_b ),
10482 static_cast <float >(*presetPerFrameContext.mv_a ));
10583
106- glBindVertexArray (m_vaoID);
107- glBindBuffer (GL_ARRAY_BUFFER, m_vboID);
108-
10984 glLineWidth (1 );
11085#ifndef USE_GLES
11186 glEnable (GL_LINE_SMOOTH);
11287#endif
11388
89+ auto & lineVertices = m_motionVectorMesh.Vertices ();
90+
11491 for (int y = 0 ; y < countY; y++)
11592 {
11693 float const posY = (static_cast <float >(y) + 0 .25f ) / (static_cast <float >(countY) + divertY + 0 .25f - 1 .0f ) - divertY2;
@@ -124,40 +101,25 @@ void MotionVectors::Draw(const PerFrameContext& presetPerFrameContext, std::shar
124101
125102 if (posX > 0 .0001f && posX < 0 .9999f )
126103 {
127- lineVertices[vertex].x = posX;
128- lineVertices[vertex].y = posY;
129- lineVertices[vertex].index = vertex;
130-
131- lineVertices[vertex + 1 ] = lineVertices[vertex];
132- lineVertices[vertex + 1 ].index ++;
104+ lineVertices[vertex + 1 ] = lineVertices[vertex] = {posX, posY};
133105
134106 vertex += 2 ;
135107 }
136108 }
137109
138110 // Draw a row of lines.
139- if (m_lastVertexCount >= vertex)
140- {
141- glBufferSubData (GL_ARRAY_BUFFER, 0 , sizeof (MotionVectorVertex) * vertex, lineVertices.data ());
142- }
143- else
144- {
145- glBufferData (GL_ARRAY_BUFFER, sizeof (MotionVectorVertex) * vertex, lineVertices.data (), GL_STREAM_DRAW);
146- m_lastVertexCount = vertex;
147- }
148- glDrawArrays (GL_LINES, 0 , static_cast <GLsizei>(vertex));
111+ m_motionVectorMesh.Update ();
112+ m_motionVectorMesh.Draw ();
149113 }
150114 }
151115
152- glBindBuffer (GL_ARRAY_BUFFER, 0 );
153- glBindVertexArray ( 0 );
116+ Renderer::Mesh::Unbind ( );
117+ Renderer::Shader::Unbind ( );
154118
155119#ifndef USE_GLES
156120 glDisable (GL_LINE_SMOOTH);
157121#endif
158122
159- Renderer::Shader::Unbind ();
160-
161123 glDisable (GL_BLEND);
162124}
163125
0 commit comments