OpenGL(ES 2.0)动态更改线宽 [英] OpenGL (ES 2.0) Dynamically Change Line Width
问题描述
目前,我正在使用大量的 GL_LINES
来绘制我的模型。我知道, glLineWidth
将改变所有的半径,但每个半径应该有不同的半径。我想知道它是否可能使用 glLineWidth
(以不同的方式)或另一个功能?我还应该怎么做?
I am currently drawing my model right now using a ton of GL_LINES
all at a uniform radius. I know that glLineWidth
will change the radius of the all the lines but they each should have a different radius. I am wondering if its possible using glLineWidth
(in a different fashion) or another function? How else should I go about it?
推荐答案
将它们渲染成特殊构造的三角形条。具体来说,将每个线段渲染成一对三角形以形成四边形,四边形的长度与线的长度相匹配,宽度与radius匹配。然而,真正的技巧是模拟GL_LINES,因为线段不一定必须彼此连接。要做到这一点,在三角形条的情况下,您需要连接三角形与零区域三角形,这将不会被渲染(除非您的OpenGL ES实现不一致)。
Render them as a specially constructed triangle strip. Specifically, render each line segment as a pair of triangles to form a quadraliterial, with the length of the quad matching the length of the line, and the width matching your "radius". The real trick, however, is simulating GL_LINES, since line segments don't necessarily have to be connected to one another. To do this in the triangle-strip case, you'll need to connect the pairs of triangles with a zero-area triangle, which won't be rendered (unless your OpenGL ES implementation isn't conformant).
例如,假设我们处于2D(为简单起见,3D几乎相同),说您的一个线段有端点(x1,y1)和(x2,y2),而宽度W,如下所示。
将此替换为quad,我们需要确定角的坐标,这需要一点数学。对于合适的2D向量类, vec2
For example, assuming we're in 2D (for simplicity; 3D's almost the same) say one of your line segment has endpoints (x1,y1) and (x2,y2), and a width of W, as shown below.
To replace this with a quad, we need to determine the coordinates of the corners, which requires a bit of math. For a suitable 2D vector class, vec2
vec2 p1(x1, y1);
vec2 p2(x2, y2);
vec2 v = p2 - p1;
v /= v.length(); // make it a unit vector
vec2 vp(-v.y, v.x); // compute the vector perpendicular to v
vec2 v[4];
v[0] = p1 + W/2 * vp;
v[1] = p1 - W/2 * vp;
v[2] = p2 + W/2 * vp;
v[3] = p2 - W/2 * vp;
// Load the v[] array into a vertex-buffer object
这提供了将一条线插入四边形的解决方案。为了将它们连接在一起,我们需要在其中创建具有退化三角形的三角形条。如果我们使用 glDrawElements
绘制,我们可以构造一个简单的索引数组来实现。假设我们将两行转换成四边形,如上所述,给出了顶点v [0] ... v [7]。要使它们成为一个单一的三角形条,请创建一个索引列表
That provides the solution taking a single line into a quad. To connect them together, we need to create the triangle strip with degenerate triangles in it. If we draw this as using glDrawElements
, we can construct a simple index array to do this. Say we converted two lines into quads as described above, giving us vertices v[0] ... v[7]. To make them into a single triangle strip, make an index list of
{ 0, 1, 2, 3, 3, 4, 5, 6, 7 }
通过在列表中重复3两次,我们创建一个新的三角形连接在一起,但不显示。如果您重复四边形的最后一个顶点,则可以将整组GL_LINES渲染为单个三角形条。
by repeating '3' in the list twice, we create a new triangle that connects the others together, yet doesn't show up. If you repeat the last vertex of a quad, you can render your entire set of GL_LINES as a single triangle strip.
这篇关于OpenGL(ES 2.0)动态更改线宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!