示例#1
0
 public Matrix(Vector right, Vector up, Vector forward)
 {
     m00 = right.X;
     m10 = right.Y;
     m20 = right.Z;
     m01 = up.X;
     m11 = up.Y;
     m21 = up.Z;
     m02 = forward.X;
     m12 = forward.Y;
     m22 = forward.Z;
 }
示例#2
0
        void rebuildMatrices()
        {
            if (!needsRebuild) return;
            needsRebuild=false;

            Vector forward,up,right;

            forward=Vector.Subtract(lookat,pos);
            up=new Vector(0f,1f,0f);
            right=Vector.GetNormal(up,forward);
            up=Vector.GetNormal(forward,right);

            forward.Normalize();
            up.Normalize();
            right.Normalize();

            normalmatrix=new Matrix(right,up,forward);
            normalmatrix.rotate(0,0,_roll);
            matrix=normalmatrix.Clone();
            matrix.shift(pos.X,pos.Y,pos.Z);

            normalmatrix=normalmatrix.inverse();
            matrix=matrix.inverse();
        }
示例#3
0
 public void shift(Vector v)
 {
     shift(v.X,v.Y,v.Z);
 }
示例#4
0
        // P U B L I C   M E T H O D S
        /// <summary>
        /// Projects this vertex into camera space
        /// </summary>
        /// <param name="vertexProjection"></param>
        /// <param name="normalProjection"></param>
        /// <param name="camera"></param>
        public void Project(Matrix vertexProjection, Matrix normalProjection, Camera camera)
        {
            pos2 = pos.Transform(vertexProjection);
            n2 = n.Transform(normalProjection);

            fact = camera.screenscale / camera.fovfact / ((pos2.Z > 0.1) ? pos2.Z : 0.1f);
            X = (int) (pos2.X * fact + (camera.screenwidth >> 1));
            Y = (int) (-pos2.Y * fact + (camera.screenheight >> 1));
            Z = (int) (65536f * pos2.Z);
            nx = (int) (n2.X * 127 + 127);
            ny = (int) (n2.Y * 127 + 127);
            if (parent.material == null) return;
            if (parent.material.texture == null) return;
            tx = (int) ((float) parent.material.texture.width * Tu);
            ty = (int) ((float) parent.material.texture.height * Tv);
        }
示例#5
0
 /// <summary>
 /// Returns the angle between 2 vectors.
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public static float Angle(Vector a, Vector b)
 {
     a.Normalize();
     b.Normalize();
     return (a.X * b.X + a.Y * b.Y + a.Z * b.Z);
 }
示例#6
0
 /// <summary>
 /// Returns (b-a) x (c-a).
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <param name="c"></param>
 /// <returns></returns>
 public static Vector VectorProduct(Vector a, Vector b, Vector c)
 {
     return VectorProduct(Subtract(b, a), Subtract(c, a));
 }
示例#7
0
 /// <summary>
 /// Subtracts 2 vectors.
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public static Vector Subtract(Vector a, Vector b)
 {
     return new Vector(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
 }
示例#8
0
 /// <summary>
 /// Returns the length of a vector.
 /// </summary>
 /// <param name="a"></param>
 /// <returns></returns>
 public static float Length(Vector a)
 {
     return (float) Math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z);
 }
示例#9
0
 public void lookAt(Vector p)
 {
     lookat=p;
     needsRebuild=true;
 }
示例#10
0
 public void lookAt(float px, float py, float pz)
 {
     lookat=new Vector(px,py,pz);
     needsRebuild=true;
 }
示例#11
0
 public Vertex(Vector ppos)
 {
     pos = ppos.Clone();
 }
示例#12
0
 public Vertex(float xpos, float ypos, float zpos, float u, float v)
 {
     pos = new Vector(xpos, ypos, zpos);
     this.Tu = u;
     this.Tv = v;
 }
示例#13
0
 public Vertex(float xpos, float ypos, float zpos)
 {
     pos = new Vector(xpos, ypos, zpos);
 }
示例#14
0
        private List<Triangle> neighbor = new List<Triangle>(); //Neighbor triangles of vertex

        #endregion Fields

        #region Constructors

        public Vertex()
        {
            pos = new Vector(0f, 0f, 0f);
        }
