Skip to content

Commit

Permalink
Merge pull request #112 from resibots/fix_specularity
Browse files Browse the repository at this point in the history
Fix specularity
  • Loading branch information
costashatz authored Oct 12, 2020
2 parents ba00faf + 149568a commit 3d225f3
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/python/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ namespace robot_dart {
.def_readwrite("data", &gui::GrayscaleImage::data);

py::class_<GraphicsConfiguration>(sm, "GraphicsConfiguration")
.def(py::init<size_t, size_t, const std::string&, bool, bool, size_t, size_t, bool, bool>(),
.def(py::init<size_t, size_t, const std::string&, bool, bool, size_t, size_t, double, bool, bool>(),
py::arg("width") = 640,
py::arg("height") = 480,
py::arg("title") = "DART",
py::arg("shadowed") = true,
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)

Expand All @@ -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_<gui::Base, std::shared_ptr<gui::Base>>(sm, "Base");
py::class_<BaseWindowedGraphics, gui::Base, std::shared_ptr<BaseWindowedGraphics>>(sm, "BaseWindowedGraphics");
Expand Down
12 changes: 9 additions & 3 deletions src/robot_dart/gui/magnum/base_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(width), static_cast<int>(height)));
new gs::Camera(_scene, static_cast<int>(configuration.width), static_cast<int>(configuration.height)));

/* Shadow camera */
_shadow_camera_object = new Object3D{&_scene};
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion src/robot_dart/gui/magnum/base_application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -195,6 +196,9 @@ namespace robot_dart {

bool _done = false;

/* GUI Config */
GraphicsConfiguration _configuration;

/* DART */
RobotDARTSimu* _simu;
std::unique_ptr<Magnum::DartIntegration::World> _dart_world;
Expand Down
5 changes: 4 additions & 1 deletion src/robot_dart/gui/magnum/glfw_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/robot_dart/gui/magnum/gs/phong_multi_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/robot_dart/gui/magnum/gs/phong_multi_light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
};
Expand Down
9 changes: 7 additions & 2 deletions src/robot_dart/gui/magnum/resources/PhongMultiLight.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/robot_dart/gui/magnum/windowless_gl_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 3d225f3

Please sign in to comment.