From 2ac2155f2cc7e7ac63d440bcb46fa72a4c907dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 7 Feb 2022 21:38:32 +0100 Subject: [PATCH] Shaders: fix MeshVisualizer vertex and object ID to work with TBN. And also expand the docs to say the TBN visualization is not AAd when used together with those, not just with wireframe. --- doc/changelog.dox | 2 ++ src/Magnum/Shaders/MeshVisualizer.geom | 12 ++++++------ src/Magnum/Shaders/MeshVisualizerGL.cpp | 2 ++ src/Magnum/Shaders/MeshVisualizerGL.h | 9 +++++---- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 814fb9841..4bf4a10c5 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -608,6 +608,8 @@ See also: compilation failed on OpenGL ES 3.2 with missing @glsl gl_PrimitiveID @ce due to GLSL ES 3.20 not being properly used for the @glsl #version @ce directive +- @ref Shaders::MeshVisualizerGL3D vertex ID visualization didn't work when + enabled together with TBN visualization - @ref Shaders::PhongGL was normalizing light direction in vertex shader, causing the fragment-interpolated direction being incorrect with visible artifacts on long polygons under low light angle diff --git a/src/Magnum/Shaders/MeshVisualizer.geom b/src/Magnum/Shaders/MeshVisualizer.geom index 6aef91340..0176e6b43 100644 --- a/src/Magnum/Shaders/MeshVisualizer.geom +++ b/src/Magnum/Shaders/MeshVisualizer.geom @@ -44,7 +44,7 @@ layout(location = 0) uniform lowp vec2 viewportSize; /* defaults to zero */ #ifndef UNIFORM_BUFFERS -#if (defined(TANGENT_DIRECTION) || defined(BITANGENT_DIRECTION) || defined(NORMAL_DIRECTION)) && (defined(WIREFRAME_RENDERING) || defined(INSTANCED_OBJECT_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID)) +#if (defined(TANGENT_DIRECTION) || defined(BITANGENT_DIRECTION) || defined(NORMAL_DIRECTION)) && (defined(WIREFRAME_RENDERING) || defined(OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID)) #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 1) #endif @@ -234,8 +234,8 @@ void emitQuad( /* Calculate screen-space locations for the bar vertices and form two triangles out of them. In case TBN is rendered alone, half bar width is lineWidth + smoothness to allow for antialiasing, in case it's rendered - together with wireframe, it's just lineWidth, as antialiasing would - cause depth order issues. + together with wireframe, object ID, vertex ID or primitive ID, it's just + lineWidth, as antialiasing would cause depth order issues. 3 - e - 1 -S_-w_0__w_S -w_0__w | /| | | | | | | @@ -251,7 +251,7 @@ void emitQuad( and to 0 otherwise. See the fragment shader for details. */ vec2 direction = normalize(endpointScreen - positionScreen); - #if defined(WIREFRAME_RENDERING) || defined(INSTANCED_OBJECT_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID) + #if defined(WIREFRAME_RENDERING) || defined(OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID) float edgeDistance = 0.0; vec2 halfSide = lineWidth*vec2(-direction.y, direction.x); #else @@ -292,7 +292,7 @@ void main() { #endif mediump const uint materialId = draws[drawId].draw_materialIdReserved & 0xffffu; - #if (defined(TANGENT_DIRECTION) || defined(BITANGENT_DIRECTION) || defined(NORMAL_DIRECTION)) && (defined(WIREFRAME_RENDERING) || defined(INSTANCED_OBJECT_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID)) + #if (defined(TANGENT_DIRECTION) || defined(BITANGENT_DIRECTION) || defined(NORMAL_DIRECTION)) && (defined(WIREFRAME_RENDERING) || defined(OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID)) lowp const vec4 color = materials[materialId].color; lowp const vec4 wireframeColor = materials[materialId].wireframeColor; #endif @@ -340,7 +340,7 @@ void main() { #endif } - #if defined(WIREFRAME_RENDERING) || defined(INSTANCED_OBJECT_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID) + #if defined(WIREFRAME_RENDERING) || defined(OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID) /* Vector of each triangle side */ const vec2 v[3] = vec2[3]( p[2]-p[1], diff --git a/src/Magnum/Shaders/MeshVisualizerGL.cpp b/src/Magnum/Shaders/MeshVisualizerGL.cpp index 246208d70..c93e43813 100644 --- a/src/Magnum/Shaders/MeshVisualizerGL.cpp +++ b/src/Magnum/Shaders/MeshVisualizerGL.cpp @@ -447,6 +447,7 @@ MeshVisualizerGL2D::MeshVisualizerGL2D(const Flags flags .addSource("#define WIREFRAME_RENDERING\n#define MAX_VERTICES 3\n") .addSource(_flags >= FlagBase::ObjectIdTexture ? "#define TEXTURED\n" : "") .addSource(_flags & FlagBase::TextureArrays ? "#define TEXTURE_ARRAYS\n" : "") + .addSource(_flags & FlagBase::ObjectId ? "#define OBJECT_ID\n" : "") .addSource(_flags >= FlagBase::InstancedObjectId ? "#define INSTANCED_OBJECT_ID\n" : "") .addSource(_flags & FlagBase::VertexId ? "#define VERTEX_ID\n" : "") .addSource(_flags & FlagBase::PrimitiveId ? @@ -782,6 +783,7 @@ MeshVisualizerGL3D::MeshVisualizerGL3D(const Flags flags .addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") .addSource(_flags >= FlagBase::ObjectIdTexture ? "#define TEXTURED\n" : "") .addSource(_flags & FlagBase::TextureArrays ? "#define TEXTURE_ARRAYS\n" : "") + .addSource(_flags & FlagBase::ObjectId ? "#define OBJECT_ID\n" : "") .addSource(_flags >= FlagBase::InstancedObjectId ? "#define INSTANCED_OBJECT_ID\n" : "") .addSource(_flags & FlagBase::VertexId ? "#define VERTEX_ID\n" : "") .addSource(_flags & FlagBase::PrimitiveId ? diff --git a/src/Magnum/Shaders/MeshVisualizerGL.h b/src/Magnum/Shaders/MeshVisualizerGL.h index 96a237634..19cd9f287 100644 --- a/src/Magnum/Shaders/MeshVisualizerGL.h +++ b/src/Magnum/Shaders/MeshVisualizerGL.h @@ -959,10 +959,11 @@ Rendering setup the same as above. @section Shaders-MeshVisualizerGL3D-tbn Tangent space visualization On platforms with geometry shaders (desktop GL, OpenGL ES 3.2), the shader is -able to visualize tangents, bitangent and normal direction via colored lines -coming out of vertices (red, green and blue for tangent, bitangent and normal, respectively). This can be enabled together with wireframe visualization, -however note that when both are enabled, the lines are not antialiased to avoid -depth ordering artifacts. +able to visualize tangent, bitangent and normal direction via colored lines +coming out of vertices (red, green and blue for tangent, bitangent and normal, respectively). This can be enabled together with wireframe, object ID, +vertex ID or primitive ID visualization, however note that when both are +enabled, the lines are not antialiased to avoid depth ordering artifacts +between faces and lines. For tangents and normals, you need to provide the @ref Tangent and @ref Normal attributes and enable @ref Flag::TangentDirection and