Last active
August 29, 2015 14:10
-
-
Save BeRo1985/84d4c689b7867796b2bf to your computer and use it in GitHub Desktop.
Clipping line in homogeneous coordinates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// by Benjamin 'BeRo' Rosseaux | |
// licensed under CC0 1.0 Universal (CC0 1.0) Public Domain Dedication ( http://creativecommons.org/publicdomain/zero/1.0/ ) | |
void clipLineInHomogeneousCoordinates(inout vec4 pP0, inout vec4 pP1){ | |
vec3 lWC0P = pP0.www + pP0.xyz; | |
vec3 lWC0S = pP0.www - pP0.xyz; | |
vec3 lWC1P = pP1.www + pP1.xyz; | |
vec3 lWC1S = pP1.www - pP1.xyz; | |
vec3 lTTP = lWC0P / (lWC0P - lWC1P); | |
vec3 lTTS = lWC0S / (lWC0S - lWC1S); | |
#ifdef NO_BOOLEAN_OPERATIONS | |
vec3 lTV0 = max(mix(lTTP, vec3(0.0), step(vec3(0.0), lWC0P)), mix(lTTS, vec3(0.0), step(vec3(0.0), lWC0S))); | |
vec3 lTV1 = min(mix(lTTP, vec3(1.0), step(vec3(0.0), lWC1P)), mix(lTTS, vec3(1.0), step(vec3(0.0), lWC1S))); | |
#else | |
vec3 lTV0 = max(mix(vec3(0.0), lTTP, lessThan(lWC0P, vec3(0.0))), mix(vec3(0.0), lTTS, lessThan(lWC0S, vec3(0.0)))); | |
vec3 lTV1 = min(mix(vec3(1.0), lTTP, lessThan(lWC1P, vec3(0.0))), mix(vec3(1.0), lTTS, lessThan(lWC1S, vec3(0.0)))); | |
#endif | |
vec2 lT = vec2(max(max(max(lTV0.x, lTV0.y), lTV0.z), 0.0), min(min(min(lTV1.x, lTV1.y), lTV1.z), 1.0)); | |
#ifdef NO_BOOLEAN_OPERATIONS | |
float lB = step(lT.x, lT.y); | |
#else | |
bool lB = lT.x <= lT.y; | |
#endif | |
vec4 lP0 = pP0; | |
pP0 = mix(pP0, mix(pP0, pP1, lT.x), lB); | |
pP1 = mix(pP1, mix(lP0, pP1, lT.y), lB); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment