#region Using directives

using System;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Math;
using OpenTK.Input;
using OpenTK.Platform;

#endregion

namespace DataProcessing
{
    public class Drawing
    {
        //vykresleni stredoveho bodu
        public static void DrawCentrePoint()
        {            
            GL.Color3(0.2, 0.2, 0.2);
            GL.PointSize(2.0f);
            GL.Begin(BeginMode.Points);
            GL.Vertex3(0.0, 0.0, -1.0);
            GL.End();
            GL.PointSize(1.0f);
        }

        //vykresleni mrizky
        public static void DrawGrid()
        {
            GL.Color3(0.3, 0.3, 0.3);
            GL.Begin(BeginMode.Lines);
            for (int i = -1000; i <= 1000; i += 100)
            {
                GL.Vertex3(i, 0, -1000);
                GL.Vertex3(i, 0, 1000);
                GL.Vertex3(1000, 0, i);
                GL.Vertex3(-1000, 0, i);
            }
            GL.End();
           
            DrawNumbers();
        }

        //vykresleni os
        public static void DrawWorldAxes(float xRotation, float yRotation, float radius, float ratio, int width, int height, string type)
        {
            float coord = radius / 30;
            float coordLine = coord * 0.8f;  
            float charX = radius / 600;
            float charY = radius / 300;
            float translateX = (ratio * radius / 3.7f / width)*(width - 100f);
            float translateY = radius * 0.2f;

            float cosYrotation = (float)(Math.Cos(Functions.DegreesToRadians(yRotation)));
            float sinYrotation = (float)(Math.Sin(Functions.DegreesToRadians(yRotation)));
            float cosXrotation = (float)(Math.Cos(Functions.DegreesToRadians(xRotation)));
            float sinXrotation = (float)(Math.Sin(Functions.DegreesToRadians(xRotation)));
           
            GL.MatrixMode(MatrixMode.Projection);  
            GL.LoadIdentity();  
            GL.Ortho(-ratio * radius / 3.7, ratio * radius / 3.7, -radius / 3.7, radius / 3.7, -100000.0, 100000.0);
            
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();            

            GL.PushMatrix();
            GL.Translate(-translateX + cosYrotation * coord, -translateY + sinYrotation * sinXrotation * coord, 0.0f);

            GL.Color3(1.0, 0.0, 0.0);
            GL.Begin(BeginMode.Lines);
            GL.Vertex3(-charX, -charY, 0.0);
            GL.Vertex3(charX, charY, 0.0);
            GL.Vertex3(-charX, charY, 0.0);
            GL.Vertex3(charX, -charY, 0.0);
            GL.End();
            GL.PopMatrix();

            GL.Color3(0.0, 1.0, 0.0);
            GL.PushMatrix();
            GL.Translate(-translateX, -translateY + cosXrotation * coord, 0.0f);
            GL.Begin(BeginMode.Lines);
            GL.Vertex3(0.0, -charY, 0.0);
            GL.Vertex3(0.0, 0.0, 0.0);
            GL.Vertex3(0.0, 0.0, 0.0);
            GL.Vertex3(charX, charY, 0.0);
            GL.Vertex3(0.0, 0.0, 0.0);
            GL.Vertex3(-charX, charY, 0.0);
            GL.End();
            GL.PopMatrix();

            GL.Color3(0.0, 0.0, 1.0);
            GL.PushMatrix();
            GL.Translate(-translateX + sinYrotation * coord, -translateY - cosYrotation * sinXrotation * coord, 0.0f);
            GL.Begin(BeginMode.LineStrip);
            GL.Vertex3(charX, -charY, 0.0);
            GL.Vertex3(-charX, -charY, 0.0);            
            GL.Vertex3(charX, charY, 0.0);
            GL.Vertex3(-charX, charY, 0.0);
            GL.End(); 
            GL.PopMatrix();
            
            GL.PushMatrix();
            GL.Translate(-translateX, -translateY, 0.0f);
            GL.Rotate(xRotation, 1.0, 0.0, 0.0);
            GL.Rotate(yRotation, 0.0, 1.0, 0.0);

            GL.LineWidth(1.0f);
            GL.Begin(BeginMode.Lines);
            GL.Color3(1.0, 0.0, 0.0);
            GL.Vertex3(0.0, 0.0, 0.0);
            GL.Vertex3(coordLine, 0.0, 0.0);            

            GL.Color3(0.0, 1.0, 0.0);
            GL.Vertex3(0.0, 0.0, 0.0);
            GL.Vertex3(0.0, coordLine, 0.0);            

            GL.Color3(0.0, 0.0, 1.0);
            GL.Vertex3(0.0, 0.0, 0.0);
            GL.Vertex3(0.0, 0.0, coordLine);
           
            GL.End();
            GL.PopMatrix();

            if (type == "perspective")
            {
                GL.MatrixMode(MatrixMode.Projection);  
                GL.LoadIdentity();  
                Glu.Perspective(30.0, ratio, 1.0, 100000.0);

                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
            }
        }

