diff --git a/.dockerignore b/.dockerignore new file mode 120000 index 0000000..3e4e48b --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.gitignore \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 01c6532..3751add 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,11 @@ os: dist: trusty addons: + sources: + - llvm-toolchain-trusty-7 # clang-7 apt: packages: + - clang-7 - libglew-dev - libglm-dev - xorg-dev diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d45069..446a2fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,14 +77,6 @@ set(SOURCES src/Algo2DEntropy.cc src/Algo2DFourColors.cc src/Algo2DGrayscale.cc - src/Algo3DCubeFull.cc - src/Algo3DCubeContiBnW.cc - src/Algo3DCubeContiRainbow.cc - src/Algo3DCubeContiFrebet.cc - src/Algo3DSphereFull.cc - src/Algo3DSphereContiBnW.cc - src/Algo3DSphereContiRainbow.cc - src/Algo3DSphereContiFrebet.cc src/GlfwManager.cc diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e24ef63 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM debian +WORKDIR /app +COPY . $PWD +RUN set -x \ + && apt update \ + && apt install -y \ + x11-apps \ + libglew-dev libglm-dev libglfw3-dev pkg-config \ + libc++-dev clang cmake +RUN set -x \ + && ./configure \ + && cd build \ + && make -j7 +ENV DISPLAY :0 +ENTRYPOINT ["/app/build/voidstar"] diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..eb02c16 --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,10 @@ +FROM debian +RUN set -x \ + && apt update \ + && apt install -y \ + x11-apps \ + libglew-dev libglm-dev libglfw3-dev pkg-config \ + libc++-dev clang cmake +ENV DISPLAY :0 +WORKDIR /app +ENTRYPOINT ["/bin/sh", "-c", "set -x && ./configure && cd build && make -j7 && ./voidstar ./voidstar ../data/*"] diff --git a/README.md b/README.md index ec4f1b0..90580a8 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,16 @@ The actual file being represented below [is data/BigPictureBG.tga](http://www.do ## Installing +### docker + +Known to work with +* ubuntu 18.10 + docker 18.09.0 + +``` +docker build -t void . +./contained.sh +``` + ### on MacOS 1. Download one of the `osx` files of [the latest release](https://github.com/fenollp/voidstar/releases/latest) diff --git a/docker-dev.sh b/docker-dev.sh new file mode 100755 index 0000000..ec7d014 --- /dev/null +++ b/docker-dev.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +#$ docker build -f Dockerfile.dev -t voiddev . + +# https://stackoverflow.com/a/25168483/1418165 + +XSOCK=/tmp/.X11-unix +XAUTH=/tmp/.docker.xauth +xauth nlist :0 | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - + +docker run --rm -it \ + -v $XSOCK:$XSOCK \ + -v $XAUTH:$XAUTH \ + -v /dev/input \ + -v "$PWD":/app \ + -e XAUTHORITY=$XAUTH \ + voiddev diff --git a/docker.sh b/docker.sh new file mode 100755 index 0000000..f12fdfd --- /dev/null +++ b/docker.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# https://stackoverflow.com/a/25168483/1418165 + +XSOCK=/tmp/.X11-unix +XAUTH=/tmp/.docker.xauth +xauth nlist :0 | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - + +docker run --rm -it \ + -v $XSOCK:$XSOCK \ + -v $XAUTH:$XAUTH \ + -v /dev/input \ + -e XAUTHORITY=$XAUTH \ + void \ + /app/build/voidstar diff --git a/src/Algo3DCubeContiBnW.cc b/src/Algo3DCubeContiBnW.cc deleted file mode 100644 index f5b6eca..0000000 --- a/src/Algo3DCubeContiBnW.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include - -bool -Algo3DCubeContiBnW::apply(Floats& vertices, Floats& colors, VertIndices& indices, - size_t width, size_t height, size_t depth) { - make_vertices(vertices, width, height, depth); - - size_t size; - const u8* data = loadDataRange(size); - u8 x = data[0]; - u8 y = data[1]; - - for (size_t i = 2; i < size; ++i) { - u8 z = data[i]; - Index id = (y * width + x) * depth + z; - Index idx = 3 * id; - colors[idx + 0] = 1.0f; - colors[idx + 1] = 1.0f; - colors[idx + 2] = 1.0f; - indices.push_back(id); - x = y; - y = z; - } - - return true; -} diff --git a/src/Algo3DCubeContiFrebet.cc b/src/Algo3DCubeContiFrebet.cc deleted file mode 100644 index 8fcd6ef..0000000 --- a/src/Algo3DCubeContiFrebet.cc +++ /dev/null @@ -1,28 +0,0 @@ -#include - -bool -Algo3DCubeContiFrebet::apply(Floats& vertices, Floats& colors, VertIndices& indices, - size_t width, size_t height, size_t depth) { - make_vertices(vertices, width, height, depth); - - size_t size; - const u8* data = loadDataRange(size); - u8 x = data[0]; - u8 y = data[1]; - - for (size_t i = 2; i+3 < size; ++i) { - u8 z = data[i]; - Index id = (y * width + x) * depth + z; - Index idx = 3 * id; - - colors[idx+0] = static_cast(data[i+1]) / 255.0f; - colors[idx+1] = static_cast(data[i+2]) / 255.0f; - colors[idx+2] = static_cast(data[i+3]) / 255.0f; - indices.push_back(id); - - x = y; - y = z; - } - - return true; -} diff --git a/src/Algo3DCubeContiRainbow.cc b/src/Algo3DCubeContiRainbow.cc deleted file mode 100644 index 53d0ad8..0000000 --- a/src/Algo3DCubeContiRainbow.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include - -bool -Algo3DCubeContiRainbow::apply(Floats& vertices, Floats& colors, VertIndices& indices, - size_t width, size_t height, size_t depth) { - make_vertices(vertices, width, height, depth); - - size_t size; - const u8* data = loadDataRange(size); - u8 x = data[0]; - u8 y = data[1]; - - for (size_t i = 2; i < size; ++i) { - u8 z = data[i]; - Index id = (y * width + x) * depth + z; - Index idx = 3 * id; - colors[idx + 0] = static_cast(x) / 255.0f; - colors[idx + 1] = static_cast(y) / 255.0f; - colors[idx + 2] = static_cast(z) / 255.0f; - indices.push_back(id); - x = y; - y = z; - } - - return true; -} diff --git a/src/Algo3DCubeFull.cc b/src/Algo3DCubeFull.cc deleted file mode 100644 index 0a47f83..0000000 --- a/src/Algo3DCubeFull.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include - -bool -Algo3DCubeFull::apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth) { - auto nb_verts = make_vertices(vertices, width, height, depth); - - for (size_t i = 0; i < nb_verts; ++i) { - colors[3 * i + 0] = 1.0f; - colors[3 * i + 1] = 1.0f; - colors[3 * i + 2] = 1.0f; - selected.push_back(i); - } - - return true; -} diff --git a/src/Algo3DSphereContiBnW.cc b/src/Algo3DSphereContiBnW.cc deleted file mode 100644 index ffc3c95..0000000 --- a/src/Algo3DSphereContiBnW.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include - -bool -Algo3DSphereContiBnW::apply(Floats& vertices, Floats& colors, VertIndices& indices, - size_t width, size_t height, size_t depth) { - make_vertices(vertices, width, height, depth); - - size_t size; - const u8* data = loadDataRange(size); - u8 x = data[0]; - u8 y = data[1]; - - for (size_t i = 2; i < size; ++i) { - u8 z = data[i]; - Index id = x + y * height + z * depth * height; - Index idx = 3 * id; - colors[idx + 0] = 1.0f; - colors[idx + 1] = 1.0f; - colors[idx + 2] = 1.0f; - indices.push_back(id); - x = y; - y = z; - } - - return true; -} diff --git a/src/Algo3DSphereContiFrebet.cc b/src/Algo3DSphereContiFrebet.cc deleted file mode 100644 index e568d36..0000000 --- a/src/Algo3DSphereContiFrebet.cc +++ /dev/null @@ -1,32 +0,0 @@ -#include - -bool -Algo3DSphereContiFrebet::apply(Floats& vertices, Floats& colors, VertIndices& indices, - size_t width, size_t height, size_t depth) { - make_vertices(vertices, width, height, depth); - - size_t size; - const u8* data = loadDataRange(size); - u8 x = data[0]; - u8 y = data[1]; - - for (size_t i = 2; i+3 < size; ++i) { - u8 z = data[i]; - Index id = x + y * height + z * depth * height; - // float r = std::sqrt(std::pow(static_cast(x), 2.0f) + - // std::pow(static_cast(y), 2.0f) + - // std::pow(static_cast(z), 2.0f)); - // float theta = std::acos(static_cast(z) / r); - // float phi = std::atan(static_cast(y) / static_cast(x)); - // size_t id = r + theta * height + phi * depth * height; - Index idx = 3 * id; - colors[idx + 0] = static_cast(data[i+1]) / 255.0f; - colors[idx + 1] = static_cast(data[i+2]) / 255.0f; - colors[idx + 2] = static_cast(data[i+3]) / 255.0f; - indices.push_back(id); - x = y; - y = z; - } - - return true; -} diff --git a/src/Algo3DSphereContiRainbow.cc b/src/Algo3DSphereContiRainbow.cc deleted file mode 100644 index dc64192..0000000 --- a/src/Algo3DSphereContiRainbow.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include - -bool -Algo3DSphereContiRainbow::apply(Floats& vertices, Floats& colors, VertIndices& indices, - size_t width, size_t height, size_t depth) { - make_vertices(vertices, width, height, depth); - - size_t size; - const u8* data = loadDataRange(size); - u8 x = data[0]; - u8 y = data[1]; - - for (size_t i = 2; i < size; ++i) { - u8 z = data[i]; - Index id = x + y * height + z * depth * height; - Index idx = 3 * id; - colors[idx + 0] = static_cast(x) / 255.0f; - colors[idx + 1] = static_cast(y) / 255.0f; - colors[idx + 2] = static_cast(z) / 255.0f; - indices.push_back(id); - x = y; - y = z; - } - - return true; -} diff --git a/src/Algo3DSphereFull.cc b/src/Algo3DSphereFull.cc deleted file mode 100644 index 50dd09f..0000000 --- a/src/Algo3DSphereFull.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include - -bool -Algo3DSphereFull::apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth) { - auto nb_verts = make_vertices(vertices, width, height, depth); - - for (size_t i = 0; i < nb_verts; ++i) { - colors[3 * i + 0] = 1.0f; - colors[3 * i + 1] = 1.0f; - colors[3 * i + 2] = 1.0f; - selected.push_back(i); - } - - return true; -} diff --git a/src/Algorithm.cc b/src/Algorithm.cc index 4419c57..b8aa836 100644 --- a/src/Algorithm.cc +++ b/src/Algorithm.cc @@ -6,14 +6,9 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include <ΡΦZ.hh> void Algorithm::use(std::shared_ptr loader, std::shared_ptr range) { @@ -57,12 +52,7 @@ const std::map algorithms = { {"entropy", []() { return std::make_shared(); }}, {"4col", []() { return std::make_shared(); }}, {"gray", []() { return std::make_shared(); }}, - {"cube", []() { return std::make_shared(); }}, - {"contibnw", []() { return std::make_shared(); }}, - {"contirb", []() { return std::make_shared(); }}, - {"conti", []() { return std::make_shared(); }}, - {"sphere", []() { return std::make_shared(); }}, - {"sphere_bnw", []() { return std::make_shared(); }}, - {"sphere_rb", []() { return std::make_shared(); }}, - {"sphere_frebet", []() { return std::make_shared(); }}, + {"cube", []() { return std::make_shared(); }}, + {"sphere", []() { return std::make_shared(); }}, + {"cylinder", []() { return std::make_shared<ΡΦZ>(); }}, }; diff --git a/src/Manager.cc b/src/Manager.cc index f8cd85b..62e6af0 100644 --- a/src/Manager.cc +++ b/src/Manager.cc @@ -3,16 +3,6 @@ #include #include -void -Manager::loadScene(std::shared_ptr scene) { - // XXX delete scene prior to creating a new one - if (scene_) { - std::cout << "deleting scene" << std::endl; - } - scene_ = scene; - scene->init(args_); -} - void Manager::loadFile(const std::string& filename) { args_->paths.push_back(filename); @@ -37,13 +27,29 @@ Manager::loadFile(size_t index) { auto range = DataRange::create(args_->range_begin, args_->range_end); if (scene_) { - auto algo = scene_->algorithm(); - algo->use(loader, range); + scene_->algorithm()->use(loader, range); scene_->reload(); - } else { - auto algo = createAlgorithm(args_->algo); + return true; + } + + auto uglyhack_algo = createAlgorithm(args_->algo); + if (auto algo = std::dynamic_pointer_cast(uglyhack_algo)) { + algo->use(loader, range); + if (scene_) { + std::cout << "deleting 3D scene" << std::endl; + } + scene_ = Scene::with_algo(args_, algo); + scene_->init(args_); + return true; + } + + if (auto algo = std::dynamic_pointer_cast(uglyhack_algo)) { algo->use(loader, range); - loadScene(Scene::with_algo(args_, algo)); + if (scene_) { + std::cout << "deleting 2D scene" << std::endl; + } + scene_ = Scene::with_algo(args_, algo); + scene_->init(args_); } return true; } diff --git a/src/Scene.cc b/src/Scene.cc index 2c1dba8..03ede87 100644 --- a/src/Scene.cc +++ b/src/Scene.cc @@ -2,37 +2,17 @@ #include #include -void -Scene::load(std::shared_ptr algo) { - if (type_ != algo->sceneType()) - throw std::runtime_error("Invalid algo for scene"); - if (algo_ && algo_ != algo) - std::cerr << "deleting algo" << std::endl; - algo_ = algo; -} - -void -Scene::unload() { -} - -void -Scene::reload() { +std::shared_ptr +Scene::with_algo(const std::shared_ptr args, const std::shared_ptr algo) { + auto scene = std::make_shared(); + scene->init(args); + scene->load(algo); + return scene; } -#define SCENE(Body) []() -> std::shared_ptr { Body } -using SceneFactoryFunc = std::function()>; -std::map scenes = { - {SCENE_2D, SCENE( return std::make_shared(); )}, - {SCENE_3D, SCENE( return std::make_shared(); )}, -}; - std::shared_ptr -Scene::with_algo(std::shared_ptr args, std::shared_ptr algo) { - auto it = scenes.find(algo->sceneType()); - if (it == scenes.end()) { - throw std::runtime_error("Unknown scene type for algorithm"); - } - auto scene = it->second(); +Scene::with_algo(const std::shared_ptr args, const std::shared_ptr algo) { + auto scene = std::make_shared(); scene->init(args); scene->load(algo); return scene; diff --git a/src/Scene2D.cc b/src/Scene2D.cc index 0833b95..84d001e 100644 --- a/src/Scene2D.cc +++ b/src/Scene2D.cc @@ -54,10 +54,9 @@ Scene2D::unload() { void Scene2D::reload() { - auto algo = std::static_pointer_cast(algo_); reset_points(); - algo->apply(vertices_, colors_, width_, height_) - || std::cerr << "!apply" << std::endl; + apply(); + glBindVertexArray(vao_); glBindBuffer(GL_ARRAY_BUFFER, colors_id_); glBufferData(GL_ARRAY_BUFFER, Algorithm::vsize(colors_), colors_.data(), GL_STATIC_DRAW); @@ -65,18 +64,15 @@ Scene2D::reload() { } void -Scene2D::load(std::shared_ptr algorithm) { - Scene::load(algorithm); - auto algo = std::static_pointer_cast(algorithm); +Scene2D::load(std::shared_ptr algo) { + Scene::load(algo); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); load_shaders(); - algo->apply(vertices_, colors_, width_, height_) - || std::cerr << "!apply" << std::endl; - load_buffers(); + apply(); } bool diff --git a/src/Scene3D.cc b/src/Scene3D.cc index 2d90cdf..cdea8db 100644 --- a/src/Scene3D.cc +++ b/src/Scene3D.cc @@ -60,12 +60,9 @@ Scene3D::unload() { void Scene3D::reload() { - auto algo = std::static_pointer_cast(algo_); reset_points(); - algo->apply(vertices_, colors_, indices_, width_, height_, depth_) - || std::cerr << "!apply" << std::endl; - std::cout << "#indices: " << Manager::size2str(indices_.size()) << std::endl; - load_buffers(); + apply(); + glBindVertexArray(vao_); glBindBuffer(GL_ARRAY_BUFFER, colors_id_); glBufferData(GL_ARRAY_BUFFER, Algorithm::vsize(colors_), colors_.data(), GL_STATIC_DRAW); @@ -73,9 +70,8 @@ Scene3D::reload() { } void -Scene3D::load(std::shared_ptr algorithm) { - Scene::load(algorithm); - auto algo = std::static_pointer_cast(algorithm); +Scene3D::load(std::shared_ptr algo) { + Scene::load(algo); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); @@ -83,10 +79,7 @@ Scene3D::load(std::shared_ptr algorithm) { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); load_shaders(); - algo->apply(vertices_, colors_, indices_, width_, height_, depth_) - || std::cerr << "!apply" << std::endl; - std::cout << "#indices: " << Manager::size2str(indices_.size()) << std::endl; - load_buffers(); + apply(); // camera_.setPosition(glm::vec3(0, -0.1, 3)); // camera_.lookAt(glm::vec3(0,0,4)); diff --git a/src/include/Algo2D.hh b/src/include/Algo2D.hh index a34b184..b3d84df 100644 --- a/src/include/Algo2D.hh +++ b/src/include/Algo2D.hh @@ -6,7 +6,6 @@ class Algo2D : public Algorithm { public: virtual ~Algo2D() {} - virtual SceneType sceneType() const { return SCENE_2D; } virtual bool apply(Floats& vertices, Floats& colors, size_t width, size_t height) = 0; diff --git a/src/include/Algo3D.hh b/src/include/Algo3D.hh index ccc32ea..f9ed77a 100644 --- a/src/include/Algo3D.hh +++ b/src/include/Algo3D.hh @@ -8,7 +8,10 @@ class Algo3D : public Algorithm { public: virtual ~Algo3D() {} - virtual SceneType sceneType() const { return SCENE_3D; } - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth) = 0; + virtual void make_vertices(Floats& vertices, + size_t width, size_t height, size_t depth) = 0; + + virtual size_t value_size() const = 0; + virtual Index cast(const u8* data, Floats& colors, + size_t width, size_t height, size_t depth) const = 0; }; diff --git a/src/include/Algo3DCube.hh b/src/include/Algo3DCube.hh index b40e9e3..94765b8 100644 --- a/src/include/Algo3DCube.hh +++ b/src/include/Algo3DCube.hh @@ -1,17 +1,18 @@ #pragma once +#include + #include class Algo3DCube : public Algo3D { public: virtual ~Algo3DCube() {} - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth) = 0; + virtual void make_vertices(Floats& vertices, + size_t width, size_t height, size_t depth) { + std::cerr << "drawing " << width << "x" << height << "x" << depth + << " cube" << std::endl; -protected: - size_t - make_vertices(Floats& vertices, size_t width, size_t height, size_t depth) { const float w = static_cast(width) / 2; const float h = static_cast(height) / 2; const float d = static_cast(depth) / 2; @@ -25,6 +26,7 @@ protected: vertices[pos++] = (static_cast(z) - d) / d; } - return vertices.size() / 3; + std::cerr << "drawn cube of " << Manager::size2str(vertices.size() / 3) + << " vertices" << std::endl; } }; diff --git a/src/include/Algo3DCubeContiBnW.hh b/src/include/Algo3DCubeContiBnW.hh deleted file mode 100644 index 37c2489..0000000 --- a/src/include/Algo3DCubeContiBnW.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DCubeContiBnW : public Algo3DCube { -public: - Algo3DCubeContiBnW() {} - virtual ~Algo3DCubeContiBnW() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algo3DCubeContiFrebet.hh b/src/include/Algo3DCubeContiFrebet.hh deleted file mode 100644 index b88daee..0000000 --- a/src/include/Algo3DCubeContiFrebet.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DCubeContiFrebet : public Algo3DCube { -public: - Algo3DCubeContiFrebet() {} - virtual ~Algo3DCubeContiFrebet() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algo3DCubeContiRainbow.hh b/src/include/Algo3DCubeContiRainbow.hh deleted file mode 100644 index e1ae5bf..0000000 --- a/src/include/Algo3DCubeContiRainbow.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DCubeContiRainbow : public Algo3DCube { -public: - Algo3DCubeContiRainbow() {} - virtual ~Algo3DCubeContiRainbow() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algo3DCubeFull.hh b/src/include/Algo3DCubeFull.hh deleted file mode 100644 index 939a6a5..0000000 --- a/src/include/Algo3DCubeFull.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DCubeFull : public Algo3DCube { -public: - Algo3DCubeFull() {} - virtual ~Algo3DCubeFull() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algo3DCylinder.hh b/src/include/Algo3DCylinder.hh new file mode 100644 index 0000000..bbb51f3 --- /dev/null +++ b/src/include/Algo3DCylinder.hh @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include + +#include + +class Algo3DCylinder : public Algo3D { +public: + virtual ~Algo3DCylinder() {} + + virtual void make_vertices(Floats& vertices, + size_t /*width*/, size_t /*height*/, size_t /*depth*/) { + // TODO + + std::cerr << "drawn cylinder of " << Manager::size2str(vertices.size() / 3) + << " vertices" << std::endl; + } +}; diff --git a/src/include/Algo3DSphere.hh b/src/include/Algo3DSphere.hh index a5ff131..03bd494 100644 --- a/src/include/Algo3DSphere.hh +++ b/src/include/Algo3DSphere.hh @@ -1,7 +1,8 @@ #pragma once -#include #include +#include +#include #include @@ -9,12 +10,8 @@ class Algo3DSphere : public Algo3D { public: virtual ~Algo3DSphere() {} - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth) = 0; - -protected: - size_t - make_vertices(Floats& vertices, size_t width, size_t height, size_t depth) { + virtual void make_vertices(Floats& vertices, + size_t width, size_t height, size_t depth) { const float w = static_cast(width) / 2; const float h = static_cast(height) / 2; const float d = static_cast(depth) / 2; @@ -35,6 +32,7 @@ protected: vertices[pos++] = r * std::cos(theta) / d; } - return vertices.size() / 3; + std::cerr << "drawn sphere of " << Manager::size2str(vertices.size() / 3) + << " vertices" << std::endl; } }; diff --git a/src/include/Algo3DSphereContiBnW.hh b/src/include/Algo3DSphereContiBnW.hh deleted file mode 100644 index 6f30db9..0000000 --- a/src/include/Algo3DSphereContiBnW.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DSphereContiBnW : public Algo3DSphere { -public: - Algo3DSphereContiBnW() {} - virtual ~Algo3DSphereContiBnW() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algo3DSphereContiFrebet.hh b/src/include/Algo3DSphereContiFrebet.hh deleted file mode 100644 index f67d4d1..0000000 --- a/src/include/Algo3DSphereContiFrebet.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DSphereContiFrebet : public Algo3DSphere { -public: - Algo3DSphereContiFrebet() {} - virtual ~Algo3DSphereContiFrebet() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algo3DSphereContiRainbow.hh b/src/include/Algo3DSphereContiRainbow.hh deleted file mode 100644 index 04fb53f..0000000 --- a/src/include/Algo3DSphereContiRainbow.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DSphereContiRainbow : public Algo3DSphere { -public: - Algo3DSphereContiRainbow() {} - virtual ~Algo3DSphereContiRainbow() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algo3DSphereFull.hh b/src/include/Algo3DSphereFull.hh deleted file mode 100644 index 225e5eb..0000000 --- a/src/include/Algo3DSphereFull.hh +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class Algo3DSphereFull : public Algo3DSphere { -public: - Algo3DSphereFull() {} - virtual ~Algo3DSphereFull() {} - - virtual bool apply(Floats& vertices, Floats& colors, VertIndices& selected, - size_t width, size_t height, size_t depth); -}; diff --git a/src/include/Algorithm.hh b/src/include/Algorithm.hh index 559a1b0..358d15b 100644 --- a/src/include/Algorithm.hh +++ b/src/include/Algorithm.hh @@ -9,13 +9,6 @@ #include #include -// XXX moved here as a placeholder due to circular dependency #badDesign -enum SceneType { - SCENE_UNDEFINED, - SCENE_2D, - SCENE_3D, -}; - class Algorithm { public: Algorithm(size_t min_size=0, size_t max_size=0) @@ -23,7 +16,6 @@ public: loader_(0), range_(0) {} virtual ~Algorithm() {} - virtual SceneType sceneType() const = 0; virtual void use(std::shared_ptr loader, std::shared_ptr range=NULL); const u8* loadDataRange(const DataRange& range, size_t& size); diff --git a/src/include/Arguments.hh b/src/include/Arguments.hh index 30c3d78..5e8698e 100644 --- a/src/include/Arguments.hh +++ b/src/include/Arguments.hh @@ -7,7 +7,7 @@ struct Arguments { Arguments() : name("void*"), - algo("conti"), + algo("cube"), manager("glfw"), width(800), height(600), range_begin(0), range_end(0), diff --git a/src/include/Manager.hh b/src/include/Manager.hh index 173a36c..d8590a0 100644 --- a/src/include/Manager.hh +++ b/src/include/Manager.hh @@ -18,7 +18,6 @@ public: {} virtual ~Manager() {} - virtual void loadScene(std::shared_ptr scene); virtual void init() = 0; virtual void run() = 0; diff --git "a/src/include/R\316\230\316\246.hh" "b/src/include/R\316\230\316\246.hh" new file mode 100644 index 0000000..481fba1 --- /dev/null +++ "b/src/include/R\316\230\316\246.hh" @@ -0,0 +1,34 @@ +#pragma once + +#include + +class RΘΦ : public Algo3DSphere { +public: + RΘΦ() {} + virtual ~RΘΦ() {} + + typedef struct { + u8 r, θ, φ, R, G, B; + } value_type; + + virtual size_t value_size() const { return sizeof (value_type); } + + virtual Index cast(const u8* data, Floats& colors, + size_t /*width*/, size_t height, size_t depth) const { + auto* cast = (value_type*)(data); + + auto r = static_cast(cast->r); + auto θ = static_cast(cast->θ); + auto φ = static_cast(cast->φ); + + // TODO: actually use spherical coordinates + Index id = (r * height + θ) * depth + φ; + Index idx = 3 * id; + + colors[idx+0] = static_cast(cast->R) / 255.0f; + colors[idx+1] = static_cast(cast->G) / 255.0f; + colors[idx+2] = static_cast(cast->B) / 255.0f; + + return id; + } +}; diff --git a/src/include/Scene.hh b/src/include/Scene.hh index ccd0b98..7e379b8 100644 --- a/src/include/Scene.hh +++ b/src/include/Scene.hh @@ -13,24 +13,43 @@ #include #include +#include +#include #include #include class Manager; +enum SceneType { + SCENE_UNDEFINED, + SCENE_2D, + SCENE_3D, +}; + class Scene { public: Scene(SceneType type) - : type_(type), algo_(nullptr) + : type_(type) {} virtual ~Scene() {} virtual void init(std::shared_ptr args) = 0; virtual bool update(std::shared_ptr manager, float elapsedTime) = 0; virtual void render() = 0; - virtual void load(std::shared_ptr algo); - virtual void unload(); - virtual void reload(); + + virtual void load(std::shared_ptr algo) { + if (algo2D_ && algo2D_ != algo) + std::cerr << "deleting 2D algo" << std::endl; + algo2D_ = algo; + } + virtual void load(std::shared_ptr algo) { + if (algo3D_ && algo3D_ != algo) + std::cerr << "deleting 3D algo" << std::endl; + algo3D_ = algo; + } + + virtual void unload() {} + virtual void reload() {} virtual void resize(int viewport_width, int viewport_height) { float aspect_ratio = static_cast(viewport_width) / static_cast(viewport_height); @@ -40,11 +59,18 @@ public: } inline SceneType type() const { return type_; } - std::shared_ptr algorithm() const { return algo_; } - static std::shared_ptr with_algo(std::shared_ptr args, std::shared_ptr algo); + std::shared_ptr algorithm() const { + if (algo3D_) + return static_cast>(algo3D_); + return static_cast>(algo2D_); + } + + static std::shared_ptr with_algo(const std::shared_ptr args, const std::shared_ptr algo); + static std::shared_ptr with_algo(const std::shared_ptr args, const std::shared_ptr algo); protected: SceneType type_; - std::shared_ptr algo_; + std::shared_ptr algo2D_; + std::shared_ptr algo3D_; tdogl::Camera camera_; }; diff --git a/src/include/Scene2D.hh b/src/include/Scene2D.hh index fe4f34b..418522f 100644 --- a/src/include/Scene2D.hh +++ b/src/include/Scene2D.hh @@ -17,7 +17,7 @@ public: virtual ~Scene2D() { unload(); } virtual void init(std::shared_ptr args); - virtual void load(std::shared_ptr algo); + virtual void load(std::shared_ptr algo); virtual void unload(); virtual void reload(); virtual bool update(std::shared_ptr manager, float elapsedTime); @@ -30,6 +30,11 @@ private: vertices_ = Floats(3 * n_points_); colors_ = Floats(3 * n_points_); } + void apply() { + algo2D_->apply(vertices_, colors_, width_, height_) + || std::cerr << "!apply" << std::endl; + load_buffers(); + } protected: GLuint vao_; diff --git a/src/include/Scene3D.hh b/src/include/Scene3D.hh index 995ded3..3301ad8 100644 --- a/src/include/Scene3D.hh +++ b/src/include/Scene3D.hh @@ -4,6 +4,8 @@ #include #include +#include +#include class Scene3D : public Scene { public: @@ -18,7 +20,7 @@ public: virtual ~Scene3D() { unload(); } virtual void init(std::shared_ptr args); - virtual void load(std::shared_ptr algo); + virtual void load(std::shared_ptr algo); virtual void unload(); virtual void reload(); virtual bool update(std::shared_ptr manager, float elapsedTime); @@ -33,6 +35,23 @@ private: indices_.clear(); } + void apply() { + algo3D_->make_vertices(vertices_, width_, height_, depth_); + + size_t size; + const u8* data = algo3D_->loadDataRange(size); + const auto value_size = algo3D_->value_size(); + + for (size_t i = 0; i+value_size < size; ++i) { + auto id = algo3D_->cast(data + i, colors_, width_, height_, depth_); + indices_.push_back(id); + } + + std::cout << "#indices: " << Manager::size2str(indices_.size()) << std::endl; + + load_buffers(); + } + protected: GLuint vao_; GLuint vbo_; diff --git a/src/include/XYZRGB.hh b/src/include/XYZRGB.hh new file mode 100644 index 0000000..9b0cad0 --- /dev/null +++ b/src/include/XYZRGB.hh @@ -0,0 +1,33 @@ +#pragma once + +#include + +class XYZRGB : public Algo3DCube { +public: + XYZRGB() {} + virtual ~XYZRGB() {} + + typedef struct { + u8 x, y, z, R, G, B; + } value_type; + + virtual size_t value_size() const { return sizeof (value_type); } + + virtual Index cast(const u8* data, Floats& colors, + size_t /*width*/, size_t height, size_t depth) const { + auto* cast = (value_type*)(data); + + auto x = static_cast(cast->x); + auto y = static_cast(cast->y); + auto z = static_cast(cast->z); + + Index id = (x * height + y) * depth + z; + Index idx = 3 * id; + + colors[idx+0] = static_cast(cast->R) / 255.0f; + colors[idx+1] = static_cast(cast->G) / 255.0f; + colors[idx+2] = static_cast(cast->B) / 255.0f; + + return id; + } +}; diff --git "a/src/include/\316\241\316\246Z.hh" "b/src/include/\316\241\316\246Z.hh" new file mode 100644 index 0000000..ef7f2f7 --- /dev/null +++ "b/src/include/\316\241\316\246Z.hh" @@ -0,0 +1,34 @@ +#pragma once + +#include + +class ΡΦZ : public Algo3DCylinder { +public: + ΡΦZ() {} + virtual ~ΡΦZ() {} + + typedef struct { + u8 ρ, φ, z, R, G, B; + } value_type; + + virtual size_t value_size() const { return sizeof (value_type); } + + virtual Index cast(const u8* data, Floats& colors, + size_t /*width*/, size_t height, size_t depth) const { + auto* cast = (value_type*)(data); + + auto ρ = static_cast(cast->ρ); + auto φ = static_cast(cast->φ); + auto z = static_cast(cast->z); + + // TODO: actually use cylindrical coordinates + Index id = (ρ * height + φ) * depth + z; + Index idx = 3 * id; + + colors[idx+0] = static_cast(cast->R) / 255.0f; + colors[idx+1] = static_cast(cast->G) / 255.0f; + colors[idx+2] = static_cast(cast->B) / 255.0f; + + return id; + } +};