Welcome to mirror list, hosted at ThFree Co, Russian Federation.

path_renderer.cpp « graphics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 90bb57a49db48db0cb4092f21da586804b5d970b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "graphics/path_renderer.hpp"
#include "graphics/resource.hpp"
#include "graphics/pen.hpp"
#include "graphics/resource_cache.hpp"
#include "graphics/path_view.hpp"

#include "graphics/opengl/base_texture.hpp"

#include "base/logging.hpp"

namespace graphics
{
  PathRenderer::Params::Params()
    : m_drawPathes(true)
  {}

  PathRenderer::PathRenderer(Params const & p)
    : base_t(p),
      m_drawPathes(p.m_drawPathes)
  {}

  void PathRenderer::drawPath(m2::PointD const * pts, size_t ptsCount, double offset, uint32_t resID, double depth)
  {
    ++m_pathCount;
    m_pointsCount += ptsCount;

    if (!m_drawPathes)
      return;

    ASSERT_GREATER_OR_EQUAL(ptsCount, 2, ());
    ASSERT_NOT_EQUAL(resID, uint32_t(-1), ());

    Resource const * res = base_t::fromID(resID);

    if (res == 0)
    {
      LOG(LDEBUG, ("drawPath: resID=", resID, "wasn't found on current skin"));
      return;
    }

    ASSERT(res->m_cat == Resource::EPen, ());

    Pen const * pen = static_cast<Pen const *>(res);

    if (!pen->m_info.m_icon.m_name.empty())
      drawSymbolPath(pts, ptsCount, offset, pen, depth);
    else
      if (pen->m_isSolid)
        drawSolidPath(pts, ptsCount, offset, pen, depth);
      else
        drawStipplePath(pts, ptsCount, offset, pen, depth);
  }

  void PathRenderer::drawSymbolPath(m2::PointD const * pts, size_t ptsCount, double offset, Pen const * pen, double depth)
  {
    PathView pv(pts, ptsCount);

    PathPoint pt = pv.front();

    double step = pen->m_info.m_step;

    offset += pen->m_info.m_offset;

    if (offset < 0)
      offset = fmod(offset, step);

    pt = pv.offsetPoint(pt, offset);

    m2::RectU texRect = pen->m_texRect;
    texRect.Inflate(-1, -1);

    double const w = texRect.SizeX();
    double const h = texRect.SizeY();

    double const hw = w / 2.0;
    double const hh = h / 2.0;

    /// do not render start symbol if it's
    /// completely outside the first segment.
    if (offset + w < 0)
    {
      pv.offsetPoint(pt, step);
      offset += step;
    }

    shared_ptr<gl::BaseTexture> tex = pipeline(pen->m_pipelineID).texture();

    while (true)
    {
      PivotPoint pvPt = pv.findPivotPoint(pt, hw - 1);

      if (pvPt.m_pp.m_i == -1)
        break;

      ang::AngleD ang = pvPt.m_angle;

      m2::PointD pts[4];

      pts[0] = pvPt.m_pp.m_pt.Move(-hw, ang.sin(), ang.cos());
      pts[0] = pts[0].Move(hh + 1, -ang.cos(), ang.sin());

      pts[1] = pts[0].Move(w, ang.sin(), ang.cos());
      pts[2] = pts[0].Move(-h, -ang.cos(), ang.sin());
      pts[3] = pts[2].Move(w, ang.sin(), ang.cos());

      m2::PointF texPts[4] =
      {
        tex->mapPixel(m2::PointF(texRect.minX(), texRect.minY())),
        tex->mapPixel(m2::PointF(texRect.maxX(), texRect.minY())),
        tex->mapPixel(m2::PointF(texRect.minX(), texRect.maxY())),
        tex->mapPixel(m2::PointF(texRect.maxX(), texRect.maxY()))
      };

      m2::PointF normal(0, 0);

      addTexturedStripStrided(pts, sizeof(m2::PointD),
                              &normal, 0,
                              texPts, sizeof(m2::PointF),
                              4,
                              depth,
                              pen->m_pipelineID);

      pt = pv.offsetPoint(pvPt.m_pp, pen->m_info.m_step);

      if (pt.m_i == -1)
        break;
    }
  }

