]> git.lizzy.rs Git - irrlicht.git/commitdiff
Unify plain image drawing
authornumzero <numzer0@yandex.ru>
Mon, 27 Feb 2023 18:27:23 +0000 (21:27 +0300)
committernumzero <numzer0@yandex.ru>
Mon, 27 Feb 2023 18:27:23 +0000 (21:27 +0300)
source/Irrlicht/OpenGL/Driver.cpp

index 1a0bac0ef2386b0a3b8c8ce5e80741a1113e1592..7603d8696213edfd8284f4e58700b67915d53f09 100644 (file)
@@ -828,128 +828,8 @@ COpenGL3Driver::~COpenGL3Driver()
                if (!sourceRect.isValid())\r
                        return;\r
 \r
-               core::position2d<s32> targetPos(destPos);\r
-               core::position2d<s32> sourcePos(sourceRect.UpperLeftCorner);\r
-               core::dimension2d<s32> sourceSize(sourceRect.getSize());\r
-               if (clipRect)\r
-               {\r
-                       if (targetPos.X < clipRect->UpperLeftCorner.X)\r
-                       {\r
-                               sourceSize.Width += targetPos.X - clipRect->UpperLeftCorner.X;\r
-                               if (sourceSize.Width <= 0)\r
-                                       return;\r
-\r
-                               sourcePos.X -= targetPos.X - clipRect->UpperLeftCorner.X;\r
-                               targetPos.X = clipRect->UpperLeftCorner.X;\r
-                       }\r
-\r
-                       if (targetPos.X + sourceSize.Width > clipRect->LowerRightCorner.X)\r
-                       {\r
-                               sourceSize.Width -= (targetPos.X + sourceSize.Width) - clipRect->LowerRightCorner.X;\r
-                               if (sourceSize.Width <= 0)\r
-                                       return;\r
-                       }\r
-\r
-                       if (targetPos.Y < clipRect->UpperLeftCorner.Y)\r
-                       {\r
-                               sourceSize.Height += targetPos.Y - clipRect->UpperLeftCorner.Y;\r
-                               if (sourceSize.Height <= 0)\r
-                                       return;\r
-\r
-                               sourcePos.Y -= targetPos.Y - clipRect->UpperLeftCorner.Y;\r
-                               targetPos.Y = clipRect->UpperLeftCorner.Y;\r
-                       }\r
-\r
-                       if (targetPos.Y + sourceSize.Height > clipRect->LowerRightCorner.Y)\r
-                       {\r
-                               sourceSize.Height -= (targetPos.Y + sourceSize.Height) - clipRect->LowerRightCorner.Y;\r
-                               if (sourceSize.Height <= 0)\r
-                                       return;\r
-                       }\r
-               }\r
-\r
-               // clip these coordinates\r
-\r
-               if (targetPos.X < 0)\r
-               {\r
-                       sourceSize.Width += targetPos.X;\r
-                       if (sourceSize.Width <= 0)\r
-                               return;\r
-\r
-                       sourcePos.X -= targetPos.X;\r
-                       targetPos.X = 0;\r
-               }\r
-\r
-               const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();\r
-\r
-               if (targetPos.X + sourceSize.Width > (s32)renderTargetSize.Width)\r
-               {\r
-                       sourceSize.Width -= (targetPos.X + sourceSize.Width) - renderTargetSize.Width;\r
-                       if (sourceSize.Width <= 0)\r
-                               return;\r
-               }\r
-\r
-               if (targetPos.Y < 0)\r
-               {\r
-                       sourceSize.Height += targetPos.Y;\r
-                       if (sourceSize.Height <= 0)\r
-                               return;\r
-\r
-                       sourcePos.Y -= targetPos.Y;\r
-                       targetPos.Y = 0;\r
-               }\r
-\r
-               if (targetPos.Y + sourceSize.Height > (s32)renderTargetSize.Height)\r
-               {\r
-                       sourceSize.Height -= (targetPos.Y + sourceSize.Height) - renderTargetSize.Height;\r
-                       if (sourceSize.Height <= 0)\r
-                               return;\r
-               }\r
-\r
-               // ok, we've clipped everything.\r
-               // now draw it.\r
-\r
-               // texcoords need to be flipped horizontally for RTTs\r
-               const bool isRTT = texture->isRenderTarget();\r
-               const core::dimension2d<u32>& ss = texture->getOriginalSize();\r
-               const f32 invW = 1.f / static_cast<f32>(ss.Width);\r
-               const f32 invH = 1.f / static_cast<f32>(ss.Height);\r
-               const core::rect<f32> tcoords(\r
-                       sourcePos.X * invW,\r
-                       (isRTT ? (sourcePos.Y + sourceSize.Height) : sourcePos.Y) * invH,\r
-                       (sourcePos.X + sourceSize.Width) * invW,\r
-                       (isRTT ? sourcePos.Y : (sourcePos.Y + sourceSize.Height)) * invH);\r
-\r
-               const core::rect<s32> poss(targetPos, sourceSize);\r
-\r
-               chooseMaterial2D();\r
-               if (!setMaterialTexture(0, texture ))\r
-                       return;\r
-\r
-               setRenderStates2DMode(color.getAlpha() < 255, true, useAlphaChannelOfTexture);\r
-\r
-               f32 left = (f32)poss.UpperLeftCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;\r
-               f32 right = (f32)poss.LowerRightCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;\r
-               f32 down = 2.f - (f32)poss.LowerRightCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;\r
-               f32 top = 2.f - (f32)poss.UpperLeftCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;\r
-\r
-               u16 indices[] = {0, 1, 2, 3};\r
-               S3DVertex vertices[4];\r
-               vertices[0] = S3DVertex(left, top, 0, 0, 0, 1, color, tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);\r
-               vertices[1] = S3DVertex(right, top, 0, 0, 0, 1, color, tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);\r
-               vertices[2] = S3DVertex(right, down, 0, 0, 0, 1, color, tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y);\r
-               vertices[3] = S3DVertex(left, down, 0, 0, 0, 1, color, tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y);\r
-\r
-               glEnableVertexAttribArray(EVA_POSITION);\r
-               glEnableVertexAttribArray(EVA_COLOR);\r
-               glEnableVertexAttribArray(EVA_TCOORD0);\r
-               glVertexAttribPointer(EVA_POSITION, 3, GL_FLOAT, false, sizeof(S3DVertex), &(static_cast<const S3DVertex*>(vertices))[0].Pos);\r
-               glVertexAttribPointer(EVA_COLOR, 4, GL_UNSIGNED_BYTE, true, sizeof(S3DVertex), &(static_cast<const S3DVertex*>(vertices))[0].Color);\r
-               glVertexAttribPointer(EVA_TCOORD0, 2, GL_FLOAT, false, sizeof(S3DVertex), &(static_cast<const S3DVertex*>(vertices))[0].TCoords);\r
-               glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, indices);\r
-               glDisableVertexAttribArray(EVA_TCOORD0);\r
-               glDisableVertexAttribArray(EVA_COLOR);\r
-               glDisableVertexAttribArray(EVA_POSITION);\r
+               SColor colors[4] = {color, color, color, color};\r
+               draw2DImage(texture, {destPos, sourceRect.getSize()}, sourceRect, clipRect, colors, useAlphaChannelOfTexture);\r
        }\r
 \r
 \r
