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

testing_engine.cpp « drape_head - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2adca662f9bfd80cd7b1a6fd910942c9880a7f6 (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
485
#include "testing_engine.hpp"

#include "../coding/file_reader.hpp"
#include "../platform/platform.hpp"

#include "../drape/vertex_array_buffer.hpp"
#include "../drape/shader_def.hpp"
#include "../drape/overlay_tree.hpp"

#include "../drape_frontend/visual_params.hpp"
#include "../drape_frontend/line_shape.hpp"
#include "../drape_frontend/text_shape.hpp"
#include "../drape_frontend/path_text_shape.hpp"
#include "../drape_frontend/path_symbol_shape.hpp"
#include "../drape_frontend/area_shape.hpp"
#include "../drape_frontend/circle_shape.hpp"

#include "../geometry/transformations.hpp"

#include "../base/stl_add.hpp"
#include "../base/timer.hpp"

#include "../std/bind.hpp"
#include "../std/function.hpp"
#include "../std/vector.hpp"

#include "../../3party/jansson/myjansson.hpp"

namespace df
{

class SquareHandle : public OverlayHandle
{
public:
  static const uint8_t NormalAttributeID = 1;
  SquareHandle(vector<m2::PointF> const & formingVector)
    : OverlayHandle(FeatureID(), dp::Center, 0.0f)
    , m_vectors(formingVector)
  {
    SetIsVisible(true);
  }

  virtual m2::RectD GetPixelRect(ScreenBase const & screen) const { return m2::RectD(); }

  virtual void GetAttributeMutation(RefPointer<AttributeBufferMutator> mutator) const
  {
    static const my::Timer timer;
    double const angle = timer.ElapsedSeconds();

    math::Matrix<double, 3, 3> const m = math::Rotate(math::Identity<double, 3>(), angle);

    vector<m2::PointF> data(4);
    for (size_t i = 0; i < m_vectors.size(); ++i)
      data[i] = m_vectors[i] * m;

    TOffsetNode const & node = GetOffsetNode(NormalAttributeID);
    MutateNode mutateNode;
    mutateNode.m_region = node.second;
    mutateNode.m_data = MakeStackRefPointer<void>(&data[0]);
    mutator->AddMutation(node.first, mutateNode);
  }

private:
  vector<m2::PointF> m_vectors;
};

class SquareShape : public MapShape
{
public:
  SquareShape(m2::PointF const & center, float radius)
    : m_center(center)
    , m_radius(radius)
  {
  }

  virtual void Draw(RefPointer<Batcher> batcher, RefPointer<TextureSetHolder> textures) const
  {
    vector<m2::PointF> vertexes(4, m_center);

    vector<m2::PointF> formingVectors(4);
    formingVectors[0] = m2::PointF(-m_radius,  m_radius);
    formingVectors[1] = m2::PointF(-m_radius, -m_radius);
    formingVectors[2] = m2::PointF( m_radius,  m_radius);
    formingVectors[3] = m2::PointF( m_radius, -m_radius);

    AttributeProvider provider(2, 4);
    {
      BindingInfo info(1);
      BindingDecl & decl = info.GetBindingDecl(0);
      decl.m_attributeName = "a_position";
      decl.m_componentCount = 2;
      decl.m_componentType = gl_const::GLFloatType;
      decl.m_offset = 0;
      decl.m_stride = 0;
      provider.InitStream(0, info, MakeStackRefPointer<void>(&vertexes[0]));
    }
    {
      BindingInfo info(1, SquareHandle::NormalAttributeID);
      BindingDecl & decl = info.GetBindingDecl(0);
      decl.m_attributeName = "a_normal";
      decl.m_componentCount = 2;
      decl.m_componentType = gl_const::GLFloatType;
      decl.m_offset = 0;
      decl.m_stride = 0;
      provider.InitStream(1, info, MakeStackRefPointer<void>(&formingVectors[0]));
    }

    GLState state(gpu::TEST_DYN_ATTR_PROGRAM, GLState::GeometryLayer);
    state.SetColor(Color(150, 130, 120, 255));

    OverlayHandle * handle = new SquareHandle(formingVectors);

    batcher->InsertTriangleStrip(state, MakeStackRefPointer<AttributeProvider>(&provider), MovePointer(handle));
  }

private:
  m2::PointF m_center;
  float m_radius;
};

class MapShapeFactory
{
  typedef function<MapShape * (json_t *)> TCreateFn;
  typedef map<string, TCreateFn> TCreatorsMap;

public:
  MapShapeFactory()
  {
    m_creators["line"] = bind(&MapShapeFactory::CreateLine, this, _1);
    m_creators["area"] = bind(&MapShapeFactory::CreateArea, this, _1);
    m_creators["dyn_square"] = bind(&MapShapeFactory::CreateDynSquare, this, _1);
    m_creators["circle"] = bind(&MapShapeFactory::CreateCircle, this, _1);
  }

  void CreateShapes(vector<MapShape *> & shapes, json_t * object)
  {
    void * iter = json_object_iter(object);
    while(iter)
    {
      json_t * entry = json_object_iter_value(iter);
      if (entry)
      {
        string const type(json_object_iter_key(iter));
        if (type != "_comment_")
        {
          TCreatorsMap::const_iterator it = m_creators.find(type);
          ASSERT(it != m_creators.end(), ());
          shapes.push_back(it->second(entry));
        }
        iter = json_object_iter_next(object, iter);
      }
    }
  }

private:
  Color ParseColor(json_t * object)
  {
    size_t const channelCount = json_array_size(object);
    ASSERT(channelCount == 4, ());
    int const r = json_integer_value(json_array_get(object, 0));
    int const g = json_integer_value(json_array_get(object, 1));
    int const b = json_integer_value(json_array_get(object, 2));
    int const a = json_integer_value(json_array_get(object, 3));
    return Color(r, g, b, a);
  }

  float ParseCoord(json_t * object)
  {
    if (json_is_real(object))
      return json_real_value(object);
    else if (json_is_integer(object))
      return json_integer_value(object);

    ASSERT(false, ());
    return 0.0f;
  }

  void ParseGeometry(json_t * object, vector<m2::PointF> & points)
  {
    size_t const count = json_array_size(object);
    ASSERT((count & 1) == 0, ());
    points.reserve(count >> 1);
    for (size_t i = 0; i < count; i += 2)
    {
      double const x = ParseCoord(json_array_get(object, i));
      double const y = ParseCoord(json_array_get(object, i + 1));
      points.push_back(m2::PointF(x, y));
    }
  }

  dp::LineJoin ParseJoin(json_t * object)
  {
    return (dp::LineJoin)json_integer_value(object);
  }

  dp::LineCap ParseCap(json_t * object)
  {
    return (dp::LineCap)json_integer_value(object);
  }

  MapShape * CreateLine(json_t * object)
  {
    LineViewParams params;
    params.m_depth = json_real_value(json_object_get(object, "depth"));
    params.m_color = ParseColor(json_object_get(object, "color"));
    params.m_width = json_real_value(json_object_get(object, "width"));
    params.m_join = ParseJoin(json_object_get(object, "join"));
    params.m_cap = ParseCap(json_object_get(object, "cap"));

    vector<m2::PointF> points;
    ParseGeometry(json_object_get(object, "geometry"), points);
    return new LineShape(points, params);
  }

  MapShape * CreateArea(json_t * object)
  {
    AreaViewParams params;
    params.m_depth = json_real_value(json_object_get(object, "depth"));
    params.m_color = ParseColor(json_object_get(object, "color"));
    vector<m2::PointF> points;
    ParseGeometry(json_object_get(object, "geometry"), points);

    return new AreaShape(points, params);
  }

  MapShape * CreateDynSquare(json_t * object)
  {
    float radius = json_real_value(json_object_get(object, "radius"));
    vector<m2::PointF> point;
    ParseGeometry(json_object_get(object, "geometry"), point);
    return new SquareShape(point[0], radius);
  }

  MapShape * CreateCircle(json_t * object)
  {
    CircleViewParams params(FeatureID(-1, 0));
    params.m_depth = json_real_value(json_object_get(object, "depth"));
    params.m_color = ParseColor(json_object_get(object, "color"));
    params.m_radius = json_real_value(json_object_get(object, "radius"));
    vector<m2::PointF> point;
    ParseGeometry(json_object_get(object, "geometry"), point);

    return new CircleShape(point[0], params);
  }

private:
  TCreatorsMap m_creators;
};

TestingEngine::TestingEngine(RefPointer<OGLContextFactory> oglcontextfactory,
                             double vs, df::Viewport const & viewport)
  : m_contextFactory(oglcontextfactory)
  , m_viewport(viewport)
{
  GLFunctions::Init();
  df::VisualParams::Init(vs, df::CalculateTileSize(viewport.GetWidth(), viewport.GetHeight()));
  m_contextFactory->getDrawContext()->makeCurrent();

  m_textures.Reset(new TextureManager());
  m_textures->Init(df::VisualParams::Instance().GetResourcePostfix());
  m_batcher.Reset(new Batcher());
  m_programManager.Reset(new GpuProgramManager());

  ModelViewInit();
  ProjectionInit();

  m_timerId = startTimer(1000 / 30);
}

TestingEngine::~TestingEngine()
{
  killTimer(m_timerId);
  ClearScene();
  m_batcher.Destroy();
  m_textures->Release();
  m_textures.Destroy();
  m_programManager.Destroy();
}

void TestingEngine::Draw()
{
  static bool isInitialized = false;
  if (!isInitialized)
  {
    m_batcher->StartSession(bind(&df::TestingEngine::OnFlushData, this, _1, _2));
    DrawImpl();
    m_batcher->EndSession();
    isInitialized = true;
  }

  OGLContext * context = m_contextFactory->getDrawContext();
  context->setDefaultFramebuffer();

  m_viewport.Apply();
  GLFunctions::glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
  GLFunctions::glClear();

  TScene::iterator it = m_scene.begin();
  for(; it != m_scene.end(); ++it)
  {
    GLState const & state = it->first;
    RefPointer<GpuProgram> prg = m_programManager->GetProgram(state.GetProgramIndex());
    prg->Bind();
    TextureSetBinder binder(m_textures.GetRefPointer());
    ApplyState(state, prg, MakeStackRefPointer<TextureSetController>(&binder));
    ApplyUniforms(m_generalUniforms, prg);

    vector<MasterPointer<RenderBucket> > & buckets = it->second;
    OverlayTree tree;
    tree.StartOverlayPlacing(m_modelView, true);
    for (size_t i = 0; i < buckets.size(); ++i)
      buckets[i]->CollectOverlayHandles(MakeStackRefPointer(&tree));
    for (size_t i = 0; i < buckets.size(); ++i)
      buckets[i]->Render();
    tree.EndOverlayPlacing();
  }

  context->present();
}

void TestingEngine::Resize(int w, int h)
{
  m_modelView.OnSize(0, 0, w, h);
  m_viewport.SetViewport(0, 0, w, h);
  ModelViewInit();
  ProjectionInit();
  Draw();
}

void TestingEngine::DragStarted(m2::PointF const & p) {}
void TestingEngine::Drag(m2::PointF const & p) {}
void TestingEngine::DragEnded(m2::PointF const & p) {}
void TestingEngine::Scale(m2::PointF const & p, double factor) {}

void TestingEngine::timerEvent(QTimerEvent * e)
{
  if (e->timerId() == m_timerId)
    Draw();
}

void TestingEngine::DrawImpl()
{
  ReaderPtr<ModelReader> reader = GetPlatform().GetReader("test_scene.json");
  string jsonString;
  reader.ReadAsString(jsonString);

  vector<MapShape *> shapes;
  try
  {
    my::Json json(jsonString.c_str());
    MapShapeFactory factory;
    factory.CreateShapes(shapes, json.get());
  }
  catch (RootException & e)
  {
    LOG(LCRITICAL, (e.Msg()));
  }

  for (size_t i = 0; i < shapes.size(); ++i)
    shapes[i]->Draw(m_batcher.GetRefPointer(), m_textures.GetRefPointer());

  DeleteRange(shapes, DeleteFunctor());

  FontDecl fd;
  fd.m_color = Color(200, 80, 240, 255);
  fd.m_needOutline = true;
  fd.m_outlineColor = Color(255, 255, 255, 255);
  fd.m_size = 60.0f;
  FontDecl auxFd;
  auxFd.m_color = Color(0, 80, 240, 255);
  auxFd.m_needOutline = false;
  auxFd.m_outlineColor = Color(0, 255, 0, 255);
  auxFd.m_size = 20.0f;

  TextViewParams params;
  params.m_featureID = FeatureID(23, 567);
  params.m_depth = 10.0f;
  params.m_anchor = dp::Left;
  params.m_primaryOffset = m2::PointF(0,0);
  params.m_primaryText = "hjksdfhjajsdf";
  params.m_primaryTextFont = fd;
  params.m_secondaryTextFont = auxFd;
  params.m_secondaryText = "Small fix bugs";
  TextShape sh1(m2::PointF(200.0f, 300.0f), params);
  sh1.Draw(m_batcher.GetRefPointer(), m_textures.GetRefPointer());

  params.m_featureID = FeatureID(23, 78);
  params.m_depth = -10.0f;
  params.m_anchor = dp::RightTop;
  //params.m_primaryTextFont.m_needOutline = false;
  TextShape sh2(m2::PointF(250.0f, 250.0f), params);
  //sh2.Draw(m_batcher.GetRefPointer(), m_textures.GetRefPointer());

  vector<m2::PointF> path;

  path.push_back(m2::PointF(200, 650));
  path.push_back(m2::PointF(200, 450));
  for(int i = 16; i >= 0 ; --i)
  {
    float r = 200.0f;
    float x = r * cos((float)i / 32.0f * 2.0f * M_PI) + 800.0f;
    float y = r * sin((float)i / 32.0f * 2.0f * M_PI) + 450.0f;
    path.push_back(m2::PointF(x, y));
  }
  path.push_back(m2::PointF(1600, 450));

  PathTextViewParams params3;
  params3.m_featureID = FeatureID(23, 78);
  params3.m_depth = -10.0f;
  params3.m_OffsetEnd = 600.0f;
  params3.m_OffsetStart = 100.0f;
  params3.m_Text = "√2+√3=?-fghjkfghjf---_________----+";
  params3.m_TextFont = params.m_primaryTextFont;
  PathTextShape sh3(path, params3);
  sh3.Draw(m_batcher.GetRefPointer(), m_textures.GetRefPointer());

  PathSymbolViewParams params4;
  params4.m_featureID = FeatureID(23, 78);
  params4.m_depth = 30.0f;
  params4.m_Offset = 40.0f;
  params4.m_OffsetStart = 0.0f;
  params4.m_symbolName = "arrow";
  PathSymbolShape sh4(path, params4, 10);
  sh4.Draw(m_batcher.GetRefPointer(), m_textures.GetRefPointer());
}

void TestingEngine::ModelViewInit()
{
  math::Matrix<double, 3, 3> m = math::Shift(
                                        math::Rotate(
                                            math::Scale(math::Identity<double, 3>(), 1.0, 1.0),
                                                        0.0),
                                             0.0, 0.0);

  m_modelView.SetGtoPMatrix(m);

  math::Matrix<float, 4, 4> mv;

  /// preparing ModelView matrix

  mv(0, 0) = m(0, 0); mv(0, 1) = m(1, 0); mv(0, 2) = 0; mv(0, 3) = m(2, 0);
  mv(1, 0) = m(0, 1); mv(1, 1) = m(1, 1); mv(1, 2) = 0; mv(1, 3) = m(2, 1);
  mv(2, 0) = 0;       mv(2, 1) = 0;       mv(2, 2) = 1; mv(2, 3) = 0;
  mv(3, 0) = m(0, 2); mv(3, 1) = m(1, 2); mv(3, 2) = 0; mv(3, 3) = m(2, 2);

  m_generalUniforms.SetMatrix4x4Value("modelView", mv.m_data);
}

void TestingEngine::ProjectionInit()
{
  float left = m_viewport.GetX0();
  float right = left + m_viewport.GetWidth();
  float bottom = m_viewport.GetY0();
  float top = bottom + m_viewport.GetHeight();
  float near = -20000.0f;
  float far = 20000.0f;

  float m[4 * 4];
  memset(m, 0, sizeof(m));
  m[0]  = 2.0f / (right - left);
  m[3]  = - (right + left) / (right - left);
  m[5]  = 2.0f / (bottom - top);
  m[7]  = - (bottom + top) / (bottom - top);
  m[10] = -2.0f / (far - near);
  m[11] = - (far + near) / (far - near);
  m[15] = 1.0;

  m_generalUniforms.SetMatrix4x4Value("projection", m);
}

void TestingEngine::OnFlushData(GLState const & state, TransferPointer<RenderBucket> vao)
{
  MasterPointer<RenderBucket> bucket(vao);
  bucket->GetBuffer()->Build(m_programManager->GetProgram(state.GetProgramIndex()));
  m_scene[state].push_back(bucket);
}

void TestingEngine::ClearScene()
{
  TScene::iterator it = m_scene.begin();
  for(; it != m_scene.end(); ++it)
    DeleteRange(it->second, MasterPointerDeleter());
}

} // namespace df