  void PathRenderer::drawStipplePath(m2::PointD const * points, size_t pointsCount, double offset, Pen const * pen, double depth)
  {
    bool const hasRoundJoin = (pen->m_info.m_join == pen->m_info.ERoundJoin);
//    bool const hasBevelJoin = (pen->m_info.m_join == pen->m_info.EBevelJoin);
    bool const hasJoin = (pen->m_info.m_join != pen->m_info.ENoJoin);

    float rawTileStartLen = 0;

    float rawTileLen = (float)pen->rawTileLen();

    if (offset < 0)
      offset = offset - rawTileLen * ceil(offset / rawTileLen);

    bool skipToOffset = true;

    /// Geometry width. It's 1px wider than the pattern width.
    float const geomWidth = pen->m_info.m_w + 4 - 2 * aaShift();
    float const geomHalfWidth = geomWidth / 2.0;

    for (size_t i = 0; i < pointsCount - 1; ++i)
    {
      m2::PointD dir = points[i + 1] - points[i];
      dir *= 1.0 / dir.Length(m2::PointD(0, 0));
      m2::PointD norm(-dir.y, dir.x);

      /// The length of the current segment.
      float segLen = points[i + 1].Length(points[i]);
      /// The remaining length of the segment
      float segLenRemain = segLen;

      if (skipToOffset)
      {
        offset -= segLen;
        if (offset >= 0)
          continue;
        else
        {
          skipToOffset = false;
          segLenRemain = -offset;
        }
      }

      /// Starting point of the tiles on this segment
      m2::PointF rawTileStartPt = points[i] + dir * (segLen - segLenRemain);

      /// Tiling procedes as following :
      /// The leftmost tile goes antialiased at left and non-antialiased at right.
      /// The inner tiles goes non-antialiased at both sides.
      /// The rightmost tile goes non-antialised at left and antialiased at right side.

      /// Length of the actual pattern data being tiling(without antialiasing zones).
      rawTileLen = 0;

      GeometryPipeline & p = pipeline(pen->m_pipelineID);

      shared_ptr<gl::BaseTexture> texture = p.texture();

      if (!texture)
      {
        LOG(LDEBUG, ("returning as no texture is reserved"));
        return;
      }

      float texMaxY = pen->m_texRect.maxY() - aaShift();
      float texMinY = pen->m_texRect.minY() + aaShift();

      m2::PointF const fNorm = norm * geomHalfWidth;  // enough to calc it once

      while (segLenRemain > 0)
      {
        rawTileLen = std::min(((float)pen->rawTileLen() - rawTileStartLen), segLenRemain);

        float texMinX = pen->m_texRect.minX() + 2 + rawTileStartLen;
        float texMaxX = texMinX + rawTileLen;

        rawTileStartLen += rawTileLen;
        if (rawTileStartLen >= pen->rawTileLen())
          rawTileStartLen -= pen->rawTileLen();
        ASSERT(rawTileStartLen < pen->rawTileLen(), ());

        m2::PointF rawTileEndPt(rawTileStartPt.x + dir.x * rawTileLen, rawTileStartPt.y + dir.y * rawTileLen);

        m2::PointF coords[4];

        coords[0] = rawTileStartPt;
        coords[1] = rawTileStartPt;
        coords[2] = rawTileEndPt;
        coords[3] = rawTileEndPt;

        m2::PointF texCoords[4] =
        {
          texture->mapPixel(m2::PointF(texMinX, texMinY)),
          texture->mapPixel(m2::PointF(texMinX, texMaxY)),
          texture->mapPixel(m2::PointF(texMaxX, texMaxY)),
          texture->mapPixel(m2::PointF(texMaxX, texMinY))
        };

        m2::PointF normals[4] =
        {
          fNorm,
          -fNorm,
          -fNorm,
          fNorm
        };

        addTexturedFan(coords,
                       normals,
                       texCoords,
                       4,
                       depth,
                       pen->m_pipelineID);

        segLenRemain -= rawTileLen;

        rawTileStartPt = rawTileEndPt;
      }

      bool isColorJoin = hasJoin && pen->m_info.atDashOffset(rawTileLen);

      /// Adding geometry for a line join between previous and current segment.
      if ((i != pointsCount - 2) && isColorJoin)
      {
        m2::PointD nextDir = points[i + 2] - points[i + 1];
        nextDir *= 1.0 / nextDir.Length(m2::PointD(0, 0));
        m2::PointD nextNorm(-nextDir.y, nextDir.x);

        /// Computing the sin of angle between directions.
        double alphaSin = dir.x * nextDir.y - dir.y * nextDir.x;
        double alphaCos = dir.x * nextDir.x + dir.y * nextDir.y;
        double alpha = atan2(alphaSin, alphaCos);

        int angleSegCount = 1; // bevel join - 1 segment
        if (hasRoundJoin)
          angleSegCount= int(ceil(fabs(alpha) / (math::pi / 6)));

        double angleStep = alpha / angleSegCount;

        m2::PointD startVec;

        if (alpha > 0)
        {
          /// The outer side is on the prevNorm direction.
          startVec = -norm;
        }
        else
        {
          /// The outer side is on the -prevNorm direction
          startVec = norm;
        }

        GeometryPipeline & p = pipeline(pen->m_pipelineID);

        shared_ptr<gl::BaseTexture> texture = p.texture();

        if (!texture)
        {
          LOG(LDEBUG, ("returning as no texture is reserved"));
          return;
        }

        m2::PointF joinSegTex[3] =
        {
          texture->mapPixel(pen->m_centerColorPixel),
          texture->mapPixel(pen->m_borderColorPixel),
          texture->mapPixel(pen->m_borderColorPixel)
        };

        m2::PointD prevStartVec = startVec;
        for (int j = 0; j < angleSegCount; ++j)
        {
          /// Rotate start vector to find another point on a join.
          startVec.Rotate(angleStep);

          m2::PointF joinSeg[3] =
          {
            points[i + 1],
            points[i + 1],
            points[i + 1]
          };
          m2::PointF joinSegNormals[3] =
          {
            m2::PointF(0, 0),
            startVec * geomHalfWidth,
            prevStartVec * geomHalfWidth
          };

          addTexturedFan(joinSeg,
                         joinSegNormals,
                         joinSegTex,
                         3,
                         depth,
                         pen->m_pipelineID);

          prevStartVec = startVec;
        }
      }
    }
  }


