LibreOffice Module vcl (master)  1
VertexUtils.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  */
10 
11 #ifndef INCLUDED_VCL_INC_OPENGL_VERTEXUTILS_H
12 #define INCLUDED_VCL_INC_OPENGL_VERTEXUTILS_H
13 
15 #include <epoxy/gl.h>
16 #include <glm/gtx/norm.hpp>
17 #include <tools/color.hxx>
18 #include <vector>
19 
20 namespace vcl
21 {
22 namespace vertex
23 {
24 
25 template<GLenum TYPE>
26 inline void addRectangle(std::vector<GLfloat>& rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2);
27 
28 template<>
29 inline void addRectangle<GL_TRIANGLES>(std::vector<GLfloat>& rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
30 {
31  rVertices.insert(rVertices.end(), {
32  x1, y1, x2, y1, x1, y2,
33  x1, y2, x2, y1, x2, y2
34  });
35 }
36 
37 template<>
38 inline void addRectangle<GL_TRIANGLE_FAN>(std::vector<GLfloat>& rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
39 {
40  rVertices.insert(rVertices.end(), {
41  x1, y2, x1, y1,
42  x2, y1, x2, y2
43  });
44 }
45 
46 inline void createColor(Color nColor, GLfloat fTransparency, GLubyte& nR, GLubyte& nG, GLubyte& nB, GLubyte& nA)
47 {
48  nR = nColor.GetRed();
49  nG = nColor.GetGreen();
50  nB = nColor.GetBlue();
51  nA = (1.0f - fTransparency) * 255.0f;
52 }
53 
54 template<GLenum TYPE>
55 inline void addQuadColors(std::vector<GLubyte>& rColors, Color nColor, GLfloat fTransparency);
56 
57 template<>
58 inline void addQuadColors<GL_TRIANGLES>(std::vector<GLubyte>& rColors, Color nColor, GLfloat fTransparency)
59 {
60  GLubyte nR, nG, nB, nA;
61  createColor(nColor, fTransparency, nR, nG, nB, nA);
62 
63  rColors.insert(rColors.end(), {
64  nR, nG, nB, nA,
65  nR, nG, nB, nA,
66  nR, nG, nB, nA,
67  nR, nG, nB, nA,
68  nR, nG, nB, nA,
69  nR, nG, nB, nA,
70  });
71 }
72 
73 inline void addLineSegmentVertices(std::vector<GLfloat>& rVertices, std::vector<GLfloat>& rExtrusionVectors,
74  glm::vec2 prevPoint, glm::vec2 prevExtrusionVector, GLfloat prevLength,
75  glm::vec2 currPoint, glm::vec2 currExtrusionVector, GLfloat currLength)
76 {
77  rVertices.insert(rVertices.end(), {
78  prevPoint.x, prevPoint.y,
79  prevPoint.x, prevPoint.y,
80  currPoint.x, currPoint.y,
81  currPoint.x, currPoint.y,
82  prevPoint.x, prevPoint.y,
83  currPoint.x, currPoint.y,
84  });
85 
86  rExtrusionVectors.insert(rExtrusionVectors.end(), {
87  -prevExtrusionVector.x, -prevExtrusionVector.y, -prevLength,
88  prevExtrusionVector.x, prevExtrusionVector.y, prevLength,
89  -currExtrusionVector.x, -currExtrusionVector.y, -currLength,
90  -currExtrusionVector.x, -currExtrusionVector.y, -currLength,
91  prevExtrusionVector.x, prevExtrusionVector.y, prevLength,
92  currExtrusionVector.x, currExtrusionVector.y, currLength,
93  });
94 }
95 
96 inline glm::vec2 normalize(const glm::vec2& vector)
97 {
98  if (glm::length(vector) > 0.0)
99  return glm::normalize(vector);
100  return vector;
101 }
102 
103 inline glm::vec2 perpendicular(const glm::vec2& vector)
104 {
105  return glm::vec2(-vector.y, vector.x);
106 }
107 
108 inline float lineVectorAngle(const glm::vec2& previous, const glm::vec2& next)
109 {
110  float angle = std::atan2(previous.x * next.y - previous.y * next.x,
111  previous.x * next.x + previous.y * next.y);
112 
113  return F_PI - std::fabs(angle);
114 }
115 
116 }} // end vcl::vertex
117 
118 #endif // INCLUDED_VCL_INC_OPENGL_VERTEXUTILS_H
119 
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void addQuadColors(std::vector< GLubyte > &rColors, Color nColor, GLfloat fTransparency)
sal_uInt8 GetRed() const
glm::vec2 normalize(const glm::vec2 &vector)
Definition: VertexUtils.hxx:96
void addRectangle< GL_TRIANGLES >(std::vector< GLfloat > &rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
Definition: VertexUtils.hxx:29
float lineVectorAngle(const glm::vec2 &previous, const glm::vec2 &next)
void addLineSegmentVertices(std::vector< GLfloat > &rVertices, std::vector< GLfloat > &rExtrusionVectors, glm::vec2 prevPoint, glm::vec2 prevExtrusionVector, GLfloat prevLength, glm::vec2 currPoint, glm::vec2 currExtrusionVector, GLfloat currLength)
Definition: VertexUtils.hxx:73
void addRectangle< GL_TRIANGLE_FAN >(std::vector< GLfloat > &rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
Definition: VertexUtils.hxx:38
void createColor(Color nColor, GLfloat fTransparency, GLubyte &nR, GLubyte &nG, GLubyte &nB, GLubyte &nA)
Definition: VertexUtils.hxx:46
sal_uInt8 GetBlue() const
glm::vec2 perpendicular(const glm::vec2 &vector)
void addRectangle(std::vector< GLfloat > &rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
void addQuadColors< GL_TRIANGLES >(std::vector< GLubyte > &rColors, Color nColor, GLfloat fTransparency)
Definition: VertexUtils.hxx:58
#define F_PI
sal_uInt8 GetGreen() const
void(* f)(TrueTypeTable *)
Definition: ttcr.cxx:466