Example #1
0
        private static void updateTransform()
        {
            Point3 normalized = (camera.FocalPosition - camera.Position).Normalize();

            // Forward movement
            if (VirtualKey.VK_KEY_W.IsDown())
            {
                camera.Position      += (normalized * transformMod);
                camera.FocalPosition += (normalized * transformMod);
            }

            // Backwards movement
            if (VirtualKey.VK_KEY_S.IsDown())
            {
                camera.Position      -= (normalized * transformMod);
                camera.FocalPosition -= (normalized * transformMod);
            }

            // Sideways movements
            if (VirtualKey.VK_KEY_A.IsDown())
            {
                Point3 normalizedCopy = normalized.Clone();

                normalizedCopy.X = normalized.Z;
                normalizedCopy.Y = 0;
                normalizedCopy.Z = -normalized.X;
                normalizedCopy   = normalizedCopy.Normalize();

                camera.Position      += (normalizedCopy * transformMod);
                camera.FocalPosition += (normalizedCopy * transformMod);
            }

            if (VirtualKey.VK_KEY_D.IsDown())
            {
                Point3 normalizedCopy = normalized.Clone();

                normalizedCopy.X = -normalized.Z;
                normalizedCopy.Y = 0;
                normalizedCopy.Z = normalized.X;
                normalizedCopy   = normalizedCopy.Normalize();

                camera.Position      += (normalizedCopy * transformMod);
                camera.FocalPosition += (normalizedCopy * transformMod);
            }

            // Vertical movements
            if (VirtualKey.VK_KEY_R.IsDown())
            {
                camera.Position      += new Point3(0, 1, 0) * transformMod;
                camera.FocalPosition += new Point3(0, 1, 0) * transformMod;
            }

            if (VirtualKey.VK_KEY_F.IsDown())
            {
                camera.Position      -= new Point3(0, 1, 0) * transformMod;
                camera.FocalPosition -= new Point3(0, 1, 0) * transformMod;
            }
        }
Example #2
0
		public Triangle (Point3 a, Point3 b, Point3 c, Point3 na, Point3 nb, Point3 nc, Point3 ta, Point3 tb, Point3 tc, Material material) : base(material) {
			this.p0 = a;
			this.p1 = b;
			this.p2 = c;
			if(na == null || nb == null || nc == null) {
				Point3 norm = new Point3();
				Point3.Cross(p1.X-p0.X, p1.Y-p0.Y, p1.Z-p0.Z, p2.X-p0.X, p2.Y-p0.Y, p2.Z-p0.Z, out norm.X, out norm.Y, out norm.Z);
				norm.Normalize();
				this.n0 = this.n1 = this.n2 = norm;
			}
			else {
				this.n0 = na;
				this.n1 = nb;
				this.n2 = nc;
			}
			if(ta == null || tb == null || tc == null) {
				this.t0 = Point3.DummyPoint;
				this.t1 = Point3.DummyPoint;
				this.t2 = Point3.DummyPoint;
				this.bumpx = Point3.DummyPoint;
				this.bumpy = Point3.DummyPoint;
			}
			else {
				this.t0 = ta;
				this.t1 = tb;
				this.t2 = tc;
				double txa = tb.X-ta.X;
				double tya = tb.Y-ta.Y;
				double txb = tc.X-ta.X;
				double tyb = tc.Y-ta.Y;
				double frac = txb*tya-txa*tyb;
				double alpha, beta, gamma;
				if(Math.Abs(frac) >= Maths.GlobalEpsilon) {
					frac = 1.0d/frac;
					alpha = -tyb*frac;
					beta = tya*frac;
					gamma = 1.0d-alpha-beta;
					this.bumpx = new Point3(a.X*gamma+b.X*alpha+c.X*beta,
					                        a.Y*gamma+b.Y*alpha+c.Y*beta,
					                        a.Z*gamma+b.Z*alpha+c.Z*beta);
					this.bumpx.Normalize();
					alpha = txb*frac;
					beta = -txa*frac;
					gamma = 1.0d-alpha-beta;
					this.bumpy = new Point3(a.X*gamma+b.X*alpha+c.X*beta,
					                        a.Y*gamma+b.Y*alpha+c.Y*beta,
					                        a.Z*gamma+b.Z*alpha+c.Z*beta);
					this.bumpy.Normalize();
				}
				else {
					this.bumpx = Point3.DummyPoint;
					this.bumpy = Point3.DummyPoint;
				}
			}
		}
