To detect the horizontal surfaces on a 3D object, OpenGL Shading Language or GLSL can be used. Qt3D material class along with Qt3D effect class can be employed to implement any custom GLSL code. A Q & A shows how to use those classes.
To detect the horizontal surfaces on a 3D object and show them with red color, the fragment shader for OpenGL 2.0 can be developed like this on a Qt3D custom material/effect:
#define FP highp
varying FP vec3 worldNormal;
void main()
{
vec3 n = normalize(worldNormal);
vec3 z = vec3(0.0, 0.0, -1.0); // Normalized already
float zDotN = dot(z, n);
if ( zDotN > 0.7 ) { // 0.7 is my threshold/criterion
vec3 oc = vec3(1.0, 0.0, 0.0); // Use red color!
gl_FragColor = vec4(oc, 1.0);
} else {
vec3 oc = vec3(0.0, 0.0, 1.0); // Use blue color!
gl_FragColor = vec4(oc, 1.0 );
}
}
On the above code, the dot-product of surface normal
vector with the (0.0, 0.0, -1.0)
is employed to decide if the surface is horizontal or not.
For OpenGL 3.0, the GLSL code would be modified like this:
#version 150 core
in vec3 worldNormal;
out vec4 fragColor;
void main()
{
vec3 n = normalize(worldNormal);
vec3 z = vec3(0.0, 0.0, -1.0); // Normalized already
float zDotN = dot(z, n);
if ( zDotN > 0.7 ) { // 0.7 is my threshold/criterion
vec3 oc = vec3(1.0, 0.0, 0.0); // Use red color!
fragColor = vec4( oc, 1.0);
} else {
vec3 oc = vec3(0.0, 0.0, 1.0); // Use blue color!
fragColor = vec4( oc, 1.0 );
}
}