Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions data/shaders/clip.vert
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#pragma import_defines (NUM_CLIPS)

layout(push_constant) uniform PushConstants {
mat4 projection;
Expand All @@ -19,7 +20,9 @@ layout(location = 1) out vec2 fragTexCoord;

out gl_PerVertex {
vec4 gl_Position;
float gl_ClipDistance[1];
#if NUM_CLIPS > 0
float gl_ClipDistance[NUM_CLIPS];
#endif
//float gl_CullDistance[1];
};

Expand All @@ -36,6 +39,16 @@ void main() {

vec4 eye_Position = pc.modelview * vec4(inPosition, 1.0);

gl_ClipDistance[0] = (length(eye_Position.xyz - clipSettings.sphere.xyz) - clipSettings.sphere.w);
#if NUM_CLIPS > 0
for (uint cOffset = 0; cOffset < NUM_CLIPS; ++cOffset) {
if (cOffset == 0) {
// clip volume inside the sphere using eye coordinates
gl_ClipDistance[cOffset] = (length(eye_Position.xyz - clipSettings.sphere.xyz) - clipSettings.sphere.w);
} else if (cOffset == 1) {
// simple clip on world coordinate X less than zero
gl_ClipDistance[cOffset] = inPosition.x;
}
}
#endif
// gl_CullDistance[0] = (length(inPosition - clipSettings.sphere.xyz) - 3.0);
}
11 changes: 11 additions & 0 deletions examples/state/vsgclip/vsgclip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ int main(int argc, char** argv)

auto numFrames = arguments.value(-1, "-f");

auto numClips = arguments.value(1, "-n");
if (numClips < 0 || numClips > 2)
{
std::cout << "Choose between 0 and 2 clips. If n is zero clipping is disabled. If n is 1 a clipping sphere is enabled in eye space, and if n is 2, world coordinate values less than X=0 are clipped in addition to the clipping sphere." << std::endl;
return 1;
}

if (arguments.errors()) return arguments.writeErrorMessages(std::cerr);

if (argc <= 1)
Expand Down Expand Up @@ -160,6 +167,10 @@ int main(int argc, char** argv)
return 1;
}

// specify the number of clips to perform; gl_ClipDistance array is sized by NUM_CLIPS
vertexShader->module->hints = vsg::ShaderCompileSettings::create();
vertexShader->module->hints->defines.insert("NUM_CLIPS="+std::to_string(numClips));

// create the viewer and assign window(s) to it
auto viewer = vsg::Viewer::create();

Expand Down