Menu

Far/near clipping plane

Help
Daniel
2020-05-06
2020-05-07
  • Daniel

    Daniel - 2020-05-06

    Hi!

    I am trying out this tutorial: https://github.com/martin-pr/possumwood/wiki/Infinite-ground-plane-using-GLSL-shaders

    But I really cannot figure out how to get the "vec3 iNearPositionVert" and "vec3 iFarPositionVert".
    I guess it is the near and far plane of the camera frustum, but how can I find it with GLScene or directly in the shader?

    Cheers

     
  • Jerome.D (BeanzMaster)

    Hi Daniel you can get far and near in

    GLSceneViewer1.Buffer.RenderingContext.PipelineTransformation.Frustum

    But i think you should create a new shader like Shaders i've made (ex GLYvoryShader) and retreive frustrum in DoApply method with rci

    Cheers

     

    Last edit: Jerome.D (BeanzMaster) 2020-05-06
  • Daniel

    Daniel - 2020-05-07

    Hi Jerome!
    Thanks! That seems to work, but still my code doesn't work...
    I will try to figure out what's wrong.
    Here is the files if someone wants to try to solve it.

     
  • Daniel

    Daniel - 2020-05-07

    Vertex shader:

    uniform vec3 nVal;
    uniform vec3 fVal;
    
    varying vec3 vP;
    
    varying vec3 nVal2;
    varying vec3 fVal2;
    
    void main() {
    
    vP = gl_Vertex.xyz;
    
    nVal2 = nVal;
    fVal2 = fVal;
    
    gl_Position = vec4(vP, 1);  
    }
    

    Frag shader:

    varying vec3 nVal2;
    varying vec3 fVal2;
    
    varying vec3 vP;
    
    void main()
    {
    
        float t = -nVal2.y / (fVal2.y-nVal2.y);
    
        //vec3 R = nVal2 + t * (fVal2 - nVal2); 
        float c = (
            int(round(vP.x * 5.0)) + int(round(vP.y * 5.0))
            ) % 2;     
    
        gl_FragColor = vec4(vec3(c/2.0 + 0.3), 1) * float(t > 0);
    }
    
     
  • Jerome.D (BeanzMaster)

    Hi Daniel i 've took a llok at the site web you give i think the better approch is using raymarch

    Like here https://www.shadertoy.com/view/Ws2yWd

    This the FragShader with only the chessboard plane ;)

    const int RAY_STEPS = 256;
    const float FOV = 45.0 * 3.14159 / 180.0;
    
    struct Intersection {
        float t;
        vec3 color;
        vec3 p;
        int object;
    };
    
    mat4 scale(vec3 s){
        return mat4(
            vec4(s.x,   0.0, 0.0, 0.0),
            vec4(0.0, s.y,   0.0, 0.0),
            vec4(0.0, 0.0, s.z,   0.0),
            vec4(0.0, 0.0, 0.0, 1.0)
        );
    }
    
    mat4 translate(vec3 t){
        return mat4(
            vec4(1.0, 0.0, 0.0, 0.0),
            vec4(0.0, 1.0, 0.0, 0.0),
            vec4(0.0, 0.0, 1.0, 0.0),
            vec4(t.x,   t.y,   t.z,   1.0)
        );
    }
    
    mat4 rotateX(float theta){
        theta *= 3.14159 / 180.0;
        return mat4(
            vec4(1.,0.,0.,0),
            vec4(0.,cos(theta),-sin(theta),0.),
            vec4(0.,sin(theta),cos(theta),0.),
            vec4(0.,0.,0.,1.));
    }
    
    mat4 rotateY(float theta){
        theta *= 3.14159 / 180.0;
        return mat4(
            vec4(cos(theta),0.,-sin(theta),0),
            vec4(0.,1.,0.,0.),
            vec4(sin(theta),0.,cos(theta),0.),
            vec4(0.,0.,0.,1.));
    }
    
    mat4 rotateZ(float theta){
        theta *= 3.14159 / 180.0;
        return mat4(
            vec4(cos(theta),-sin(theta),0.,0),
            vec4(sin(theta),cos(theta),0.,0.),
            vec4(0.,0.,1.,0.),
            vec4(0.,0.,0.,1.));
    }
    
    float plane(vec3 p, vec4 n) {
        return dot(p, n.xyz) + n.w;
    }
    
    float sphere(vec3 p, float r, vec3 c) {
        return distance(p, c) - r;
    }
    
    float dot2( vec3 v ) { return dot(v,v); }
    float udQuad( vec3 p, vec3 a, vec3 b, vec3 c, vec3 d ) {
      vec3 ba = b - a; vec3 pa = p - a;
      vec3 cb = c - b; vec3 pb = p - b;
      vec3 dc = d - c; vec3 pc = p - c;
      vec3 ad = a - d; vec3 pd = p - d;
      vec3 nor = cross( ba, ad );
    
      return sqrt(
        (sign(dot(cross(ba,nor),pa)) +
         sign(dot(cross(cb,nor),pb)) +
         sign(dot(cross(dc,nor),pc)) +
         sign(dot(cross(ad,nor),pd))<3.0)
         ?
         min( min( min(
         dot2(ba*clamp(dot(ba,pa)/dot2(ba),0.0,1.0)-pa),
         dot2(cb*clamp(dot(cb,pb)/dot2(cb),0.0,1.0)-pb) ),
         dot2(dc*clamp(dot(dc,pc)/dot2(dc),0.0,1.0)-pc) ),
         dot2(ad*clamp(dot(ad,pd)/dot2(ad),0.0,1.0)-pd) )
         :
         dot(nor,pa)*dot(nor,pa)/dot2(nor) );
    }
    
    float box(vec3 pos, vec3 t, vec3 r, vec3 s) {
        mat4 worldInverse = inverse(translate(t) * rotateX(r.x) * rotateY(r.y) * rotateZ(r.z) * scale(s));
        vec3 p = vec3(worldInverse * vec4(pos, 1));
        float sFactor = min(min(abs(s.x), abs(s.y)), abs(s.z));
        float dX = 0.0;
        float dY = 0.0;
        float dZ = 0.0;
        if (p.x > 0.5) {
            dX = p.x - 0.5;
        }
        else if (p.x < -0.5) {
            dX = -0.5 - p.x;
        }
        if (p.y > 0.5) {
            dY = p.y - 0.5;
        }
        else if (p.y < -0.5) {
            dY = -0.5 - p.y;
        }
        if (p.z > 0.5) {
            dZ = p.z - 0.5;
        }
        else if (p.z < -0.5) {
            dZ = -0.5 - p.z;
        }
        if (dX == 0.0 && dY == 0.0 && dZ == 0.0) {
            float xmin = min(0.5 - p.x, p.x + 0.5);
            float ymin = min(0.5 - p.y, p.y + 0.5);
            float zmin = min(0.5 - p.z, p.z + 0.5);
            return -min(min(xmin, ymin), zmin) * sFactor;
        }
        else {
            return sqrt(dX * dX + dY * dY + dZ * dZ) * sFactor;
        }
    }
    
    void rayCast(vec2 uv, out vec3 dir, out vec3 eye, out vec3 ref) {
        eye = vec3(15.0 * sin(0.3 * iTime), 5.0, 15.0 * cos(0.3 * iTime));
        ref = vec3(0.0, 0.0, 0.0);
    
        float len = tan(FOV * 0.5) * distance(eye, ref);
        vec3 H = normalize(cross(ref - eye, vec3(0.0, 1.0, 0.0)));
        vec3 V = normalize(cross(H, ref - eye));
        V *= len;
        H *= len * iResolution.x / iResolution.y;
        vec3 p = ref + uv.x * H + uv.y * V;
        dir = normalize(p - eye);
    }
    
    vec3 computeMaterial(int hitObj, vec3 p, vec3 d, vec3 n) {
    
            float t = mod(floor(0.25 * p.x), 2.0) == mod(floor(0.25 * p.z), 2.0) ? 1.0 : 0.0;
            return mix(vec3(0.5), vec3(1.0), t);
    }
    
    void sceneMap3D(vec3 pos, out float t, out int obj) {
        t = plane(pos, vec4(0.0, 1.0, 0.0, 2.5)); 
    
    }
    
    float sceneMap3D(vec3 pos) {
        float t;
        int o;
        sceneMap3D(pos, t, o);
        return t;
    }
    
    void march(vec3 origin, vec3 dir, out float t, out int hitObj) {
        t = 0.001;
        for(int i = 0; i < RAY_STEPS; ++i)
        {
            vec3 pos = origin + t * dir;
            float m;
            sceneMap3D(pos, m, hitObj);
            if(m < 0.01)
            {
                return;
            }
            t += m;
        }
        t = -1.0;
        hitObj = -1;
    }
    
    vec3 computeNormal(vec3 pos) {
        vec3 epsilon = vec3(0.0, 0.001, 0.0);
        return normalize( vec3( sceneMap3D(pos + epsilon.yxx) - sceneMap3D(pos - epsilon.yxx),
                                sceneMap3D(pos + epsilon.xyx) - sceneMap3D(pos - epsilon.xyx),
                                sceneMap3D(pos + epsilon.xxy) - sceneMap3D(pos - epsilon.xxy)));
    }
    
    float softShadow(vec3 dir, vec3 origin, float min_t, float k) {
        float res = 1.0;
        float t = min_t;
        for (int i = 0; i < RAY_STEPS; i++ ) {
            float m = sceneMap3D(origin + t * dir);
            if (m < 0.0001) {
                return 0.0;
            }
            res = min(res, k * m / t);
            t += m;
        }
        return res;
    }
    
    Intersection sdf3D(vec3 dir, vec3 eye) {
        float t;
        int hitObj;
        march(eye, dir, t, hitObj);
        if (t == -1.0) {
            return Intersection(t, vec3(0.0), eye + 1000.0 * dir, -1);
        }
        vec3 isect = eye + t * dir;
        vec3 nor = computeNormal(isect);
        vec3 material = computeMaterial(hitObj, isect, dir, nor);
    
        vec3 light = vec3(10.0, 7.45, 10.0);
        float lambert = dot(normalize(light - isect), nor);
    
        float k = 10.0;
        float shadow = softShadow(normalize(light - isect), isect, 0.1, k);
    
        vec3 col = shadow * lambert * material;
    
        return Intersection(t, col, isect, hitObj);
    }
    
    void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
        // Normalized pixel coordinates (from 0 to 1)
        vec2 uv = fragCoord/iResolution.xy;
        // [-1, 1]
        uv = 2.0 * uv - vec2(1.0);
    
        vec3 dir, eye, ref;
        rayCast(uv, dir, eye, ref);
        vec3 col = sdf3D(dir, eye).color;
    
        // Output to screen
        fragColor = vec4(col,1.0);
    }
    

    and for the vertex normaly like ine the link you gave

    #version 130
    
    in vec3 P;                     // position attr from the vbo
    out vec3 vertexPosition;       // vertex position for the fragment shader
    
    void main() {
        vertexPosition = P.xyz;
        gl_Position = vec4(P, 1);
    }
    

    it Should work

    A very cool site for beginning with raymarching
    http://xdpixel.com/category/ray-marching/

    and this little article for a symply floor https://www.geeks3d.com/20130524/building-worlds-with-distance-functions-in-glsl-raymarching-glslhacker-tutorial-opengl/3/

    Cheers

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.