From 9adc1e69faf9ca2bb1f383dcdf3da3f16fa3505d Mon Sep 17 00:00:00 2001 From: Jonas Strandstedt Date: Fri, 10 Oct 2014 15:18:16 +0200 Subject: [PATCH] Added RenderablePlane - Only first version of the RenderablePlane Todo: - Support rotation of planes - Support billboarding - Support different local origin (LowerLeft, LowerRight, Center...) --- include/openspace/rendering/renderableplane.h | 70 +++++++ openspace-data | 2 +- shaders/plane_fs.glsl | 57 ++++++ shaders/plane_vs.glsl | 49 +++++ src/rendering/renderableplane.cpp | 188 ++++++++++++++++++ src/scenegraph/scenegraph.cpp | 9 + src/util/factorymanager.cpp | 6 +- 7 files changed, 377 insertions(+), 4 deletions(-) create mode 100644 include/openspace/rendering/renderableplane.h create mode 100644 shaders/plane_fs.glsl create mode 100644 shaders/plane_vs.glsl create mode 100644 src/rendering/renderableplane.cpp diff --git a/include/openspace/rendering/renderableplane.h b/include/openspace/rendering/renderableplane.h new file mode 100644 index 0000000000..b5f987c881 --- /dev/null +++ b/include/openspace/rendering/renderableplane.h @@ -0,0 +1,70 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * 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 RENDERABLEPLANE_H_ +#define RENDERABLEPLANE_H_ + +// open space includes +#include +#include + +// ghoul includes +#include +#include +#include + +namespace openspace { + struct LinePoint; + +class RenderablePlane : public Renderable { + + enum class Origin { + LowerLeft, LowerRight, UpperLeft, UpperRight, Center + }; + +public: + RenderablePlane(const ghoul::Dictionary& dictionary); + ~RenderablePlane(); + + bool initialize(); + bool deinitialize(); + + void render(const RenderData& data) override; + void update(const UpdateData& data) override; + +private: + void loadTexture(); + + properties::StringProperty _texturePath; + + glm::vec2 _size; + Origin _origin; + + ghoul::opengl::ProgramObject* _shader; + ghoul::opengl::Texture* _texture; + GLuint _quad; +}; + +} // namespace openspace +#endif // RENDERABLEFIELDLINES_H_ diff --git a/openspace-data b/openspace-data index f11e50b102..1b18fedd39 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit f11e50b10255fed5f1ea6e8ddc02aa33156e8dc7 +Subproject commit 1b18fedd391cf433f055bb2e8f6c0b1bc7cb09a1 diff --git a/shaders/plane_fs.glsl b/shaders/plane_fs.glsl new file mode 100644 index 0000000000..41184e1a18 --- /dev/null +++ b/shaders/plane_fs.glsl @@ -0,0 +1,57 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * 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. * + ****************************************************************************************/ + +#version 430 + +uniform float time; +uniform sampler2D texture1; + +in vec2 vs_st; +in vec4 vs_position; + +#include "ABuffer/abufferStruct.hglsl" +#include "ABuffer/abufferAddToBuffer.hglsl" +#include "PowerScaling/powerScaling_fs.hglsl" + +void main() +{ + vec4 position = vs_position; + float depth = pscDepth(position); + vec4 diffuse; + if(gl_FrontFacing) + diffuse = texture(texture1, vs_st); + else + diffuse = texture(texture1, vec2(1-vs_st.s,vs_st.t)); + + //vec4 diffuse = vec4(1,vs_st,1); + //vec4 diffuse = vec4(1,0,0,1); + // if(position.w > 9.0) { + // diffuse = vec4(1,0,0,1); + // } + + ABufferStruct_t frag = createGeometryFragment(diffuse, position, depth); + addToBuffer(frag); + + discard; +} \ No newline at end of file diff --git a/shaders/plane_vs.glsl b/shaders/plane_vs.glsl new file mode 100644 index 0000000000..7ec1ecde41 --- /dev/null +++ b/shaders/plane_vs.glsl @@ -0,0 +1,49 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * 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. * + ****************************************************************************************/ + +#version 430 + +uniform mat4 ViewProjection; +uniform mat4 ModelTransform; + +layout(location = 0) in vec4 in_position; +layout(location = 1) in vec2 in_st; + +out vec2 vs_st; +out vec4 vs_position; +out float s; + +#include "PowerScaling/powerScaling_vs.hglsl" + +void main() +{ + vec4 tmp = in_position; + vec4 position = pscTransform(tmp, ModelTransform); + + vs_position = tmp; + vs_st = in_st; + + position = ViewProjection * position; + gl_Position = z_normalization(position); +} \ No newline at end of file diff --git a/src/rendering/renderableplane.cpp b/src/rendering/renderableplane.cpp new file mode 100644 index 0000000000..7a2bacde76 --- /dev/null +++ b/src/rendering/renderableplane.cpp @@ -0,0 +1,188 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * 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 + +namespace { + const std::string _loggerCat = "RenderablePlane"; + + const std::string keyFieldlines = "Fieldlines"; + const std::string keyFilename = "File"; + const std::string keyHints = "Hints"; + const std::string keyShaders = "Shaders"; + const std::string keyVertexShader = "VertexShader"; + const std::string keyFragmentShader = "FragmentShader"; +} + +namespace openspace { + +RenderablePlane::RenderablePlane(const ghoul::Dictionary& dictionary) + : Renderable(dictionary) + , _texturePath("texture", "Texture") + , _size(glm::vec2(1,1)) + , _origin(Origin::Center) + , _shader(nullptr) + , _texture(nullptr) + , _quad(0) +{ + + dictionary.getValue("Size", _size); + + std::string origin; + if (dictionary.getValue("Origin", origin)) { + if (origin == "LowerLeft") { + _origin = Origin::LowerLeft; + } + else if (origin == "LowerRight") { + _origin = Origin::LowerRight; + } + else if (origin == "UpperLeft") { + _origin = Origin::UpperLeft; + } + else if (origin == "UpperRight") { + _origin = Origin::UpperRight; + } + else if (origin == "Center") { + _origin = Origin::Center; + } + } + + std::string texturePath = ""; + bool success = dictionary.getValue("Texture", texturePath); + if (success) + _texturePath = findPath(texturePath); + + + addProperty(_texturePath); + _texturePath.onChange(std::bind(&RenderablePlane::loadTexture, this)); + + setBoundingSphere(_size); +} + +RenderablePlane::~RenderablePlane() { +} + +bool RenderablePlane::initialize() { + + // ============================ + // GEOMETRY (quad) + // ============================ + const GLfloat size = _size[0]; + const GLfloat w = _size[1]; + LDEBUG("size:" << size); + LDEBUG("w:" << w); + const GLfloat vertex_data[] = { // square of two triangles (sigh) + // x y z w s t + -size, -size, 0.0f, w, 0,1, + size, size, 0.0f, w, 1, 0, + -size, size, 0.0f, w, 0, 0, + -size, -size, 0.0f, w, 0, 1, + size, -size, 0.0f, w, 1, 1, + size, size, 0.0f, w, 1, 0, + }; + + GLuint vertexPositionBuffer; + glGenVertexArrays(1, &_quad); // generate array + glBindVertexArray(_quad); // bind array + glGenBuffers(1, &vertexPositionBuffer); // generate buffer + 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)); + + OsEng.ref().configurationManager().getValue("PlaneProgram", _shader); + assert(_shader); + + loadTexture(); + + + + return true; +} + +bool RenderablePlane::deinitialize() { + return true; +} + +void RenderablePlane::render(const RenderData& data) { + + if (!_shader) + return; + if (!_texture) + return; + + glm::mat4 transform = glm::mat4(1.0); + //transform = glm::scale(transform, glm::vec3(0.01)); + + // Activate shader + _shader->activate(); + + _shader->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); + _shader->setUniform("ModelTransform", transform); + setPscUniforms(_shader, &data.camera, data.position); + + ghoul::opengl::TextureUnit unit; + unit.activate(); + _texture->bind(); + _shader->setUniform("texture1", unit); + + glBindVertexArray(_quad); + glDrawArrays(GL_TRIANGLES, 0, 6); + + _shader->deactivate(); +} + +void RenderablePlane::update(const UpdateData& data) { +} + +void RenderablePlane::loadTexture() +{ + LDEBUG("loadTexture"); + if (_texturePath.value() != "") { + LDEBUG("loadTexture2"); + ghoul::opengl::Texture* texture = ghoul::opengl::loadTexture(absPath(_texturePath)); + if (texture) { + 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::AnisotropicMipMap); + + if (_texture) + delete _texture; + _texture = texture; + } + } +} + +} // namespace openspace diff --git a/src/scenegraph/scenegraph.cpp b/src/scenegraph/scenegraph.cpp index 3134041bd5..c86d1a7b00 100644 --- a/src/scenegraph/scenegraph.cpp +++ b/src/scenegraph/scenegraph.cpp @@ -213,6 +213,15 @@ bool SceneGraph::initialize() _programs.push_back(tmpProgram); OsEng.ref().configurationManager().setValue("GridProgram", tmpProgram); + // Plane program + tmpProgram = ProgramObject::Build("Plane", + "${SHADERS}/plane_vs.glsl", + "${SHADERS}/plane_fs.glsl", + cb); + if (!tmpProgram) return false; + _programs.push_back(tmpProgram); + OsEng.ref().configurationManager().setValue("PlaneProgram", tmpProgram); + // Done building shaders double elapsed = std::chrono::duration_cast(clock_::now()-beginning).count(); LINFO("Time to load shaders: " << elapsed); diff --git a/src/util/factorymanager.cpp b/src/util/factorymanager.cpp index e4c985b440..c913734ad0 100644 --- a/src/util/factorymanager.cpp +++ b/src/util/factorymanager.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include @@ -62,8 +62,8 @@ void FactoryManager::initialize() "RenderableStars"); _manager->factory()->registerClass( "RenderableSphericalGrid"); - //_manager->factory()->registerClass( - // "RenderableVolumeCL"); + _manager->factory()->registerClass( + "RenderablePlane"); _manager->factory()->registerClass( "RenderableVolumeGL"); _manager->factory()->registerClass("RenderableFieldlines");