From 4fa126ec53253066fe22cccc9878f0f85c0fb34a Mon Sep 17 00:00:00 2001 From: exmix Date: Sat, 12 Jul 2014 21:47:25 +0200 Subject: [PATCH] [drape] loading shapes in testing engine from json file --- data/test_scene.json | 49 +++++++ drape_head/drape_head.pro | 4 +- drape_head/testing_engine.cpp | 233 ++++++++++++++++++++++------------ 3 files changed, 207 insertions(+), 79 deletions(-) create mode 100644 data/test_scene.json diff --git a/data/test_scene.json b/data/test_scene.json new file mode 100644 index 0000000000..29d43ea5aa --- /dev/null +++ b/data/test_scene.json @@ -0,0 +1,49 @@ +{ + "_comment_":"Join = { -1 = MitterJoin, 0 = BevelJoin, 1 = RoundJoin } ", + "_comment_":"Cap = { -1 = SquareCap, 0 = RoundCap, 1 = ButtCap } ", + "_comment_":{ + "_comment_":"LineFormat", + "join" : "{ -1 = MitterJoin, 0 = BevelJoin, 1 = RoundJoin }", + "cap" : "{ -1 = SquareCap, 0 = RoundCap, 1 = ButtCap }", + "depth" : "float", + "width" : "float on pixels", + "color" : ["r", "g", "b", "a"], + "geometry" : ["float x0", "float y0", "x1", "y1", "xn", "yn"] + }, + "_comment_":{ + "_comment_":"AreaFormat", + "depth" : "float", + "color" : ["r", "g", "b", "a"], + "geometry" : ["trg1.x0", "trg1.y0", "trg1.x1", "trg1.y1", "trg1.x1", "trg1.y1", "trg2.x0", "trg2.y0", "trg2.x1", "trg2.y1", "trg2.x1", "trg2.y1"] + }, + "_comment_":{ + "_comment_":"CircleFormat", + "depth" : "float", + "color" : ["r", "g", "b", "a"], + "radius": "float", + "geometry" : ["centex.x", "centex.y"] + }, + "line":{ + "join":1, + "cap":0, + "depth":0.0, + "width":33.0, + "color":[255, 0, 0, 255], + "geometry": [720, 375, + 720, 625, + 845, 625, + 845, 425, + 680, 425] + }, + "area":{ + "depth":0.0, + "color":[255, 255, 0, 255], + "geometry":[300, 300, 200, 300, 200, 200, 300, 300, 200, 200, 300, 200] + }, + "circle":{ + "depth" : 10.0, + "color" : [30, 255, 10, 255], + "radius": 55.0, + "geometry" : [500, 200] + } +} \ No newline at end of file diff --git a/drape_head/drape_head.pro b/drape_head/drape_head.pro index c20b223f7f..4ef24733d2 100644 --- a/drape_head/drape_head.pro +++ b/drape_head/drape_head.pro @@ -1,9 +1,11 @@ # Head project for drape develop and debuging ROOT_DIR = .. -DEPENDENCIES = drape_frontend map drape indexer platform geometry coding base expat protobuf zlib +DEPENDENCIES = drape_frontend map drape indexer platform geometry coding base expat protobuf jansson zlib include($$ROOT_DIR/common.pri) +INCLUDEPATH += $$ROOT_DIR/3party/jansson/src + TARGET = DrapeHead TEMPLATE = app CONFIG += warn_on diff --git a/drape_head/testing_engine.cpp b/drape_head/testing_engine.cpp index cc8cb2f8ac..1be84bdf8a 100644 --- a/drape_head/testing_engine.cpp +++ b/drape_head/testing_engine.cpp @@ -1,17 +1,154 @@ #include "testing_engine.hpp" +#include "../coding/file_reader.hpp" +#include "../platform/platform.hpp" + #include "../drape/vertex_array_buffer.hpp" #include "../drape_frontend/visual_params.hpp" #include "../drape_frontend/line_shape.hpp" +#include "../drape_frontend/area_shape.hpp" +#include "../drape_frontend/circle_shape.hpp" #include "../base/stl_add.hpp" #include "../std/bind.hpp" +#include "../std/function.hpp" +#include "../std/vector.hpp" + +#include "../../3party/jansson/myjansson.hpp" namespace df { +class MapShapeFactory +{ + typedef function create_fn; + typedef map creators_map_t; + +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 & shapes, json_t * object) + { + void * iter = json_object_iter(object); + while(iter) + { + json_t * entry = json_object_iter_value(iter); + if (entry) + { + string type(json_object_iter_key(iter)); + if (type != "_comment_") + { + creators_map_t::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 channelCount = json_array_size(object); + ASSERT(channelCount == 4, ()); + int r = json_integer_value(json_array_get(object, 0)); + int g = json_integer_value(json_array_get(object, 1)); + int b = json_integer_value(json_array_get(object, 2)); + int 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 & points) + { + size_t count = json_array_size(object); + ASSERT((count & 1) == 0, ()); + points.reserve(count >> 1); + for (size_t i = 0; i < count; i += 2) + { + double x = ParseCoord(json_array_get(object, i)); + double y = ParseCoord(json_array_get(object, i + 1)); + points.push_back(m2::PointF(x, y)); + } + } + + df::LineJoin ParseJoin(json_t * object) + { + return (df::LineJoin)json_integer_value(object); + } + + df::LineCap ParseCap(json_t * object) + { + return (df::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 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 points; + ParseGeometry(json_object_get(object, "geometry"), points); + + return new AreaShape(points, params); + } + + MapShape * CreateDynSquare(json_t * object) + { + /// TODO + ASSERT(false, ()); + return NULL; + } + + 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 point; + ParseGeometry(json_object_get(object, "geometry"), point); + + return new CircleShape(point[0], params); + } + +private: + creators_map_t m_creators; +}; + TestingEngine::TestingEngine(RefPointer oglcontextfactory, double vs, df::Viewport const & viewport) : m_contextFactory(oglcontextfactory) @@ -52,6 +189,7 @@ void TestingEngine::Draw() m_viewport.Apply(); GLFunctions::glClearColor(0.65f, 0.65f, 0.65f, 1.0f); GLFunctions::glClear(); + GLFunctions::glDisable(gl_const::GLDepthTest); scene_t::iterator it = m_scene.begin(); for(; it != m_scene.end(); ++it) @@ -85,87 +223,26 @@ void TestingEngine::Scale(m2::PointF const & p, double factor) {} void TestingEngine::DrawImpl() { - // Insert shape batchering here - float left = m_viewport.GetX0(); - float right = left + m_viewport.GetWidth(); - float bottom = m_viewport.GetY0(); - float top = bottom + m_viewport.GetHeight(); + ReaderPtr reader = GetPlatform().GetReader("test_scene.json"); + string jsonString; + reader.ReadAsString(jsonString); - float const resolution = 32; - float const stepX = right/resolution; - float const stepY = top/resolution; - m2::PointF grid[32][32]; // to simpify testing - for (size_t i = 1; i <= resolution; ++i) - for (size_t j = 1; j <= resolution; ++j) - grid[i-1][j-1] = m2::PointF(stepX*i, stepY*j); + vector shapes; + try + { + my::Json json(jsonString.c_str()); + MapShapeFactory factory; + factory.CreateShapes(shapes, json.get()); + } + catch (RootException & e) + { + LOG(LCRITICAL, (e.Msg())); + } - // grid: - // [31,31] ... [31,31] - // ... ... - // [0,0] ... [0,31] + for (size_t i = 0; i < shapes.size(); ++i) + shapes[i]->Draw(m_batcher.GetRefPointer(), m_textures.GetRefPointer()); - //1 - vector linePoints1; - - linePoints1.push_back(grid[1][1]); - linePoints1.push_back(grid[4][6]); - linePoints1.push_back(grid[8][1]); - linePoints1.push_back(grid[16][10]); - linePoints1.push_back(grid[24][1]); - linePoints1.push_back(grid[29][4]); - - LineViewParams params1; - params1.m_depth = 0.0f; - params1.m_cap = RoundCap; - params1.m_join = RoundJoin; - params1.m_color = Color(255, 255, 50, 255); - params1.m_width = 80.f; - df::LineShape * line1 = new df::LineShape(linePoints1, params1); - line1->Draw(m_batcher.GetRefPointer(), MakeStackRefPointer(NULL)); - delete line1; - // - - //2 - vector linePoints2; - - linePoints2.push_back(grid[2][28]); - linePoints2.push_back(grid[6][28]); - linePoints2.push_back(grid[6][22]); - linePoints2.push_back(grid[2][22]); - linePoints2.push_back(grid[2][16]); - linePoints2.push_back(grid[6][16]); - - LineViewParams params2; - params1.m_depth = 0.0f; - params2.m_cap = SquareCap; - params2.m_join = RoundJoin; - params2.m_color = Color(0, 255, 255, 255); - params2.m_width = 50.f; - df::LineShape * line2 = new df::LineShape(linePoints2, params2); - line2->Draw(m_batcher.GetRefPointer(), MakeStackRefPointer(NULL)); - delete line2; - // - - //3 - vector linePoints3; - - linePoints3.push_back(grid[1][9]); - linePoints3.push_back(grid[4][10]); - linePoints3.push_back(grid[8][9]); - linePoints3.push_back(grid[16][12]); - linePoints3.push_back(grid[24][9]); - linePoints3.push_back(grid[29][12]); - - LineViewParams params3; - params1.m_depth = 0.0f; - params3.m_cap = ButtCap; - params3.m_join = MiterJoin; - params3.m_color = Color(255, 0, 255, 255); - params3.m_width = 60.f; - df::LineShape * line3 = new df::LineShape(linePoints3, params3); - line3->Draw(m_batcher.GetRefPointer(), MakeStackRefPointer(NULL)); - delete line3; - // + DeleteRange(shapes, DeleteFunctor()); } void TestingEngine::ModelViewInit()