2 * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #ifndef _TVG_GL_GEOMETRY_H_
24 #define _TVG_GL_GEOMETRY_H_
28 #include "tvgGlCommon.h"
30 #define PI 3.1415926535897932384626433832795f
32 #define MVP_MATRIX() \
34 mTransform.scale, 0.0, 0.0f, 0.0f, \
35 0.0, mTransform.scale, 0.0f, 0.0f, \
36 0.0f, 0.0f, mTransform.scale, 0.0f, \
37 (mTransform.x * 2.0f) * (mTransform.scale / mTransform.w), -(mTransform.y * 2.0f) * (mTransform.scale / mTransform.h), 0.0f, 1.0f \
40 #define ROTATION_MATRIX(xPivot, yPivot) \
41 auto radian = mTransform.angle / 180.0f * PI; \
42 auto cosVal = cosf(radian); \
43 auto sinVal = sinf(radian); \
44 float rotate[4*4] = { \
45 cosVal, -sinVal, 0.0f, 0.0f, \
46 sinVal, cosVal, 0.0f, 0.0f, \
47 0.0f, 0.0f, 1.0f, 0.0f, \
48 (xPivot * (1.0f - cosVal)) - (yPivot * sinVal), (yPivot *(1.0f - cosVal)) + (xPivot * sinVal), 0.0f, 1.0f \
51 #define MULTIPLY_MATRIX(A, B, transform) \
52 for(auto i = 0; i < 4; ++i) \
54 for(auto j = 0; j < 4; ++j) \
57 for (auto k = 0; k < 4; ++k) \
58 sum += A[k*4+i] * B[j*4+k]; \
59 transform[j*4+i] = sum; \
63 #define GET_TRANSFORMATION(xPivot, yPivot, transform) \
65 ROTATION_MATRIX(xPivot, yPivot); \
66 MULTIPLY_MATRIX(mvp, rotate, transform);
76 GlPoint(float pX, float pY):x(pX), y(pY)
80 GlPoint(const Point& rhs):GlPoint(rhs.x, rhs.y)
84 GlPoint(const GlPoint& rhs) = default;
85 GlPoint(GlPoint&& rhs) = default;
87 GlPoint& operator= (const GlPoint& rhs) = default;
88 GlPoint& operator= (GlPoint&& rhs) = default;
90 GlPoint& operator= (const Point& rhs)
97 bool operator== (const GlPoint& rhs)
99 if (&rhs == this) return true;
100 if (rhs.x == this->x && rhs.y == this->y) return true;
104 bool operator!= (const GlPoint& rhs)
106 if (&rhs == this) return true;
107 if (rhs.x != this->x || rhs.y != this->y) return true;
111 GlPoint operator+ (const GlPoint& rhs) const
113 return GlPoint(x + rhs.x, y + rhs.y);
116 GlPoint operator+ (const float c) const
118 return GlPoint(x + c, y + c);
121 GlPoint operator- (const GlPoint& rhs) const
123 return GlPoint(x - rhs.x, y - rhs.y);
126 GlPoint operator- (const float c) const
128 return GlPoint(x - c, y - c);
131 GlPoint operator* (const GlPoint& rhs) const
133 return GlPoint(x * rhs.x, y * rhs.y);
136 GlPoint operator* (const float c) const
138 return GlPoint(x * c, y * c);
141 GlPoint operator/ (const GlPoint& rhs) const
143 return GlPoint(x / rhs.x, y / rhs.y);
146 GlPoint operator/ (const float c) const
148 return GlPoint(x / c, y / c);
159 auto length = sqrtf((x * x) + (y * y));
160 if (length != 0.0f) {
161 const auto inverseLen = 1.0f / length;
168 typedef GlPoint GlSize;
173 GlPoint fillOuterBlur;
175 GlPoint strokeOuterBlur;
177 GlPoint strokeInnerBlur;
180 SmoothPoint(GlPoint pt)
202 float opacity = 0.0f;
205 struct VertexDataArray
207 vector<VertexData> vertices;
208 vector<uint32_t> indices;
213 vector<SmoothPoint> mAAPoints;
214 VertexDataArray mFill;
215 VertexDataArray mStroke;
217 GlPoint mBottomRight;
218 bool mIsClosed = false;
238 uint32_t getPrimitiveCount();
239 const GlSize getPrimitiveSize(const uint32_t primitiveIndex) const;
240 bool decomposeOutline(const RenderShape& rshape);
241 bool generateAAPoints(TVG_UNUSED const RenderShape& rshape, float strokeWd, RenderUpdateFlag flag);
242 bool tesselate(TVG_UNUSED const RenderShape& rshape, float viewWd, float viewHt, RenderUpdateFlag flag);
243 void disableVertex(uint32_t location);
244 void draw(const uint32_t location, const uint32_t primitiveIndex, RenderUpdateFlag flag);
245 void updateTransform(const RenderTransform* transform, float w, float h);
246 float* getTransforMatrix();
249 GlPoint normalizePoint(const GlPoint &pt, float viewWd, float viewHt);
250 void addGeometryPoint(VertexDataArray &geometry, const GlPoint &pt, float viewWd, float viewHt, float opacity);
251 GlPoint getNormal(const GlPoint &p1, const GlPoint &p2);
252 float dotProduct(const GlPoint &p1, const GlPoint &p2);
253 GlPoint extendEdge(const GlPoint &pt, const GlPoint &normal, float scalar);
255 void addPoint(GlPrimitive& primitve, const GlPoint &pt, GlPoint &min, GlPoint &max);
256 void addTriangleFanIndices(uint32_t &curPt, vector<uint32_t> &indices);
257 void addQuadIndices(uint32_t &curPt, vector<uint32_t> &indices);
258 bool isBezierFlat(const GlPoint &p1, const GlPoint &c1, const GlPoint &c2, const GlPoint &p2);
259 void decomposeCubicCurve(GlPrimitive& primitve, const GlPoint &pt1, const GlPoint &cpt1, const GlPoint &cpt2, const GlPoint &pt2, GlPoint &min, GlPoint &max);
260 void updateBuffer(const uint32_t location, const VertexDataArray& vertexArray);
262 unique_ptr<GlGpuBuffer> mGpuBuffer;
263 vector<GlPrimitive> mPrimitives;
264 GlTransform mTransform;
267 #endif /* _TVG_GL_GEOMETRY_H_ */