|
| 1 | +#include "paparazzi.h" |
| 2 | + |
| 3 | +#define AA_SCALE 1.0 |
| 4 | +#define IMAGE_DEPTH 4 |
| 5 | + |
| 6 | +#if PLATFORM_LINUX |
| 7 | +#include "platform_linux.h" |
| 8 | +#elif PLATFORM_OSX |
| 9 | +#include "platform_osx.h" |
| 10 | +#endif |
| 11 | + |
| 12 | +#include "log.h" |
| 13 | +#include "gl/texture.h" |
| 14 | + |
| 15 | +#include "hash-library/md5.h" |
| 16 | + |
| 17 | +#include <functional> |
| 18 | +#include <csignal> |
| 19 | +#include <fstream> |
| 20 | +#include <sstream> |
| 21 | +#include <sys/time.h> |
| 22 | +#include <unistd.h> |
| 23 | +#include "glm/trigonometric.hpp" |
| 24 | + |
| 25 | +#include "stb_image_write.h" |
| 26 | + |
| 27 | + |
| 28 | +using namespace Tangram; |
| 29 | + |
| 30 | +unsigned long long timeStart; |
| 31 | + |
| 32 | +double getTime() { |
| 33 | + static struct timeval tv; |
| 34 | + |
| 35 | + gettimeofday(&tv, NULL); |
| 36 | + return ((unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000) * 0.001; |
| 37 | +} |
| 38 | + |
| 39 | +Paparazzi::Paparazzi() |
| 40 | + : m_scene(""), |
| 41 | + m_lat(0.0), |
| 42 | + m_lon(0.0), |
| 43 | + m_zoom(0.0f), |
| 44 | + m_rotation(0.0f), |
| 45 | + m_tilt(0.0), |
| 46 | + m_width(0), |
| 47 | + m_height(0) { |
| 48 | + |
| 49 | + m_glContext = std::make_unique<HeadlessContext>(); |
| 50 | + if (!m_glContext->init()) { |
| 51 | + throw std::runtime_error("Could not initialize GL context"); |
| 52 | + } |
| 53 | + m_glContext->resize(1, 1); |
| 54 | + if (!m_glContext->makeCurrent()) { |
| 55 | + throw std::runtime_error("Could not activate GL context"); |
| 56 | + } |
| 57 | + |
| 58 | +#if PLATFORM_LINUX |
| 59 | + UrlClient::Options urlClientOptions; |
| 60 | + urlClientOptions.numberOfThreads = 10; |
| 61 | + auto platform = std::make_shared<LinuxPlatform>(urlClientOptions); |
| 62 | +#elif PLATFORM_OSX |
| 63 | + auto platform = std::make_shared<OSXPlatform>(); |
| 64 | +#endif |
| 65 | + |
| 66 | + timeStart = getTime(); |
| 67 | + |
| 68 | + m_map = std::unique_ptr<Tangram::Map>(new Tangram::Map(platform)); |
| 69 | + |
| 70 | + setScene("scene.yaml"); |
| 71 | + |
| 72 | + m_map->setupGL(); |
| 73 | + m_map->setPixelScale(AA_SCALE); |
| 74 | + |
| 75 | + //m_map->resize(m_width*AA_SCALE, m_height*AA_SCALE); |
| 76 | + //m_glContext->setScale(AA_SCALE); |
| 77 | + //setSize(m_width, m_height, 1.0); |
| 78 | + |
| 79 | + Tangram::setDebugFlag(DebugFlags::tile_bounds, true); |
| 80 | + |
| 81 | + logMsg("Done Init!\n"); |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +Paparazzi::~Paparazzi() { |
| 86 | +} |
| 87 | + |
| 88 | +void Paparazzi::setSize (const int &_width, const int &_height, const float &_density) { |
| 89 | + if (_density*_width == m_width && _density*_height == m_height && |
| 90 | + _density*AA_SCALE == m_map->getPixelScale()) { return; } |
| 91 | + |
| 92 | + m_width = _width*_density; |
| 93 | + m_height = _height*_density; |
| 94 | + |
| 95 | + // Setup the size of the image |
| 96 | + if (_density*AA_SCALE != m_map->getPixelScale()) { |
| 97 | + m_map->setPixelScale(_density*AA_SCALE); |
| 98 | + } |
| 99 | + m_map->resize(m_width*AA_SCALE, m_height*AA_SCALE); |
| 100 | + |
| 101 | + m_glContext->resize(m_width, m_height); |
| 102 | +} |
| 103 | + |
| 104 | +void Paparazzi::setZoom(const float &_zoom) { |
| 105 | + if (_zoom == m_zoom) { return; } |
| 106 | + m_zoom = _zoom; |
| 107 | + m_map->setZoom(_zoom); |
| 108 | +} |
| 109 | + |
| 110 | +void Paparazzi::setTilt(const float &_deg) { |
| 111 | + if (_deg == m_tilt) { return; } |
| 112 | + m_tilt = _deg; |
| 113 | + m_map->setTilt(glm::radians(m_tilt)); |
| 114 | +} |
| 115 | +void Paparazzi::setRotation(const float &_deg) { |
| 116 | + if (_deg == m_rotation) { return; } |
| 117 | + |
| 118 | + m_rotation = _deg; |
| 119 | + m_map->setRotation(glm::radians(m_rotation)); |
| 120 | +} |
| 121 | + |
| 122 | +void Paparazzi::setPosition(const double &_lon, const double &_lat) { |
| 123 | + if (_lon == m_lon && _lat == m_lat) { return; } |
| 124 | + |
| 125 | + m_lon = _lon; |
| 126 | + m_lat = _lat; |
| 127 | + m_map->setPosition(m_lon, m_lat); |
| 128 | +} |
| 129 | + |
| 130 | +void Paparazzi::setScene(const std::string &_url) { |
| 131 | + if (_url == m_scene) { return; } |
| 132 | + m_scene = _url; |
| 133 | + m_map->loadScene(m_scene.c_str(), false, |
| 134 | + {SceneUpdate("global.sdk_mapzen_api_key", m_apiKey)}); |
| 135 | +} |
| 136 | + |
| 137 | +void Paparazzi::setSceneContent(const std::string &_yaml_content) { |
| 138 | + MD5 md5; |
| 139 | + std::string md5_scene = md5(_yaml_content); |
| 140 | + |
| 141 | + if (md5_scene == m_scene) { return; } |
| 142 | + m_scene = md5_scene; |
| 143 | + |
| 144 | + // TODO: |
| 145 | + // - This is waiting for LoadSceneConfig to be implemented in Tangram::Map |
| 146 | + // Once that's done there is no need to save the file. |
| 147 | + std::string name = "cache/"+md5_scene+".yaml"; |
| 148 | + std::ofstream out(name.c_str()); |
| 149 | + out << _yaml_content.c_str(); |
| 150 | + out.close(); |
| 151 | + |
| 152 | + m_map->loadScene(name.c_str(), false, {SceneUpdate("global.sdk_mapzen_api_key", m_apiKey)}); |
| 153 | +} |
| 154 | + |
| 155 | +bool Paparazzi::update(int32_t _maxWaitTime) { |
| 156 | + double startTime = getTime(); |
| 157 | + float delta = 0.0; |
| 158 | + |
| 159 | + while (_maxWaitTime < 0 || delta < _maxWaitTime) { |
| 160 | + |
| 161 | + bool bFinish = m_map->update(10.); |
| 162 | + delta = float(getTime() - startTime); |
| 163 | + |
| 164 | + if (bFinish) { |
| 165 | + logMsg("Update: Finish!\n"); |
| 166 | + return true; |
| 167 | + } |
| 168 | + usleep(10000); |
| 169 | + } |
| 170 | + logMsg("Update: Done waiting...\n"); |
| 171 | + return false; |
| 172 | +} |
| 173 | + |
| 174 | +void Paparazzi::render(std::string& _image) { |
| 175 | + m_glContext->makeCurrent(); |
| 176 | + |
| 177 | + m_map->render(); |
| 178 | + |
| 179 | + GL::finish(); |
| 180 | + |
| 181 | + stbi_write_png_to_func([](void *context, void *data, int size) { |
| 182 | + static_cast<std::string*>(context)->append(static_cast<const char*>(data), size); |
| 183 | + }, |
| 184 | + &_image, |
| 185 | + m_glContext->width(), |
| 186 | + m_glContext->height(), |
| 187 | + IMAGE_DEPTH, |
| 188 | + m_glContext->buffer(), |
| 189 | + m_glContext->width() * IMAGE_DEPTH); |
| 190 | +} |
0 commit comments