public PlanePrimitive(Vector3F normal, float distance) { PrimitiveType = PrimitiveType.Plane; Normal = normal; Distance = distance; }
public override Vector3F GetNormal(Vector3F position) { var x = position.X - Center.X == 0.0f ? 0 : Math.Sign(Center.X - position.X); var y = position.Y - Center.Y == 0.0f ? 0 : Math.Sign(Center.Y - position.Y); var z = position.Z - Center.Z == 0.0f ? 0 : Math.Sign(Center.Z - position.Z); return new Vector3F(x, y, z); }
public SpherePrimitive(Vector3F center, float radius) { Center = center; Radius = radius; SquareRadius = radius * radius; RRadius = 1.0f / radius; PrimitiveType = PrimitiveType.Sphere; }
public override bool Render() { // Render scene var o = new Vector3F(0.0f, 0.0f, -5.0f); // Initialize timer var msecs = Environment.TickCount; // Render remaining lines for (var lineY = _currLine; lineY < (Height); lineY++) { _sx = _worldX1; // Render pixels for current line for (var lineX = 0; lineX < Width; lineX++) { // Fire primary ray var dir = new Vector3F(_sx, _sy, 0.0f) - o; dir.Normalize(); var r = new Ray(o, dir); var acc = Raytrace(r, 1); var red = (int)(acc.X * 256); var green = (int)(acc.Y * 256); var blue = (int)(acc.Z * 256); if (red > 255) red = 255; if (green > 255) green = 255; if (blue > 255) blue = 255; PixelBuffer[_pPos++] = (uint)((red << 16) + (green << 8) + blue); _sx += _deltaX; } _sy += _deltaY; // See if we've been working to long already if ((Environment.TickCount - msecs) > 100) { // return control to windows so the screen gets updated _currLine = lineY + 1; return false; } } // all done return true; }
public override Vector3F GetNormal(Vector3F position) { return Normal; }
public BoxPrimitive(Vector3F min, Vector3F max) { Min = min; Max = max; }
public BoxPrimitive() { PrimitiveType = PrimitiveType.Box; Min = new Vector3F(0); Max = new Vector3F(1); }
public override Vector3F GetNormal(Vector3F position) { return (position - Center) * RRadius; }
/// <summary> /// Initializes the scene. /// </summary> public void InitScene() { dynamic json = JsonConvert.DeserializeObject(File.ReadAllText("scene.json")); #region Read materials from json var defaultColor = new Color(0.2f, 0.2f, 0.2f); const float defaultReflection = 0.0f; const float defaultDiffuse = 0.2f; var materialList = new List<Material>(); var materials = json.materials; foreach (var material in materials) { materialList.Add(new Material { Name = material.name, Color = material.color == null ? defaultColor : new Color((float)material.color[0], (float)material.color[1], (float)material.color[2]), Diffuse = material.diffuse ?? defaultDiffuse, Reflection = material.reflection ?? defaultReflection }); } #endregion Read materials from json #region Read primitives from json var primitives = json.primitives; foreach (var primitive in primitives) { var primitiveType = (PrimitiveType)primitive.primitiveType; Primitive newPrimitive; switch (primitiveType) { case PrimitiveType.Box: var min = new Vector3F((float)primitive.min[0], (float)primitive.min[1], (float)primitive.min[2]); var max = new Vector3F((float)primitive.max[0], (float)primitive.max[1], (float)primitive.max[2]); newPrimitive = new BoxPrimitive(min, max) { Name = primitive.name, IsLight = primitive.isLight ?? false, Material = materialList.FirstOrDefault(x => x.Name == (string)primitive.material) }; break; case PrimitiveType.Plane: var normal = new Vector3F((float)primitive.normal[0], (float)primitive.normal[1], (float)primitive.normal[2]); float distance = primitive.distance ?? 0.0f; newPrimitive = new PlanePrimitive(normal, distance) { Name = primitive.name, IsLight = primitive.isLight ?? false, Material = materialList.FirstOrDefault(x => x.Name == (string)primitive.material) }; break; case PrimitiveType.Sphere: var center = new Vector3F((float)primitive.center[0], (float)primitive.center[1], (float)primitive.center[2]); float radius = primitive.radius ?? 0.0f; newPrimitive = new SpherePrimitive(center, radius) { Name = primitive.name, IsLight = primitive.isLight ?? false, Material = materialList.FirstOrDefault(x => x.Name == (string)primitive.material) }; break; default: throw new ArgumentException("Invalid primitive type!"); } Primitives.Add(newPrimitive); } #endregion Read primitives from json }
public float Dot(Vector3F vector) { return Dot(this, vector); }
public Vector3F Cross(Vector3F b) { return new Vector3F(Y * b.Z - Z * b.Y, Z * b.X - X * b.Z, X * b.Y - Y * b.X); }
/// <summary> /// Calculates the dot product of two vectors a and b. /// </summary> /// <param name="vector1">a.</param> /// <param name="vector2">The b.</param> /// <returns></returns> public static float Dot(Vector3F vector1, Vector3F vector2) { return vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z; }