Example #3
0
 public void TestNormalizedRotateLikeZVector()
 {
     Point3 norm, p;
     Random rand = new Random();
     for(int i = 0x00; i < TestParameters.PointTest; i++) {
         double x = 2.0*rand.NextDouble()-1.0d;
         double y = 2.0d*rand.NextDouble()-1.0d;
         double z = 2.0d*rand.NextDouble()-1.0d;
         p = new Point3(x, y, z);
         p.Normalize();
         norm = new Point3(0.0d, 0.0d, 1.0d);
         norm.NormalizedRotateLikeZVector(p.X, p.Y, p.Z);
         Assert.IsTrue(Math.Abs(norm.X-p.X) <= Math.Sqrt(Maths.GlobalEpsilon));
         Assert.IsTrue(Math.Abs(norm.Y-p.Y) <= Math.Sqrt(Maths.GlobalEpsilon));
         Assert.IsTrue(Math.Abs(norm.Z-p.Z) <= Math.Sqrt(Maths.GlobalEpsilon));
     }
 }
Example #4
0
 public static Matrix4 Parse(string toParse)
 {
     if(toParse == null || toParse == string.Empty) {
         return null;
     }
     toParse = toParse.Trim().ToLower();
     Match m = rgx.Match(toParse);
     if(m.Success) {
         if(m.Groups["id"].Captures.Count > 0x00) {
             return new Matrix4();
         }
         else if(m.Groups["rotxyz"].Captures.Count > 0x00) {
             double theta = double.Parse(m.Groups["theta"].Value);
             switch(m.Groups["dim"].Value) {
                 case "x":
                     return Matrix4.CreateRotateXMatrix(theta);
                 case "y":
                     return Matrix4.CreateRotateYMatrix(theta);
                 default :
                     return Matrix4.CreateRotateZMatrix(theta);
             }
         }
         else if(m.Groups["rotv"].Captures.Count > 0x00) {
             double theta = double.Parse(m.Groups["theta"].Value);
             Point3 u = new Point3(double.Parse(m.Groups["ux"].Value), double.Parse(m.Groups["uy"].Value), double.Parse(m.Groups["uz"].Value));
             u.Normalize();
             return CreateRotateMatrix(u, theta);
         }
         else if(m.Groups["shift"].Captures.Count > 0x00) {
             Point3 u = new Point3(double.Parse(m.Groups["ux"].Value), double.Parse(m.Groups["uy"].Value), double.Parse(m.Groups["uz"].Value));
             return CreateShiftMatrix(u);
         }
         else if(m.Groups["scale"].Captures.Count > 0x00) {
             Point3 u;
             if(m.Groups["uy"].Captures.Count > 0x00) {
                 u = new Point3(double.Parse(m.Groups["ux"].Value), double.Parse(m.Groups["uy"].Value), double.Parse(m.Groups["uz"].Value));
             }
             else {
                 double xyz = double.Parse(m.Groups["ux"].Value);
                 u = new Point3(xyz, xyz, xyz);
             }
             return CreateScaleMatrix(u);
         }
         else if(m.Groups["matrix"].Captures.Count > 0x00) {
             double m00 = double.Parse(m.Groups["theta"].Value);
             double m01 = double.Parse(m.Groups["ux"].Value);
             double m02 = double.Parse(m.Groups["uy"].Value);
             double m03 = double.Parse(m.Groups["uz"].Value);
             double m10 = double.Parse(m.Groups["m10"].Value);
             double m11 = double.Parse(m.Groups["m11"].Value);
             double m12 = double.Parse(m.Groups["m12"].Value);
             double m13 = double.Parse(m.Groups["m13"].Value);
             double m20 = double.Parse(m.Groups["m10"].Value);
             double m21 = double.Parse(m.Groups["m11"].Value);
             double m22 = double.Parse(m.Groups["m12"].Value);
             double m23 = double.Parse(m.Groups["m13"].Value);
             return new Matrix4(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23);
         }
     }
     return null;
 }
Example #5
0
		public void RebuildMatrix () {
			if(!this.dirty) {
				return;
			}
			this.dirty = false;
			Point3 forward = this.lookAt-this.position;
			forward.Normalize();
			Point3 right = new Point3(forward.Z, 0.0d, -forward.X);
			right.Normalize();
			Point3 up = Point3.CrossNormalize(forward, right);
			forward.Normalize();
			this.matrix.LoadColumns(right, up, forward);
			this.matrix.RotateZ(this.roll);
			this.matrix.Shift(this.position);
		}