        //vykresleni cisel
        public static void DrawNumbers()
        {
            for (int i = 100; i <= 900; i += 100)
            {
                DrawChar(i + 10.0f, 0.0f, 10.0f, (i / 100).ToString());
                DrawChar(i + 30.0f, 0.0f, 10.0f, "m");
            }

            for (int i = 100; i <= 900; i += 100)
            {
                DrawChar(10.0f, 0.0f, -(i + 30.0f), (i / 100).ToString());
                DrawChar(30.0f, 0.0f, -(i + 30.0f), "m");
            }
        }

        //vykresleni znaku
        public static void DrawChar(float X, float Y, float Z, string Char)
        {
            GL.Translate(X, Y, Z);
            switch (Char)
            {
                case "0":
                    GL.Begin(BeginMode.LineLoop);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.End();
                    break;

                case "1":
                    GL.Begin(BeginMode.Lines);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.End();
                    break;

                case "2":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.End();
                    break;

                case "3":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.End();
                    GL.Begin(BeginMode.Lines);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.End();
                    break;

                case "4":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.End();
                    GL.Begin(BeginMode.Lines);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.End();
                    break;

                case "5":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.End();
                    break;

                case "6":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.End();
                    break;

                case "7":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.End();
                    break;

                case "8":
                    GL.Begin(BeginMode.LineLoop);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.End();
                    break;

                case "9":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.Vertex3(9.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 10.0);
                    GL.End();
                    break;

                case "m":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(0.0, 0.0, 20.0);
                    GL.Vertex3(0.0, 0.0, 12.0);
                    GL.Vertex3(2.0, 0.0, 10.0);
                    GL.Vertex3(7.0, 0.0, 10.0);
                    GL.Vertex3(9.0, 0.0, 12.0);
                    GL.Vertex3(9.0, 0.0, 20.0);
                    GL.Vertex3(9.0, 0.0, 12.0);
                    GL.Vertex3(11.0, 0.0, 10.0);
                    GL.Vertex3(16.0, 0.0, 10.0);
                    GL.Vertex3(18.0, 0.0, 12.0);
                    GL.Vertex3(18.0, 0.0, 20.0);
                    GL.End();
                    break;

                case "X":
                    GL.Begin(BeginMode.Lines);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(5.0, 0.0, 10.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(5.0, 0.0, 0.0);
                    GL.End();
                    break;

                case "Y":
                    GL.Begin(BeginMode.Lines);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(2.5, 0.0, 5.0);
                    GL.Vertex3(2.5, 0.0, 5.0);
                    GL.Vertex3(5.0, 0.0, 0.0);
                    GL.Vertex3(2.5, 0.0, 10.0);
                    GL.Vertex3(2.5, 0.0, 5.0);
                    GL.End();
                    break;

                case "Z":
                    GL.Begin(BeginMode.LineStrip);
                    GL.Vertex3(0.0, 0.0, 0.0);
                    GL.Vertex3(5.0, 0.0, 0.0);
                    GL.Vertex3(0.0, 0.0, 10.0);
                    GL.Vertex3(5.0, 0.0, 10.0);
                    GL.End();
                    break;

                default:
                    break;
            }
            GL.Translate(-X, -Y, -Z);
        }
    }
}
