如何创建大纲
How to create outline
我有一组点,呈直线状。
我们如何创建新的点集,这些点将与当前的点集有一定的偏移距离,并使用 GL_TRIANGLE_STRIP 我们将能够创建多边形形状。
这是我当前的代码,但我无法从中得到任何有意义的结果。
// 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 between 2 line segments. To do this, you need to calculate the vectors along the line segments to be connected. Calculate the normalized normal vectors to the line segments. The vector along the miter joint is the sum of the normal vectors. (You can even do this in the vertex shader: OpenGL Line Width)
如果你有3分(p1,p2,p3)
p1 p2
+-----+
\
\
+
p3
那么 p2 中斜接的法向量是:
// 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);
和关节上的点(pa,pb)是:
pa
-----+
p2 / \
-----+ \
/ \
-----+ \
pb \
\
float d = thickness / glm::dot(vm2, vn12);
glm::vec2 pa = p2 + vm2 * d/2;
glm::vec2 pb = p2 - vm2 * d/2;
我有一组点,呈直线状。
我们如何创建新的点集,这些点将与当前的点集有一定的偏移距离,并使用 GL_TRIANGLE_STRIP 我们将能够创建多边形形状。
这是我当前的代码,但我无法从中得到任何有意义的结果。
// 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 between 2 line segments. To do this, you need to calculate the vectors along the line segments to be connected. Calculate the normalized normal vectors to the line segments. The vector along the miter joint is the sum of the normal vectors. (You can even do this in the vertex shader: OpenGL Line Width)
如果你有3分(p1,p2,p3)
p1 p2
+-----+
\
\
+
p3
那么 p2 中斜接的法向量是:
// 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);
和关节上的点(pa,pb)是:
pa
-----+
p2 / \
-----+ \
/ \
-----+ \
pb \
\
float d = thickness / glm::dot(vm2, vn12);
glm::vec2 pa = p2 + vm2 * d/2;
glm::vec2 pb = p2 - vm2 * d/2;