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; }
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(); }
public void shift(Vector v) { shift(v.X,v.Y,v.Z); }
// 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); }
/// <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); }
/// <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)); }
/// <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); }
/// <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); }
public void lookAt(Vector p) { lookat=p; needsRebuild=true; }
public void lookAt(float px, float py, float pz) { lookat=new Vector(px,py,pz); needsRebuild=true; }
public Vertex(Vector ppos) { pos = ppos.Clone(); }
public Vertex(float xpos, float ypos, float zpos, float u, float v) { pos = new Vector(xpos, ypos, zpos); this.Tu = u; this.Tv = v; }
public Vertex(float xpos, float ypos, float zpos) { pos = new Vector(xpos, ypos, zpos); }
private List<Triangle> neighbor = new List<Triangle>(); //Neighbor triangles of vertex #endregion Fields #region Constructors public Vertex() { pos = new Vector(0f, 0f, 0f); }
// 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(); }
public void rotate(float dx, float dy, float dz) { pos=pos.Transform(Matrix.rotateMatrix(dx,dy,dz)); needsRebuild=true; }
/// <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(); }
public void rotate(Vector v) { rotate(v.X,v.Y,v.Z); }
/// <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); }
public void setPos(float px, float py, float pz) { pos=new Vector(px,py,pz); needsRebuild=true; }
/// <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); }
public void setPos(Vector p) { pos=p; needsRebuild=true; }
/// <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); }
// 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; }
// 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); }
public Vertex(Vector ppos, float u, float v) { pos = ppos.Clone(); this.Tu = u; this.Tv = v; }