From 4b4bea32bf11735f1bc85fe8ed7ba7b2a37d375d Mon Sep 17 00:00:00 2001 From: Sebastian Piwell Date: Wed, 23 Mar 2016 13:41:28 -0400 Subject: [PATCH] Create dataplane through dataSurfaceContainer moudle --- data/scene/datasurface/datasurface.mod | 2 +- data/scene/earth/earth.mod | 12 - modules/datasurface/CMakeLists.txt | 8 +- modules/datasurface/datasurfacemodule.cpp | 4 +- modules/datasurface/rendering/dataplane.cpp | 189 +++++++++++++++ .../{renderabledataplane.h => dataplane.h} | 36 ++- modules/datasurface/rendering/datasurface.cpp | 66 ++++++ modules/datasurface/rendering/datasurface.h | 58 +++++ .../rendering/datasurfacecontainer.cpp | 70 ++++++ .../rendering/datasurfacecontainer.h | 53 +++++ .../rendering/renderabledataplane.cpp | 217 ------------------ 11 files changed, 462 insertions(+), 253 deletions(-) create mode 100644 modules/datasurface/rendering/dataplane.cpp rename modules/datasurface/rendering/{renderabledataplane.h => dataplane.h} (78%) create mode 100644 modules/datasurface/rendering/datasurface.cpp create mode 100644 modules/datasurface/rendering/datasurface.h create mode 100644 modules/datasurface/rendering/datasurfacecontainer.cpp create mode 100644 modules/datasurface/rendering/datasurfacecontainer.h delete mode 100644 modules/datasurface/rendering/renderabledataplane.cpp diff --git a/data/scene/datasurface/datasurface.mod b/data/scene/datasurface/datasurface.mod index 1c57ff25e6..64e4e21078 100644 --- a/data/scene/datasurface/datasurface.mod +++ b/data/scene/datasurface/datasurface.mod @@ -3,7 +3,7 @@ return { Name = "DataSurfaces", Parent = "Root", Renderable = { - Type = "RenderableDataPlane" + Type = "DataSurfaceContainer" } } } \ No newline at end of file diff --git a/data/scene/earth/earth.mod b/data/scene/earth/earth.mod index 820b1945e5..987ed1f0be 100644 --- a/data/scene/earth/earth.mod +++ b/data/scene/earth/earth.mod @@ -87,18 +87,6 @@ return { Position = {0, 0, 0, 5} } } - --, - --{ - -- Name = "EarthPlane", - -- Parent = "Earth", - -- Renderable = { - -- Type = "RenderableDataPlane" - -- }, - -- Ephemeris = { - -- Type = "Static", - -- Position = {10, 0, 0, 1} - -- } - --} -- Plane -- { -- Name = "EarthPlane", diff --git a/modules/datasurface/CMakeLists.txt b/modules/datasurface/CMakeLists.txt index ff9ea8694d..6ffc53b613 100644 --- a/modules/datasurface/CMakeLists.txt +++ b/modules/datasurface/CMakeLists.txt @@ -25,12 +25,16 @@ include(${OPENSPACE_CMAKE_EXT_DIR}/module_definition.cmake) set(HEADER_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderabledataplane.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/datasurfacecontainer.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/datasurface.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/dataplane.h ) source_group("Header Files" FILES ${HEADER_FILES}) set(SOURCE_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderabledataplane.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/datasurfacecontainer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/datasurface.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/dataplane.cpp ) source_group("Source Files" FILES ${SOURCE_FILES}) diff --git a/modules/datasurface/datasurfacemodule.cpp b/modules/datasurface/datasurfacemodule.cpp index a06f06acf8..71d5e0f161 100644 --- a/modules/datasurface/datasurfacemodule.cpp +++ b/modules/datasurface/datasurfacemodule.cpp @@ -29,7 +29,7 @@ #include - #include + #include namespace openspace { @@ -41,6 +41,6 @@ auto fRenderable = FactoryManager::ref().factory(); ghoul_assert(fRenderable, "No renderable factory existed"); - fRenderable->registerClass("RenderableDataPlane"); + fRenderable->registerClass("DataSurfaceContainer"); } } \ No newline at end of file diff --git a/modules/datasurface/rendering/dataplane.cpp b/modules/datasurface/rendering/dataplane.cpp new file mode 100644 index 0000000000..ca730b332d --- /dev/null +++ b/modules/datasurface/rendering/dataplane.cpp @@ -0,0 +1,189 @@ +// /***************************************************************************************** +// * * +// * OpenSpace * +// * * +// * Copyright (c) 2014-2016 * +// * * +// * Permission is hereby granted, free of charge, to any person obtaining a copy of this * +// * software and associated documentation files (the "Software"), to deal in the Software * +// * without restriction, including without limitation the rights to use, copy, modify, * +// * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +// * permit persons to whom the Software is furnished to do so, subject to the following * +// * conditions: * +// * * +// * The above copyright notice and this permission notice shall be included in all copies * +// * or substantial portions of the Software. * +// * * +// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +// * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +// * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +// * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +// * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +// ****************************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace openspace { + +DataPlane::DataPlane(std::shared_ptr kw, std::string path) + :DataSurface(kw, path) + ,_texture(nullptr) + , _quad(0) + , _vertexPositionBuffer(0) +{} + + +DataPlane::~DataPlane(){} + + +bool DataPlane::initialize(){ + DataSurface::initialize(); + + _dimensions = glm::size3_t(1000,1000,1); + float zSlice = 0.5f; + + _dataSlice = _kw->getUniformSliceValues(std::string("p"), _dimensions, zSlice); + + glGenVertexArrays(1, &_quad); // generate array + glGenBuffers(1, &_vertexPositionBuffer); // generate buffer + createPlane(); + + if (_shader == nullptr) { + // Plane Program + + RenderEngine& renderEngine = OsEng.renderEngine(); + _shader = renderEngine.buildRenderProgram("PlaneProgram", + "${MODULE_DATASURFACE}/shaders/dataplane_vs.glsl", + "${MODULE_DATASURFACE}/shaders/dataplane_fs.glsl" + ); + if (!_shader) + return false; + } + + loadTexture(); + + return isReady(); +} + +bool DataPlane::deinitialize(){ + DataSurface::deinitialize(); + + glDeleteVertexArrays(1, &_quad); + _quad = 0; + + glDeleteBuffers(1, &_vertexPositionBuffer); + _vertexPositionBuffer = 0; + + RenderEngine& renderEngine = OsEng.renderEngine(); + if (_shader) { + renderEngine.removeRenderProgram(_shader); + _shader = nullptr; + } + + return true; + + return true; +} + + +bool DataPlane::isReady() const { + bool ready = true; + if (!_shader) + ready &= false; + if(!_texture) + ready &= false; + return ready; +}; + +void DataPlane::render(){ + psc position = _parent->worldPosition(); + position += glm::vec4(-_pscOffset.x, _pscOffset.y, _pscOffset.z, _pscOffset.w); + + glm::mat4 transform = glm::mat4(1.0); + // transform = glm::rotate(transform, _roatation.value()[0], glm::vec3(1,0,0)); + // transform = glm::rotate(transform, _roatation.value()[1], glm::vec3(0,1,0)); + // transform = glm::rotate(transform, _roatation.value()[2], glm::vec3(0,0,1)); + + // Activate shader + _shader->activate(); + glEnable(GL_ALPHA_TEST); + glDisable(GL_CULL_FACE); + + _shader->setUniform("ViewProjection", OsEng.renderEngine().camera()->viewProjectionMatrix()); + _shader->setUniform("ModelTransform", transform); + setPscUniforms(_shader.get(), OsEng.renderEngine().camera(), position); + + ghoul::opengl::TextureUnit unit; + unit.activate(); + _texture->bind(); + _shader->setUniform("texture1", unit); + + glBindVertexArray(_quad); + glDrawArrays(GL_TRIANGLES, 0, 6); + glEnable(GL_CULL_FACE); + _shader->deactivate(); + + +} + + +void DataPlane::loadTexture() { + + //std::unique_ptr texture = ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath)); + ghoul::opengl::Texture::FilterMode filtermode = ghoul::opengl::Texture::FilterMode::Linear; + ghoul::opengl::Texture::WrappingMode wrappingmode = ghoul::opengl::Texture::WrappingMode::ClampToEdge; + std::unique_ptr texture = + std::make_unique(_dataSlice, _dimensions, ghoul::opengl::Texture::Format::Red, GL_RED, GL_FLOAT, filtermode, wrappingmode); + if (texture) { + // std::cout << "texture path: " << absPath(_texturePath) << std::endl; + // LDEBUG("Loaded texture from '" << absPath(_texturePath) << "'"); + + texture->uploadTexture(); + + // Textures of planets looks much smoother with AnisotropicMipMap rather than linear + texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear); + + _texture = std::move(texture); + } + +} + +void DataPlane::createPlane() { + // ============================ + // GEOMETRY (quad) + // ============================ + const GLfloat x = _modelScale.x/2.0; + const GLfloat y = _modelScale.y/2.0; + const GLfloat w = _modelScale.w; + const GLfloat vertex_data[] = { // square of two triangles (sigh) + // x y z w s t + -x, -y, 0.0f, w, 0, 1, + x, y, 0.0f, w, 1, 0, + -x, y, 0.0f, w, 0, 0, + -x, -y, 0.0f, w, 0, 1, + x, -y, 0.0f, w, 1, 1, + x, y, 0.0f, w, 1, 0, + }; + + glBindVertexArray(_quad); // bind array + glBindBuffer(GL_ARRAY_BUFFER, _vertexPositionBuffer); // bind buffer + glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, reinterpret_cast(0)); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, reinterpret_cast(sizeof(GLfloat) * 4)); +} + +}// namespace openspace \ No newline at end of file diff --git a/modules/datasurface/rendering/renderabledataplane.h b/modules/datasurface/rendering/dataplane.h similarity index 78% rename from modules/datasurface/rendering/renderabledataplane.h rename to modules/datasurface/rendering/dataplane.h index 58737c6ddf..107d54d1d8 100644 --- a/modules/datasurface/rendering/renderabledataplane.h +++ b/modules/datasurface/rendering/dataplane.h @@ -22,10 +22,11 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#ifndef __RENDERABLEDATAPLANE_H__ -#define __RENDERABLEDATAPLANE_H__ +#ifndef __DATAPLANE_H__ +#define __DATAPLANE_H__ -#include +// #include +#include #include #include #include @@ -34,40 +35,37 @@ namespace openspace{ - class RenderableDataPlane : public Renderable { + class DataPlane : public DataSurface { public: - RenderableDataPlane(const ghoul::Dictionary& dictionary); - ~RenderableDataPlane(); + DataPlane(std::shared_ptr kw, std::string path); + ~DataPlane(); - bool initialize() override; - bool deinitialize() override; + virtual bool initialize(); + virtual bool deinitialize(); - bool isReady() const override; + bool isReady() const; - virtual void render(const RenderData& data) override; - virtual void update(const UpdateData& data) override; + virtual void render(); + // virtual void update(); private: + void loadTexture(); void createPlane(); - properties::StringProperty _texturePath; - properties::Vec3Property _roatation; + // properties::StringProperty _texturePath; + // properties::Vec3Property _roatation; - std::unique_ptr _shader; + // std::unique_ptr _shader; std::unique_ptr _texture; float* _dataSlice; glm::size3_t _dimensions; - glm::vec4 _pscOffset; - glm::vec4 _modelScale; - psc _parentPos; GLuint _quad; GLuint _vertexPositionBuffer; - KameleonWrapper* _kw; - bool _planeIsDirty; + // bool _planeIsDirty; }; } // namespace openspace diff --git a/modules/datasurface/rendering/datasurface.cpp b/modules/datasurface/rendering/datasurface.cpp new file mode 100644 index 0000000000..4af6ce6c7f --- /dev/null +++ b/modules/datasurface/rendering/datasurface.cpp @@ -0,0 +1,66 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2015 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ +#include +#include +#include +#include +#include + +namespace openspace{ +DataSurface::DataSurface(std::shared_ptr kw, std::string path) + :_kw(kw) + ,_path("path", "Path", path) + ,_shader(nullptr) +{} + +DataSurface::~DataSurface(){} + +bool DataSurface::initialize(){ + _parent = OsEng.renderEngine().scene()->sceneGraphNode("Earth"); + + _modelScale = _kw->getModelScaleScaled(); + _pscOffset = _kw->getModelBarycenterOffsetScaled(); + + return true; +} + +bool DataSurface::deinitialize(){ + _parent = nullptr; +} + +void DataSurface::render(){ + +} + +void DataSurface::setPscUniforms( + ghoul::opengl::ProgramObject* program, + const Camera* camera, + const PowerScaledCoordinate& position) +{ + program->setUniform("campos", camera->position().vec4()); + program->setUniform("objpos", position.vec4()); + program->setUniform("camrot", camera->viewRotationMatrix()); + program->setUniform("scaling", camera->scaling()); +} +}//namespace openspac \ No newline at end of file diff --git a/modules/datasurface/rendering/datasurface.h b/modules/datasurface/rendering/datasurface.h new file mode 100644 index 0000000000..44c0daa03f --- /dev/null +++ b/modules/datasurface/rendering/datasurface.h @@ -0,0 +1,58 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2015 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#ifndef __DATASURFACE_H__ +#define __DATASURFACE_H__ +#include +#include +#include +#include +#include + +namespace openspace{ +class DataSurface : public properties::PropertyOwner{ +public: + DataSurface(std::shared_ptr kw, std::string path); + ~DataSurface(); + + virtual bool initialize(); + virtual bool deinitialize(); + + virtual void render(); + // virtual void update(); + +protected: + void setPscUniforms(ghoul::opengl::ProgramObject* program, const Camera* camera, const PowerScaledCoordinate& position); + + std::shared_ptr _kw; + properties::StringProperty _path; + std::unique_ptr _shader; + + glm::vec4 _pscOffset; + glm::vec4 _modelScale; + SceneGraphNode* _parent; +}; + +}//namespace openspace +#endif \ No newline at end of file diff --git a/modules/datasurface/rendering/datasurfacecontainer.cpp b/modules/datasurface/rendering/datasurfacecontainer.cpp new file mode 100644 index 0000000000..b75b97ae1a --- /dev/null +++ b/modules/datasurface/rendering/datasurfacecontainer.cpp @@ -0,0 +1,70 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2015 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ +#include +#include +#include + +#include +#include + +namespace openspace{ + DataSurfaceContainer::DataSurfaceContainer(const ghoul::Dictionary& dictionary) + :Renderable(dictionary) + { + std::cout << "Created datasurface container" << std::endl; + } + + DataSurfaceContainer::~DataSurfaceContainer(){} + + bool DataSurfaceContainer::initialize(){ + std::cout << "Initialized datasurface container" << std::endl; + + addDataSurface("${OPENSPACE_DATA}/BATSRUS.cdf"); + } + bool DataSurfaceContainer::deinitialize(){} + bool DataSurfaceContainer::isReady() const {} + + void DataSurfaceContainer::render(const RenderData& data){ + for(dataSurface : _dataSurfaces) + dataSurface->render(); + } + + void DataSurfaceContainer::update(const UpdateData& data){} + + void DataSurfaceContainer::addDataSurface(std::string path){ + if(FileSys.fileExists(absPath(path))) { + std::shared_ptr ds; + std::shared_ptr kw = std::make_shared(absPath(path)); + + //find out what class to create + ds = std::make_shared(kw, path); + + + ds->initialize(); + _dataSurfaces.push_back(ds); + }else{ + std::cout << "file does not exist"; + } + } +} \ No newline at end of file diff --git a/modules/datasurface/rendering/datasurfacecontainer.h b/modules/datasurface/rendering/datasurfacecontainer.h new file mode 100644 index 0000000000..58e6b0ed9b --- /dev/null +++ b/modules/datasurface/rendering/datasurfacecontainer.h @@ -0,0 +1,53 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2015 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#ifndef __DATASURFACECONTAINER_H__ +#define __DATASURFACECONTAINER_H__ +#include + +namespace openspace{ +class DataSurface; + +class DataSurfaceContainer : public Renderable { +public: + DataSurfaceContainer(const ghoul::Dictionary& dictionary); + ~DataSurfaceContainer(); + + bool initialize() override; + bool deinitialize() override; + + bool isReady() const override; + + virtual void render(const RenderData& data) override; + virtual void update(const UpdateData& data) override; + + void addDataSurface(std::string path); + +private: +std::vector> _dataSurfaces; + +}; +}//namespace openspace + +#endif \ No newline at end of file diff --git a/modules/datasurface/rendering/renderabledataplane.cpp b/modules/datasurface/rendering/renderabledataplane.cpp deleted file mode 100644 index 6630e701c6..0000000000 --- a/modules/datasurface/rendering/renderabledataplane.cpp +++ /dev/null @@ -1,217 +0,0 @@ -/***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014-2016 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -namespace openspace { - -RenderableDataPlane::RenderableDataPlane(const ghoul::Dictionary& dictionary) - : Renderable(dictionary) - ,_texturePath("texture", "Texture", "${OPENSPACE_DATA}/test.png") - ,_roatation("rotation", "Roatation", glm::vec3(0), glm::vec3(0), glm::vec3(2*M_PI)) - , _shader(nullptr) - , _texture(nullptr) - , _quad(0) - , _vertexPositionBuffer(0) -{ - std::cout << "Creating RenderableDataPlane" << std::endl; - - addProperty(_texturePath); - addProperty(_roatation); - - _texturePath.onChange(std::bind(&RenderableDataPlane::loadTexture, this)); - -} - - -RenderableDataPlane::~RenderableDataPlane(){ -} - -bool RenderableDataPlane::initialize() { - - KameleonWrapper kw(absPath("${OPENSPACE_DATA}/BATSRUS.cdf")); - - _dimensions = glm::size3_t(1000,1000,1); - float zSlice = 0.5f; - - _dataSlice = kw.getUniformSliceValues(std::string("p"), _dimensions, zSlice); - - _modelScale = kw.getModelScaleScaled(); - _pscOffset = kw.getModelBarycenterOffsetScaled(); - - std::cout << "Scale: " << _modelScale.x << ", " << _modelScale.y << ", " << _modelScale.z << ", " << _modelScale.w << std::endl; - std::cout << "Offset: " << _pscOffset.x << ", " << _pscOffset.y << ", " << _pscOffset.z << ", " << _pscOffset.w << std::endl; - - - - glGenVertexArrays(1, &_quad); // generate array - glGenBuffers(1, &_vertexPositionBuffer); // generate buffer - createPlane(); - - if (_shader == nullptr) { - // Plane Program - - RenderEngine& renderEngine = OsEng.renderEngine(); - _shader = renderEngine.buildRenderProgram("PlaneProgram", - "${MODULE_DATASURFACE}/shaders/dataplane_vs.glsl", - "${MODULE_DATASURFACE}/shaders/dataplane_fs.glsl" - ); - if (!_shader) - return false; - } - - loadTexture(); - - return isReady(); -}; -bool RenderableDataPlane::deinitialize() { - glDeleteVertexArrays(1, &_quad); - _quad = 0; - - glDeleteBuffers(1, &_vertexPositionBuffer); - _vertexPositionBuffer = 0; - - RenderEngine& renderEngine = OsEng.renderEngine(); - if (_shader) { - renderEngine.removeRenderProgram(_shader); - _shader = nullptr; - } - - return true; -}; - -bool RenderableDataPlane::isReady() const { - bool ready = true; - if (!_shader) - ready &= false; - if(!_texture) - ready &= false; - return ready; -}; - -void RenderableDataPlane::render(const RenderData& data) -{ - - auto parent = OsEng.renderEngine().scene()->sceneGraphNode("Earth"); - psc _parentPos = parent->worldPosition(); - glm::dvec4 pos = _parentPos.dvec4(); - // std::cout << pos.x << ", " << pos.y << ", " << pos.z << ", " << pos.w << std::endl; - // std::cout << parent->name() << std::endl; - - // float w = (float)_texture->width(); - // float h = (float)_texture->height(); - // float textureRatio = h/w; - - glm::mat4 transform = glm::mat4(1.0); - transform = glm::rotate(transform, _roatation.value()[0], glm::vec3(1,0,0)); - transform = glm::rotate(transform, _roatation.value()[1], glm::vec3(0,1,0)); - transform = glm::rotate(transform, _roatation.value()[2], glm::vec3(0,0,1)); - - // Activate shader - _shader->activate(); - glEnable(GL_ALPHA_TEST); - glDisable(GL_CULL_FACE); - auto psc = _parentPos; - glm::dvec4 dpc = psc.dvec4(); - psc += glm::vec4(-_pscOffset.x, _pscOffset.y, _pscOffset.z, _pscOffset.w); - std::cout << dpc.x << ", " << dpc.y << ", " << dpc.z << ", " << dpc.w << std::endl; - _shader->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); - _shader->setUniform("ModelTransform", transform); - setPscUniforms(_shader.get(), &data.camera, psc); - - ghoul::opengl::TextureUnit unit; - unit.activate(); - _texture->bind(); - _shader->setUniform("texture1", unit); - - glBindVertexArray(_quad); - glDrawArrays(GL_TRIANGLES, 0, 6); - glEnable(GL_CULL_FACE); - _shader->deactivate(); -}; -void RenderableDataPlane::update(const UpdateData& data){ - if (_planeIsDirty) - createPlane(); -}; - - -void RenderableDataPlane::loadTexture() { - if (_texturePath.value() != "") { - //std::unique_ptr texture = ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath)); - ghoul::opengl::Texture::FilterMode filtermode = ghoul::opengl::Texture::FilterMode::Linear; - ghoul::opengl::Texture::WrappingMode wrappingmode = ghoul::opengl::Texture::WrappingMode::ClampToEdge; - std::unique_ptr texture = - std::make_unique(_dataSlice, _dimensions, ghoul::opengl::Texture::Format::Red, GL_RED, GL_FLOAT, filtermode, wrappingmode); - if (texture) { - std::cout << "texture path: " << absPath(_texturePath) << std::endl; - // LDEBUG("Loaded texture from '" << absPath(_texturePath) << "'"); - - texture->uploadTexture(); - - // Textures of planets looks much smoother with AnisotropicMipMap rather than linear - texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear); - - _texture = std::move(texture); - } - } -} - -void RenderableDataPlane::createPlane() { - // ============================ - // GEOMETRY (quad) - // ============================ - const GLfloat x = _modelScale.x/2.0; - const GLfloat y = _modelScale.y/2.0; - const GLfloat w = _modelScale.w; - const GLfloat vertex_data[] = { // square of two triangles (sigh) - // x y z w s t - -x, -y, 0.0f, w, 0, 1, - x, y, 0.0f, w, 1, 0, - -x, y, 0.0f, w, 0, 0, - -x, -y, 0.0f, w, 0, 1, - x, -y, 0.0f, w, 1, 1, - x, y, 0.0f, w, 1, 0, - }; - - glBindVertexArray(_quad); // bind array - glBindBuffer(GL_ARRAY_BUFFER, _vertexPositionBuffer); // bind buffer - glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, reinterpret_cast(0)); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, reinterpret_cast(sizeof(GLfloat) * 4)); - _planeIsDirty = false; -} -}// namespace openspace \ No newline at end of file