summaryrefslogtreecommitdiffstats
path: root/vcl/inc/opengl/VertexUtils.hxx
blob: 910b8725a1e9416d7e8781b645de281b3b3996de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#ifndef INCLUDED_VCL_INC_OPENGL_VERTEXUTILS_H
#define INCLUDED_VCL_INC_OPENGL_VERTEXUTILS_H

#include <basegfx/numeric/ftools.hxx>
#include <epoxy/gl.h>
#include <glm/gtx/norm.hpp>
#include <tools/color.hxx>
#include <vector>

namespace vcl
{
namespace vertex
{

template<GLenum TYPE>
inline void addRectangle(std::vector<GLfloat>& rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2);

template<>
inline void addRectangle<GL_TRIANGLES>(std::vector<GLfloat>& rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
{
    rVertices.insert(rVertices.end(), {
        x1, y1, x2, y1, x1, y2,
        x1, y2, x2, y1, x2, y2
    });
}

template<>
inline void addRectangle<GL_TRIANGLE_FAN>(std::vector<GLfloat>& rVertices, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
{
    rVertices.insert(rVertices.end(), {
        x1, y2, x1, y1,
        x2, y1, x2, y2
    });
}

inline void createColor(Color nColor, GLfloat fTransparency, GLubyte& nR, GLubyte& nG, GLubyte& nB, GLubyte& nA)
{
    nR = nColor.GetRed();
    nG = nColor.GetGreen();
    nB = nColor.GetBlue();
    nA = (1.0f - fTransparency) * 255.0f;
}

template<GLenum TYPE>
inline void addQuadColors(std::vector<GLubyte>& rColors, Color nColor, GLfloat fTransparency);

template<>
inline void addQuadColors<GL_TRIANGLES>(std::vector<GLubyte>& rColors, Color nColor, GLfloat fTransparency)
{
    GLubyte nR, nG, nB, nA;
    createColor(nColor, fTransparency, nR, nG, nB, nA);

    rColors.insert(rColors.end(), {
        nR, nG, nB, nA,
        nR, nG, nB, nA,
        nR, nG, nB, nA,
        nR, nG, nB, nA,
        nR, nG, nB, nA,
        nR, nG, nB, nA,
    });
}

inline 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)
{
    rVertices.insert(rVertices.end(), {
        prevPoint.x, prevPoint.y,
        prevPoint.x, prevPoint.y,
        currPoint.x, currPoint.y,
        currPoint.x, currPoint.y,
        prevPoint.x, prevPoint.y,
        currPoint.x, currPoint.y,
    });

    rExtrusionVectors.insert(rExtrusionVectors.end(), {
        -prevExtrusionVector.x, -prevExtrusionVector.y, -prevLength,
         prevExtrusionVector.x,  prevExtrusionVector.y,  prevLength,
        -currExtrusionVector.x, -currExtrusionVector.y, -currLength,
        -currExtrusionVector.x, -currExtrusionVector.y, -currLength,
         prevExtrusionVector.x,  prevExtrusionVector.y,  prevLength,
         currExtrusionVector.x,  currExtrusionVector.y,  currLength,
    });
}

inline glm::vec2 normalize(const glm::vec2& vector)
{
    if (glm::length(vector) > 0.0)
        return glm::normalize(vector);
    return vector;
}

inline glm::vec2 perpendicular(const glm::vec2& vector)
{
    return glm::vec2(-vector.y, vector.x);
}

inline float lineVectorAngle(const glm::vec2& previous, const glm::vec2& next)
{
    float angle = std::atan2(previous.x * next.y - previous.y * next.x,
                             previous.x * next.x + previous.y * next.y);

    return F_PI - std::fabs(angle);
}

}} // end vcl::vertex

#endif // INCLUDED_VCL_INC_OPENGL_VERTEXUTILS_H

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */