Coloring polygons using GL.QUADS

Hi,

I’m trying to color loops (polygons drawn with GL.QUADS) and it’s only working when the loop has 4 points.

Here’s a screenshot :

1763760--111704--Capture d’écran 2014-09-06 à 16.33.07.png

When there are 4 points, the loops is fully colored, but when there are more than 4 points, only one part of the loop is colored.

Here’s the function I’ve written :

public void ColorLoop(Loop l){
        // Color loop if closed, bool canColor is true and loops is not inversed
        if (l.isClosed() && l.canColorLoop && !l.isInverse()){
            float nearClip = cam.nearClipPlane + 0.00001f;
            Vector2[] points = l.getPoints();
       
            GL.Begin (GL.QUADS);
            GL.Color (l.getLoopColor());

            for (int i = 0; i < points.Length - 1; i++){
                GL.Vertex(cam.ViewportToWorldPoint(new Vector3(points[i].x, points[i].y, nearClip)));
            }

            GL.End();   
        }
    }

Any idea on how to color all the loop, even when it has more thant 4 points ?

Thanks !

QUADS stands for quadrilaterals, the fifth vertex will be interpreted as the first vertex of the second quad. For drawing polygons, triangulate them and then use GL.TRIANGLES.

Thanks for your answer. I found this Triangulator script : http://wiki.unity3d.com/index.php?title=Triangulator
This script is creating a mesh using a Vector2 array containing all the points of the polygon.

Anyone coud help me with this script and the one I’ve written to draw and color a triangulated polygon using GL.TRIANGLES ? :slight_smile:

This would be the gist of it:

  public void DrawPoly( Vector2[] points)
   {
     int[] indices = new Triangulator(points).Triangulate();

     GL.Begin(GL.TRIANGLES);

     for (int i = 0; i < indices.Length; i++)
     {
       var vertex2d = points[indices[i]];
       GL.Vertex( new Vector3(vertex2d.x, vertex2d.y, 0) );
     }

     GL.End();
   }

Drawing GL primitives this way is slower than using meshes, by the way.