public Light(LightType type, Vector3D p1, Vector3D p2, Vector3D p3, Color3D color) { Type = type; Color = color; _grid = new Vector3D[16]; _grid[0] = new Vector3D(1, 2, 0); _grid[1] = new Vector3D(3, 3, 0); _grid[2] = new Vector3D(2, 0, 0); _grid[3] = new Vector3D(0, 1, 0); _grid[4] = new Vector3D(2, 3, 0); _grid[5] = new Vector3D(0, 3, 0); _grid[6] = new Vector3D(0, 0, 0); _grid[7] = new Vector3D(2, 2, 0); _grid[8] = new Vector3D(3, 1, 0); _grid[9] = new Vector3D(1, 3, 0); _grid[10] = new Vector3D(1, 0, 0); _grid[11] = new Vector3D(3, 2, 0); _grid[12] = new Vector3D(2, 1, 0); _grid[13] = new Vector3D(3, 0, 0); _grid[14] = new Vector3D(1, 1, 0); _grid[15] = new Vector3D(0, 2, 0); CellX = (p2 - p1) * .25f; CellY = (p3 - p1) * .25f; for (int i = 0; i < 16; i++) _grid[i] = _grid[i][0] * CellX + _grid[i][1] * CellY + p1; Position = p1 + 2 * CellX + 2 * CellY; }
public void TestEquals() { float r1 = .25f, g1 = .1f, b1 = .75f; float r2 = .75f, g2 = 1.0f, b2 = 1.0f; Color3D c1 = new Color3D(r1, g1, b1); Color3D c2 = new Color3D(r1, g1, b1); Color3D c3 = new Color3D(r2, g2, b2); //Test IEquatable Equals Assert.IsTrue(c1.Equals(c2), "Test IEquatable equals"); Assert.IsFalse(c1.Equals(c3), "Test IEquatable equals"); //Test object equals override Assert.IsTrue(c1.Equals((object) c2), "Tests object equals"); Assert.IsFalse(c1.Equals((object) c3), "Tests object equals"); //Test op equals Assert.IsTrue(c1 == c2, "Testing OpEquals"); Assert.IsFalse(c1 == c3, "Testing OpEquals"); //Test op not equals Assert.IsTrue(c1 != c3, "Testing OpNotEquals"); Assert.IsFalse(c1 != c2, "Testing OpNotEquals"); }
public void TestEquals() { float r1 = .25f, g1 = .1f, b1 = .75f; float r2 = .75f, g2 = 1.0f, b2 = 1.0f; Color3D c1 = new Color3D(r1, g1, b1); Color3D c2 = new Color3D(r1, g1, b1); Color3D c3 = new Color3D(r2, g2, b2); //Test IEquatable Equals Assert.IsTrue(c1.Equals(c2), "Test IEquatable equals"); Assert.IsFalse(c1.Equals(c3), "Test IEquatable equals"); //Test object equals override Assert.IsTrue(c1.Equals((object)c2), "Tests object equals"); Assert.IsFalse(c1.Equals((object)c3), "Tests object equals"); //Test op equals Assert.IsTrue(c1 == c2, "Testing OpEquals"); Assert.IsFalse(c1 == c3, "Testing OpEquals"); //Test op not equals Assert.IsTrue(c1 != c3, "Testing OpNotEquals"); Assert.IsFalse(c1 != c2, "Testing OpNotEquals"); }
public static Vector4 ToNumerics(this Color3D value) { return(new Vector4(value.R, value.G, value.B, 1.0f)); }
private void objectListView1_CellEditStarting(object sender, CellEditEventArgs e) { if (e.Value is BrgMatFlag) { CheckedListBox clb = new CheckedListBox(); Rectangle rect = new Rectangle(e.CellBounds.Left, e.CellBounds.Bottom, e.CellBounds.Width, 300); clb.Bounds = rect; foreach (object value in Enum.GetValues(typeof(BrgMatFlag))) { clb.Items.Add(value); } this.SetCheckedListBoxSelectedEnums <BrgMatFlag>(clb, (uint)e.Value); e.Control = clb; } else if (e.Value is BrgMeshFlag) { CheckedListBox clb = new CheckedListBox(); Rectangle rect = new Rectangle(e.CellBounds.Left, e.CellBounds.Bottom, e.CellBounds.Width, 300); clb.Bounds = rect; foreach (object value in Enum.GetValues(typeof(BrgMeshFlag))) { clb.Items.Add(value); } this.SetCheckedListBoxSelectedEnums <BrgMeshFlag>(clb, (UInt16)e.Value); e.Control = clb; } else if (e.Value is BrgMeshFormat) { CheckedListBox clb = new CheckedListBox(); Rectangle rect = new Rectangle(e.CellBounds.Left, e.CellBounds.Bottom, e.CellBounds.Width, 300); clb.Bounds = rect; foreach (object value in Enum.GetValues(typeof(BrgMeshFormat))) { clb.Items.Add(value); } this.SetCheckedListBoxSelectedEnums <BrgMeshFormat>(clb, (UInt16)e.Value); e.Control = clb; } else if (e.Value is Color3D) { ThemeColorPicker tcp = new ThemeColorPicker(); tcp.Location = new Point(e.CellBounds.Left, e.CellBounds.Bottom); Color3D col = (Color3D)e.Value; tcp.Color = Color.FromArgb(Convert.ToByte(col.R * Byte.MaxValue), Convert.ToByte(col.G * Byte.MaxValue), Convert.ToByte(col.B * Byte.MaxValue)); e.Control = tcp; } }
public Color3D ReadColor3D() { Color3D color = new Color3D(); color.R = this.ReadSingle(); color.G = this.ReadSingle(); color.B = this.ReadSingle(); return(color); }
public void TestIsBlack() { Color3D c1 = new Color3D(0, 0, 0); Color3D c2 = new Color3D(.25f, 1.0f, .5f) * .002f; Color3D c3 = new Color3D(.25f, .65f, 1.0f); Assert.IsTrue(c1.IsBlack(), "Testing isBlack"); Assert.IsTrue(c2.IsBlack(), "Testing isBlack"); Assert.IsFalse(c3.IsBlack(), "Testing !isBlack"); }
private void objectListView1_FormatCell(object sender, FormatCellEventArgs e) { if (e.CellValue is Color3D) { Color3D col = (Color3D)e.CellValue; e.SubItem.BackColor = Color.FromArgb(Convert.ToByte(col.R * Byte.MaxValue), Convert.ToByte(col.G * Byte.MaxValue), Convert.ToByte(col.B * Byte.MaxValue)); //e.SubItem.ForeColor = this.ContrastColor(e.SubItem.BackColor); e.SubItem.Text = string.Empty; } }
public Material() { Color = new Color3D(0.2f, 0.2f, 0.2f); Reflection = 0f; Diffuse = 0.2f; DiffuseReflection = 0f; Specular = 0.8f; RefractionIndex = 1.5f; Texture = null; SetUVScale(1, 1); }
public void TestIndexer() { float r = .25f, g = .5f, b = .05f; Color3D c = new Color3D(); c[0] = r; c[1] = g; c[2] = b; TestHelper.AssertEquals(r, c[0], "Test Indexer, R"); TestHelper.AssertEquals(g, c[1], "Test Indexer, G"); TestHelper.AssertEquals(b, c[2], "Test Indexer, B"); }
public void TestDivideByFactor() { float r1 = .5f, g1 = .25f, b1 = .7f; float value = .2f; float r = r1 / value; float g = g1 / value; float b = b1 / value; Color3D c1 = new Color3D(r1, g1, b1); Color3D c = c1 / value; TestHelper.AssertEquals(r, g, b, c, "Testing OpDivideByFactor"); }
public void TestDivide() { float r1 = .5f, g1 = .25f, b1 = .7f; float r2 = .2f, g2 = .1f, b2 = .05f; float r = r1 / r2; float g = g1 / g2; float b = b1 / b2; Color3D c1 = new Color3D(r1, g1, b1); Color3D c2 = new Color3D(r2, g2, b2); Color3D c = c1 / c2; TestHelper.AssertEquals(r, g, b, c, "Testing OpDivide"); }
public void TestOpAdd() { float r1 = .5f, g1 = .25f, b1 = .7f; float r2 = .2f, g2 = .1f, b2 = .05f; float r = r1 + r2; float g = g1 + g2; float b = b1 + b2; Color3D c1 = new Color3D(r1, g1, b1); Color3D c2 = new Color3D(r2, g2, b2); Color3D c = c1 + c2; TestHelper.AssertEquals(r, g, b, c, "Testing OpAdd"); }
public void TestOpSubtract() { float r1 = .5f, g1 = .25f, b1 = .7f; float r2 = .2f, g2 = .1f, b2 = .05f; float r = r1 - r2; float g = g1 - g2; float b = b1 - b2; Color3D c1 = new Color3D(r1, g1, b1); Color3D c2 = new Color3D(r2, g2, b2); Color3D c = c1 - c2; TestHelper.AssertEquals(r, g, b, c, "Testing OpSubtract"); }
public void TestOpMultiply() { float r1 = .5f, g1 = .25f, b1 = .7f; float r2 = .2f, g2 = .1f, b2 = .05f; float r = r1 * r2; float g = g1 * g2; float b = b1 * b2; Color3D c1 = new Color3D(r1, g1, b1); Color3D c2 = new Color3D(r2, g2, b2); Color3D c = c1 * c2; TestHelper.AssertEquals(r, g, b, c, "Testing OpMultiply"); }
public void TestOpAddValue() { float r1 = .5f, g1 = .25f, b1 = .7f; float value = .2f; float r = r1 + value; float g = g1 + value; float b = b1 + value; Color3D c1 = new Color3D(r1, g1, b1); //Test left to right Color3D c = c1 + value; TestHelper.AssertEquals(r, g, b, c, "Testing OpAddValue"); //Test right to left c = value + c1; TestHelper.AssertEquals(r, g, b, c, "Testing OpAddValue"); }
public void TestOpMultiplyByScalar() { float r1 = .5f, g1 = .25f, b1 = .7f; float value = .2f; float r = r1 * value; float g = g1 * value; float b = b1 * value; Color3D c1 = new Color3D(r1, g1, b1); //Test left to right Color3D c = c1 * value; TestHelper.AssertEquals(r, g, b, c, "Testing OpMultiplyByValue"); //Test right to left c = value * c1; TestHelper.AssertEquals(r, g, b, c, "Testing OpMultiplyByValue"); }
public void TestOpSubtractByValue() { float r1 = .5f, g1 = .25f, b1 = .7f; float value = .2f; float r = r1 - value; float g = g1 - value; float b = b1 - value; Color3D c1 = new Color3D(r1, g1, b1); //Test left to right Color3D c = c1 - value; TestHelper.AssertEquals(r, g, b, c, "Testing OpSubtractValue"); r = value - r1; g = value - g1; b = value - b1; //Test right to left c = value - c1; TestHelper.AssertEquals(r, g, b, c, "Testing OpSubtractValue"); }
private float[] Colo3DToFloat(Color3D col) { float[] floatType = { col.B, col.G, col.R, 1 }; return(floatType); }
public Light(LightType type, Vector3D position, Color3D color) { Type = type; Position = position; Color = color; }
private Vector3 Colo3DToVector3(Color3D col) { return(new Vector3(col.R, col.G, col.B)); }
public static void AssertEquals(float r, float g, float b, Color3D c, String msg) { AssertEquals(r, c.R, msg + String.Format(" => checking R component ({0} == {1}", r, c.R)); AssertEquals(g, c.G, msg + String.Format(" => checking G component ({0} == {1}", g, c.G)); AssertEquals(b, c.B, msg + String.Format(" => checking B component ({0} == {1}", b, c.B)); }
public static void AssertEquals(float r, float g, float b, Color3D c) { AssertEquals(r, c.R); AssertEquals(g, c.G); AssertEquals(b, c.B); }
public static Vector3 ToMath3D(this Color3D v) => new Vector3(v.R, v.G, v.B);
public void SetParameters(double refl, double refr, Color3D col, double diff, double spec) { Reflection = refl; Refraction = refr; Color = col; Diffuse = diff; Specular = spec; }
public static int ToInteger(Color3D color) { return((int)(color.R * 255) | ((int)(color.G * 255) << 8) | ((int)(color.B * 255) << 16)); }
public void WriteColor3D(Color3D color) { this.Write(color.R); this.Write(color.G); this.Write(color.B); }
public float ToFloat(Color3D color) { return((color.R + color.G + color.B) / 3f); }
private void Form1_Load(object sender, EventArgs e) { //摄像头 Point3D cameraPos = new Point3D(0, 0, -4); //球 Sphere sphere1 = new Sphere(new Point3D(0, -31, 1), 30); sphere1.Metiral.Color = new Color3D(1, 0, 0); sphere1.Metiral.Kd = 1; sphere1.Metiral.Ks = 0.3; sphere1.Metiral.Ns = 20; Sphere sphere2 = new Sphere(new Point3D(0.8, 0, 1), 0.1); sphere2.Metiral.Color = new Color3D(0, 1, 0); sphere2.Metiral.Kd = 1.0; sphere2.Metiral.Ks = 0.3; sphere2.Metiral.Ns = 20; Sphere sphere3 = new Sphere(new Point3D(-1.2, 0, 1), 0.5); sphere3.Metiral.Color = new Color3D(0, 0, 1); sphere3.Metiral.Kd = 1.0; sphere3.Metiral.Ks = 0.3; sphere3.Metiral.Ns = 20; //添加场景 Scenes scenes = new Scenes(); scenes.Add(sphere1); scenes.Add(sphere2); scenes.Add(sphere3); //成像平面 ViewPlane vp = new ViewPlane(); vp.LeftTopCorner = new Point3D(-2, 1, 1); vp.Width = 4; vp.Height = 2; vp.ResW = 800; vp.ResH = 400; //光源的位置 Point3D lightPos = new Point3D(3, -4, 6); //光源的强度 Color3D lightColor = new Color3D(0.4); //环境光 Color3D ambientColor = new Color3D(1); double ka = 0.1; double xOffset = vp.GetxOffset(); double yOffset = vp.GetyOffset(); bmp = new Bitmap(vp.ResW, vp.ResH); for (int i = 0; i < vp.ResW; i++) { for (int j = 0; j < vp.ResH; j++) { //计算成像平面上的每一个像素点坐标 Point3D vpPoint = new Point3D(-2 + i * xOffset, 1 - j * yOffset, vp.LeftTopCorner.Z); //方向 Vector3D dir = vpPoint - cameraPos; //初始光线 Ray primaryRay = new Ray(cameraPos, dir); //计算交点 HitRecord hitRecord = primaryRay.HitScenes(scenes); if (hitRecord.IsHit) { //交点到光源的方向 Vector3D L = hitRecord.HitPoint - lightPos; L.Nomalize(); double cosTheta = (L * hitRecord.Nomal) / Math.Sqrt(L.Length() * L.Length() + hitRecord.Nomal.Length() * hitRecord.Nomal.Length()); //double cosTheta = (L * hitRecord.Nomal); if (cosTheta > 0) { cosTheta = 0; } //交点到眼睛方向 Vector3D V = hitRecord.HitPoint - cameraPos; V.Nomalize(); //阴影光线 Ray shadowRay = new Ray(hitRecord.HitPoint, L); if (shadowRay.shadowHitScenes(scenes) == false) { //镜面反射光路 Vector3D R = 2 * hitRecord.Nomal * (hitRecord.Nomal * L) - L; R.Nomalize(); //计算漫反射颜色+环境光+镜面反射 Color3D diffuseColor = ka * ambientColor + hitRecord.Metiral.Kd * hitRecord.Metiral.Color * lightColor * (L * hitRecord.Nomal) //+ hitRecord.Metiral.Kd * hitRecord.Metiral.Color * lightColor * cosTheta + hitRecord.Metiral.Ks * hitRecord.Metiral.Color * lightColor * Math.Pow((V * R), hitRecord.Metiral.Ns); bmp.SetPixel(i, j, diffuseColor.ToSystemColor()); } else { bmp.SetPixel(i, j, Color.Black); } } else { bmp.SetPixel(i, j, Color.Gray); } } } pictureBox1.BackgroundImage = bmp; }