using System;
using System.Threading;
using Tao.OpenGl;

namespace Log740
{
    class Sphere : ObjectGL
    {
        public Vecteur3F Color;


        static private System.Threading.Mutex mtx = new Mutex();
        static private Glu.GLUquadric quad;
        static private int count = 0;

        public Sphere(Vecteur3F color)
        {
            this.Color = color;


            if (mtx.WaitOne())
            {
                count++;
                if (count == 1)
                {
                    quad = Glu.gluNewQuadric();
                    Glu.gluQuadricNormals(quad, Glu.GLU_SMOOTH);
                }
            }
        }

        ~Sphere()
        {
            try
            {
                if (mtx.WaitOne())
                {
                    count--;
                    if (count == 0)
                    {
                        Glu.gluDeleteQuadric(quad);
                    }
                }
            }
            catch (AbandonedMutexException)
            {
                count--;
                if (count == 0)
                {
                    Glu.gluDeleteQuadric(quad);
                }
            }
        }

        protected override void DrawInternal()
        {
            Gl.glColor3d(Color.R, Color.G, Color.B);
            Glu.gluSphere(quad, 1, 32, 32);
        }
    }
}
