using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Tao.OpenGl;

namespace Log740
{
    class Cube : ObjectGL
    {
        private static Vecteur3F[] Vertices = {new Vecteur3F(1, 1, 1),
                                               new Vecteur3F(1, -1, 1),
                                               new Vecteur3F(-1, -1, 1),
                                               new Vecteur3F(-1, 1, 1),
                                               new Vecteur3F(1, 1, -1),
                                               new Vecteur3F(1, -1, -1),
                                               new Vecteur3F(-1, -1, -1),
                                               new Vecteur3F(-1, 1, -1)};
        private static int[][] Faces = {new int[]{ 0, 1, 2 }, new int[]{ 2, 3, 0 },
                                       new int[]{ 0, 1, 5 }, new int[]{ 5, 4, 0 },
                                       new int[]{ 1, 2, 6 }, new int[]{ 6, 5, 1 },
                                       new int[]{ 2, 3, 7 }, new int[]{ 7, 6, 2 },
                                       new int[]{ 3, 0, 4 }, new int[]{ 4, 7, 3 },
                                       new int[]{ 4, 5, 6 }, new int[]{ 6, 7, 4 }};
        public Vecteur3F Color;

        public Cube(Vecteur3F color)
        {
            this.Color = color;
        }

        protected override bool CollisionInternal(Vecteur3F point, Vecteur3F vitesse)
        {
            Vecteur3F p1 = point - vitesse;

            foreach (int[] f in Faces)
            {
                if (Vecteur3F.LineIntersectsTriangle(p1, point, Vertices[f[0]], Vertices[f[1]], Vertices[f[2]]))
                {
                    return true;
                }
            }

            return false;
        }

        protected override void DrawInternal()
        {
            Gl.glColor3d(Color.R, Color.G, Color.B);
            Gl.glBegin(Gl.GL_QUADS);                                            // Draw A Quad

            Gl.glNormal3f(0, 1, 0);
            Gl.glVertex3f(1, 1, -1);                                        // Top Right Of The Quad (Top)
            Gl.glVertex3f(-1, 1, -1);                                       // Top Left Of The Quad (Top)
            Gl.glVertex3f(-1, 1, 1);                                        // Bottom Left Of The Quad (Top)
            Gl.glVertex3f(1, 1, 1);                                         // Bottom Right Of The Quad (Top)

            Gl.glNormal3f(0, -1, 0);
            Gl.glVertex3f(1, -1, 1);                                        // Top Right Of The Quad (Bottom)
            Gl.glVertex3f(-1, -1, 1);                                       // Top Left Of The Quad (Bottom)
            Gl.glVertex3f(-1, -1, -1);                                      // Bottom Left Of The Quad (Bottom)
            Gl.glVertex3f(1, -1, -1);                                       // Bottom Right Of The Quad (Bottom)

            Gl.glNormal3f(0, 0, 1);
            Gl.glVertex3f(1, 1, 1);                                         // Top Right Of The Quad (Front)
            Gl.glVertex3f(-1, 1, 1);                                        // Top Left Of The Quad (Front)
            Gl.glVertex3f(-1, -1, 1);                                       // Bottom Left Of The Quad (Front)
            Gl.glVertex3f(1, -1, 1);                                        // Bottom Right Of The Quad (Front)

            Gl.glNormal3f(0, 0, -1);
            Gl.glVertex3f(1, -1, -1);                                       // Top Right Of The Quad (Back)
            Gl.glVertex3f(-1, -1, -1);                                      // Top Left Of The Quad (Back)
            Gl.glVertex3f(-1, 1, -1);                                       // Bottom Left Of The Quad (Back)
            Gl.glVertex3f(1, 1, -1);                                        // Bottom Right Of The Quad (Back)

            Gl.glNormal3f(-1, 0, 0);
            Gl.glVertex3f(-1, 1, 1);                                        // Top Right Of The Quad (Left)
            Gl.glVertex3f(-1, 1, -1);                                       // Top Left Of The Quad (Left)
            Gl.glVertex3f(-1, -1, -1);                                      // Bottom Left Of The Quad (Left)
            Gl.glVertex3f(-1, -1, 1);                                       // Bottom Right Of The Quad (Left)

            Gl.glNormal3f(1, 0, 0);
            Gl.glVertex3f(1, 1, -1);                                        // Top Right Of The Quad (Right)
            Gl.glVertex3f(1, 1, 1);                                         // Top Left Of The Quad (Right)
            Gl.glVertex3f(1, -1, 1);                                        // Bottom Left Of The Quad (Right)
            Gl.glVertex3f(1, -1, -1);                                       // Bottom Right Of The Quad (Right)
            Gl.glEnd();                                                         // Done Drawing The Quad            
        }
    }
}
