OpenGL (ES 2.0) Dynamically Change Line Width

久未见 提交于 2019-12-10 09:44:13

问题


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?


回答1:


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.


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.



来源:https://stackoverflow.com/questions/14514543/opengl-es-2-0-dynamically-change-line-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!