Beispiel #1
0
        // __ Draw ____________________________________________________________


        private bool DrawScene()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);        // Clear The Screen And The Depth Buffer

            Gl.glLoadIdentity();

            Vect3f eye = CalcEye(mElevation, mRotation);

            double[] projBefore = GetProjection();

            Glu.gluLookAt(eye.X, eye.Y, eye.Z, 0, 0, 0, 0, 1, 0);

            double[] projAfter = GetProjection();

            Gl.glPushMatrix();
            Gl.glTranslatef(mTranslation.X, mTranslation.Y, mTranslation.Z);

            if (DrawGlScene != null)
            {
                DrawGlScene(this, EventArgs.Empty);
            }
            if (mDrawAxis)
            {
                DoDrawAxis();
            }

            Gl.glPopMatrix();

            return(true);
        }
Beispiel #2
0
        public BoundingBox GetBoundingBox(Vect3f pos)
        {
            BoundingBox b      = ((Mesh)mMeshes[0]).GetBoundingBox();
            BoundingBox result = new BoundingBox(b.MinPoint + pos, b.MaxPoint + pos);

            return(result);
        }
Beispiel #3
0
 public void crossProd(Vect3f v1, Vect3f v2)
 {
     X = v1.Y * v2.Z - v1.Z * v2.Y;
     Y = v1.Z * v2.X - v1.X * v2.Z;
     Z = v1.X * v2.Y - v1.Y * v2.X;
     Normalize();
 }
Beispiel #4
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            mMouseButtons = e.Button;
            mBeginX       = e.X;
            mBeginY       = e.Y;

            if (e.Button == MouseButtons.Left)
            {
                // Select object
                Vect3f p = Get3DPoint(e.X, e.Y);
                mCurrentPoint   = p;
                mCurrentPoint.Y = -mCurrentPoint.Y;

                mEye             = CalcEye(mElevation, mRotation);
                mCurrentPoint    = mCurrentPoint - mEye;
                mCurrentPoint.X *= 1000;
                mCurrentPoint.Y *= 1000;
                mCurrentPoint.Z *= 1000;

                // DAVE: launch event for object selection here.
                // Should be used together with Object3D

                Invalidate();
            }
        }
Beispiel #5
0
        private void MotionTranslate(int x, int y)
        {
            Vect3f origin      = Get3DPoint(mBeginX, mBeginY);
            Vect3f destination = Get3DPoint(x, y);

            mTranslation.X += (destination.X - origin.X);
            mTranslation.Y -= (destination.Y - origin.Y);
            mTranslation.Z += (destination.Z - origin.Z);
        }
Beispiel #6
0
        public Vect3f Minus(Vect3f v1)
        {
            Vect3f result = new Vect3f(X, Y, Z);

            result.X -= v1.X;
            result.Y -= v1.Y;
            result.Z -= v1.Z;
            return(result);
        }
Beispiel #7
0
        public bool ContainsPoint(Vect3f pos, Vect3f point)
        {
            Vect3f min = MinPoint + pos;
            Vect3f max = MaxPoint + pos;

            return
                ((point.X <= max.X) && (point.X >= min.X) &&
                 (point.Y <= max.Y) && (point.Y >= min.Y) &&
                 (point.Z <= max.Z) && (point.Z >= min.Z));
        }
Beispiel #8
0
 public bool ContainsPoint(Vect3f pos, Vect3f point)
 {
     foreach (Mesh m in mMeshes)
     {
         if (m.GetBoundingBox().ContainsPoint(pos, point))
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #9
0
        public BoundingBox GetBoundingBox()
        {
            if (mBoundingBox != null)
            {
                return(mBoundingBox);
            }

            Vect3f min = new Vect3f(float.MaxValue, float.MaxValue, float.MaxValue);
            Vect3f max = new Vect3f(float.MinValue, float.MinValue, float.MinValue);


            Vect3f p = new Vect3f(0, 0, 0);

            for (int i = 0; i < Vertexes.Length - 3; i += 3)
            {
                p.X = Vertexes[i];
                p.Y = Vertexes[i + 1];
                p.Z = Vertexes[i + 2];

                if (p.X < min.X)
                {
                    min.X = p.X;
                }
                if (p.Y < min.Y)
                {
                    min.Y = p.Y;
                }
                if (p.Z < min.Z)
                {
                    min.Z = p.Z;
                }

                if (p.X > max.X)
                {
                    max.X = p.X;
                }
                if (p.Y > max.Y)
                {
                    max.Y = p.Y;
                }
                if (p.Z > max.Z)
                {
                    max.Z = p.Z;
                }
            }

            mBoundingBox = new BoundingBox(min, max);

            return(mBoundingBox);
        }
Beispiel #10
0
        public void crossProd(Vect3f v1, Vect3f v2, Vect3f v3)
        {
            float x1, y1, z1, x2, y2, z2;

            x1 = v3.X - v2.X;
            y1 = v3.Y - v2.Y;
            z1 = v3.Z - v2.Z;

            x2 = v1.X - v2.X;
            y2 = v1.Y - v2.Y;
            z2 = v1.Z - v2.Z;

            X = y1 * z2 - y2 * z1;
            Y = x2 * z1 - x1 * z2;
            Z = x1 * y2 - x2 * y1;
            Normalize();
        }
Beispiel #11
0
        private void SetAxisAngle(Vect3f axis, float angle)
        {
            axis.Normalize();
            float norm = axis.Length();

            if (norm < 1E-8)
            {
                q[0] = 0.0f;
                q[1] = 0.0f;
                q[2] = 0.0f;
                q[3] = 1.0f;
            }
            else
            {
                float sin_half_angle = (float)Math.Sin(angle / 2.0);
                q[0] = sin_half_angle * axis.X / norm;
                q[1] = sin_half_angle * axis.Y / norm;
                q[2] = sin_half_angle * axis.Z / norm;
                q[3] = (float)Math.Cos(angle / 2.0);
            }
        }
Beispiel #12
0
        private Vect3f Get3DPoint(int x, int y)
        {
            double dX, dY, dZ;

            double[] modelview  = new double[16];
            double[] projmatrix = new double[16];
            int[]    viewport   = new int[4];

            Gl.glLoadIdentity();
            Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
            Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projmatrix);

            Vect3f eye = CalcEye(-mElevation, mRotation);

            Glu.gluLookAt(eye.X, eye.Y, eye.Z, 0, 0, 0, 0, 1, 0);

            // get the current modelview matrix
            Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, modelview);

            Glu.gluUnProject((double)x, (double)y, 0.996, modelview, projmatrix, viewport,
                             out dX, out dY, out dZ);

            return(new Vect3f((float)dX, (float)(dY), (float)(dZ)));
        }
