Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / opengl / LineRenderUtils.cxx
blobe130fb93bb22d11a06f33036bcf138d27fabe95f
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/LineRenderUtils.hxx>
12 #include <opengl/VertexUtils.hxx>
14 namespace vcl
17 LineBuilder::LineBuilder(std::vector<Vertex>& rVertices, std::vector<GLuint>& rIndices,
18 Color nColor, GLfloat fTransparency,
19 GLfloat fLineWidth, bool bUseAA)
20 : mrVertices(rVertices)
21 , mrIndices(rIndices)
22 , mR(nColor.GetRed())
23 , mG(nColor.GetGreen())
24 , mB(nColor.GetBlue())
25 , mA((1.0f - fTransparency) * 255.0f)
26 , mfLineWidth(fLineWidth)
27 , mfLineWidthAndAA(bUseAA ? fLineWidth : -fLineWidth)
28 , mnInitialIndexSize(rIndices.size())
29 , mbIncomplete(false)
33 void LineBuilder::appendLineSegment(const glm::vec2& rPoint1, const glm::vec2& rNormal1, GLfloat aExtrusion1,
34 const glm::vec2& rPoint2, const glm::vec2& rNormal2, GLfloat aExtrusion2)
36 GLuint zero = mrVertices.size();
38 mrVertices.insert(mrVertices.end(), {
39 {rPoint1, glm::vec4{mR, mG, mB, mA}, glm::vec4{-rNormal1.x, -rNormal1.y, -aExtrusion1, mfLineWidthAndAA}},
40 {rPoint1, glm::vec4{mR, mG, mB, mA}, glm::vec4{ rNormal1.x, rNormal1.y, aExtrusion1, mfLineWidthAndAA}},
41 {rPoint2, glm::vec4{mR, mG, mB, mA}, glm::vec4{-rNormal2.x, -rNormal2.y, -aExtrusion2, mfLineWidthAndAA}},
42 {rPoint2, glm::vec4{mR, mG, mB, mA}, glm::vec4{ rNormal2.x, rNormal2.y, aExtrusion2, mfLineWidthAndAA}},
43 });
45 mrIndices.insert(mrIndices.end(), {
46 zero + 0, zero + 1, zero + 2,
47 zero + 2, zero + 1, zero + 3
48 });
52 void LineBuilder::appendLine(const glm::vec2& rPoint1, const glm::vec2& rPoint2)
54 glm::vec2 aLineVector = vcl::vertex::normalize(rPoint2 - rPoint1);
55 glm::vec2 aNormal = vcl::vertex::perpendicular(aLineVector);
57 appendLineSegment(rPoint1, aNormal, 1.0f,
58 rPoint2, aNormal, 1.0f);
61 void LineBuilder::appendAndConnectLinePoint(const glm::vec2& rPoint, const glm::vec2& aNormal, GLfloat aExtrusion)
63 GLuint zero = mrVertices.size();
65 mrVertices.insert(mrVertices.end(), {
66 {rPoint, glm::vec4{mR, mG, mB, mA}, glm::vec4{-aNormal.x, -aNormal.y, -aExtrusion, mfLineWidthAndAA}},
67 {rPoint, glm::vec4{mR, mG, mB, mA}, glm::vec4{ aNormal.x, aNormal.y, aExtrusion, mfLineWidthAndAA}},
68 });
70 if (mnInitialIndexSize == mrIndices.size())
72 mrIndices.insert(mrIndices.end(), {
73 zero + 0, zero + 1
74 });
75 mbIncomplete = true;
77 else
79 if (mbIncomplete)
81 mrIndices.insert(mrIndices.end(), {
82 zero + 0,
83 zero + 0, zero - 1, zero + 1
84 });
85 mbIncomplete = false;
87 else
89 mrIndices.insert(mrIndices.end(), {
90 zero - 2, zero - 1, zero + 0,
91 zero + 0, zero - 1, zero + 1
92 });
97 void LineBuilder::appendMiterJoint(glm::vec2 const& point, const glm::vec2& prevLineVector,
98 glm::vec2 const& nextLineVector)
100 // With miter join we calculate the extrusion vector by adding normals of
101 // previous and next line segment. The vector shows the way but we also
102 // need the length (otherwise the line will be deformed). Length factor is
103 // calculated as dot product of extrusion vector and one of the normals.
104 // The value we get is the inverse length (used in the shader):
105 // length = line_width / dot(extrusionVector, normal)
107 glm::vec2 normal(-prevLineVector.y, prevLineVector.x);
109 glm::vec2 tangent = vcl::vertex::normalize(nextLineVector + prevLineVector);
110 glm::vec2 extrusionVector(-tangent.y, tangent.x);
111 GLfloat length = glm::dot(extrusionVector, normal);
113 appendAndConnectLinePoint(point, extrusionVector, length);
116 void LineBuilder::appendBevelJoint(glm::vec2 const& point, const glm::vec2& prevLineVector,
117 const glm::vec2& nextLineVector)
119 // For bevel join we just add 2 additional vertices and use previous
120 // line segment normal and next line segment normal as extrusion vector.
121 // All the magic is done by the fact that we draw triangle strips, so we
122 // cover the joins correctly.
124 glm::vec2 prevNormal(-prevLineVector.y, prevLineVector.x);
125 glm::vec2 nextNormal(-nextLineVector.y, nextLineVector.x);
127 appendAndConnectLinePoint(point, prevNormal, 1.0f);
128 appendAndConnectLinePoint(point, nextNormal, 1.0f);
131 void LineBuilder::appendRoundJoint(glm::vec2 const& point, const glm::vec2& prevLineVector,
132 const glm::vec2& nextLineVector)
134 // For round join we do a similar thing as in bevel, we add more intermediate
135 // vertices and add normals to get extrusion vectors in the between the
136 // both normals.
138 // 3 additional extrusion vectors + normals are enough to make most
139 // line joins look round. Ideally the number of vectors could be
140 // calculated.
142 glm::vec2 prevNormal(-prevLineVector.y, prevLineVector.x);
143 glm::vec2 nextNormal(-nextLineVector.y, nextLineVector.x);
145 glm::vec2 middle = vcl::vertex::normalize(prevNormal + nextNormal);
146 glm::vec2 middleLeft = vcl::vertex::normalize(prevNormal + middle);
147 glm::vec2 middleRight = vcl::vertex::normalize(middle + nextNormal);
149 appendAndConnectLinePoint(point, prevNormal, 1.0f);
150 appendAndConnectLinePoint(point, middleLeft, 1.0f);
151 appendAndConnectLinePoint(point, middle, 1.0f);
152 appendAndConnectLinePoint(point, middleRight, 1.0f);
153 appendAndConnectLinePoint(point, nextNormal, 1.0f);
156 void LineBuilder::appendRoundLineCapVertices(const glm::vec2& rPoint1, const glm::vec2& rPoint2)
158 constexpr int nRoundCapIteration = 12;
160 glm::vec2 lineVector = vcl::vertex::normalize(rPoint2 - rPoint1);
161 glm::vec2 normal(-lineVector.y, lineVector.x);
162 glm::vec2 previousRoundNormal = normal;
164 for (int nFactor = 1; nFactor <= nRoundCapIteration; nFactor++)
166 float angle = float(nFactor) * (M_PI / float(nRoundCapIteration));
167 glm::vec2 roundNormal(normal.x * glm::cos(angle) - normal.y * glm::sin(angle),
168 normal.x * glm::sin(angle) + normal.y * glm::cos(angle));
170 appendLineSegment(rPoint1, previousRoundNormal, 1.0f,
171 rPoint1, roundNormal, 1.0f);
172 previousRoundNormal = roundNormal;
176 void LineBuilder::appendSquareLineCapVertices(const glm::vec2& rPoint1, const glm::vec2& rPoint2)
178 glm::vec2 lineVector = vcl::vertex::normalize(rPoint2 - rPoint1);
179 glm::vec2 normal(-lineVector.y, lineVector.x);
181 glm::vec2 extrudedPoint = rPoint1 + -lineVector * (mfLineWidth / 2.0f);
183 appendLineSegment(extrudedPoint, normal, 1.0f,
184 rPoint1, normal, 1.0f);
187 } // end vcl
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */