how to extrude a path in 3d?

前端 未结 1 1826
南笙
南笙 2021-02-05 20:37

I\'m trying to extrude a path in 3d. Nothing fancy yet, just following some points and using a regular polygon for \'tubing\'. I\'m using Processing for now to quickly prototype

相关标签:
1条回答
  • 2021-02-05 21:05

    Assuming your shape has a normal vector S. In your example S would be (0,0,1), because your shape is flat in xy. You can use the cross product between the current path vector V (normalized) and S to obtain the rotation axis vector R. You need to rotate your shape around R. The angle of rotation can be obtained from the dot product between S and V. So:

    R = S x V
    a = arc cos(S . V)
    

    Now you can setup a rotation matrix with R and a and rotate the shape by it.

    You can use glRotate(...) to rotate the current matrix on the stack, but this can't be done between glBegin() and glEnd(). So you have to do the matrix multiplication by yourself or with a library.

    Edit: After a short look at the library you are using, you should be able to setup the rotation matrix with

    PVector s = new PVector(0,0,1);  // is already normalized (meaning is has length 1)
    PVector cn;
    current.normalize(cn);
    PVector r = s.cross(cn);
    float a = acos(s.dot(cn));
    PMatrix rot = new PMatrix(1, 0, 0, 0,
                              0, 1, 0, 0,
                              0, 0, 1, 0,
                              0, 0, 0, 1);
    rot.rotate(a, r.x, r.y, r.z);
    

    and now multiply each element of your shape with rot and translate it by your current path vector:

    PVector rotVec;
    rot.mult((PVector)shape[i], rotVec);
    rotVec.add(current);
    
    0 讨论(0)
提交回复
热议问题