I’m trying to use an OpenGL shader I have that renders an infinite regular grid for use as a reference in the visualization of a vehicle trajectory. I’ve confirmed it works in base OpenGL, but I’m a bit confused on how I would implement it in the current shader system in VTK. It seems there is no way to add additional shader programs to the pipeline directly but instead I have to add them as overrides to the shader templates that VTK uses. However, it’s not clear which parts of the templates I should override. Can anyone help provide guidance on this? The shaders are included below for reference.
fs.glsl
#version 450
in vec3 nearPoint; // nearPointPoint calculated in vertex shader
in vec3 farPoint; // farPointPoint calculated in vertex shader
uniform mat4 view;
uniform mat4 proj;
out vec4 outColor;
float checkerboard(vec2 R, float scale) {
return float((int(floor(R.x / scale)) + int(floor(R.y / scale))) % 2);
}
float computeDepth(vec3 pos) {
vec4 clip_space_pos = proj * view * vec4(pos.xyz, 1.0);
float clip_space_depth = clip_space_pos.z / clip_space_pos.w;
float farPoint = gl_DepthRange.far;
float nearPoint = gl_DepthRange.near;
float depth =
(((farPoint - nearPoint) * clip_space_depth) + nearPoint + farPoint) /
2.0;
return depth;
}
void main() {
float t = -nearPoint.y / (farPoint.y - nearPoint.y);
vec3 R = nearPoint + t * (farPoint - nearPoint);
float c = checkerboard(R.xz, 1) * 0.3 + checkerboard(R.xz, 10) * 0.2 +
checkerboard(R.xz, 100) * 0.1 + 0.1;
c = c * float(t > 0);
float spotlight = min(1.0, 1.5 - 0.02 * length(R.xz));
outColor = vec4(vec3(c * spotlight), 1);
gl_FragDepth = computeDepth(R);
}
vs.glsl
#version 450
uniform mat4 view;
uniform mat4 proj;
out vec3 nearPoint;
out vec3 farPoint;
// Grid position are in clipped space
vec3 gridPlane[6] = vec3[](vec3(1, 1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(-1, -1, 0), vec3(1, 1, 0), vec3(1, -1, 0));
vec3 UnprojectPoint(float x, float y, float z, mat4 V, mat4 P) {
mat4 Vinv = inverse(V);
mat4 Pinv = inverse(P);
vec4 unprojectedPoint = Vinv * Pinv * vec4(x, y, z, 1.0);
return unprojectedPoint.xyz / unprojectedPoint.w;
}
void main() {
vec3 p = gridPlane[gl_VertexID].xyz;
nearPoint = UnprojectPoint(p.x, p.y, 0.0, view, proj)
.xyz; // unprojecting on the near plane
farPoint = UnprojectPoint(p.x, p.y, 1.0, view, proj)
.xyz; // unprojecting on the far plane
gl_Position = vec4(p, 1.0); // using directly the clipped coordinates
}