c++ - 如何创建轮廓

标签 c++ opengl glm-math

我有一组点,呈直线形状。

enter image description here

我们如何创建与当前点集有一定偏移距离的新点集,并使用 GL_TRIANGLE_STRIP 我们将能够创建多边形形状。

enter image description here

这是我当前的代码,但我无法从中获得任何有意义的结果。

// outlineVertices are existing set of points from which we would generate the offsetPoints

for (int i = 0; i < outlineVertices.size() - 3 ; i += 3) {
        finalVertices.push_back(outlineVertices[i]);
        finalVertices.push_back(outlineVertices[i + 1]);
        finalVertices.push_back(outlineVertices[i + 2]);        

        glm::vec3 point1 = glm::vec3(outlineVertices[i], outlineVertices[i + 1], outlineVertices[i + 2]);
        glm::vec3 point2 = glm::vec3(outlineVertices[i + 7], outlineVertices[i + 1 + 7], outlineVertices[i + 2 + 7]);
        glm::vec4 directionVector = GetPerpendicularVectorDirection(point1, point2);
    
        finalVertices.push_back(outlineVertices[i] - (directionVector.x * outlineWidth ));
        finalVertices.push_back(outlineVertices[i + 1] + (directionVector.y * outlineWidth));
        finalVertices.push_back(outlineVertices[i + 2]);
        finalVertices.push_back(outlineVertices[i + 3]);
        
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

glm::vec4 RectangleOutline::GetPerpendicularVectorDirection(glm::vec3 point1, glm::vec3 point2) {

    glm::vec3 Direction = glm::normalize(point1 - point2);
    float x, y;
    x = Direction.x;
    Direction.x = -y;
    Direction.y = x;
    return glm::vec4(Direction, 0);
}

最佳答案

您必须计算 Miter joint 2 条线段之间。为此,您需要计算沿要连接的线段的 vector 。计算线段的归一化法 vector 。沿斜接关节的 vector 是法向 vector 之和。 (您甚至可以在顶点着色器中执行此操作:OpenGL Line Width)

如果您有 3 分(p1p2p3)

p1       p2
  +-----+
         \
          \
           +
             p3

那么p2中沿斜接关节的法向 vector 为:

// vectors along the line
glm::vec2 v12 = p2 - p1;
glm::vec2 v23 = p3 - p2;

// normalized normal vectors to the line segments
glm::vec2 vn12 = glm::normlalize(glm::vec2(-v12.y, v12.x));
glm::vec2 vn23 = glm::normlalize(glm::vec2(-v23.y, v23.x));

// normalized vector along miter joint
glm::vec2 vm2 = glm::normalize(vn12 + vn23);

关节上的点(papb)为:

          pa
    -----+
     p2 / \
  -----+    \
      / \
-----+    \
  pb  \
        \
float d = thickness / glm::dot(vm2, vn12);

glm::vec2 pa = p2 + vm2 * d/2;
glm::vec2 pb = p2 - vm2 * d/2;

关于c++ - 如何创建轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68973103/

相关文章:

c++ - 在编译的二进制文件中包含 CSV 文件

c++ - 使用 glm 创建 View 矩阵

c++ - 从正交投影切换到透视投影

c++ - 如何在各种标志处切断绳子

c++ - 如何在 CLion 中设置 -v 编译器选项并查看相应结果

c++ - 在类中渲染 OpenGL 对象?

opengl - OpenCL 中等效的 GLSL 统一变量

opengl - GL_TEXTURE_3D 颜色和模板 FBO 附件

c++ - OpenGL GLI 不支持 VS2015?

java - OOP 术语 : "Container" & "Collection"