1
\$\begingroup\$

I'm trying to draw a 2D polygon using GL.Line; however I'm not getting a polygon but separated lines instead. In the example below, I would expect a rectangle formed by the spheres:

enter image description here

My code is the following:

void OnPostRender( )
 {
     GL.PushMatrix ( );
     mat.SetPass ( 0 );
     GL.LoadOrtho( );

     GL.Begin( GL.LINES );
     GL.Color( Color.white );

     for ( int i = 0; i < spheres.Length; i++ )
     {
         currentVector = Camera.main.WorldToViewportPoint ( spheres[i].transform.position );
         GL.Vertex3 ( currentVector.x, currentVector.y , 0  );
     }

     GL.End();
     GL.PopMatrix();
 }

Also, I found this tutorial/example but is not exactly what i'm looking for, since in the example, all have the same origin point.

I'm asumming that the correct way of achieving what I want, would be to call the Gl.Vertex3 method once for each of the 2 points that form each line:

  • From A to B (GL.Vertex3 call 2 times)
  • From B to C (GL.Vertex3 call 2 times)
  • From C to D (GL.Vertex3 call 2 times)
  • From D to A (GL.Vertex3 call 2 times)

Is this the right way of doing this?

Thanks in advance.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I don't understand, have you not tried what you're suggesting? It seems like a pretty quick and easy thing to test. I'd suggest you try it and tell us if it's not working and how it's not working. \$\endgroup\$
    – House
    Commented Aug 31, 2015 at 17:39
  • 1
    \$\begingroup\$ If you're trying to draw a polygon, why are you not using GL.Begin( GL.TRIANGLE_STRIPS ) or GL.Begin( GL.QUADS ) instead of GL.Begin( GL.LINES )? (As seen in the docs.) \$\endgroup\$
    – Vaillancourt
    Commented Aug 31, 2015 at 17:54
  • \$\begingroup\$ Thanks, I've tried already and yes, it does the trick. But I wanted to know if there was a better way of doing so. \$\endgroup\$ Commented Sep 1, 2015 at 22:05
  • \$\begingroup\$ @AlexandreVaillancourt I don't know if that would work since the number of points on the polygon may vary (lets say from 3 points to 10 points)... Will I be able to do that by using TRIANGLE or QUADS? Thanks \$\endgroup\$ Commented Sep 1, 2015 at 22:11

1 Answer 1

0
\$\begingroup\$

The GL_LINES primitive type draws a line for each pair of vertices. The lines are not connected, so your assumption of drawing 2 vertices per line segment would be correct.

Unfortunately, Unity only supports the GL_LINES primitive type for line drawing, otherwise the GL_LINE_LOOP primitive would be better suited and your code would already be correct.

Refer to the GL documentation here for a desrcription of the different primitive types.

GL_LINES Treats each pair of vertices as an independent line segment. Vertices 2n-1 and 2n define line n. N/2 lines are drawn.

A better way might be to use GL_QUADS. If you just want the outline with no fill, then set GL.wireframe before calling GL.Begin(). The benefit of using a quad would be that you could turn wireframe on or off to show a filled poly if you want. Also, it only needs 4 vertices instead of 8, so it should map to your existing code with no changes.

You could also use GL_TRIANGLE_STRIP but if you want to display it in wireframe, it will have a diagonal line through the rectangle.

Keep in mind that when using the polygon primitives, the order of the vertices matters. You must specify them in clockwise order in Unity or your shape will not render properly. This doesn't matter if you are using GL_LINES.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks! The number of points on the polygon may vary (let's say from 3 to 10), that's why my approach was to use LINES. For the same reason, I don't know if I could use TRIANGLE or QUAD... I will try with GL_LINE_STRIP \$\endgroup\$ Commented Sep 1, 2015 at 22:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .