Drawing lines with GLSL

我只是一个虾纸丫 提交于 2019-12-19 04:57:14

问题


is it possible to draw a line with GLSL using GL_TRIANGLES?

The reason why i ask is i'm trying to draw a line with adobes molehill and it only draws triangle.

Cheers


回答1:


if you set edge rendering to be on, just draw a triangle with point 1 and 3 at the same position. It's not efficient, but it works.




回答2:


If you can, use a geometry shader. Pass in two vertices and a line width, generate 4 points forming two triangles by shifting the vertices, render and be done... But drawing wide lines using triangles w/o a geometry shader can also be done in OpenGL ES 2.0 (no geometry shaders) and desktop OpenGL using only vertex shaders:

Generate your vertex data so that you duplicate the vertices for every line (4 vertices per line). For each vertex, pass two extra vec2/vec3 attributes for the vertices of the previous and the next line (for calculating correct line joins) OR simply pass the vec2/vec3 line direction. Also add a float vertex attribute that represents a "normal", a direction in which to shift each vertex. For one endpoint of a line (that now uses two vertices), set the "normal" to one direction and for the duplicate vertex to the opposite direction. Generate triangles of those vertices (via indexing or whatever) and render with GL_TRIANGLES.

In the vertex shader use the vertex and the previous or next vertex to calculate a line direction and (simple in the 2D case) generate a vector perpendicular to it. Multiply that by the "normal" you passed as a vertex attribute. This will shift your lines "to the sides" so that an actual triangle will be generated.

Generating lines that have a correct pixel size is simple in 2D (albeit with uniform scaling, but also possible with non uniform scaling) - use the scale factor from the model-view matrix (e.g. modelView[0][0]) and divide your line width by it in the vertex shader. In 3D, this is a bit more involved...

There is a good explanation of the technique here, animated examples here and source code here.



来源:https://stackoverflow.com/questions/5458185/drawing-lines-with-glsl

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