@@ -15,13 +15,6 @@ static const double pi = std::acos(-1); // TODO: Use std::numbers::pi in C++20
1515
1616std::unordered_map<libscratchcpp::IEngine *, IPenLayer *> PenLayer::m_projectPenLayers;
1717
18- // TODO: Move this to a separate class
19- template <typename T>
20- short sgn (T x)
21- {
22- return (T (0 ) < x) - (x < T (0 ));
23- }
24-
2518PenLayer::PenLayer (QNanoQuickItem *parent) :
2619 IPenLayer(parent)
2720{
@@ -219,101 +212,17 @@ void PenLayer::stamp(IRenderedTarget *target)
219212
220213 Q_ASSERT (m_fbo->isBound ());
221214
222- const float stageWidth = m_engine->stageWidth () * m_scale;
223- const float stageHeight = m_engine->stageHeight () * m_scale;
224-
225- libscratchcpp::Rect bounds = target->getFastBounds ();
226- bounds.snapToInt ();
227-
228- if (!bounds.intersects (libscratchcpp::Rect (-stageWidth / 2 , stageHeight / 2 , stageWidth / 2 , -stageHeight / 2 )))
229- return ;
230-
231- float angle = 180 ;
232- float scaleX = 1 ;
233- float scaleY = 1 ;
234-
235- SpriteModel *spriteModel = target->spriteModel ();
236-
237- if (spriteModel) {
238- libscratchcpp::Sprite *sprite = spriteModel->sprite ();
239-
240- switch (sprite->rotationStyle ()) {
241- case libscratchcpp::Sprite::RotationStyle::AllAround:
242- angle = 270 - sprite->direction ();
243- break ;
244-
245- case libscratchcpp::Sprite::RotationStyle::LeftRight:
246- scaleX = sgn (sprite->direction ());
247- break ;
248-
249- default :
250- break ;
251- }
252-
253- scaleY = sprite->size () / 100 ;
254- scaleX *= scaleY;
255- }
256-
257- scaleX *= m_scale;
258- scaleY *= m_scale;
259-
260- const Texture &texture = target->cpuTexture ();
261-
262- if (!texture.isValid ())
263- return ;
264-
265- const float textureScale = texture.width () / static_cast <float >(target->costumeWidth ());
266- const float skinWidth = texture.width ();
267- const float skinHeight = texture.height ();
268-
269- // Projection matrix
270- QMatrix4x4 projectionMatrix;
271- const float aspectRatio = skinHeight / skinWidth;
272- projectionMatrix.ortho (1 .0f , -1 .0f , aspectRatio, -aspectRatio, 0 .1f , 0 .0f );
273- projectionMatrix.scale (skinWidth / bounds.width () / m_scale, skinHeight / bounds.height () / m_scale);
274-
275- // Model matrix
276- // TODO: This should be calculated and cached by targets
277- QMatrix4x4 modelMatrix;
278- modelMatrix.rotate (angle, 0 , 0 , 1 );
279- modelMatrix.scale (scaleX / textureScale, aspectRatio * scaleY / textureScale);
280215 m_glF->glDisable (GL_SCISSOR_TEST);
281216 m_glF->glDisable (GL_DEPTH_TEST);
282- m_glF->glEnable (GL_BLEND);
283- m_glF->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
284-
285- // Set viewport
286- m_glF->glViewport ((stageWidth / 2 ) + bounds.left () * m_scale, (stageHeight / 2 ) + bounds.bottom () * m_scale, bounds.width () * m_scale, bounds.height () * m_scale);
287-
288- // Get the shader program for the current set of effects
289- ShaderManager *shaderManager = ShaderManager::instance ();
290-
291- const auto &effects = target->graphicEffects ();
292- QOpenGLShaderProgram *shaderProgram = shaderManager->getShaderProgram (effects);
293- Q_ASSERT (shaderProgram);
294- Q_ASSERT (shaderProgram->isLinked ());
295217
296218 m_glF->glBindBuffer (GL_ARRAY_BUFFER, m_vbo);
297-
298- // Render to the target framebuffer
299- m_glF->glBindFramebuffer (GL_FRAMEBUFFER, m_fbo->handle ());
300- shaderProgram->bind ();
301219 m_glF->glBindVertexArray (m_vao);
302- m_glF->glActiveTexture (GL_TEXTURE0);
303- m_glF->glBindTexture (GL_TEXTURE_2D, texture.handle ());
304- shaderManager->setUniforms (shaderProgram, 0 , texture.size (), effects); // set texture and effect uniforms
305- shaderProgram->setUniformValue (" u_projectionMatrix" , projectionMatrix);
306- shaderProgram->setUniformValue (" u_modelMatrix" , modelMatrix);
307- m_glF->glDrawArrays (GL_TRIANGLES, 0 , 6 );
308-
309- // Cleanup
310- shaderProgram->release ();
220+
221+ target->render (m_scale);
222+
311223 m_glF->glBindVertexArray (0 );
312224 m_glF->glBindBuffer (GL_ARRAY_BUFFER, 0 );
313225
314- // The FBO should remain bound until the frame ends
315- // m_glF->glBindFramebuffer(GL_FRAMEBUFFER, 0);
316-
317226 m_glF->glEnable (GL_SCISSOR_TEST);
318227 m_glF->glEnable (GL_DEPTH_TEST);
319228
0 commit comments