示例#15
0
 // recalculates the vertex normal
 public void regenerateNormal()
 {
     float nx = 0;
     float ny = 0;
     float nz = 0;
     foreach (Triangle tri in neighbor)
     {
         Vector wn = tri.getWeightedNormal();
         nx += wn.X;
         ny += wn.Y;
         nz += wn.Z;
     }
     n = new Vector(nx, ny, nz).Normalize();
 }
示例#16
0
 public void rotate(float dx, float dy, float dz)
 {
     pos=pos.Transform(Matrix.rotateMatrix(dx,dy,dz));
     needsRebuild=true;
 }
示例#17
0
 /// <summary>
 /// returns the normal vector of the plane defined by the two vectors.
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <param name="c"></param>
 /// <returns></returns>
 public static Vector GetNormal(Vector a, Vector b, Vector c)
 {
     return VectorProduct(a, b, c).Normalize();
 }
示例#18
0
 public void rotate(Vector v)
 {
     rotate(v.X,v.Y,v.Z);
 }
示例#19
0
 /// <summary>
 /// Scales a vector by the specified amount.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="a"></param>
 /// <returns></returns>
 public static Vector Scale(float f, Vector a)
 {
     return new Vector(f*a.X, f*a.Y, f*a.Z);
 }
示例#20
0
 public void setPos(float px, float py, float pz)
 {
     pos=new Vector(px,py,pz);
     needsRebuild=true;
 }
示例#21
0
 /// <summary>
 /// Returns a x b.
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public static Vector VectorProduct(Vector a, Vector b)
 {
     return new Vector(a.Y * b.Z - b.Y * a.Z, a.Z * b.X - b.Z * a.X, a.X * b.Y - b.X * a.Y);
 }
示例#22
0
 public void setPos(Vector p)
 {
     pos=p;
     needsRebuild=true;
 }
示例#23
0
 /// <summary>
 /// Adds 2 vectors.
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public static Vector Add(Vector a, Vector b)
 {
     return new Vector(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
 }
示例#24
0
 // MATRIX MODIFIERS
 public void shift(float dx, float dy, float dz)
 {
     pos=pos.Transform(Matrix.shiftMatrix(dx,dy,dz));
     lookat=lookat.Transform(Matrix.shiftMatrix(dx,dy,dz));
     needsRebuild=true;
 }
示例#25
0
        // useful if you can't find your objects on the screen ;)
        public void normalize()
        {
            objectsNeedRebuild = true;
            rebuild();

            Vector min, max, tempmax, tempmin;
            if (objects == 0) return;

            matrix = new Matrix();
            normalmatrix = new Matrix();

            max = _object[0].Max();
            min = _object[0].Min();

            for (int i = 0; i < objects; i++)
            {
                tempmax = _object[i].Max();
                tempmin = _object[i].Min();
                if (tempmax.X > max.X) max.X = tempmax.X;
                if (tempmax.Y > max.Y) max.Y = tempmax.Y;
                if (tempmax.Z > max.Z) max.Z = tempmax.Z;
                if (tempmin.X < min.X) min.X = tempmin.X;
                if (tempmin.Y < min.Y) min.Y = tempmin.Y;
                if (tempmin.Z < min.Z) min.Z = tempmin.Z;
            }
            float xdist = max.X - min.X;
            float ydist = max.Y - min.Y;
            float zdist = max.Z - min.Z;
            float xmed = (max.X + min.X) / 2;
            float ymed = (max.Y + min.Y) / 2;
            float zmed = (max.Z + min.Z) / 2;

            float diameter = (xdist > ydist) ? xdist : ydist;
            diameter = (zdist > diameter) ? zdist : diameter;

            normalizedOffset = new Vector(xmed, ymed, zmed);
            normalizedScale = 2 / diameter;

            shift(normalizedOffset.Reverse());
            scale(normalizedScale);
        }
示例#26
0
 public Vertex(Vector ppos, float u, float v)
 {
     pos = ppos.Clone();
     this.Tu = u;
     this.Tv = v;
 }