- Home /
Draw 2D polygon
Hi there.
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:
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();
}
What am I doing wrong?
Am I supposed to use GL.Vertex for each pair of points (A, B, B, C, C, D, D, A)? As you can see in my code, right now, I'm only calling GL.Vertex for each point (A,B,C,D)
Thanks in advance!
screeshot01.png
(15.5 kB)
Comment
Answer by jrobertomar · Aug 31, 2015 at 05:50 PM
Ok, it seems to work by using GL.Vertex ( ) for each of the two points that form each line:
From A to B
From B to C
From C to D
From D to A
Which results in using the GL.Vertex ( ) method 8 times. Is this the correct way?
Anyone?