Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 15, 2024
1 parent a8d1b36 commit 0833248
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 36 deletions.
3 changes: 2 additions & 1 deletion examples/libs/geo/MapView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ MapView::MapView(std::unique_ptr<MapProvider> provider, std::unique_ptr<LODContr

geometry_ = root->baseGeometry();
material()->transparent = true;
material()->opacity = 0.0;
material()->depthWrite = false;
material()->colorWrite = false;
material()->opacity = 0;

scale.copy(root->baseScale());

add(*root);
root->initialize();

preSubDivide();
}
Expand Down
8 changes: 4 additions & 4 deletions examples/libs/geo/lod/LODRadial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace threepp {
class LODRadial: public LODControl {

public:
float subdivideDistance;
float simplifyDistance;

explicit LODRadial(float subdivideDistance = 50, float simplifyDistance = 300)
: subdivideDistance(subdivideDistance), simplifyDistance(simplifyDistance) {}

Expand All @@ -28,7 +31,7 @@ namespace threepp {
node.getWorldPosition(position);

auto distance = pov.distanceTo(position);
distance /= std::pow(2, view.getProvider()->maxZoom - node.getLevel());
distance /= std::pow(2.f, view.getProvider()->maxZoom - node.getLevel());
//
if (distance < this->subdivideDistance) {
node.subdivide();
Expand All @@ -39,9 +42,6 @@ namespace threepp {
}

protected:
float subdivideDistance;
float simplifyDistance;

Vector3 pov;
Vector3 position;
};
Expand Down
3 changes: 1 addition & 2 deletions examples/libs/geo/lod/LODRaycast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using namespace threepp;



void LODRaycast::updateLOD(MapView& view, Camera& camera, const GLRenderer& renderer, const Object3D& scene) {
std::vector<Intersection> intersects;

Expand All @@ -25,7 +24,7 @@ void LODRaycast::updateLOD(MapView& view, Camera& camera, const GLRenderer& rend
auto distance = intersect.distance;

if (this->powerDistance) {
distance = std::pow(distance * 2, node->getLevel());
distance = std::pow(distance * 2.f, node->getLevel());
}

if (this->scaleDistance) {
Expand Down
11 changes: 4 additions & 7 deletions examples/libs/geo/nodes/MapNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "MapNode.hpp"
#include "../MapView.hpp"

#include "threepp/materials/interfaces.hpp"

#include <iostream>

using namespace threepp;
Expand All @@ -23,6 +21,8 @@ void MapNode::subdivide() {

this->createChildNodes();

layers.disable(0);

this->subdivided = true;
}

Expand Down Expand Up @@ -55,7 +55,7 @@ void MapNode::loadData() {
}

const auto texture = Texture::create(image);
texture->generateMipmaps = false;
texture->generateMipmaps = true;
texture->format = Format::RGBA;
texture->magFilter = Filter::Linear;
texture->minFilter = Filter::Linear;
Expand All @@ -68,7 +68,6 @@ void MapNode::loadData() {
void MapNode::nodeReady() {
if (this->disposed) {
std::cerr << "Geo-Three: nodeReady() called for disposed node" << std::endl;
// this->dispose();
disposed = true;
return;
}
Expand All @@ -79,13 +78,11 @@ void MapNode::nodeReady() {
if (this->parentNode->nodesLoaded == MapNode::childrens) {
if (this->parentNode->subdivided == true) {

this->parentNode->layers.disable(0);
this->layers.disable(0);
}

for (unsigned i = 0; i < this->parentNode->children.size(); i++) {
this->parentNode->children[i]->visible = true;
// this->parentNode->children[i]->layers.enable(0);
// this->parentNode->layers.disable(0);
}
}

Expand Down
13 changes: 9 additions & 4 deletions examples/libs/geo/nodes/MapPlaneNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#ifndef THREEPP_MAPPLANENODE_HPP
#define THREEPP_MAPPLANENODE_HPP

#include "../utils/UnitUtils.hpp"
#include "MapNode.hpp"
#include "geo/geometries/MapNodeGeometry.hpp"
#include "geo/utils/UnitUtils.hpp"
#include "threepp/materials/MeshBasicMaterial.hpp"

#include <iostream>


namespace threepp {

namespace {
Expand All @@ -20,11 +23,9 @@ namespace threepp {
MapPlaneNode(MapNode* parent, MapView* mapView, int location = QuadTreePosition::root, int level = 0, float x = 0, float y = 0)
: MapNode(parent, mapView, location, level, x, y, baseGeom, MeshBasicMaterial::create({{"wireframe", false}})) {

initialize();

this->matrixAutoUpdate = false;
this->layers.enable(0);
// this->visible = false;
this->visible = false;
}

void initialize() override {
Expand Down Expand Up @@ -55,27 +56,31 @@ namespace threepp {
this->add(node);
node->updateMatrix();
node->updateMatrixWorld(true);
node->initialize();

node = std::make_shared<MapPlaneNode>(this, this->mapView, QuadTreePosition::topRight, level, x + 1, y);
node->scale.set(0.5, 1.0, 0.5);
node->position.set(0.25, 0, -0.25);
this->add(node);
node->updateMatrix();
node->updateMatrixWorld(true);
node->initialize();

node = std::make_shared<MapPlaneNode>(this, this->mapView, QuadTreePosition::bottomLeft, level, x, y + 1);
node->scale.set(0.5, 1.0, 0.5);
node->position.set(-0.25, 0, 0.25);
this->add(node);
node->updateMatrix();
node->updateMatrixWorld(true);
node->initialize();

node = std::make_shared<MapPlaneNode>(this, this->mapView, QuadTreePosition::bottomRight, level, x + 1, y + 1);
node->scale.set(0.5, 1.0, 0.5);
node->position.set(0.25, 0, 0.25);
this->add(node);
node->updateMatrix();
node->updateMatrixWorld(true);
node->initialize();
}
};

Expand Down
17 changes: 13 additions & 4 deletions examples/libs/geo/providers/OpenStreetMapsProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

#include "threepp/loaders/ImageLoader.hpp"

#include <iostream>
#include <mutex>
#include <sstream>
#include <utility>
#include <iostream>

namespace threepp {

Expand All @@ -27,11 +28,17 @@ namespace threepp {

std::stringstream ss;
ss << address << zoom << '/' << x << '/' << y << '.' << format;
const auto url = ss.str();

std::vector<unsigned char> data;
urlFetcher.fetch(ss.str(), data);

std::cout << ss.str() << std::endl;
if (cache_.count(url)) {
data = cache_.at(url);

} else if (urlFetcher.fetch(url, data)) {

cache_[url] = data;
}

return *loader.load(data, format == "png" ? 4 : 3, true);
}
Expand All @@ -40,8 +47,10 @@ namespace threepp {
std::string address;
std::string format = "png";

utils::UrlFetcher urlFetcher;
ImageLoader loader;
utils::UrlFetcher urlFetcher;

std::unordered_map<std::string, std::vector<unsigned char>> cache_{};
};

}// namespace threepp
Expand Down
17 changes: 6 additions & 11 deletions examples/libs/utility/URLFetcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
#if __has_include(<curl/curl.h>)
#define THREEPP_WITH_CURL
#include <curl/curl.h>
#else
#include <iostream>
#endif

#include <string>
#include <vector>
#include <mutex>

namespace threepp::utils {

#ifdef THREEPP_WITH_CURL

inline static size_t write_data(char* ptr, size_t size, size_t nmemb, void* userdata) {
static std::mutex m;
std::lock_guard<std::mutex> lck(m);
auto stream = (std::vector<unsigned char>*) userdata;
size_t count = size * nmemb;
stream->insert(stream->end(), ptr, ptr + count);
Expand All @@ -26,7 +31,7 @@ namespace threepp::utils {

#ifndef THREEPP_WITH_CURL
bool fetch(const std::string&, std::vector<unsigned char>&) {

std::cerr << "[URLFetcher] Curl not found. No fetch was made." << std::endl;
return false;
}
#else
Expand All @@ -37,19 +42,10 @@ namespace threepp::utils {

bool fetch(const std::string& url, std::vector<unsigned char>& data) {

if (cache_.count(url)) {
data = cache_[url];
return true;
}

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
CURLcode res = curl_easy_perform(curl);

if (res == CURLE_OK) {
cache_[url] = data;
}

return res == CURLE_OK;
}

Expand All @@ -59,7 +55,6 @@ namespace threepp::utils {

private:
CURL* curl;
std::unordered_map<std::string, std::vector<unsigned char>> cache_{};
#endif
};

Expand Down
10 changes: 7 additions & 3 deletions examples/projects/MapView/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ int main() {

OrbitControls controls{camera, canvas};

MapView map(std::make_unique<OpenStreetMapProvider>(), std::make_unique<LODRadial>());
auto lodFunc = std::make_unique<LODRadial>();
lodFunc->subdivideDistance = 100;
auto provider = std::make_unique<OpenStreetMapProvider>();

MapView map(std::move(provider), std::move(lodFunc));
scene.add(map);
map.updateMatrixWorld(true);

const auto coords = utils::datumsToSpherical(62.50094228364612, 6.09138498277564);
const auto coords = utils::datumsToSpherical(62.467400106161094, 6.156371664207545);
controls.target.set(coords.x, 0, -coords.y);
camera.position.set(coords.x, 100000, -coords.y);
camera.position.set(coords.x, 10000, -coords.y);
controls.update();

canvas.onWindowResize([&](WindowSize size) {
Expand Down

0 comments on commit 0833248

Please sign in to comment.