在 OpenGL 中绘制任意宽度的连续曲线

发布于 2024-12-10 18:14:12 字数 323 浏览 0 评论 0原文

我希望在 OpenGL 中绘制任意宽度的连续曲线。我正在开发触摸屏,并且通过在旧的 X 和 X 之间绘制线条已经达到了预期的效果。 Y 坐标和新的 X 坐标Y 坐标。当用户在屏幕上滑动手指时,会产生一条跟随手指的线。为了确保线条平滑,我使用了以下内容:

pGL.glEnable(GL10.GL_LINE_SMOOTH); 
pGL.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST);

然而,这本质上意味着我无法控制线条的宽度,因为它始终默认为 1。因此,我得出结论,我将不得不想出一些其他的方法方法来创建这种效果。

I'm looking to draw a continuous and curvy line in OpenGL with an arbitrary width. I am developing for a touch screen and have already achieved the desired effect by drawing lines between the old X & Y coordinates and the new X & Y coordinates. As the user swipes their finger across the screen, this produces a line following their finger. To ensure the line was smooth, I used the following:

pGL.glEnable(GL10.GL_LINE_SMOOTH); 
pGL.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST);

However, this essentially means I cannot control the width of the line, as it always defaults to 1. Therefore, I concluded that I'm going to have to come up with some other way to create this effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2024-12-17 18:14:12

使用四边形条带或三角形条带代替直线,沿笔划居中并具有所需的宽度。

根据注释进行编辑

使用四边形或三角形绘制粗线的方式如下:

模型视图的左上角 3×3 子矩阵是旋转部分。您想要在屏幕空间中绘制具有粗细的线,即反转局部旋转。为此,您需要模型视图旋转的反转。旋转是正交变换,因此逆(M) = 转置(M)

从技术上讲,我们只对反向投影的 Z 轴“Z_local”感兴趣。为此,我们可以简单地采用模型视图矩阵的第三行向量,最后一个元素“w”设置为零,并对整个向量进行归一化,即

Z_local_i = MV_i,3

接下来我们需要线切向量。这就像线段之间的方向一样简单。或者更数学地讲,如果直线由函数 C(t) 描述,则正切为

T=∂C(t)/∂t 

我们现在可以取正切和 Z_local 之间的叉积,得到法线

N(t) = Z_local × T(t)

将法线 N(t) 添加到 C(t) 得到我们平行于 C 的等距曲线。这使我们能够绘制四边形:

for t in range(0, T):
    T = deriv(C(t), t)
    N = cross(Z_local, T)
    emit_vertex(C(t) + width*N)
    emit_vertex(C(t) - width*N)

Instead of a line use a quad strip or a triangle strip, centered along the stroke, and with the desired width.

EDIT due to comment

Drawing a thick line using quads or triangles is done the following way:

The upper left 3×3 submatrix of the modelview is the rotational part. You want to draw the line with thicknes in screen space, i.e. reverse the local rotation. To do this you need the inverse of the modelview rotation. Rotations are orthogonal transformations, so inverse(M) = transpose(M).

Technically we're interested in only the reverse projected Z axis "Z_local". We can simply take the 3rd row vector of the modelview matrix for this, with the last element "w" being set to zero and the whole vector normalized, i.e.

Z_local_i = MV_i,3

Next we need the line tangent vector. This is as simple as the direction between the line segments. Or more mathematically, if the line is described by a function C(t), then the tangent is

T=∂C(t)/∂t 

We can now take the cross product between tangent and Z_local, giving us a normal

N(t) = Z_local × T(t)

Adding the normal N(t) to C(t) gives us a equidistant curve parallel to C. This allows us to draw quads:

for t in range(0, T):
    T = deriv(C(t), t)
    N = cross(Z_local, T)
    emit_vertex(C(t) + width*N)
    emit_vertex(C(t) - width*N)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文