openGL:带着色器的行 [英] openGL: lines with shaders

查看:215
本文介绍了openGL:带着色器的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用着色器创建线条(可能有色)?我使用可编程管道,我是一个初学者openGL。我找不到一个例子,如何用着色器绘制线。我想我必须加载一个VAO(顶点数组对象)到着色器,但然后什么?我应该使用什么函数和如何?

How would I create a line (possibly colored) with shaders? I'm using programmable pipeline and I'm a beginner with openGL. I can't find an example on how to draw lines with shaders.. I suppose I have to load a VAO (vertices array object) into the shader, but then what? What functions should I use and how?

推荐答案

首先,设置使用shaderprogram。然后使用 glDrawArrays (或如果您的数据已建立索引,则为元素)使用mode = GL_LINES或其他线条绘制模式之一。

First, set use the shaderprogram. Then draw lines using glDrawArrays (or Elements if your data is indexed) with mode=GL_LINES or one of the other line drawing modes.

这里有一个代码示例,2D线具有不同的颜色在每一端。如果阴影模式设置为平滑,OpenGL将插入沿线的颜色。

Here's a code example for 2D lines with different color in each end. If shading mode is set to smooth, OpenGL will interpolate the colors along the line.

struct LineSegment_t
{
  float x1, y1;
  float r1,g1,b1,a1;
  float x2, y2;
  float r2,g2,b2,a2;
};

int num_verts = lines.size()*2;
glBindVertexArray( line_vao ); // setup for the layout of LineSegment_t
glBindBuffer(GL_ARRAY_BUFFER, LineBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(LineSegment_t)/2 * num_verts, &lines[0], GL_DYNAMIC_DRAW);
glDrawArrays(GL_LINES, 0, num_verts );

如果您需要更多的灵活性,可以使用三角形通过创建一个矩形线端点。在2D中,您可以通过使用正常/垂直线(-y,x)的所需线翻译终点来创建4个点。在3D中,您需要确保三角形与相机对齐,如在广告牌中。

If you need more flexibility, you can draw lines using triangles by creating a rectangle (4 points) from the line endpoints. In 2D you can create the 4 points by translating the endpoints using the line normal / perpendicular (-y,x) by the desired line with. In 3D you need to make sure the triangles are aligned to the camera as in billboarding.

这篇关于openGL:带着色器的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