From 0b96d14923e0038b807ca9230ef0ad4b2654f0f9 Mon Sep 17 00:00:00 2001 From: Mathias Paulin <mathias.paulin@irit.fr> Date: Wed, 24 Mar 2021 23:44:44 +0100 Subject: [PATCH] small improvement in the simpleViewer --- src/DemoApp/main.cpp | 83 +++++++++---------- .../Passes/CustomAttribToColorPass.cpp | 5 +- 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/DemoApp/main.cpp b/src/DemoApp/main.cpp index 8082e91..bdddcfb 100644 --- a/src/DemoApp/main.cpp +++ b/src/DemoApp/main.cpp @@ -182,55 +182,43 @@ void AddCustomAttributeToMeshes() { { auto theMesh = ro->getMesh(); - /* Could be helpfull if one can do - auto displayable = dynamic_cast<Ra::Engine::Data::CoreGeometryDisplayable< ??? - >*>( theMesh.get() ); - */ - auto displayMesh = dynamic_cast<Ra::Engine::Data::Mesh*>( theMesh.get() ); - if ( displayMesh ) + auto displayMesh = + dynamic_cast<Ra::Engine::Data::AttribArrayDisplayable*>( theMesh.get() ); + + LOG( logINFO ) << "\t\tMesh " << displayMesh->getName() << " is processed."; + auto& coreMesh = displayMesh->getAttribArrayGeometry(); + + auto addAttrib = [&coreMesh, displayMesh]( const std::string& name, + auto value ) { + coreMesh.addAttrib<Ra::Core::Vector4>( "myCustomAttrib", value ); + }; + auto meshTypeHash = typeid( *( theMesh.get() ) ).hash_code(); + if ( meshTypeHash == typeid( Ra::Engine::Data::Mesh ).hash_code() ) { - LOG( logINFO ) << "\t\tMesh " << displayMesh->getName() - << " (TriangleMesh) is processed."; - displayMesh->addAttrib<Ra::Core::Vector4>( - "myCustomAttrib", - Ra::Core::Vector4Array{displayMesh->getNumVertices(), - Ra::Core::Utils::Color::Yellow()} ); - displayMesh->setDirty( "myCustomAttrib" ); + addAttrib( "myCustomAttrib", + Ra::Core::Vector4Array{displayMesh->getNumVertices(), + /*Ra::Core::Utils::Color::White()*/ { + 0.7_ra, 0.8_ra, 0.8_ra, 1_ra}} ); + } + else if ( meshTypeHash == typeid( Ra::Engine::Data::PolyMesh ).hash_code() ) + { + addAttrib( "myCustomAttrib", + Ra::Core::Vector4Array{ + displayMesh->getNumVertices(), + Ra::Core::Utils::Color{0.8_ra, 0.7_ra, 0.8_ra}} ); + } + else if ( meshTypeHash == typeid( Ra::Engine::Data::PointCloud ).hash_code() ) + { + addAttrib( "myCustomAttrib", + Ra::Core::Vector4Array{ + displayMesh->getNumVertices(), + Ra::Core::Utils::Color{0.3_ra, 0.3_ra, 0.8_ra}} ); } else { - auto polyMesh = dynamic_cast<Ra::Engine::Data::PolyMesh*>( theMesh.get() ); - if ( polyMesh ) - { - LOG( logINFO ) << "\t\tMesh " << polyMesh->getName() - << " (PolyMesh) is processed."; - polyMesh->addAttrib<Ra::Core::Vector4>( - "myCustomAttrib", - Ra::Core::Vector4Array{polyMesh->getNumVertices(), - Ra::Core::Utils::Color::Magenta()} ); - polyMesh->setDirty( "myCustomAttrib" ); - } - else - { - auto pointCloud = - dynamic_cast<Ra::Engine::Data::PointCloud*>( theMesh.get() ); - if ( pointCloud ) - { - LOG( logINFO ) << "\t\tPointCloud " << pointCloud->getName() - << " is processed."; - pointCloud->addAttrib<Ra::Core::Vector4>( - "myCustomAttrib", - Ra::Core::Vector4Array{pointCloud->getNumVertices(), - Ra::Core::Utils::Color::Blue()} ); - pointCloud->setDirty( "myCustomAttrib" ); - } - else - { - LOG( logINFO ) << "\t\tMesh " << theMesh->getName() - << " is nor a trianglemesh, nor a polymesh, nor a " - "point cloud ... skipping"; - } - } + LOG( logINFO ) << "\t\tMesh " << theMesh->getName() + << " is nor a trianglemesh, nor a polymesh, nor a " + "point cloud ... skipping"; } } } @@ -273,13 +261,16 @@ const std::string customVertexAttrib{"in vec4 myCustomAttrib;\n" * Note that this string could also be loaded from a file and/or manually edited if you * add an edition widget as in Radium app demo */ + const std::string customFragmentColor{ "in vec4 fragCustomAttrib;\n" - "\nvec4 computeCustomColor(Material mat, vec3 lightDir, vec3 viewDir) {\n" + "\nvec4 computeCustomColor(Material mat, vec3 lightDir, vec3 viewDir, vec3 normal_world) {\n" "vec3 diffColor; \n" "vec3 specColor; \n" "getSeparateBSDFComponent( mat, getPerVertexTexCoord(), lightDir, viewDir,\n" "vec3(0, 0, 1), diffColor, specColor );\n" + "vec3 col = normal_world*0.5+0.5;\n" + "col *= fragCustomAttrib.rgb;\n" "return vec4( (specColor+fragCustomAttrib.rgb)*max(lightDir.z, 0), 1); \n" "}\n"}; diff --git a/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp b/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp index 65ce21e..b5c9150 100644 --- a/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp +++ b/src/libRender/RadiumNBR/Passes/CustomAttribToColorPass.cpp @@ -190,7 +190,8 @@ bool CustomAttribToColorPass::buildRenderTechnique( "layout (location = 5) in vec3 in_viewVector;\n" "layout (location = 6) in vec3 in_lightVector;\n" "layout (location = 0) out vec4 out_color;\n" - "vec4 computeCustomColor(Material mat, vec3 light_dir, vec3 view_dir);\n" + "vec4 computeCustomColor(Material mat, vec3 light_dir, vec3 view_dir, vec3 " + "normal_world);\n" "void main()\n" "{\n" "vec3 normalWorld = getWorldSpaceNormal();\n" @@ -208,7 +209,7 @@ bool CustomAttribToColorPass::buildRenderTechnique( "world2local[2] = vec3(tangentWorld.z, binormalWorld.z, normalWorld.z);\n" "vec3 lightDir = normalize(world2local * in_lightVector);\n" "vec3 viewDir = normalize(world2local * in_viewVector);\n" - "out_color = computeCustomColor(material, lightDir, viewDir);\n" + "out_color = computeCustomColor(material, lightDir, viewDir, normalWorld);\n" "}\n"}; Ra::Engine::Data::ShaderConfiguration theConfig{"CustomAtt2ClrPass::CustomColorProgram" + -- GitLab