Beispiel #13
0
 public BoundingBox(Vect3f min, Vect3f max)
 {
     MinPoint = min;
     MaxPoint = max;
 }
Beispiel #14
0
        public Vect3f IntersectsWithRay(Vect3f origin, Vect3f dir, Vect3f pos)
        {
            Vect3f minB, maxB;
            Vect3f coord;                               /* hit point */

            bool      bInside = true;
            Quadrants quadrantX, quadrantY, quadrantZ;
            int       i;
            int       whichPlane;

            float[] maxT           = new float[3];
            float[] candidatePlane = new float[3];


            BoundingBox box = GetBoundingBox(pos);

            minB = box.MinPoint;
            maxB = box.MaxPoint;

            /* Find candidate planes; this loop can be avoided if
             * rays cast all from the eye(assume perpsective view) */
            FindCandidatePlanes(origin.X, minB.X, maxB.X, ref bInside, out quadrantX, ref candidatePlane[0]);
            FindCandidatePlanes(origin.Y, minB.Y, maxB.Y, ref bInside, out quadrantY, ref candidatePlane[1]);
            FindCandidatePlanes(origin.Z, minB.Z, maxB.Z, ref bInside, out quadrantZ, ref candidatePlane[2]);

            /* Ray origin inside bounding box */
            if (bInside)
            {
                coord = origin;
                return(coord);
            }

            /* Calculate T distances to candidate planes */
            CalculateDistance(quadrantX, dir.X, candidatePlane[0], origin.X, ref maxT[0]);
            CalculateDistance(quadrantY, dir.Y, candidatePlane[1], origin.Y, ref maxT[1]);
            CalculateDistance(quadrantZ, dir.Z, candidatePlane[2], origin.Z, ref maxT[2]);

            /* Get largest of the maxT's for final choice of intersection */
            whichPlane = 0;
            for (i = 1; i < 3; i++)
            {
                if (maxT[whichPlane] < maxT[i])
                {
                    whichPlane = i;
                }
            }

            /* Check final candidate actually inside box */
            if (maxT[whichPlane] < 0.0)
            {
                return(null);
            }


            coord = new Vect3f(0, 0, 0);
            if (!CheckPlane(0, whichPlane, maxT, origin.X, dir.X, minB.X, maxB.X, candidatePlane, ref coord.X))
            {
                return(null);
            }

            if (!CheckPlane(1, whichPlane, maxT, origin.Y, dir.Y, minB.Y, maxB.Y, candidatePlane, ref coord.Y))
            {
                return(null);
            }

            if (!CheckPlane(2, whichPlane, maxT, origin.Z, dir.Z, minB.Z, maxB.Z, candidatePlane, ref coord.Z))
            {
                return(null);
            }

            return(coord);
        }
Beispiel #15
0
 public static float AngleBetweenVectors(Vect3f v1, Vect3f v2)
 {
     return(RadiansToDegrees(
                (float)Math.Acos((v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z) /
                                 (v1.Length() * v2.Length()))));
 }
Beispiel #16
0
 public Quaternion(Vect3f axis, float angle)
 {
     SetAxisAngle(axis, angle);
 }
Beispiel #17
0
 public Vect3f(Vect3f copy)
 {
     this.X = copy.X;
     this.Y = copy.Y;
     this.Z = copy.Z;
 }