沿着 3d 曲线渲染圆

jay*_*ark 4 c++ opengl curve computational-geometry glm-math

我正在尝试围绕 3d 曲线的每个点渲染圆圈。基本上试图为曲线创建一个管状结构。但如图所示,圆圈的方向是错误的。下面是我在计算 frenet 框架后对圆形对象的模型矩阵的计算。我哪里错了?作为参考,绿线是切线,蓝色是法线,红色是副法线。

视图1 视图2

Frenet Frame Calculations

glm::vec3 pointback = curve_points[i-1];
glm::vec3 pointmid = curve_points[i];
glm::vec3 pointforward = curve_points[i+1];

glm::vec3 forward_tangent_vector =  glm::vec3(glm::normalize(pointforward - pointmid)) ;
glm::vec3 backward_tangent_vector = glm::vec3(glm::normalize(pointmid - pointback)) ;

glm::vec3 second_order_tangent = glm::normalize(forward_tangent_vector - backward_tangent_vector);

glm::vec3 binormal = glm::normalize(glm::cross(forward_tangent_vector, second_order_tangent));

glm::vec3 normal = glm::normalize(glm::cross(binormal, forward_tangent_vector));
Run Code Online (Sandbox Code Playgroud)

Model Matrix for Circle calculations

glm::mat3 tbn = glm::mat3(forward_tangent_vector,binormal,normal);

glm::vec3 normal_axis = glm::vec3(0, 1, 0);
//normal_axis = forward_tangent_vector;

glm::vec3 circleNormal = glm::normalize(tbn * normal_axis);
glm::vec3 rotationAxis = glm::cross(normal_axis, circleNormal);
float rotationAngle = glm::acos(glm::dot(normal_axis, circleNormal));

R = glm::rotate(R, glm::degrees(rotationAngle), rotationAxis);

T = glm::translate(T, pointmid);

glm::mat4 Model = T*R;
Run Code Online (Sandbox Code Playgroud)

Pau*_*oux 5

最简单的方法是使用 Frenet-Serret 框架,通常称为 TBN 框架或 TBN 矩阵。就是这样:

  1. 对曲线上的两个点进行采样。我们称它们为“当前”和“下一个”。
  2. 构建 Frenet 框架如下:

    vec3 T = normalize( next - current );
    vec3 B = normalize( cross( T, next + current ) );
    vec3 N = normalize( cross( B, T ) );
    
    Run Code Online (Sandbox Code Playgroud)
  3. 计算您的 2D 圆,类似于:

    float x = cos( angle );
    float y = sin( angle );
    
    Run Code Online (Sandbox Code Playgroud)
  4. 现在,使用 Frenet 框架计算正确的方向:

    vec3 tangent = T;
    vec3 normal = normalize( B * x + N * y );
    vec3 vertex = current + B * x + N * y; // note: not normalized!
    
    Run Code Online (Sandbox Code Playgroud)

可以在这里找到易于理解的解释: http: //www.blackpawn.com/texts/pqtorus/