  void PathRenderer::drawSolidPath(m2::PointD const * points, size_t pointsCount, double offset, Pen const * pen, double depth)
  {
    ASSERT(pen->m_isSolid, ());

    bool const hasRoundCap = (pen->m_info.m_cap == pen->m_info.ERoundCap);
    bool const hasSquareCap = (pen->m_info.m_cap == pen->m_info.ESquareCap);
    bool const hasRoundJoin = (pen->m_info.m_join == pen->m_info.ERoundJoin);
    bool const hasBevelJoin = (pen->m_info.m_join == pen->m_info.EBevelJoin);

    float geomHalfWidth = (pen->m_info.m_w + 4 - aaShift() * 2) / 2.0;

    m2::PointD dir = points[1] - points[0];
    double len = dir.Length(m2::PointD(0, 0));
    dir *= 1.0 / len;
    m2::PointD const norm(-dir.y, dir.x);
    m2::PointD fNorm = norm * geomHalfWidth;
    m2::PointD fDir(fNorm.y, -fNorm.x);

    m2::PointD fNormNextSeg;
    m2::PointD fDirNextSeg;

    for (size_t i = 0; i < pointsCount - 1; ++i)
    {      
      bool const leftIsCap  = i == 0;
      bool const rightIsCap = i == (pointsCount - 2);

      if (!leftIsCap)
      {
        fNorm = fNormNextSeg;
        fDir = fDirNextSeg;
      }

      m2::PointD const & nextPt = points[i + 1];

      if (!rightIsCap)
      {
        m2::PointD dirNextSeg = points[i + 2] - points[i + 1];
        double lenNextSeg = dirNextSeg.Length(m2::PointD(0, 0));
        dirNextSeg *= 1.0 / lenNextSeg;
        m2::PointD normNextSeg(-dirNextSeg.y, dirNextSeg.x);
        fNormNextSeg = normNextSeg * geomHalfWidth;
        fDirNextSeg = m2::PointD(fNormNextSeg.y, -fNormNextSeg.x);
      }

      float texMinX = pen->m_texRect.minX() + aaShift();
      float texMaxX = pen->m_texRect.maxX() - aaShift();

      float texMinY = pen->m_texRect.minY() + aaShift();
      float texMaxY = pen->m_texRect.maxY() - aaShift();

      float texCenterX = (texMinX + texMaxX) / 2;

      int numPoints = 4;

      if (leftIsCap && (hasRoundCap || hasSquareCap))
        numPoints += 2;
      if ((rightIsCap && (hasRoundCap || hasSquareCap))
       || (!rightIsCap && (hasRoundJoin || hasBevelJoin)))
        numPoints += 2;

      int cur = 0;

      ASSERT(numPoints <= 8, ("numPoints is more than 8, won't fit in array"));
      m2::PointF coords[8];

      if (leftIsCap && (hasRoundCap || hasSquareCap))
      {
        coords[cur++] = points[i] - fDir + fNorm;
        coords[cur++] = points[i] - fDir - fNorm;
      }

      coords[cur++] = points[i] + fNorm;
      coords[cur++] = points[i] - fNorm;
      coords[cur++] = nextPt + fNorm;
      coords[cur++] = nextPt - fNorm;

      if ((rightIsCap && (hasRoundCap || hasSquareCap))
       || (!rightIsCap && hasRoundJoin))
      {
        coords[cur++] = nextPt + fDir + fNorm;
        coords[cur++] = nextPt + fDir - fNorm;
      }
      else if (!rightIsCap && hasBevelJoin)
      {
        coords[cur++] = points[i + 1] + fNormNextSeg;
        coords[cur++] = points[i + 1] - fNormNextSeg;
      }

      GeometryPipeline & p = pipeline(pen->m_pipelineID);

      shared_ptr<gl::BaseTexture> texture = p.texture();

      if (!texture)
      {
        LOG(LDEBUG, ("returning as no texture is reserved"));
        return;
      }

      m2::PointF texCoords[8];
      cur = 0;

      if (leftIsCap && hasRoundCap)
      {
        texCoords[cur++] = texture->mapPixel(m2::PointF(texMinX, texMinY));
        texCoords[cur++] = texture->mapPixel(m2::PointF(texMinX, texMaxY));
      }
      else if (leftIsCap && hasSquareCap)
      {
        texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMinY));
        texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMaxY));
      }

      texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMinY));
      texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMaxY));
      texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMinY));
      texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMaxY));

      if ((rightIsCap && hasRoundCap)
       || (!rightIsCap && hasRoundJoin))
      {
        texCoords[cur++] = texture->mapPixel(m2::PointF(texMinX, texMinY));
        texCoords[cur++] = texture->mapPixel(m2::PointF(texMinX, texMaxY));
      }
      else if ((rightIsCap && hasSquareCap)
            || (!rightIsCap && hasBevelJoin))
      {
        texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMinY));
        texCoords[cur++] = texture->mapPixel(m2::PointF(texCenterX, texMaxY));
      }

      m2::PointF normal(0, 0);

      addTexturedStripStrided(coords, sizeof(m2::PointF),
                              &normal, 0,
                              texCoords, sizeof(m2::PointF),
                              numPoints,
                              depth,
                              pen->m_pipelineID);
    }
  }

  void PathRenderer::beginFrame()
  {
    base_t::beginFrame();
    m_pathCount = 0;
    m_pointsCount = 0;
  }

  void PathRenderer::endFrame()
  {
    base_t::endFrame();
  }

}