From 149568ac68cd85e345a4f1c359fea57dc0022f2f Mon Sep 17 00:00:00 2001 From: Konstantinos Chatzilygeroudis Date: Sat, 10 Oct 2020 12:57:49 +0300 Subject: [PATCH] [gui]: fix too much specularity; now there is a parameter to tune it --- src/python/gui.cpp | 6 ++++-- src/robot_dart/gui/magnum/base_application.cpp | 12 +++++++++--- src/robot_dart/gui/magnum/base_application.hpp | 6 +++++- src/robot_dart/gui/magnum/glfw_application.cpp | 5 ++++- src/robot_dart/gui/magnum/gs/phong_multi_light.cpp | 7 +++++++ src/robot_dart/gui/magnum/gs/phong_multi_light.hpp | 5 +++-- .../gui/magnum/resources/PhongMultiLight.frag | 9 +++++++-- .../gui/magnum/windowless_gl_application.cpp | 2 +- 8 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/python/gui.cpp b/src/python/gui.cpp index a5de9e3b..d1290c8b 100644 --- a/src/python/gui.cpp +++ b/src/python/gui.cpp @@ -86,7 +86,7 @@ namespace robot_dart { .def_readwrite("data", &gui::GrayscaleImage::data); py::class_(sm, "GraphicsConfiguration") - .def(py::init(), + .def(py::init(), py::arg("width") = 640, py::arg("height") = 480, py::arg("title") = "DART", @@ -94,6 +94,7 @@ namespace robot_dart { py::arg("transparent_shadows") = true, py::arg("shadow_map_size") = 1024, py::arg("max_lights") = 3, + py::arg("specular_strength") = 0.25, py::arg("draw_main_camera") = true, py::arg("draw_debug") = true) @@ -105,7 +106,8 @@ namespace robot_dart { .def_readwrite("transparent_shadows", &GraphicsConfiguration::transparent_shadows) .def_readwrite("shadow_map_size", &GraphicsConfiguration::shadow_map_size) - .def_readwrite("max_lights", &GraphicsConfiguration::max_lights); + .def_readwrite("max_lights", &GraphicsConfiguration::max_lights) + .def_readwrite("specular_strength", &GraphicsConfiguration::specular_strength); py::class_>(sm, "Base"); py::class_>(sm, "BaseWindowedGraphics"); diff --git a/src/robot_dart/gui/magnum/base_application.cpp b/src/robot_dart/gui/magnum/base_application.cpp index 46f0a4e6..733aa5c5 100644 --- a/src/robot_dart/gui/magnum/base_application.cpp +++ b/src/robot_dart/gui/magnum/base_application.cpp @@ -74,17 +74,18 @@ namespace robot_dart { } // BaseApplication - BaseApplication::BaseApplication(const GraphicsConfiguration& configuration) : _max_lights(configuration.max_lights), _shadow_map_size(configuration.shadow_map_size) + BaseApplication::BaseApplication(const GraphicsConfiguration& configuration) : _configuration(configuration), _max_lights(configuration.max_lights), _shadow_map_size(configuration.shadow_map_size) { enable_shadows(configuration.shadowed, configuration.transparent_shadows); } - void BaseApplication::init(RobotDARTSimu* simu, size_t width, size_t height) + void BaseApplication::init(RobotDARTSimu* simu, const GraphicsConfiguration& configuration) { + _configuration = configuration; _simu = simu; /* Camera setup */ _camera.reset( - new gs::Camera(_scene, static_cast(width), static_cast(height))); + new gs::Camera(_scene, static_cast(configuration.width), static_cast(configuration.height))); /* Shadow camera */ _shadow_camera_object = new Object3D{&_scene}; @@ -291,6 +292,9 @@ namespace robot_dart { if (_shadowed) render_shadows(); + + _color_shader->set_specular_strength(_configuration.specular_strength); + _texture_shader->set_specular_strength(_configuration.specular_strength); } void BaseApplication::update_graphics() @@ -323,6 +327,8 @@ namespace robot_dart { transparent = true; mat.specular_color() = object.drawData().materials[i].specularColor(); mat.shininess() = object.drawData().materials[i].shininess(); + if (mat.shininess() < 1.f) + mat.shininess() = 2000.f; scalings.push_back(object.drawData().scaling); diff --git a/src/robot_dart/gui/magnum/base_application.hpp b/src/robot_dart/gui/magnum/base_application.hpp index a38b0e8f..1b3fa651 100644 --- a/src/robot_dart/gui/magnum/base_application.hpp +++ b/src/robot_dart/gui/magnum/base_application.hpp @@ -102,6 +102,7 @@ namespace robot_dart { // Lights size_t max_lights = 3; + double specular_strength = 0.25; // strength of the specular component // These options are only for the main camera bool draw_main_camera = true; @@ -126,7 +127,7 @@ namespace robot_dart { BaseApplication(const GraphicsConfiguration& configuration = GraphicsConfiguration()); virtual ~BaseApplication() {} - void init(RobotDARTSimu* simu, size_t width, size_t height); + void init(RobotDARTSimu* simu, const GraphicsConfiguration& configuration); void clear_lights(); void add_light(const gs::Light& light); @@ -195,6 +196,9 @@ namespace robot_dart { bool _done = false; + /* GUI Config */ + GraphicsConfiguration _configuration; + /* DART */ RobotDARTSimu* _simu; std::unique_ptr _dart_world; diff --git a/src/robot_dart/gui/magnum/glfw_application.cpp b/src/robot_dart/gui/magnum/glfw_application.cpp index 4c556684..dcad6b71 100644 --- a/src/robot_dart/gui/magnum/glfw_application.cpp +++ b/src/robot_dart/gui/magnum/glfw_application.cpp @@ -31,7 +31,10 @@ namespace robot_dart { ROBOT_DART_EXCEPTION_ASSERT(Magnum::GL::Context::current().version() >= Magnum::GL::Version::GL320, "robot_dart requires at least OpenGL 3.2 for rendering!"); /* Initialize DART world */ - init(simu, Magnum::GL::defaultFramebuffer.viewport().size()[0], Magnum::GL::defaultFramebuffer.viewport().size()[1]); + GraphicsConfiguration config = configuration; + config.width = Magnum::GL::defaultFramebuffer.viewport().size()[0]; + config.height = Magnum::GL::defaultFramebuffer.viewport().size()[1]; + init(simu, config); /* No VSync */ setSwapInterval(0); diff --git a/src/robot_dart/gui/magnum/gs/phong_multi_light.cpp b/src/robot_dart/gui/magnum/gs/phong_multi_light.cpp index c076b37c..1d5bce2b 100644 --- a/src/robot_dart/gui/magnum/gs/phong_multi_light.cpp +++ b/src/robot_dart/gui/magnum/gs/phong_multi_light.cpp @@ -62,6 +62,7 @@ namespace robot_dart { _specular_color_uniform = uniformLocation("specularColor"); _shininess_uniform = uniformLocation("shininess"); _far_plane_uniform = uniformLocation("farPlane"); + _specular_strength_uniform = uniformLocation("specularStrength"); _is_shadowed_uniform = uniformLocation("isShadowed"); _transparent_shadows_uniform = uniformLocation("drawTransparentShadows"); } @@ -213,6 +214,12 @@ namespace robot_dart { return *this; } + PhongMultiLight& PhongMultiLight::set_specular_strength(Magnum::Float specular_strength) + { + setUniform(_specular_strength_uniform, std::max(0.f, specular_strength)); + return *this; + } + PhongMultiLight& PhongMultiLight::bind_shadow_texture(Magnum::GL::Texture2DArray& texture) { texture.bind(_shadow_textures_location); diff --git a/src/robot_dart/gui/magnum/gs/phong_multi_light.hpp b/src/robot_dart/gui/magnum/gs/phong_multi_light.hpp index 0df9fd2a..5819de9c 100644 --- a/src/robot_dart/gui/magnum/gs/phong_multi_light.hpp +++ b/src/robot_dart/gui/magnum/gs/phong_multi_light.hpp @@ -46,6 +46,7 @@ namespace robot_dart { PhongMultiLight& set_far_plane(Magnum::Float far_plane); PhongMultiLight& set_is_shadowed(bool shadows); PhongMultiLight& set_transparent_shadows(bool shadows); + PhongMultiLight& set_specular_strength(Magnum::Float specular_strength); PhongMultiLight& bind_shadow_texture(Magnum::GL::Texture2DArray& texture); PhongMultiLight& bind_shadow_color_texture(Magnum::GL::Texture2DArray& texture); @@ -58,8 +59,8 @@ namespace robot_dart { Flags _flags; Magnum::Int _max_lights = 10; Magnum::Int _transformation_matrix_uniform{0}, _camera_matrix_uniform{7}, _projection_matrix_uniform{1}, _normal_matrix_uniform{2}, - _shininess_uniform{3}, _ambient_color_uniform{4}, _diffuse_color_uniform{5}, _specular_color_uniform{6}, - _lights_uniform{11}, _lights_matrices_uniform, _far_plane_uniform{8}, _is_shadowed_uniform{9}, _transparent_shadows_uniform{10}, + _shininess_uniform{3}, _ambient_color_uniform{4}, _diffuse_color_uniform{5}, _specular_color_uniform{6}, _specular_strength_uniform{11}, + _lights_uniform{12}, _lights_matrices_uniform, _far_plane_uniform{8}, _is_shadowed_uniform{9}, _transparent_shadows_uniform{10}, _shadow_textures_location{3}, _cube_map_textures_location{4}, _shadow_color_textures_location{5}, _cube_map_color_textures_location{6}; const Magnum::Int _light_loc_size = 12; }; diff --git a/src/robot_dart/gui/magnum/resources/PhongMultiLight.frag b/src/robot_dart/gui/magnum/resources/PhongMultiLight.frag index c4b30824..70b71f03 100644 --- a/src/robot_dart/gui/magnum/resources/PhongMultiLight.frag +++ b/src/robot_dart/gui/magnum/resources/PhongMultiLight.frag @@ -117,6 +117,11 @@ uniform bool drawTransparentShadows; #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 11) #endif +uniform float specularStrength = 0.5; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 12) +#endif uniform lightSource lights[LIGHT_COUNT]; in mediump vec3 transformedNormal; @@ -261,7 +266,7 @@ void main() { for(int i = 0; i != LIGHT_COUNT; ++i) { highp vec3 lightDirection; highp float attenuation; - bool spec = any(greaterThan(lights[i].specular.rgb, vec3(0.0))); + bool spec = any(greaterThan(lights[i].specular.rgb, vec3(0.0))) && specularStrength > 0.0; bool isPoint = false; if(!any(greaterThan(lights[i].diffuse.rgb, vec3(0.0))) && !spec) @@ -333,7 +338,7 @@ void main() { specularReflection = attenuation * lights[i].specular.rgb * finalSpecularColor.rgb * specularity; } - color.rgb += (diffuseReflection + specularReflection) * visibility * colorShadow; + color.rgb += (diffuseReflection + specularStrength * specularReflection) * visibility * colorShadow; } color.a = finalDiffuseColor.a; diff --git a/src/robot_dart/gui/magnum/windowless_gl_application.cpp b/src/robot_dart/gui/magnum/windowless_gl_application.cpp index 1e563b0e..792b3c12 100644 --- a/src/robot_dart/gui/magnum/windowless_gl_application.cpp +++ b/src/robot_dart/gui/magnum/windowless_gl_application.cpp @@ -41,7 +41,7 @@ namespace robot_dart { Magnum::GL::Framebuffer::BufferAttachment::Depth, _depth); /* Initialize DART world */ - init(simu, configuration.width, configuration.height); + init(simu, configuration); _camera->record(true); }