OpenGL(ES 2.0)动态更改线宽(OpenGL (ES 2.0) Dynamically Change Line Width)

编程入门 行业动态 更新时间:2024-10-24 23:25:22
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?

最满意答案

将它们渲染为特制的三角形条。 具体而言,将每个线段渲染为一对三角形以形成四边形,四边形的长度与线的长度匹配,并且宽度与您的“半径”相匹配。 然而,真正的技巧是模拟GL_LINES,因为线段不一定必须相互连接。 要在三角形条的情况下做到这一点,您需要将三角形对与一个零区三角形连接起来,该三角形不会被渲染(除非您的OpenGL ES实现不符合)。

例如,假设我们处于2D(为简单起见,3D几乎相同),则说明您的线段中有一个线段具有端点(x1,y1)和(x2,y2),宽度为W,如下所示。 线段 要用四元组替换它,我们需要确定拐角的坐标,这需要一些数学运算。 对于一个合适的2D矢量类, 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]。 要将它们制作为单个三角形条,请创建一个索引列表

{ 0, 1, 2, 3, 3, 4, 5, 6, 7 }

通过在列表中重复'3'两次,我们创建了一个新的三角形,将其他三个连接在一起,但没有显示出来。 如果重复四边形的最后一个顶点,则可以将整组GL_LINES渲染为单个三角形条。

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).

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.line segment 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

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 }

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.

更多推荐

本文发布于:2023-07-30 01:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   ES   OpenGL   Width   Line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!