Click here to Skip to main content
15,896,359 members
Articles / Multimedia / GDI+

Quaternion Mathematics and 3D Library with C# and GDI+

Rate me:
Please Sign up or sign in to vote.
4.91/5 (42 votes)
1 Jun 2009CPOL3 min read 118.1K   7.3K   96  
Rotating 3D objects using Quaternion and Drawing 3D objects with GDI+ Graphics
using System;
using System.Collections.Generic;
using System.Drawing;

namespace YLScsDrawing.Drawing3d
{
    public class Cuboid : Shape3d
    {
        bool drawingLine = false, fillingFace = true, drawingImage = false;
        public bool DrawingLine
        {
            set { drawingLine = value; }
            get { return drawingLine; }
        }

        public bool FillingFace
        {
            set { fillingFace = value; }
            get { return fillingFace; }
        }

        public bool DrawingImage
        {
            set { drawingImage = value; }
            get { return drawingImage; }
        }

        Color[] faceColor = new Color[6] { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Purple };
        public Color[] FaceColorArray
        {
            set
            {
                int n = Math.Min(value.Length, faceColor.Length);
                for (int i = 0; i < n; i++)
                    faceColor[i] = value[i];
            }
            get { return faceColor; }
        }

        Bitmap[] bmp = new Bitmap[6];
        public Bitmap[] FaceImageArray
        {
            set
            {
                int n = Math.Min(value.Length, 6);
                for (int i = 0; i < n; i++)
                    bmp[i] = value[i];
                setupFilter();
            }
            get { return bmp; }
        }

        YLScsDrawing.Imaging.Filters.FreeTransform[] filters =
            new YLScsDrawing.Imaging.Filters.FreeTransform[6];
        private void setupFilter()
        {
            for (int i = 0; i < 6; i++)
            {
                filters[i] = new YLScsDrawing.Imaging.Filters.FreeTransform();
                filters[i].Bitmap = bmp[i];
            }
        }

        public Cuboid(double a, double b, double c)
        {
            center = new Point3d(a / 2, b / 2, c / 2);
            pts[0] = new Point3d(0, 0, 0);
            pts[1] = new Point3d(a, 0, 0);
            pts[2] = new Point3d(a, b, 0);
            pts[3] = new Point3d(0, b, 0);
            pts[4] = new Point3d(0, 0, c);
            pts[5] = new Point3d(a, 0, c);
            pts[6] = new Point3d(a, b, c);
            pts[7] = new Point3d(0, b, c);
        }

        
        public override void Draw(Graphics g, Camera cam)
        {
            PointF[] pts2d = cam.GetProjection(pts);

            PointF[][] face = new PointF[6][];
            face[0] = new PointF[] { pts2d[0], pts2d[1], pts2d[2], pts2d[3] };
            face[1] = new PointF[] { pts2d[5], pts2d[1], pts2d[0], pts2d[4] };
            face[2] = new PointF[] { pts2d[1], pts2d[5], pts2d[6], pts2d[2] };
            face[3] = new PointF[] { pts2d[2], pts2d[6], pts2d[7], pts2d[3] };
            face[4] = new PointF[] { pts2d[3], pts2d[7], pts2d[4], pts2d[0] };
            face[5] = new PointF[] { pts2d[4], pts2d[7], pts2d[6], pts2d[5] };

            for (int i = 0; i < 6; i++)
            {
                bool isout = false;
                for (int j = 0; j < 4; j++)
                {
                    if (face[i][j] == new PointF(float.MaxValue, float.MaxValue))
                    {
                        isout = true;
                        continue;
                    }
                }
                if (!isout)
                {
                    if (drawingLine) g.DrawPolygon(new Pen(lineColor), face[i]);
                    if (YLScsDrawing.Geometry.Vector.IsClockwise(face[i][0], face[i][1], face[i][2])) // the face can be seen by camera
                    {
                        if (fillingFace) g.FillPolygon(new SolidBrush(faceColor[i]), face[i]);
                        if (drawingImage && bmp[i] != null)
                        {
                            filters[i].FourCorners = face[i];
                            g.DrawImage(filters[i].Bitmap, filters[i].ImageLocation);
                        }
                    }
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions