diff --git a/modules/fieldlinessequence/CMakeLists.txt b/modules/fieldlinessequence/CMakeLists.txt index 32a65b9163..33a0b287ec 100644 --- a/modules/fieldlinessequence/CMakeLists.txt +++ b/modules/fieldlinessequence/CMakeLists.txt @@ -37,6 +37,8 @@ set(SOURCE_FILES source_group("Source Files" FILES ${SOURCE_FILES}) set(SHADER_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/shaders/fieldlinessequence_vs.glsl + ${CMAKE_CURRENT_SOURCE_DIR}/shaders/fieldlinessequence_fs.glsl ) source_group("Shader Files" FILES ${SHADER_FILES}) diff --git a/modules/fieldlinessequence/rendering/renderablefieldlinessequence.cpp b/modules/fieldlinessequence/rendering/renderablefieldlinessequence.cpp index 7abe27eff5..eb5831e681 100644 --- a/modules/fieldlinessequence/rendering/renderablefieldlinessequence.cpp +++ b/modules/fieldlinessequence/rendering/renderablefieldlinessequence.cpp @@ -24,9 +24,14 @@ #include +#include +#include #include +#include + #include +#include using std::string; @@ -86,9 +91,36 @@ void RenderableFieldlinesSequence::initialize() { } computeSequenceEndTime(); + + // Setup shader program + _shaderProgram = OsEng.renderEngine().buildRenderProgram( + "FieldlinesSequence", + "${MODULE_FIELDLINESSEQUENCE}/shaders/fieldlinessequence_vs.glsl", + "${MODULE_FIELDLINESSEQUENCE}/shaders/fieldlinessequence_fs.glsl" + ); + + if (!_shaderProgram) { + LERROR("Shader program failed initialization!"); + _sourceFileType = INVALID; + } + + //------------------ Initialize OpenGL VBOs and VAOs-------------------------------// + glGenVertexArrays(1, &_vertexArrayObject); + glGenBuffers(1, &_vertexPositionBuffer); } void RenderableFieldlinesSequence::deinitialize() { + glDeleteVertexArrays(1, &_vertexArrayObject); + _vertexArrayObject = 0; + + glDeleteBuffers(1, &_vertexPositionBuffer); + _vertexPositionBuffer = 0; + + RenderEngine& renderEngine = OsEng.renderEngine(); + if (_shaderProgram) { + renderEngine.removeRenderProgram(_shaderProgram); + _shaderProgram = nullptr; + } } bool RenderableFieldlinesSequence::isReady() const { @@ -96,9 +128,38 @@ bool RenderableFieldlinesSequence::isReady() const { } void RenderableFieldlinesSequence::render(const RenderData& data, RendererTasks&) { + if (_activeStateIndex != -1) { + _shaderProgram->activate(); + + const glm::dmat4 ROTATION_TRANSFORM = glm::dmat4(data.modelTransform.rotation); + // const glm::mat4 SCALE_TRANSFORM = glm::mat4(1.0); // TODO remove if no use + const glm::dmat4 MODEL_TRANSFORM = + glm::translate(glm::dmat4(1.0), data.modelTransform.translation) * + ROTATION_TRANSFORM * + glm::dmat4(glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale))); + const glm::dmat4 MODEL_VIEW_TRANSFORM = data.camera.combinedViewMatrix() * MODEL_TRANSFORM; + + _shaderProgram->setUniform("modelViewProjection", + data.camera.sgctInternal.projectionMatrix() * glm::mat4(MODEL_VIEW_TRANSFORM)); + + glBindVertexArray(_vertexArrayObject); + glMultiDrawArrays( + GL_LINE_STRIP, //_drawingOutputType, + _states[_activeStateIndex].lineStart().data(), + _states[_activeStateIndex].lineCount().data(), + static_cast(_states[_activeStateIndex].lineStart().size()) + ); + + glBindVertexArray(0); + _shaderProgram->deactivate(); + } } void RenderableFieldlinesSequence::update(const UpdateData& data) { + if (_shaderProgram->isDirty()) { + _shaderProgram->rebuildFromFile(); + } + // This node shouldn't do anything if its been disabled from the gui! if (_enabled) { const double CURRENT_TIME = data.time.j2000Seconds(); @@ -119,9 +180,22 @@ void RenderableFieldlinesSequence::update(const UpdateData& data) { } if (_needsUpdate) { - // Update States - // ... - // ... + glBindVertexArray(_vertexArrayObject); + glBindBuffer(GL_ARRAY_BUFFER, _vertexPositionBuffer); + + const std::vector& VERTEX_POS_VEC = + _states[_activeStateIndex].vertexPositions(); + + glBufferData(GL_ARRAY_BUFFER, VERTEX_POS_VEC.size() * sizeof(glm::vec3), + &VERTEX_POS_VEC.front(), GL_STATIC_DRAW); + + glEnableVertexAttribArray(_vertAttrVertexPos); + glVertexAttribPointer(_vertAttrVertexPos, 3, GL_FLOAT, GL_FALSE, 0, 0); + + // UNBIND + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + // Everything is set and ready for rendering! _needsUpdate = false; } diff --git a/modules/fieldlinessequence/rendering/renderablefieldlinessequence.h b/modules/fieldlinessequence/rendering/renderablefieldlinessequence.h index 56b39b4d5b..9f5c44d45a 100644 --- a/modules/fieldlinessequence/rendering/renderablefieldlinessequence.h +++ b/modules/fieldlinessequence/rendering/renderablefieldlinessequence.h @@ -58,10 +58,19 @@ private: double _sequenceEndTime; SourceFileType _sourceFileType; + std::unique_ptr _shaderProgram; + std::vector _startTimes; std::vector _states; std::vector _sourceFiles; // Stored in RAM if files are loaded at runtime, else emptied after initialization + GLuint _vertexArrayObject = 0; + GLuint _vertexPositionBuffer = 0; + + // THESE MUST CORRESPOND TO THE SHADER PROGRAM + // TODO: THIS CAN BE DETERMINED BY ASKING THE SHADER PROGRAM TOO + GLuint _vertAttrVertexPos = 0; + void computeSequenceEndTime(); bool extractInfoFromDictionary(const ghoul::Dictionary& dictionary); inline bool isWithinSequenceInterval(const double CURRENT_TIME); diff --git a/modules/fieldlinessequence/shaders/fieldlinessequence_fs.glsl b/modules/fieldlinessequence/shaders/fieldlinessequence_fs.glsl new file mode 100644 index 0000000000..8e8f3ec6d3 --- /dev/null +++ b/modules/fieldlinessequence/shaders/fieldlinessequence_fs.glsl @@ -0,0 +1,43 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2017 * + * * + * 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. * + ****************************************************************************************/ + +in vec4 vs_color; +in float vs_depth; + +#include "fragment.glsl" +#include "PowerScaling/powerScaling_fs.hglsl" + +Fragment getFragment() { + if (vs_color.a == 0) { + discard; + } + + vec4 fragColor = vs_color; + + Fragment frag; + frag.depth = vs_depth; + frag.color = fragColor; + + return frag; +} diff --git a/modules/fieldlinessequence/shaders/fieldlinessequence_vs.glsl b/modules/fieldlinessequence/shaders/fieldlinessequence_vs.glsl new file mode 100644 index 0000000000..cc6f48bc93 --- /dev/null +++ b/modules/fieldlinessequence/shaders/fieldlinessequence_vs.glsl @@ -0,0 +1,42 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2017 * + * * + * 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 __CONTEXT__ + +uniform mat4 modelViewProjection; + +layout(location = 0) in vec3 in_position; // in meters + +out vec4 vs_color; +out float vs_depth; + +#include "PowerScaling/powerScaling_vs.hglsl" + +void main() { + vs_color = vec4(0.75, 0.5, 0.0, 0.75); + vec4 position_in_meters = vec4(in_position, 1); + vec4 positionClipSpace = modelViewProjection * position_in_meters; + gl_Position = z_normalization(positionClipSpace); + vs_depth = gl_Position.w; +}