summaryrefslogtreecommitdiff
path: root/vcl/inc/opengl/VertexUtils.hxx
blob: becc62ba229e2a80633b2a300c068a8d85fede94 (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
/* -*- 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 <glm/gtx/norm.hpp>

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 addLineVertex(std::vector<GLfloat>& rVertices, std::vector<GLfloat>& rExtrusionVectors, glm::vec2 point, glm::vec2 extrusionVector, float length)
{
    rVertices.push_back(point.x);
    rVertices.push_back(point.y);

    rExtrusionVectors.push_back(extrusionVector.x);
    rExtrusionVectors.push_back(extrusionVector.y);
    rExtrusionVectors.push_back(length);
}

inline void addLineVertexPair(std::vector<GLfloat>& rVertices, std::vector<GLfloat>& rExtrusionVectors, const glm::vec2& point, const glm::vec2& extrusionVector, float length)
{
    addLineVertex(rVertices, rExtrusionVectors, point, -extrusionVector, -length);
    addLineVertex(rVertices, rExtrusionVectors, point,  extrusionVector,  length);
}

inline void addLinePointFirst(std::vector<GLfloat>& rVertices, std::vector<GLfloat>& rExtrusionVectors,
                               glm::vec2 point, glm::vec2 extrusionVector, float length)
{
    addLineVertex(rVertices, rExtrusionVectors, point, -extrusionVector, -length);
    addLineVertex(rVertices, rExtrusionVectors, point,  extrusionVector,  length);
}

inline void addLinePointNext(std::vector<GLfloat>& rVertices, std::vector<GLfloat>& rExtrusionVectors,
                             glm::vec2 prevPoint, glm::vec2 prevExtrusionVector, float prevLength,
                             glm::vec2 currPoint, glm::vec2 currExtrusionVector, float currLength)
{
    addLineVertex(rVertices, rExtrusionVectors, currPoint, -currExtrusionVector, -currLength);
    addLineVertex(rVertices, rExtrusionVectors, currPoint, -currExtrusionVector, -currLength);
    addLineVertex(rVertices, rExtrusionVectors, prevPoint,  prevExtrusionVector,  prevLength);
    addLineVertex(rVertices, rExtrusionVectors, currPoint,  currExtrusionVector,  currLength);
}

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

}} // end vcl::vertex

#endif // INCLUDED_VCL_INC_OPENGL_VERTEXUTILS_H

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