How does gl lines work




















Between the vertex and the fragment shader there is an optional shader stage called the geometry shader. A geometry shader takes as input a set of vertices that form a single primitive e. The geometry shader can then transform these vertices as it sees fit before sending them to the next shader stage. What makes the geometry shader interesting is that it is able to convert the original primitive set of vertices to completely different primitives, possibly generating more vertices than were initially given.

At the start of a geometry shader we need to declare the type of primitive input we're receiving from the vertex shader. We do this by declaring a layout specifier in front of the in keyword. This input layout qualifier can take any of the following primitive values:. These are almost all the rendering primitives we're able to give to rendering calls like glDrawArrays.

The number within the parenthesis represents the minimal number of vertices a single primitive contains. We also need to specify a primitive type that the geometry shader will output and we do this via a layout specifier in front of the out keyword.

Like the input layout qualifier, the output layout qualifier can take several primitive values:. With just these 3 output specifiers we can create almost any shape we want from the input primitives.

The geometry shader also expects us to set a maximum number of vertices it outputs if you exceed this number, OpenGL won't draw the extra vertices which we can also do within the layout qualifier of the out keyword. To generate meaningful results we need some way to retrieve the output from the previous shader stage. Note that it is declared as an array, because most render primitives contain more than 1 vertex. The geometry shader receives all vertices of a primitive as its input.

Using the vertex data from the vertex shader stage we can generate new data with 2 geometry shader functions called EmitVertex and EndPrimitive. In our case we want to at least generate one line strip primitive. Whenever EndPrimitive is called, all emitted vertices for this primitive are combined into the specified output render primitive.

By repeatedly calling EndPrimitive , after one or more EmitVertex calls, multiple primitives can be generated. This particular case emits two vertices that were translated by a small offset from the original vertex position and then calls EndPrimitive , combining the two vertices into a single line strip of 2 vertices.

Now that you sort of know how geometry shaders work you can probably guess what this geometry shader does. This geometry shader takes a point primitive as its input and creates a horizontal line primitive with the input point at its center. If we were to render this it looks something like this:. Not very impressive yet, but it's interesting to consider that this output was generated using just the following render call:. While this is a relatively simple example, it does show you how we can use geometry shaders to dynamically generate new shapes on the fly.

Later in this chapter we'll discuss a few interesting effects that we can create using geometry shaders, but for now we're going to start with a simple example. To demonstrate the use of a geometry shader we're going to render a really simple scene where we draw 4 points on the z-plane in normalized device coordinates.

The coordinates of the points are:. The vertex shader needs to draw the points on the z-plane so we'll create a basic vertex shader:. And we'll output the color green for all points which we code directly in the fragment shader:. But didn't we already learn to do all this? Yes, and now we're going to spice this little scene up by adding geometry shader magic to the scene.

For learning purposes we're first going to create what is called a pass-through geometry shader that takes a point primitive as its input and passes it to the next shader unmodified:.

By now this geometry shader should be fairly easy to understand. It simply emits the unmodified vertex position it received as input and generates a point primitive. The shader compilation code is the same as the vertex and fragment shaders.

Be sure to check for compile or linking errors! It's exactly the same as without the geometry shader! It's a bit dull, I'll admit that, but the fact that we were still able to draw the points means that the geometry shader works, so now it's time for the more funky stuff!

Drawing points and lines isn't that interesting so we're going to get a little creative by using the geometry shader to draw a house for us at the location of each point. A triangle strip in OpenGL is a more efficient way to draw triangles with fewer vertices. After the first triangle is drawn, each subsequent vertex generates another triangle next to the first triangle: every 3 adjacent vertices will form a triangle.

It might be a Known Issue. Please check with the Issue Tracker at issuetracker. Version: Language English. Scripting API. Suggest a change.

Submission failed For some reason your suggested change could not be submitted. Description Mode for Begin : draw lines. GetKeyDown KeyCode. PushMatrix ; mat. Search Unity. Log in Create a Unity ID. Unity Forum. Forums Quick Links. Unity How do I use GL. Joined: Mar 3, Posts: 1, I am trying to display a line in game view, and saw most people are using GL but I can't get them to display; I know I must be doing something wrong but can't see what it is.

Code CSharp :. PushMatrix ;. SetPass 0 ;. LoadOrtho ;. Begin GL. LINES ;.



0コメント

  • 1000 / 1000