@@ -1083,6 +963,24 @@ COpenGL3Driver::~COpenGL3Driver()
                if (!texture)\r
                        return;\r
 \r
+               chooseMaterial2D();\r
+               if (!setMaterialTexture(0, texture))\r
+                       return;\r
+\r
+               setRenderStates2DMode(color.getAlpha() < 255, true, useAlphaChannelOfTexture);\r
+\r
+               const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();\r
+\r
+               if (clipRect)\r
+               {\r
+                       if (!clipRect->isValid())\r
+                               return;\r
+\r
+                       glEnable(GL_SCISSOR_TEST);\r
+                       glScissor(clipRect->UpperLeftCorner.X, renderTargetSize.Height - clipRect->LowerRightCorner.Y,\r
+                                       clipRect->getWidth(), clipRect->getHeight());\r
+               }\r
+\r
                const irr::u32 drawCount = core::min_<u32>(positions.size(), sourceRects.size());\r
 \r
                core::array<S3DVertex> vtx(drawCount * 4);\r
@@ -1095,82 +993,6 @@ COpenGL3Driver::~COpenGL3Driver()
                        // This needs to be signed as it may go negative.\r
                        core::dimension2d<s32> sourceSize(sourceRects[i].getSize());\r
 \r
-                       if (clipRect)\r
-                       {\r
-                               if (targetPos.X < clipRect->UpperLeftCorner.X)\r
-                               {\r
-                                       sourceSize.Width += targetPos.X - clipRect->UpperLeftCorner.X;\r
-                                       if (sourceSize.Width <= 0)\r
-                                               continue;\r
-\r
-                                       sourcePos.X -= targetPos.X - clipRect->UpperLeftCorner.X;\r
-                                       targetPos.X = clipRect->UpperLeftCorner.X;\r
-                               }\r
-\r
-                               if (targetPos.X + (s32)sourceSize.Width > clipRect->LowerRightCorner.X)\r
-                               {\r
-                                       sourceSize.Width -= (targetPos.X + sourceSize.Width) - clipRect->LowerRightCorner.X;\r
-                                       if (sourceSize.Width <= 0)\r
-                                               continue;\r
-                               }\r
-\r
-                               if (targetPos.Y < clipRect->UpperLeftCorner.Y)\r
-                               {\r
-                                       sourceSize.Height += targetPos.Y - clipRect->UpperLeftCorner.Y;\r
-                                       if (sourceSize.Height <= 0)\r
-                                               continue;\r
-\r
-                                       sourcePos.Y -= targetPos.Y - clipRect->UpperLeftCorner.Y;\r
-                                       targetPos.Y = clipRect->UpperLeftCorner.Y;\r
-                               }\r
-\r
-                               if (targetPos.Y + (s32)sourceSize.Height > clipRect->LowerRightCorner.Y)\r
-                               {\r
-                                       sourceSize.Height -= (targetPos.Y + sourceSize.Height) - clipRect->LowerRightCorner.Y;\r
-                                       if (sourceSize.Height <= 0)\r
-                                               continue;\r
-                               }\r
-                       }\r
-\r
-                       // clip these coordinates\r
-\r
-                       if (targetPos.X < 0)\r
-                       {\r
-                               sourceSize.Width += targetPos.X;\r
-                               if (sourceSize.Width <= 0)\r
-                                       continue;\r
-\r
-                               sourcePos.X -= targetPos.X;\r
-                               targetPos.X = 0;\r
-                       }\r
-\r
-                       const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();\r
-\r
-                       if (targetPos.X + sourceSize.Width > (s32)renderTargetSize.Width)\r
-                       {\r
-                               sourceSize.Width -= (targetPos.X + sourceSize.Width) - renderTargetSize.Width;\r
-                               if (sourceSize.Width <= 0)\r
-                                       continue;\r
-                       }\r
-\r
-                       if (targetPos.Y < 0)\r
-                       {\r
-                               sourceSize.Height += targetPos.Y;\r
-                               if (sourceSize.Height <= 0)\r
-                                       continue;\r
-\r
-                               sourcePos.Y -= targetPos.Y;\r
-                               targetPos.Y = 0;\r
-                       }\r
-\r
-                       if (targetPos.Y + sourceSize.Height > (s32)renderTargetSize.Height)\r
-                       {\r
-                               sourceSize.Height -= (targetPos.Y + sourceSize.Height) - renderTargetSize.Height;\r
-                               if (sourceSize.Height <= 0)\r
-                                       continue;\r
-                       }\r
-\r
-                       // ok, we've clipped everything.\r
                        // now draw it.\r
 \r
                        core::rect<f32> tcoords;\r
@@ -1181,12 +1003,6 @@ COpenGL3Driver::~COpenGL3Driver()
 \r
                        const core::rect<s32> poss(targetPos, sourceSize);\r
 \r
-                       chooseMaterial2D();\r
-                       if (!setMaterialTexture(0, texture))\r
-                               return;\r
-\r
-                       setRenderStates2DMode(color.getAlpha() < 255, true, useAlphaChannelOfTexture);\r
-\r
                        f32 left = (f32)poss.UpperLeftCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;\r
                        f32 right = (f32)poss.LowerRightCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;\r
                        f32 down = 2.f - (f32)poss.LowerRightCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;\r