Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / opengl / RenderList.cxx
blob81817950d05ae367cf12c188dc0d936ee9488192
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <opengl/RenderList.hxx>
12 #include <opengl/VertexUtils.hxx>
13 #include <opengl/LineRenderUtils.hxx>
15 #include <basegfx/polygon/b2dpolygontools.hxx>
16 #include <basegfx/polygon/b2dpolygontriangulator.hxx>
17 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
18 #include <basegfx/polygon/b2dtrapezoid.hxx>
20 namespace
23 /** Append vertices for the polyline
25 * OpenGL polyline drawing algorithm inspired by:
26 * - http://mattdesl.svbtle.com/drawing-lines-is-hard
27 * - https://www.mapbox.com/blog/drawing-antialiased-lines/
28 * - https://cesiumjs.org/2013/04/22/Robust-Polyline-Rendering-with-WebGL/
29 * - http://artgrammer.blogspot.si/2011/05/drawing-nearly-perfect-2d-line-segments.html
30 * - http://artgrammer.blogspot.si/2011/07/drawing-polylines-by-tessellation.html
33 void appendPolyLine(vcl::LineBuilder& rBuilder, const basegfx::B2DPolygon& rPolygon,
34 basegfx::B2DLineJoin eLineJoin, css::drawing::LineCap eLineCap,
35 double fMiterMinimumAngle)
37 sal_uInt32 nPoints = rPolygon.count();
38 bool bClosed = rPolygon.isClosed();
40 if (nPoints == 2 || eLineJoin == basegfx::B2DLineJoin::NONE)
42 // If line joint is NONE or a simple line with 2 points, draw the polyline
43 // each line segment separately.
45 for (sal_uInt32 i = 0; i < (bClosed ? nPoints : nPoints - 1); ++i)
47 sal_uInt32 index1 = (i + 0) % nPoints; // loop indices - important when polyline is closed
48 sal_uInt32 index2 = (i + 1) % nPoints;
50 glm::vec2 aPoint1(rPolygon.getB2DPoint(index1).getX(), rPolygon.getB2DPoint(index1).getY());
51 glm::vec2 aPoint2(rPolygon.getB2DPoint(index2).getX(), rPolygon.getB2DPoint(index2).getY());
53 rBuilder.appendLine(aPoint1, aPoint2);
56 else if (nPoints > 2)
58 int i = 0;
59 int lastPoint = int(nPoints);
61 glm::vec2 p0(rPolygon.getB2DPoint(nPoints - 1).getX(), rPolygon.getB2DPoint(nPoints - 1).getY());
62 glm::vec2 p1(rPolygon.getB2DPoint(0).getX(), rPolygon.getB2DPoint(0).getY());
63 glm::vec2 p2(rPolygon.getB2DPoint(1).getX(), rPolygon.getB2DPoint(1).getY());
65 glm::vec2 nextLineVector;
66 glm::vec2 previousLineVector;
67 glm::vec2 normal; // perpendicular to the line vector
69 nextLineVector = vcl::vertex::normalize(p2 - p1);
71 if (!bClosed)
73 normal = glm::vec2(-nextLineVector.y, nextLineVector.x); // make perpendicular
74 rBuilder.appendAndConnectLinePoint(p1, normal, 1.0f);
76 i++; // first point done already
77 lastPoint--; // last point will be calculated separately from the loop
79 p0 = p1;
80 previousLineVector = nextLineVector;
82 else
84 lastPoint++; // we need to connect last point to first point so one more line segment to calculate
85 previousLineVector = vcl::vertex::normalize(p1 - p0);
88 for (; i < lastPoint; ++i)
90 int index1 = (i + 0) % nPoints; // loop indices - important when polyline is closed
91 int index2 = (i + 1) % nPoints;
93 p1 = glm::vec2(rPolygon.getB2DPoint(index1).getX(), rPolygon.getB2DPoint(index1).getY());
94 p2 = glm::vec2(rPolygon.getB2DPoint(index2).getX(), rPolygon.getB2DPoint(index2).getY());
96 if (p1 == p2) // skip equal points, normals could div-by-0
97 continue;
99 nextLineVector = vcl::vertex::normalize(p2 - p1);
101 if (eLineJoin == basegfx::B2DLineJoin::Miter)
103 if (vcl::vertex::lineVectorAngle(previousLineVector, nextLineVector) < fMiterMinimumAngle)
104 rBuilder.appendBevelJoint(p1, previousLineVector, nextLineVector);
105 else
106 rBuilder.appendMiterJoint(p1, previousLineVector, nextLineVector);
108 else if (eLineJoin == basegfx::B2DLineJoin::Bevel)
110 rBuilder.appendBevelJoint(p1, previousLineVector, nextLineVector);
112 else if (eLineJoin == basegfx::B2DLineJoin::Round)
114 rBuilder.appendRoundJoint(p1, previousLineVector, nextLineVector);
116 p0 = p1;
117 previousLineVector = nextLineVector;
120 if (!bClosed)
122 // Create vertices for the last point. There is no line join so just
123 // use the last line segment normal as the extrusion vector.
124 p1 = glm::vec2(rPolygon.getB2DPoint(nPoints - 1).getX(), rPolygon.getB2DPoint(nPoints - 1).getY());
125 normal = glm::vec2(-previousLineVector.y, previousLineVector.x);
126 rBuilder.appendAndConnectLinePoint(p1, normal, 1.0f);
130 if (!bClosed && nPoints >= 2 && (eLineCap == css::drawing::LineCap_ROUND || eLineCap == css::drawing::LineCap_SQUARE))
132 glm::vec2 aBeginCapPoint1(rPolygon.getB2DPoint(0).getX(), rPolygon.getB2DPoint(0).getY());
133 glm::vec2 aBeginCapPoint2(rPolygon.getB2DPoint(1).getX(), rPolygon.getB2DPoint(1).getY());
135 glm::vec2 aEndCapPoint1(rPolygon.getB2DPoint(nPoints - 1).getX(), rPolygon.getB2DPoint(nPoints - 1).getY());
136 glm::vec2 aEndCapPoint2(rPolygon.getB2DPoint(nPoints - 2).getX(), rPolygon.getB2DPoint(nPoints - 2).getY());
138 if (eLineCap == css::drawing::LineCap_ROUND)
140 rBuilder.appendRoundLineCapVertices(aBeginCapPoint1, aBeginCapPoint2);
141 rBuilder.appendRoundLineCapVertices(aEndCapPoint1, aEndCapPoint2);
143 else if (eLineCap == css::drawing::LineCap_SQUARE)
145 rBuilder.appendSquareLineCapVertices(aBeginCapPoint1, aBeginCapPoint2);
146 rBuilder.appendSquareLineCapVertices(aEndCapPoint1, aEndCapPoint2);
151 void appendTrapezoid(std::vector<Vertex>& rVertices, std::vector<GLuint>& rIndices,
152 GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
153 GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4,
154 Color nColor, GLfloat fTransparency)
156 GLubyte nR, nG, nB, nA;
157 vcl::vertex::createColor(nColor, fTransparency, nR, nG, nB, nA);
159 GLuint zero = rVertices.size();
161 rVertices.insert(rVertices.end(), {
162 {glm::vec2{x1, y1}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
163 {glm::vec2{x2, y2}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
164 {glm::vec2{x3, y3}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
165 {glm::vec2{x4, y4}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
168 rIndices.insert(rIndices.end(), {
169 zero + 0, zero + 1, zero + 2,
170 zero + 2, zero + 1, zero + 3
174 void appendRectangle(std::vector<Vertex>& rVertices, std::vector<GLuint>& rIndices,
175 GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
176 Color nColor, GLfloat fTransparency)
178 GLubyte nR, nG, nB, nA;
179 vcl::vertex::createColor(nColor, fTransparency, nR, nG, nB, nA);
181 GLuint zero = rVertices.size();
183 rVertices.insert(rVertices.end(), {
184 {glm::vec2{x1, y1}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
185 {glm::vec2{x2, y1}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
186 {glm::vec2{x1, y2}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
187 {glm::vec2{x2, y2}, glm::vec4{nR, nG, nB, nA}, glm::vec4{0.0f, 0.0f, 0.0f, 0.0f}},
190 rIndices.insert(rIndices.end(), {
191 zero + 0, zero + 1, zero + 2,
192 zero + 2, zero + 1, zero + 3
196 } // end anonymous namespace
198 void RenderList::addDrawPixel(long nX, long nY, Color nColor)
200 if (nColor == SALCOLOR_NONE)
201 return;
203 checkOverlapping(basegfx::B2DRange(nX, nY, nX, nY));
205 RenderParameters& rRenderParameter = maRenderEntries.back().maTriangleParameters;
206 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
207 nX - 0.5f, nY - 0.5f, nX + 0.5f, nY + 0.5f, nColor, 0.0f);
210 void RenderList::addDrawRectangle(long nX, long nY, long nWidth, long nHeight, double fTransparency,
211 Color nLineColor, Color nFillColor)
213 if (nLineColor == SALCOLOR_NONE && nFillColor == SALCOLOR_NONE)
214 return;
215 if (fTransparency == 1.0f)
216 return;
218 GLfloat fX1(nX);
219 GLfloat fY1(nY);
220 GLfloat fX2(nX + nWidth - 1);
221 GLfloat fY2(nY + nHeight - 1);
223 checkOverlapping(basegfx::B2DRange(fX1, fY1, fX2, fY2));
225 RenderParameters& rRenderParameter = maRenderEntries.back().maTriangleParameters;
227 // Draw rectangle stroke with line color
228 if (nLineColor != SALCOLOR_NONE)
230 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
231 fX1 - 0.5f, fY1 - 0.5f, fX1 + 0.5f, fY2 + 0.5f, nLineColor, fTransparency);
232 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
233 fX1 - 0.5f, fY1 - 0.5f, fX2 + 0.5f, fY1 + 0.5f, nLineColor, fTransparency);
234 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
235 fX2 - 0.5f, fY1 - 0.5f, fX2 + 0.5f, fY2 + 0.5f, nLineColor, fTransparency);
236 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
237 fX1 - 0.5f, fY2 - 0.5f, fX2 + 0.5f, fY2 + 0.5f, nLineColor, fTransparency);
240 if (nFillColor != SALCOLOR_NONE)
242 if (nLineColor == SALCOLOR_NONE)
244 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
245 fX1 - 0.5f, fY1 - 0.5f, fX1 + 0.5f, fY2 + 0.5f, nFillColor, fTransparency);
246 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
247 fX1 - 0.5f, fY1 - 0.5f, fX2 + 0.5f, fY1 + 0.5f, nFillColor, fTransparency);
248 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
249 fX2 - 0.5f, fY1 - 0.5f, fX2 + 0.5f, fY2 + 0.5f, nFillColor, fTransparency);
250 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
251 fX1 - 0.5f, fY2 - 0.5f, fX2 + 0.5f, fY2 + 0.5f, nFillColor, fTransparency);
253 // Draw rectangle fill with fill color
254 appendRectangle(rRenderParameter.maVertices, rRenderParameter.maIndices,
255 fX1 + 0.5f, fY1 + 0.5f, fX2 - 0.5f, fY2 - 0.5f, nFillColor, fTransparency);
259 void RenderList::addDrawLine(long nX1, long nY1, long nX2, long nY2, Color nLineColor, bool bUseAA)
261 if (nLineColor == SALCOLOR_NONE)
262 return;
264 checkOverlapping(basegfx::B2DRange(nX1, nY1, nX2, nY2));
266 RenderParameters& rRenderParameter = maRenderEntries.back().maLineParameters;
268 glm::vec2 aPoint1(nX1, nY1);
269 glm::vec2 aPoint2(nX2, nY2);
271 vcl::LineBuilder aBuilder(rRenderParameter.maVertices, rRenderParameter.maIndices, nLineColor, 0.0f, 1.0f, bUseAA);
272 aBuilder.appendLine(aPoint1, aPoint2);
275 void RenderList::addDrawPolyPolygon(const basegfx::B2DPolyPolygon& rPolyPolygon, double fTransparency,
276 Color nLineColor, Color nFillColor, bool bUseAA)
278 if (rPolyPolygon.count() <= 0)
279 return;
280 if (nLineColor == SALCOLOR_NONE && nFillColor == SALCOLOR_NONE)
281 return;
282 if (fTransparency == 1.0)
283 return;
285 checkOverlapping(rPolyPolygon.getB2DRange());
287 if (nFillColor != SALCOLOR_NONE)
289 basegfx::B2DTrapezoidVector aTrapezoidVector;
290 basegfx::utils::trapezoidSubdivide(aTrapezoidVector, rPolyPolygon);
292 if (!aTrapezoidVector.empty())
294 RenderParameters& rTriangleRenderParameter = maRenderEntries.back().maTriangleParameters;
296 for (const basegfx::B2DTrapezoid & rTrapezoid : aTrapezoidVector)
298 GLfloat topX1 = rTrapezoid.getTopXLeft();
299 GLfloat topX2 = rTrapezoid.getTopXRight();
300 GLfloat topY = rTrapezoid.getTopY();
302 GLfloat bottomX1 = rTrapezoid.getBottomXLeft();
303 GLfloat bottomX2 = rTrapezoid.getBottomXRight();
304 GLfloat bottomY = rTrapezoid.getBottomY();
306 appendTrapezoid(rTriangleRenderParameter.maVertices, rTriangleRenderParameter.maIndices,
307 topX1, topY, topX2, topY,
308 bottomX1, bottomY, bottomX2, bottomY,
309 nFillColor, fTransparency);
314 if (nLineColor != SALCOLOR_NONE || bUseAA)
316 RenderParameters& rLineRenderParameter = maRenderEntries.back().maLineParameters;
317 Color nColor = (nLineColor == SALCOLOR_NONE) ? nFillColor : nLineColor;
319 vcl::LineBuilder aBuilder(rLineRenderParameter.maVertices, rLineRenderParameter.maIndices,
320 nColor, fTransparency, 1.0f, bUseAA);
322 for (const basegfx::B2DPolygon& rPolygon : rPolyPolygon)
324 basegfx::B2DPolygon aPolygon(rPolygon);
325 if (rPolygon.areControlPointsUsed())
326 aPolygon = rPolygon.getDefaultAdaptiveSubdivision();
328 sal_uInt32 nPoints = aPolygon.count();
329 if (nPoints <= 1)
330 continue;
332 GLfloat x1, y1, x2, y2;
333 sal_uInt32 index1, index2;
335 for (sal_uInt32 i = 0; i <= nPoints; ++i)
337 index1 = i % nPoints;
338 index2 = (i + 1) % nPoints;
340 x1 = aPolygon.getB2DPoint(index1).getX();
341 y1 = aPolygon.getB2DPoint(index1).getY();
342 x2 = aPolygon.getB2DPoint(index2).getX();
343 y2 = aPolygon.getB2DPoint(index2).getY();
345 aBuilder.appendLine(glm::vec2(x1, y1), glm::vec2(x2, y2));
351 void RenderList::addDrawTextureWithMaskColor(OpenGLTexture const & rTexture, Color nColor, const SalTwoRect& r2Rect)
353 if (!rTexture)
354 return;
356 GLfloat fX1 = r2Rect.mnDestX;
357 GLfloat fY1 = r2Rect.mnDestY;
358 GLfloat fX2 = fX1 + r2Rect.mnDestWidth;
359 GLfloat fY2 = fY1 + r2Rect.mnDestHeight;
361 checkOverlapping(basegfx::B2DRange(fX1, fY1, fX2, fY2));
363 GLuint nTextureId = rTexture.Id();
365 RenderTextureParameters& rTextureParameter = maRenderEntries.back().maTextureParametersMap[nTextureId];
366 rTextureParameter.maTexture = rTexture;
368 rTexture.FillCoords<GL_TRIANGLES>(rTextureParameter.maTextureCoords, r2Rect);
370 vcl::vertex::addRectangle<GL_TRIANGLES>(rTextureParameter.maVertices, fX1, fY1, fX2, fY2);
371 vcl::vertex::addQuadColors<GL_TRIANGLES>(rTextureParameter.maColors, nColor, 0.0f);
374 void RenderList::addDrawPolyLine(const basegfx::B2DPolygon& rPolygon, double fTransparency,
375 const basegfx::B2DVector& rLineWidth, basegfx::B2DLineJoin eLineJoin,
376 css::drawing::LineCap eLineCap, double fMiterMinimumAngle,
377 Color nLineColor, bool bUseAA)
379 if (rPolygon.count() <= 1)
380 return;
381 if (nLineColor == SALCOLOR_NONE)
382 return;
383 if (fTransparency == 1.0)
384 return;
386 const bool bIsHairline = (rLineWidth.getX() == rLineWidth.getY()) && (rLineWidth.getX() <= 1.2);
387 const float fLineWidth = bIsHairline ? 1.0f : rLineWidth.getX();
389 basegfx::B2DPolygon aPolygon(rPolygon);
390 if (rPolygon.areControlPointsUsed())
391 aPolygon = rPolygon.getDefaultAdaptiveSubdivision();
393 checkOverlapping(aPolygon.getB2DRange());
395 RenderParameters& rParameter = maRenderEntries.back().maLineParameters;
397 vcl::LineBuilder aBuilder(rParameter.maVertices, rParameter.maIndices,
398 nLineColor, fTransparency, fLineWidth, bUseAA);
400 appendPolyLine(aBuilder, aPolygon, eLineJoin, eLineCap, fMiterMinimumAngle);
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */