summaryrefslogtreecommitdiff
path: root/canvas/source/opengl/ogl_canvastools.cxx
blob: 6490cff5bdf061d0d2e857da00f6f1d9b8b7d546 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* -*- 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/.
 */

#include "ogl_canvastools.hxx"

#include <canvas/debug.hxx>
#include <tools/diagnose_ex.h>
#include <basegfx/tools/canvastools.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <basegfx/tools/tools.hxx>
#include <basegfx/polygon/b2dpolypolygon.hxx>
#include <basegfx/polygon/b2dpolygontriangulator.hxx>
#include <basegfx/polygon/b2dpolypolygontools.hxx>
#include <vector>
#include <com/sun/star/rendering/ARGBColor.hpp>

#include <GL/glew.h>

//subdivision count of bezier segments
#define COUNT_OF_ADAPTIVE_SUBDIVISION 40

using namespace ::com::sun::star;

namespace oglcanvas
{
    // triangulates polygon before
    void renderComplexPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly, RenderHelper *renderHelper,
        glm::vec4 color)
    {
        ::basegfx::B2DPolyPolygon aPolyPoly(rPolyPoly);
        if( aPolyPoly.areControlPointsUsed() )
            aPolyPoly = rPolyPoly.getAdaptiveSubdivision(COUNT_OF_ADAPTIVE_SUBDIVISION);
        const ::basegfx::B2DPolygon& rTriangulatedPolygon(
            ::basegfx::triangulator::triangulate(aPolyPoly));
        if(rTriangulatedPolygon.count()>0)
        {
            std::vector<glm::vec2> vertices;
            vertices.reserve(rTriangulatedPolygon.count());
            for( sal_uInt32 i=0; i<rTriangulatedPolygon.count(); i++ )
            {
                const ::basegfx::B2DPoint& rPt( rTriangulatedPolygon.getB2DPoint(i) );
                vertices.push_back(glm::vec2(rPt.getX(),rPt.getY()));
            }

            renderHelper->renderVertexConstColor(vertices, color, GL_TRIANGLES);
        }
    }

    void renderTransformComplexPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly, RenderHelper *renderHelper,
        glm::vec4 color, glm::mat4 transform)
    {
        ::basegfx::B2DPolyPolygon aPolyPoly(rPolyPoly);
        if( aPolyPoly.areControlPointsUsed() )
            aPolyPoly = rPolyPoly.getAdaptiveSubdivision(COUNT_OF_ADAPTIVE_SUBDIVISION);
        const ::basegfx::B2DRange& rBounds(aPolyPoly.getB2DRange());
        const double nWidth=rBounds.getWidth();
        const double nHeight=rBounds.getHeight();
        const ::basegfx::B2DPolygon& rTriangulatedPolygon(
            ::basegfx::triangulator::triangulate(aPolyPoly));
        if(rTriangulatedPolygon.count()>0)
        {
            std::vector<glm::vec2> vertices;
            vertices.reserve(rTriangulatedPolygon.count());
            for( sal_uInt32 i=0; i<rTriangulatedPolygon.count(); i++ )
            {
                const ::basegfx::B2DPoint& rPt( rTriangulatedPolygon.getB2DPoint(i) );
                vertices.push_back(glm::vec2(rPt.getX(),rPt.getY()));
            }
            renderHelper->renderTextureTransform( vertices, nWidth, nHeight,  color, GL_TRIANGLES, transform);
        }
    }


    /** only use this for line polygons.

        better not leave triangulation to OpenGL. also, ignores texturing
    */
    void renderPolyPolygon( const ::basegfx::B2DPolyPolygon& rPolyPoly, RenderHelper *renderHelper, glm::vec4 color)
    {
        ::basegfx::B2DPolyPolygon aPolyPoly(rPolyPoly);
        if( aPolyPoly.areControlPointsUsed() )
            aPolyPoly = rPolyPoly.getAdaptiveSubdivision(COUNT_OF_ADAPTIVE_SUBDIVISION);
        for(sal_uInt32 i=0; i<aPolyPoly.count(); i++ )
        {

            const ::basegfx::B2DPolygon& rPolygon( aPolyPoly.getB2DPolygon(i) );

            const sal_uInt32 nPts=rPolygon.count();
            const sal_uInt32 nExtPts=nPts + int(rPolygon.isClosed());
            if(nExtPts>0)
            {
                std::vector<glm::vec2> vertices;
                vertices.reserve(nExtPts);
                for( sal_uInt32 j=0; j<nExtPts; j++ )
                {
                    const ::basegfx::B2DPoint& rPt( rPolygon.getB2DPoint( j % nPts ) );
                    vertices.push_back(glm::vec2(rPt.getX(),rPt.getY()));
                }
                renderHelper->renderVertexConstColor(vertices, color, GL_LINE_STRIP);
            }
        }

    }

    glm::mat4 setupState( const ::basegfx::B2DHomMatrix&   rTransform,
                     GLenum                           eSrcBlend,
                     GLenum                           eDstBlend)
    {
        const glm::mat4 aGLTransform = glm::mat4(
                (float) rTransform.get(0,0), (float) rTransform.get(1,0), 0, 0,
                (float) rTransform.get(0,1), (float) rTransform.get(1,1), 0, 0,
                0,                   0,                   1, 0,
                (float) rTransform.get(0,2), (float) rTransform.get(1,2), 0, 1);
        glEnable(GL_BLEND);
        glBlendFunc(eSrcBlend, eDstBlend);
        return aGLTransform;
    }

    void renderOSD( const std::vector<double>& rNumbers, double scale, RenderHelper *renderHelper)
    {
        double y=4.0;
        basegfx::B2DHomMatrix aTmp;
        basegfx::B2DHomMatrix aScaleShear;
        aScaleShear.shearX(-0.1);
        aScaleShear.scale(scale,scale);

        for( size_t i=0; i<rNumbers.size(); ++i )
        {
            aTmp.identity();
            aTmp.translate(0,y);
            y += 1.2*scale; //send to renderHelper

            basegfx::B2DPolyPolygon aPoly=
                basegfx::tools::number2PolyPolygon(rNumbers[i],10,3);

            aTmp=aTmp*aScaleShear;
            aPoly.transform(aTmp);
            // glColor4f(0,1,0,1);
            glm::vec4 color  = glm::vec4(1, 0, 0, 0.5);
            renderPolyPolygon(aPoly, renderHelper, color);
        }
    }
}

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