public void project(warp_Matrix vertexProjection, warp_Matrix normalProjection, warp_Camera camera) // Projects this vertex into camera space { pos2 = pos.transform(vertexProjection); n2 = n.transform(normalProjection); float fact; if (camera.isOrthographic) { x = (int)(pos2.x * (camera.screenscale / camera.orthoViewWidth) + (camera.screenwidth >> 1)); y = (int)(-pos2.y * (camera.screenscale / camera.orthoViewHeight) + (camera.screenheight >> 1)); } else { 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); sw = -(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 * u); ty = (int)((float)parent.material.texture.height * v); }
public void project(warp_Matrix vertexProjection, warp_Matrix normalProjection, warp_Camera camera) // Projects this vertex into camera space { pos2 = pos.transform(vertexProjection); n2 = n.transform(normalProjection); float fact; if (camera.isOrthographic) { x = (int)(pos2.x * (camera.screenscale / camera.orthoViewWidth) + (camera.screenwidth >> 1)); y = (int)(-pos2.y * (camera.screenscale / camera.orthoViewHeight) + (camera.screenheight >> 1)); } else { 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); sw = -(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 * u); ty = (int)((float)parent.material.texture.height * v); }
public static float angle(warp_Vector a, warp_Vector b) // returns the angle between 2 vectors { a.normalize(); b.normalize(); return(a.x * b.x + a.y * b.y + a.z * b.z); }
public warp_Vertex(warp_Vector ppos, warp_Vector norm, float u, float v) { pos = ppos; n = norm; this.u = u; this.v = v; }
void rebuildMatrices() { if (!needsRebuild) { return; } needsRebuild = false; warp_Vector forward, up, right; forward = warp_Vector.sub(lookat, pos); up = new warp_Vector(0f, 1f, 0f); right = warp_Vector.getNormal(up, forward); up = warp_Vector.getNormal(forward, right); forward.normalize(); up.normalize(); right.normalize(); normalmatrix = new warp_Matrix(right, up, forward); normalmatrix.rotate(0, 0, rollfactor); matrix = normalmatrix.getClone(); matrix.shift(pos.x, pos.y, pos.z); normalmatrix = normalmatrix.inverse(); matrix = matrix.inverse(); }
public warp_Vector getCenter() // Returns the center of this object { warp_Vector max = maximum(); warp_Vector min = minimum(); return(new warp_Vector((max.x + min.x) / 2, (max.y + min.y) / 2, (max.z + min.z) / 2)); }
public warp_Vector getDimension() // Returns the x,y,z - Dimension of this object { warp_Vector max = maximum(); warp_Vector min = minimum(); return(new warp_Vector(max.x - min.x, max.y - min.y, max.z - min.z)); }
public static warp_Vector sub(warp_Vector a, warp_Vector b) // substracts 2 vectors { a.x -= b.x; a.y -= b.y; a.z -= b.z; return(a); }
public static warp_Vector add(warp_Vector a, warp_Vector b) // adds 2 vectors { a.x += b.x; a.y += b.y; a.z += b.z; return(a); }
public warp_Light(warp_Vector direction, int diffuse, int specular, int highlightSheen, int highlightSpread) { v = direction; v.normalize(); this.diffuse = diffuse; this.specular = specular; this.highlightSheen = highlightSheen; this.highlightSpread = highlightSpread; }
public warp_Light(warp_Vector direction, int color, int highlightSheen, int highlightSpread) { v = direction.getClone(); v.normalize(); this.diffuse = color; this.specular = color; this.highlightSheen = highlightSheen; this.highlightSpread = highlightSpread; }
public static warp_Vector scale(float f, warp_Vector a) // substracts 2 vectors { a.x *= f; a.y *= f; a.z *= f; return(a); }
public warp_Light(warp_Vector direction, int color, int highlightSheen, int highlightSpread) { v = direction; v.normalize(); this.diffuse = color; this.specular = color; this.highlightSheen = highlightSheen; this.highlightSpread = highlightSpread; }
public warp_Light(warp_Vector direction, int diffuse, int specular, int highlightSheen, int highlightSpread) { v = direction.getClone(); v.normalize(); this.diffuse = diffuse; this.specular = specular; this.highlightSheen = highlightSheen; this.highlightSpread = highlightSpread; }
public static warp_Object CONE(float height, float radius, int segments) { warp_Vector[] path = new warp_Vector[4]; float h = height / 2; path[0] = new warp_Vector(0, h, 0); path[1] = new warp_Vector(radius, -h, 0); path[2] = new warp_Vector(radius, -h, 0); path[3] = new warp_Vector(0, -h, 0); return(ROTATIONOBJECT(path, segments)); }
public warp_Matrix(warp_Vector right, warp_Vector up, warp_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; }
public warp_Matrix(warp_Vector right, warp_Vector up, warp_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; }
public static warp_Vector vectorProduct(warp_Vector a, warp_Vector b, warp_Vector c) // returns (b-a) x (c-a) { float ax = b.x - a.x; float ay = b.y - a.y; float az = b.z - a.z; float bx = c.x - a.x; float by = c.y - a.y; float bz = c.z - a.z; return(new warp_Vector(ay * bz - by * az, az * bx - bz * ax, ax * by - bx * ay)); }
public static warp_Vector vectorProduct(warp_Vector a, warp_Vector b, warp_Vector c) // returns (b-a) x (c-a) { float ax = b.x - a.x; float ay = b.y - a.y; float az = b.z - a.z; float bx = c.x - a.x; float by = c.y - a.y; float bz = c.z - a.z; return new warp_Vector(ay * bz - by * az, az * bx - bz * ax, ax * by - bx * ay); }
public static void projectCylindric(warp_Object obj) { obj.rebuild(); warp_Vector min = obj.minimum(); warp_Vector max = obj.maximum(); float dz = 1 / (max.z - min.z); for (int i = 0; i < obj.vertices; i++) { obj.fastvertex[i].pos.buildCylindric(); obj.fastvertex[i].u = obj.fastvertex[i].pos.theta / (2 * 3.14159265f); obj.fastvertex[i].v = (obj.fastvertex[i].pos.z - min.z) * dz; } }
public void clipFrustrum(int w, int h) { if (parent.material==null) {visible=false; return; } outOfFrustrum=(p1.clipcode&p2.clipcode&p3.clipcode)!=0; if (outOfFrustrum) {visible=false; return; } if (n2.z>0.5) {visible=true; return; } warp_Vector triangleCenter = new warp_Vector( (p1.pos2.x+p2.pos2.x+p3.pos2.x), (p1.pos2.y+p2.pos2.y+p3.pos2.y), (p1.pos2.z+p2.pos2.z+p3.pos2.z)); visible=warp_Vector.angle(triangleCenter,n2)>0; }
public static void projectFrontal(warp_Object obj) { obj.rebuild(); warp_Vector min = obj.minimum(); warp_Vector max = obj.maximum(); float du = 1 / (max.x - min.x); float dv = 1 / (max.y - min.y); for (int i = 0; i < obj.vertices; i++) { obj.fastvertex[i].u = (obj.fastvertex[i].pos.x - min.x) * du; obj.fastvertex[i].v = 1 - (obj.fastvertex[i].pos.y - min.y) * dv; } }
public void regenerateNormal() { warp_Vector wn; float nx = 0f; float ny = 0f; float nz = 0f; for (int i = 0; i < neighbor.Count; i++) { wn = neighbor[i].getWeightedNormal(); nx += wn.x; ny += wn.y; nz += wn.z; } n = new warp_Vector(nx, ny, nz).normalize(); }
public void detach() // Centers the object in its coordinate system // The offset from origin to object center will be transfered to the matrix, // so your object actually does not move. // Usefull if you want prepare objects for self rotation. { warp_Vector center = getCenter(); for (int i = 0; i < vertices; i++) { fastvertex[i].pos.x -= center.x; fastvertex[i].pos.y -= center.y; fastvertex[i].pos.z -= center.z; } shift(center); }
public static warp_Object TORUSKNOT(float p, float q, float r_tube, float r_out, float r_in, float h, int segments, int steps) { float x, y, z, r, t, theta; warp_Vector[] path = new warp_Vector[segments + 1]; for (int i = 0; i < segments + 1; i++) { t = 2 * 3.14159265f * i / (float)segments; r = r_out + r_in * warp_Math.cos(p * t); z = h * warp_Math.sin(p * t); theta = q * t; x = r * warp_Math.cos(theta); y = r * warp_Math.sin(theta); path[i] = new warp_Vector(x, y, z); } return(TUBE(path, r_tube, steps, true)); }
public static warp_Object SPIRAL(float h, float r_out, float r_in, float r_tube, float w, float f, int segments, int steps) { float x, y, z, r, t, theta; warp_Vector[] path = new warp_Vector[segments + 1]; for (int i = 0; i < segments + 1; i++) { t = (float)i / (float)segments; r = r_out + r_in * warp_Math.sin(2 * 3.14159265f * f * t); z = (h / 2) + h * t; theta = 2 * 3.14159265f * w * t; x = r * warp_Math.cos(theta); y = r * warp_Math.sin(theta); path[i] = new warp_Vector(x, y, z); } return(TUBE(path, r_tube, steps, false)); }
public static warp_Object SPHERE(float radius, int segments) { warp_Vector[] path = new warp_Vector[segments]; float x, y, angle; path[0] = new warp_Vector(0, radius, 0); path[segments - 1] = new warp_Vector(0, -radius, 0); for (int i = 1; i < segments - 1; i++) { angle = -(((float)i / (float)(segments - 2)) - 0.5f) * 3.14159265f; x = (float)Math.Cos(angle) * radius; y = (float)Math.Sin(angle) * radius; path[i] = new warp_Vector(x, y, 0); } return(ROTATIONOBJECT(path, segments)); }
public void regenerateNormal() { float nx = 0; float ny = 0; float nz = 0; IEnumerator enumerator = neighbor.GetEnumerator(); warp_Triangle tri; warp_Vector wn; while (enumerator.MoveNext()) { tri = (warp_Triangle)enumerator.Current; wn = tri.getWeightedNormal(); nx += wn.x; ny += wn.y; nz += wn.z; } n = new warp_Vector(nx, ny, nz).normalize(); }
public void clipFrustrum(int w, int h) { if (parent.material == null) { visible = false; return; } outOfFrustrum = (p1.clipcode & p2.clipcode & p3.clipcode) != 0; if (outOfFrustrum) { visible = false; return; } if (n2.z > 0.5) { visible = true; return; } warp_Vector triangleCenter = new warp_Vector( (p1.pos2.x + p2.pos2.x + p3.pos2.x), (p1.pos2.y + p2.pos2.y + p3.pos2.y), (p1.pos2.z + p2.pos2.z + p3.pos2.z)); visible = warp_Vector.angle(triangleCenter, n2) > 0; }
void rebuildMatrices() { if (!needsRebuild) return; needsRebuild = false; warp_Vector forward, up, right; forward = warp_Vector.sub(lookat, pos); up = new warp_Vector(0f, 1f, 0f); right = warp_Vector.getNormal(up, forward); up = warp_Vector.getNormal(forward, right); forward.normalize(); up.normalize(); right.normalize(); normalmatrix = new warp_Matrix(right, up, forward); normalmatrix.rotate(0, 0, rollfactor); matrix = normalmatrix.getClone(); matrix.shift(pos.x, pos.y, pos.z); normalmatrix = normalmatrix.inverse(); matrix = matrix.inverse(); }
public static warp_Vector add(warp_Vector a, warp_Vector b) // adds 2 vectors { return new warp_Vector(a.x+b.x,a.y+b.y,a.z+b.z); }
public void addVertex(warp_Vector pos, warp_Vector norm, float u, float v) { addVertex(new warp_Vertex(pos, norm, u, v)); }
public void addVertex(warp_Vector pos, warp_Vector norm) { addVertex(new warp_Vertex(pos, norm)); }
public static warp_Object BOX(warp_Vector size) { return BOX(size.x, size.y, size.z); }
public static float angle(warp_Vector a, warp_Vector b) // returns the angle between 2 vectors { a.normalize(); b.normalize(); return (a.x*b.x+a.y*b.y+a.z*b.z); }
public static warp_Vector vectorProduct(warp_Vector a, warp_Vector b) // returns a x b { return new warp_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 static float len(warp_Vector a) // length of vector { return (float)Math.Sqrt(a.x*a.x+a.y*a.y+a.z*a.z); }
public static warp_Vector sub(warp_Vector a, warp_Vector b) // substracts 2 vectors { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; }
public static warp_Vector scale(float f, warp_Vector a) // substracts 2 vectors { a.x *= f; a.y *= f; a.z *= f; return a; }
public void project(warp_Matrix m) { matrix2 = m.getClone(); matrix2.transform(m); v2 = v.transform(matrix2); }
public static warp_Object ROTATIONOBJECT(warp_Vector[] path, int sides) { int steps = sides + 1; warp_Object newObject = new warp_Object(); double alpha = 2 * pi / ( (double) steps - 1); float qx, qz; int nodes = path.GetLength(0); warp_Vertex vertex = null; float u, v; // Texture coordinates for (int j = 0; j < steps; j++) { u = (float) (steps - j - 1) / (float) (steps - 1); for (int i = 0; i < nodes; i++) { v = (float) i / (float) (nodes - 1); qx = (float) (path[i].x * Math.Cos(j * alpha) + path[i].z * Math.Sin(j * alpha)); qz = (float) (path[i].z * Math.Cos(j * alpha) - path[i].x * Math.Sin(j * alpha)); vertex = new warp_Vertex(qx, path[i].y, qz); vertex.u = u; vertex.v = v; newObject.addVertex(vertex); } } for (int j = 0; j < steps - 1; j++) { for (int i = 0; i < nodes - 1; i++) { newObject.addTriangle(i + nodes * j, i + nodes * (j + 1), i + 1 + nodes * j); newObject.addTriangle(i + nodes * (j + 1), i + 1 + nodes * (j + 1), i + 1 + nodes * j); } } for (int i = 0; i < nodes - 1; i++) { newObject.addTriangle(i + nodes * (steps - 1), i, i + 1 + nodes * (steps - 1)); newObject.addTriangle(i, i + 1, i + 1 + nodes * (steps - 1)); } return newObject; }
public warp_Light(warp_Vector direction, int diffuse) { v = direction.getClone(); v.normalize(); this.diffuse = diffuse; }
public static warp_Vector sub(warp_Vector a, warp_Vector b) // substracts 2 vectors { return new warp_Vector(a.x-b.x,a.y-b.y,a.z-b.z); }
public static warp_Vector scale(float f, warp_Vector a) // substracts 2 vectors { return new warp_Vector(f*a.x,f*a.y,f*a.z); }
public void regenerateNormal() { n = warp_Vector.getNormal(p1.pos, p2.pos, p3.pos); }
public static warp_Vector getNormal(warp_Vector a, warp_Vector b, warp_Vector c) // returns the normal vector of the plane defined by the two vectors { return vectorProduct(a,b,c).normalize(); }
public void project(warp_Matrix normalProjection) { n2=n.transform(normalProjection); dist=getDist(); }
public static warp_Vector vectorProduct(warp_Vector a, warp_Vector b, warp_Vector c) // returns (b-a) x (c-a) { return vectorProduct(sub(b,a),sub(c,a)); }
public void regenerateNormal() { n=warp_Vector.getNormal(p1.pos,p2.pos,p3.pos); }
public void addVertex(warp_Vector pos) { addVertex(new warp_Vertex(pos)); }
public static warp_Object SPIRAL(float h, float r_out, float r_in, float r_tube, float w, float f, int segments, int steps) { float x, y, z, r, t, theta; warp_Vector[] path = new warp_Vector[segments + 1]; for (int i = 0; i < segments + 1; i++) { t = (float) i / (float) segments; r = r_out + r_in * warp_Math.sin(2 * 3.14159265f * f * t); z = (h / 2) + h * t; theta = 2 * 3.14159265f * w * t; x = r * warp_Math.cos(theta); y = r * warp_Math.sin(theta); path[i] = new warp_Vector(x, y, z); } return TUBE(path, r_tube, steps, false); }
public static warp_Object SPHERE(float radius, int segments) { warp_Vector[] path = new warp_Vector[segments]; float x, y, angle; path[0] = new warp_Vector(0, radius, 0); path[segments - 1] = new warp_Vector(0, -radius, 0); for (int i = 1; i < segments - 1; i++) { angle = - ( ( (float) i / (float) (segments - 2)) - 0.5f) * 3.14159265f; x = (float) Math.Cos(angle) * radius; y = (float) Math.Sin(angle) * radius; path[i] = new warp_Vector(x, y, 0); } return ROTATIONOBJECT(path, segments); }
public static warp_Object TORUSKNOT(float p, float q, float r_tube,float r_out, float r_in, float h,int segments, int steps) { float x, y, z, r, t, theta; warp_Vector[] path = new warp_Vector[segments + 1]; for (int i = 0; i < segments + 1; i++) { t = 2 * 3.14159265f * i / (float) segments; r = r_out + r_in * warp_Math.cos(p * t); z = h * warp_Math.sin(p * t); theta = q * t; x = r * warp_Math.cos(theta); y = r * warp_Math.sin(theta); path[i] = new warp_Vector(x, y, z); } return TUBE(path, r_tube, steps, true); }
public static warp_Object TUBE(warp_Vector[] path, float r, int steps, bool closed) { warp_Vector[] circle = new warp_Vector[steps]; float angle; for (int i = 0; i < steps; i++) { angle = 2 * 3.14159265f * (float) i / (float) steps; circle[i] = new warp_Vector(r * warp_Math.cos(angle), r * warp_Math.sin(angle), 0f); } warp_Object newObject = new warp_Object(); int segments = path.GetLength(0); warp_Vector forward, up, right; warp_Matrix frenetmatrix; warp_Vertex tempvertex; float relx, rely; int a, b, c, d; for (int i = 0; i < segments; i++) { // Calculate frenet frame matrix if (i != segments - 1) { forward = warp_Vector.sub(path[i + 1], path[i]); } else { if (!closed) { forward = warp_Vector.sub(path[i], path[i - 1]); } else { forward = warp_Vector.sub(path[1], path[0]); } } forward.normalize(); up = new warp_Vector(0f, 0f, 1f); right = warp_Vector.getNormal(forward, up); up = warp_Vector.getNormal(forward, right); frenetmatrix = new warp_Matrix(right, up, forward); frenetmatrix.shift(path[i].x, path[i].y, path[i].z); // Add nodes relx = (float) i / (float) (segments - 1); for (int k = 0; k < steps; k++) { rely = (float) k / (float) steps; tempvertex = new warp_Vertex(circle[k].transform(frenetmatrix)); tempvertex.u = relx; tempvertex.v = rely; newObject.addVertex(tempvertex); } } for (int i = 0; i < segments - 1; i++) { for (int k = 0; k < steps - 1; k++) { a = i * steps + k; b = a + 1; c = a + steps; d = b + steps; newObject.addTriangle(a, c, b); newObject.addTriangle(b, c, d); } a = (i + 1) * steps - 1; b = a + 1 - steps; c = a + steps; d = b + steps; newObject.addTriangle(a, c, b); newObject.addTriangle(b, c, d); } return newObject; }
public static warp_Object CONE(float height, float radius, int segments) { warp_Vector[] path = new warp_Vector[4]; float h = height / 2; path[0] = new warp_Vector(0, h, 0); path[1] = new warp_Vector(radius, -h, 0); path[2] = new warp_Vector(radius, -h, 0); path[3] = new warp_Vector(0, -h, 0); return ROTATIONOBJECT(path, segments); }
public warp_Light(warp_Vector direction) { v = direction.getClone(); v.normalize(); }