public ParticleAnchoredSpring(Particle particle, Point3D anchor, float springConstant, float restLength) : base(particle) { Anchor = anchor; SpringConstant = springConstant; RestLength = restLength; }
protected static Mesh CreateFromPrimitive(ParserContext context, BasicPrimitiveTessellator tessellator, Point3D center) { tessellator.Tessellate(); Mesh mesh = new Mesh(); mesh.Positions.AddRange(tessellator.Positions); mesh.Indices.AddRange(tessellator.Indices); mesh.Normals.AddRange(tessellator.Normals); mesh.Material = context.CurrentMaterial; mesh.Transform = new TranslateTransform { OffsetX = center.X, OffsetY = center.Y, OffsetZ = center.Z }; return mesh; }
public static Vector3 ToSharpDXVector3(Point3D v) { return new Vector3(v.X, v.Y, v.Z); }
/// <summary> /// Tessellates the specified bezier patch. /// </summary> private void TessellatePatch(BezierPatch patch, Vector3D scale) { // Look up the 16 control points for this patch. Point3D[] controlPoints = new Point3D[16]; for (int i = 0; i < 16; i++) { int index = patch.Indices[i]; controlPoints[i] = ControlPoints[index] * scale; } // Is this patch being mirrored? bool isMirrored = Math.Sign(scale.X) != Math.Sign(scale.Z); // Create the index and vertex data. CreatePatchIndices(isMirrored); CreatePatchVertices(controlPoints, isMirrored); }
public static Point3D GetPoint3D(string key, Point3D def = default(Point3D)) { var vals = GetFloats(key); return vals == null || vals.Length != 3 ? def : new Point3D(vals[0], vals[1], vals[2]); }
protected void AddVertex(Point3D position, Vector3D normal, Point2D textureCoordinate) { AddVertex(position, normal); TextureCoordinates.Add(textureCoordinate); }
public VertexPositionColor(Point3D position, Vector3D color) { Position = position; Color = color; }
/// <summary> /// Computes the tangent of a cubic bezier curve at the specified time, /// when given four Point3D control points. This is used for calculating /// normals (by crossing the horizontal and vertical tangent vectors). /// </summary> private static Vector3D BezierTangent(Point3D p1, Point3D p2, Point3D p3, Point3D p4, float t) { Vector3D result = new Vector3D(); result.X = BezierTangent(p1.X, p2.X, p3.X, p4.X, t); result.Y = BezierTangent(p1.Y, p2.Y, p3.Y, p4.Y, t); result.Z = BezierTangent(p1.Z, p2.Z, p3.Z, p4.Z, t); result.Normalize(); return result; }
public Point4D(Point3D point, float w) : this(point.X, point.Y, point.Z, w) { }
public static Point3D Parse(string source) { IFormatProvider cultureInfo = CultureInfo.InvariantCulture; TokenizerHelper helper = new TokenizerHelper(source, cultureInfo); string str = helper.NextTokenRequired(); Point3D pointd = new Point3D(Convert.ToSingle(str, cultureInfo), Convert.ToSingle(helper.NextTokenRequired(), cultureInfo), Convert.ToSingle(helper.NextTokenRequired(), cultureInfo)); helper.LastTokenRequired(); return pointd; }
public static float DistanceSquared(Point3D p1, Point3D p2) { return (p1 - p2).LengthSquared(); }
public static float Distance(Point3D p1, Point3D p2) { return (p1 - p2).Length(); }
public VertexPositionNormalTexture(Point3D position, Vector3D normal, Point2D textureCoordinate) { Position = position; Normal = normal; TextureCoordinate = textureCoordinate; }
public Camera(Point3D position, Vector3D up, Vector3D forward) { Position = position; Up = up; Forward = forward; }
// // Compute scene vertex normals // public void computeNormals(Scene3ds scene, Scene result) { Point3D vcenter = Point3D.Zero; float vcounter = 0.0f; for (int i = 0; i < scene.Meshes.Count(); i++) { Mesh3ds m = scene.Meshes.ElementAt(i); // Alloc memory Mesh mesh = new Mesh { Name = m.Name }; result.Meshes.Add(mesh); //mesh._numTexCoords = 0; Vector3D[] tmpFaceNormals = new Vector3D[m.faces()]; // Compute face normals for (int fi = 0; fi < m.faces(); fi++) { Face3ds f = m.face(fi); Vertex3ds p0 = m.vertex(f.P0); Vertex3ds p1 = m.vertex(f.P1); Vertex3ds p2 = m.vertex(f.P2); /*// Compute face middle point mesh.faceMiddlePoint[fi] = new PVector(); mesh.faceMiddlePoint[fi].x = (p0.X + p1.X + p2.X) / 3.0f; mesh.faceMiddlePoint[fi].y = (p0.Y + p1.Y + p2.Y) / 3.0f; mesh.faceMiddlePoint[fi].z = (p0.Z + p1.Z + p2.Z) / 3.0f;*/ Point3D v0 = new Point3D(p0.X, p0.Y, p0.Z); Point3D v1 = new Point3D(p1.X, p1.Y, p1.Z); Point3D v2 = new Point3D(p2.X, p2.Y, p2.Z); Vector3D e0 = v1 - v0; Vector3D e1 = v2 - v0; //mesh.faceNormals[fi] = e1.cross(e0); // save a copy of the unnormalized face normal. used for average vertex normals tmpFaceNormals[fi] = Vector3D.Cross(e1, e0); // normalize face normal //mesh.faceNormals[fi].normalize(); } // // Compute vertex normals.Take average from adjacent face normals.find coplanar faces or get weighted normals. // One could also use the smooth groups from 3ds to compute normals, we'll see about that. // //PVector v = new PVector(); TexCoord3ds tc = new TexCoord3ds(0, 0); for (int vi = 0; vi < m.vertices(); vi++) { Vertex3ds p = m.vertex(vi); vcenter += new Point3D(p.X, p.Y, p.Z); vcounter++; if (m.texCoords() > 0) tc = m.texCoord(vi); Vector3D n = Vector3D.Zero; float num = 0; for (int fi = 0; fi < m.faces(); fi++) { Face3ds f = m.face(fi); // Vertex3ds p0 = m.vertex(f.P0); // Vertex3ds p1 = m.vertex(f.P1); // Vertex3ds p2 = m.vertex(f.P2); if (vi == f.P0 || vi == f.P1 || vi == f.P2) { num++; n += tmpFaceNormals[fi]; //mesh.faceNormals[fi] ); } } if (num > 0) n *= 1.0f/(float) num; n.Normalize(); mesh.Normals.Add(n); if (FLIPYZ) { Vector3D tmp = mesh.Normals[vi]; mesh.Normals[vi] = new Vector3D(tmp.X, -tmp.Z, tmp.Y); } // Save vertex data if (FLIPYZ) mesh.Positions.Add(new Point3D(p.X, -p.Z, p.Y)); else mesh.Positions.Add(new Point3D(p.X, p.Y, p.Z)); // Save texcoord data //mesh._numTexCoords = m.texCoords(); if (m.texCoords() > 0) { if (FLIPV) mesh.TextureCoordinates.Add(new Point3D(tc.U, 1.0f - tc.V, 0)); else mesh.TextureCoordinates.Add(new Point3D(tc.U, tc.V, 0)); } } } if (vcounter > 0.0) vcenter /= vcounter; }
/// <summary> /// Transforms a Point3D by the given Matrix. /// </summary> /// <param name="position">The source Point3D.</param> /// <param name="matrix">The transformation matrix.</param> /// <returns>The HomogeneousPoint3D resulting from the transformation.</returns> public static Point4D Transform(Point3D position, Matrix3D matrix) { Point4D result; result.X = (((position.X * matrix.M11) + (position.Y * matrix.M21)) + (position.Z * matrix.M31)) + matrix.M41; result.Y = (((position.X * matrix.M12) + (position.Y * matrix.M22)) + (position.Z * matrix.M32)) + matrix.M42; result.Z = (((position.X * matrix.M13) + (position.Y * matrix.M23)) + (position.Z * matrix.M33)) + matrix.M43; result.W = (((position.X * matrix.M14) + (position.Y * matrix.M24)) + (position.Z * matrix.M34)) + matrix.M44; return result; }
public static void Subtract(ref Point3D value1, ref Point3D value2, out Vector3D result) { result = new Vector3D(value1.X - value2.X, value1.Y - value2.Y, value1.Z - value2.Z); }
/// <summary> /// Performs a cubic bezier interpolation between four Point3D control /// points, returning the value at the specified time (t ranges 0 to 1). /// </summary> private static Point3D Bezier(Point3D p1, Point3D p2, Point3D p3, Point3D p4, float t) { Point3D result = new Point3D(); result.X = Bezier(p1.X, p2.X, p3.X, p4.X, t); result.Y = Bezier(p1.Y, p2.Y, p3.Y, p4.Y, t); result.Z = Bezier(p1.Z, p2.Z, p3.Z, p4.Z, t); return result; }
public static Point3D Transform(Point3D position, Matrix3D matrix) { Point3D vector; float num3 = (((position.X * matrix.M11) + (position.Y * matrix.M21)) + (position.Z * matrix.M31)) + matrix.M41; float num2 = (((position.X * matrix.M12) + (position.Y * matrix.M22)) + (position.Z * matrix.M32)) + matrix.M42; float num = (((position.X * matrix.M13) + (position.Y * matrix.M23)) + (position.Z * matrix.M33)) + matrix.M43; vector.X = num3; vector.Y = num2; vector.Z = num; return vector; }
/// <summary> /// Creates vertices for a patch that is tessellated at the specified level. /// </summary> protected void CreatePatchVertices(Point3D[] patch, bool isMirrored) { Debug.Assert(patch.Length == 16); for (int i = 0; i <= TessellationLevel; i++) { float ti = (float)i / TessellationLevel; for (int j = 0; j <= TessellationLevel; j++) { float tj = (float)j / TessellationLevel; // Perform four horizontal bezier interpolations // between the control points of this patch. Point3D p1 = Bezier(patch[0], patch[1], patch[2], patch[3], ti); Point3D p2 = Bezier(patch[4], patch[5], patch[6], patch[7], ti); Point3D p3 = Bezier(patch[8], patch[9], patch[10], patch[11], ti); Point3D p4 = Bezier(patch[12], patch[13], patch[14], patch[15], ti); // Perform a vertical interpolation between the results of the // previous horizontal interpolations, to compute the position. Point3D position = Bezier(p1, p2, p3, p4, tj); // Perform another four bezier interpolations between the control // points, but this time vertically rather than horizontally. Point3D q1 = Bezier(patch[0], patch[4], patch[8], patch[12], tj); Point3D q2 = Bezier(patch[1], patch[5], patch[9], patch[13], tj); Point3D q3 = Bezier(patch[2], patch[6], patch[10], patch[14], tj); Point3D q4 = Bezier(patch[3], patch[7], patch[11], patch[15], tj); // Compute vertical and horizontal tangent vectors. Vector3D tangentA = BezierTangent(p1, p2, p3, p4, tj); Vector3D tangentB = BezierTangent(q1, q2, q3, q4, ti); // Cross the two tangent vectors to compute the normal. Vector3D normal = Vector3D.Cross(tangentA, tangentB); if (normal.Length() > 0.0001f) { normal.Normalize(); // If this patch is mirrored, we must invert the normal. if (isMirrored) normal = -normal; } else { // In a tidy and well constructed bezier patch, the preceding // normal computation will always work. But the classic teapot // model is not tidy or well constructed! At the top and bottom // of the teapot, it contains degenerate geometry where a patch // has several control points in the same place, which causes // the tangent computation to fail and produce a zero normal. // We 'fix' these cases by just hard-coding a normal that points // either straight up or straight down, depending on whether we // are on the top or bottom of the teapot. This is not a robust // solution for all possible degenerate bezier patches, but hey, // it's good enough to make the teapot work correctly! if (position.Y > 0) normal = Vector3D.Up; else normal = Vector3D.Down; } // Create the vertex. AddVertex(position, normal); } } }
public static void SetPoint3D(string key, Point3D value) { SetFloats(key, new []{value.X, value.Y, value.Z}); }
public ParticleAnchoredBungee(Particle particle, Point3D anchor, float springConstant, float restLength) : base(particle, anchor, springConstant, restLength) { }
protected void AddVertex(Point3D position, Vector3D normal) { Positions.Add(position + PositionOffset); Normals.Add(normal); }