Glm math for a polyboard algorithm

Example.py

    //from Mathematics for 3D Game Programming Third Edition
    // Eric Lengyel... (Polyboard keyword...)
   #size = 50.
    #cam_pos
    #vec3( 0, 0, -1920 )
    #near
    #0.0001
    #far
    #3840
    #GLM::::
    P1 = glm.vec3(-size,0.,0.)
    P2 = glm.vec3(-size,0.,0.)
    P3 = glm.vec3(0.,size,0.)
    P4 = glm.vec3(-size,0.,0.)

    r=.01;
    C = camera.cam_pos;

    Z1 = (C-P1)/(glm.normalize(C-P1));
    Z2 = (C-P2)/(glm.normalize(C-P2));
    Z3 = (C-P3)/(glm.normalize(C-P3));
    Z4 = (C-P4)/(glm.normalize(C-P4));

    T1 = (P2-P1)/(glm.normalize(P2-P1));
    T2 = (P3-P2)/(glm.normalize(P3-P2));
    T3 = (P4-P3)/(glm.normalize(P4-P3));
    T4 = (P4-P3)/(glm.normalize(P4-P3));

    G1 = P1 + r*(glm.cross(T1,Z1));
    H1 = P1 - r*(glm.cross(T1,Z1));

    G2 = P2 + r*(glm.cross(T2,Z2));
    H2 = P2 - r*(glm.cross(T2,Z2));

    G3 = P3 + r*(glm.cross(T3,Z3));
    H3 = P3 - r*(glm.cross(T3,Z3));

    G4 = P4 + r*(glm.cross(T4,Z4));
    H4 = P4 - r*(glm.cross(T4,Z4));

So far after investigating G1 I am finding that G1’s components are assigned vec3( -nan(ind), -nan(ind), -nan(ind) ).

So then in my effort to transcribe the algorithm from the book annotated in my comment block I am getting nan (not a number?). I feel a bit lost so far, and after lookingover things so far I don’t really have an understanding as to why things are showing up nan however it seems that G1 should describe a vertices offset from one of the triangles vertices. Anyone know why the nan is showing up and maybe how to get the correct point at G1 to start to come forth from the glm system (or some other way to?)?

In my experience NaNs often come from division by zero, and since P1 == P2 the calculation of T1 does indeed divide by zero.
Also, without knowing anything about this algorithm, are you sure you don’t want something like, Z1 = (C-P1) / length(C-P1) or more compact Z1 = normalize(C-P1)?

1 Like

Well, after testing out such an algorithm described it, I am left with a hunch that sometimes there will be a zero there.

Is there a way to have glm interpret 0 as some small amount close to zero say .00000001, in all cases where a zero is computed?

Things do leave me with the question of what to do in this particular case about the zero to correctly compute a G1 point in space?

Also at some point GLSL will also have to detect these zero conditions. So far I became aware of glm.epsilon() in the Python language, and am looking around to see if GLSL has something similar to or equal to glm.epsilon() to look to and detect 0…