From d3f574ba235fbe6583dfe4671a8511047a775997 Mon Sep 17 00:00:00 2001 From: Jonathan Bosson Date: Fri, 9 Jun 2017 15:24:36 -0600 Subject: [PATCH] render TouchMarkers, currently causes crash on runtime --- modules/touch/CMakeLists.txt | 12 ++- modules/touch/include/TouchMarker.h | 47 +++++++-- modules/touch/shaders/marker_fs.glsl | 66 ++++++++++++ modules/touch/shaders/marker_vs.glsl | 43 ++++++++ modules/touch/src/TouchMarker.cpp | 147 +++++++++++++++++++++++++-- modules/touch/src/TuioEar.cpp | 1 - modules/touch/touchmodule.cpp | 22 +++- modules/touch/touchmodule.h | 5 +- 8 files changed, 323 insertions(+), 20 deletions(-) create mode 100644 modules/touch/shaders/marker_fs.glsl create mode 100644 modules/touch/shaders/marker_vs.glsl diff --git a/modules/touch/CMakeLists.txt b/modules/touch/CMakeLists.txt index 0538960a92..bceb2918a8 100644 --- a/modules/touch/CMakeLists.txt +++ b/modules/touch/CMakeLists.txt @@ -25,7 +25,7 @@ include(${OPENSPACE_CMAKE_EXT_DIR}/module_definition.cmake) set(HEADER_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/ext/levmarq.h + ${CMAKE_CURRENT_SOURCE_DIR}/ext/levmarq.h ${CMAKE_CURRENT_SOURCE_DIR}/include/TuioEar.h ${CMAKE_CURRENT_SOURCE_DIR}/include/TouchInteraction.h ${CMAKE_CURRENT_SOURCE_DIR}/include/TouchMarker.h @@ -33,17 +33,23 @@ set(HEADER_FILES source_group("Header Files" FILES ${HEADER_FILES}) set(SOURCE_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/ext/levmarq.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ext/levmarq.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/TuioEar.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/TouchInteraction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/TouchMarker.cpp ) source_group("Source Files" FILES ${SOURCE_FILES}) + +set(SHADER_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/shaders/marker_fs.glsl + ${CMAKE_CURRENT_SOURCE_DIR}/shaders/marker_vs.glsl +) +source_group("Shader Files" FILES ${SHADER_FILES}) create_new_module( "Touch" touch_module - ${HEADER_FILES} ${SOURCE_FILES} + ${HEADER_FILES} ${SOURCE_FILES} ${SHADER_FILES} ) include_external_library(${touch_module} libTUIO ${CMAKE_CURRENT_SOURCE_DIR}/ext/libTUIO) \ No newline at end of file diff --git a/modules/touch/include/TouchMarker.h b/modules/touch/include/TouchMarker.h index b582c870de..533512969b 100644 --- a/modules/touch/include/TouchMarker.h +++ b/modules/touch/include/TouchMarker.h @@ -25,29 +25,64 @@ #ifndef __OPENSPACE_TOUCH___MARKER___H__ #define __OPENSPACE_TOUCH___MARKER___H__ +#include +#include +#include #include #include +#include #include +#include + #include - -#include +#include #include -#include -#include -#include +namespace ghoul { +namespace opengl { + class ProgramObject; + class Texture; +} // namespace opengl +} // namespace ghoul namespace openspace { +struct RenderData; +struct UpdateData; + class TouchMarker : public properties::PropertyOwner { public: TouchMarker(); - private: + bool initialize(const std::vector list); + bool deinitialize(); + bool isReady() const; + + void render(const std::vector list); + void update(); + + + private: + void loadTexture(); + void createVertexList(const std::vector list); + + properties::StringProperty _texturePath; + properties::BoolProperty _visible; + properties::FloatProperty _radiusSize; + + std::unique_ptr _shader; + std::unique_ptr _texture; + + GLuint _quad; + GLuint _vertexPositionBuffer; + int _numFingers; + + bool _listIsDirty; + bool _textureIsDirty; }; } // openspace namespace diff --git a/modules/touch/shaders/marker_fs.glsl b/modules/touch/shaders/marker_fs.glsl new file mode 100644 index 0000000000..5921c7262a --- /dev/null +++ b/modules/touch/shaders/marker_fs.glsl @@ -0,0 +1,66 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-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 vec2 out_position; +in float pointRadius; + +out vec4 FragColor; + +void main () { + + // calculate normal from texture coordinates + //vec3 n; + //n.xy = gl_PointCoord.st*vec2(2.0, -2.0) + vec2(-1.0, 1.0); + //float mag = dot(n.xy, n.xy); + //if (mag > 1.0) discard; // kill pixels outside circle + //n.z = sqrt(1.0-mag); + + //vec3 eye = mvPosition + vec3(0.0, 0.0, pointRadius * n.z); + //float depth = (P[2][2] * eye.z + P[3][2]) / (P[2][3] * eye.z + P[3][3]); + + //gl_FragDepth = (depth + 1.0) / 2.0; + + + // calculate lighting + //const vec3 light_dir = vec3(0.0, 0.0, 1.0); + //float diffuse = max(0.0, dot(light_dir, n)); + + //vec3 halfVector = normalize( eye + light_dir); + //float spec = pow(max(0.0, dot(n,halfVector)), 100.0); + + // modify color according to the velocity + //vec3 color = vec3(0.3, 0.3, 0.9); + //vec3 hsv = rgb2hsv(color); + //float v = length(outVelocity); + //v = min((1.0/maxVelocity)*v*v, 1.0); + //vec3 fluidColor = hsv2rgb(vec3(hsv.x, max(1.0 - v, 0.0), 1.0)); + + // compute final color + //vec3 outColor = 0.25 * fluidColor; + //outColor += 0.7 * diffuse * fluidColor; + //outColor += 0.05 * spec * vec3(1.0); + //outColor = clamp(outColor, 0.0, 1.0); + + FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} \ No newline at end of file diff --git a/modules/touch/shaders/marker_vs.glsl b/modules/touch/shaders/marker_vs.glsl new file mode 100644 index 0000000000..ccd7e0e94a --- /dev/null +++ b/modules/touch/shaders/marker_vs.glsl @@ -0,0 +1,43 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-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__ + +// Vertex attributes +layout(location = 0) in vec2 in_position; + +// Uniforms +uniform float radius; + +// Outputs +out vec2 out_position; +out float pointRadius; + +void main() { + out_position = in_position; + pointRadius = 0.05; //radius; + + gl_PointSize = 0.05; //radius; + gl_Position = in_position; +} \ No newline at end of file diff --git a/modules/touch/src/TouchMarker.cpp b/modules/touch/src/TouchMarker.cpp index 71281b0972..ce9a5ecf29 100644 --- a/modules/touch/src/TouchMarker.cpp +++ b/modules/touch/src/TouchMarker.cpp @@ -25,11 +25,12 @@ #include #include -#include -#include -#include #include -#include +#include +#include +#include +#include +#include #include @@ -38,7 +39,141 @@ namespace { } namespace openspace { -TouchMarker::TouchMarker() - : properties::PropertyOwner("TouchMarker") { } +TouchMarker::TouchMarker() + : properties::PropertyOwner("TouchMarker") + , _visible("TouchMarkers visible", "Toggle visibility of markers", false) + , _radiusSize("Marker size", "Set marker size in radius", 0.5, 0, 1) + , _texturePath("texturePath", "Color Texture") + , _shader(nullptr) + , _texture(nullptr) + , _listIsDirty(false) + , _textureIsDirty(false) + , _numFingers(0) +{ + addProperty(_visible); + addProperty(_radiusSize); + addProperty(_texturePath); + _texturePath.onChange(std::bind(&TouchMarker::loadTexture, this)); + + +} + +bool TouchMarker::isReady() const { + if (_shader == nullptr) { + std::cout << "something went wrong\n"; + } + return (_shader != nullptr); // && _texture; +} + +bool TouchMarker::initialize(const std::vector list) { + glGenVertexArrays(1, &_quad); // generate array + glGenBuffers(1, &_vertexPositionBuffer); // generate buffer + createVertexList(list); + + _shader = OsEng.renderEngine().buildRenderProgram("MarkerProgram", + "${MODULE_TOUCH}/shaders/marker_vs.glsl", + "${MODULE_TOUCH}/shaders/marker_fs.glsl" + ); + + //loadTexture(); + + return isReady(); +} + +bool TouchMarker::deinitialize() { + glDeleteVertexArrays(1, &_quad); + _quad = 0; + + glDeleteBuffers(1, &_vertexPositionBuffer); + _vertexPositionBuffer = 0; + + _texture = nullptr; + + RenderEngine& renderEngine = OsEng.renderEngine(); + if (_shader) { + renderEngine.removeRenderProgram(_shader); + _shader = nullptr; + } + + return true; +} + +void TouchMarker::render(const std::vector list) { + if (_visible) { + createVertexList(list); + _shader->activate(); + + // Bind texture + /*ghoul::opengl::TextureUnit unit; + unit.activate(); + _texture->bind(); + _shader->setUniform("texture1", unit);*/ + _shader->setUniform("radius", _radiusSize); + + glEnable(GL_PROGRAM_POINT_SIZE); // Enable gl_PointSize in vertex shader + glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); + glBindVertexArray(_quad); + glDrawArrays(GL_POINTS, 0, _numFingers); + + _shader->deactivate(); + } +} + +void TouchMarker::update() { + if (_shader->isDirty()) + _shader->rebuildFromFile(); + + if (_listIsDirty) + //createFingerList(); + + if (_textureIsDirty) { + loadTexture(); + _textureIsDirty = false; + } +} + +void TouchMarker::loadTexture() { + if (_texturePath.value() != "") { + std::unique_ptr texture = + ghoul::io::TextureReader::ref().loadTexture(absPath(_texturePath)); + + if (texture) { + LDEBUGC( + "TouchMarker", + "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); + + _texture = std::move(texture); + } + } +} + +void TouchMarker::createVertexList(const std::vector list) { + _numFingers = list.size(); + std::vector vertexData(_numFingers * 2, 0); + int i = 0; + for (const TUIO::TuioCursor& c : list) { + vertexData.at(i) = 2 * (c.getX() - 0.5); + vertexData.at(i + 1) = -2 * (c.getY() - 0.5); + i += 2; + } + + glBindVertexArray(_quad); + glBindBuffer(GL_ARRAY_BUFFER, _vertexPositionBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData.data()), vertexData.data(), GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer( + 0, + 2, + GL_FLOAT, + GL_FALSE, + 0, + reinterpret_cast(0) + ); +} } // openspace namespace \ No newline at end of file diff --git a/modules/touch/src/TuioEar.cpp b/modules/touch/src/TuioEar.cpp index 10f652441e..adab44040f 100644 --- a/modules/touch/src/TuioEar.cpp +++ b/modules/touch/src/TuioEar.cpp @@ -23,7 +23,6 @@ ****************************************************************************************/ #include -#include #include #include diff --git a/modules/touch/touchmodule.cpp b/modules/touch/touchmodule.cpp index 92f8306cf7..4e58ef4e1d 100644 --- a/modules/touch/touchmodule.cpp +++ b/modules/touch/touchmodule.cpp @@ -102,7 +102,24 @@ TouchModule::TouchModule() : OpenSpaceModule("Touch") { addPropertySubOwner(touch); - + addPropertySubOwner(markers); + + OsEng.registerModuleCallback( + OpenSpaceEngine::CallbackOption::InitializeGL, + [&]() { + LDEBUGC("TouchModule", "Initializing TouchMarker OpenGL"); + markers.initialize(listOfContactPoints); + } + ); + + OsEng.registerModuleCallback( + OpenSpaceEngine::CallbackOption::DeinitializeGL, + [&]() { + LDEBUGC("TouchMarker", "Deinitialize TouchMarker OpenGL"); + markers.deinitialize(); + } + ); + OsEng.registerModuleCallback( OpenSpaceEngine::CallbackOption::PreSync, [&]() { @@ -128,7 +145,8 @@ TouchModule::TouchModule() OsEng.registerModuleCallback( OpenSpaceEngine::CallbackOption::PostDraw, - []() { + [&]() { + markers.render(listOfContactPoints); } ); } diff --git a/modules/touch/touchmodule.h b/modules/touch/touchmodule.h index 3eb38e1593..1bcd88b49f 100644 --- a/modules/touch/touchmodule.h +++ b/modules/touch/touchmodule.h @@ -26,9 +26,9 @@ #define __OPENSPACE_MODULE_TOUCH___TOUCHMODULE___H__ #include -#include -#include #include +#include + namespace openspace { @@ -42,6 +42,7 @@ namespace openspace { TuioEar ear; TouchInteraction touch; + TouchMarker markers; std::vector listOfContactPoints; std::vector lastProcessed; // contains an id and the TuioPoint that was processed last frame };