Hello, I am looking for a way to change the display color of the triangular patch based on its normal vector and camera orientation, as shown in the following picture. If the normal vector is facing the camera, it is displayed in blue; if the normal vector is in the same direction as the camera, it is displayed in yellow.
by the examples, it shows that the color of triangle is defined by normal, i fond that it is defiend by
" ```
diffuseColor = abs(myNormalMCVSOutput);\n
in https://examples.vtk.org/site/Cxx/Shaders/ColorByNormal/, but how can i change the triangle color by its normal and camera normal?
① how to define the triangle color instead of diffusecolor
② how to get the camera's normal in shaders priority.
Based on your response, I guess I need to make modifications using the OpenGL language. Could you please provide the relevant code?
Or is there another way to make the two faces of a triangle appear differently under lighting, such as making one side brighter and the other side darker? I tried to modify DiffuseColor, but it didn’t work.
GLSL code for the vertex shader (the shader in which you can do geometry computations). Notice the out modifier of the viewNV vector, signaling you want to communicate it to other shaders down the rendering pipeline:
You actually need only the viewNV information, but writing to the built-in gl_Position variable is required in order to have a valid vertex shader.
Next, here’s the GLSL code for the fragment (pixel) shader (the shader where you can calculate the pixel color). Notice it receives the viewNV vector (the in modifier) previously computed in the vertex shader:
in vec3 viewNV;
void main()
{
vec3 N = normalize(viewNV);
vec3 L = vec3(0.0, 0.0, 1.0);
float NdotL = dot(N, L);
vec3 color = vec3(NdotL, 0.0, 1.0-NdotL);
gl_FragColor = vec4( color.rgb, 1.0 );
}
The fragment shader above will give redder hues for pixels of faces facing the camera and more violet hues for faces more edge-on.
Thank you for your answer.
by looking for the vtk function, i found that the actor can use SetBackfaceProperty, which can solve the problem. anyway, thank you for your answer.
the complete code is
vtkSmartPointer backProperty = vtkSmartPointer::New();
backProperty->SetColor(colors->GetColor3d(“Red”).GetData());
actor->SetBackfaceProperty(backProperty);