-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
executable file
·101 lines (87 loc) · 4.48 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Numerics;
using System.IO;
namespace SimplexRay
{
public class PPMImageSaver
{
public void Save(Image image, string path)
{
using (FileStream steam = File.OpenWrite(path))
{
using (StreamWriter writer = new StreamWriter(steam))
{
writer.WriteLine("P3");
writer.WriteLine("{0} {1}", image.Width, image.Height);
writer.WriteLine("255");
foreach (Vector3 pixel in image.Pixels)
writer.WriteLine("{0} {1} {2}", (int)(255.99f * pixel.X), (int)(255.99f * pixel.Y), (int)(255.99f * pixel.Z));
}
}
}
}
class Program
{
static void Main(string[] args)
{
ISceneGraph scene_graph = SceneGraphFactory.Create();
ITexture checker_texture = new CheckerTexture(new ConstantTexture(new Vector3(0.2f, 0.3f, 0.1f)), new ConstantTexture(new Vector3(0.9f, 0.9f, 0.9f)));
SphereNode sphere = scene_graph.AddShape(Shape.Sphere) as SphereNode;
sphere.Translation = new Vector3(0.0f, -1000.0f, 0.0f);
sphere.Material = new LambertianMaterial(checker_texture);
sphere.Radius = 1000.0f;
sphere = scene_graph.AddShape(Shape.Sphere) as SphereNode;
sphere.Translation = new Vector3(0.0f, 1.0f, 0.0f);
sphere.Material = new DielectricMaterial(1.5f);
sphere.Radius = 1.0f;
sphere = scene_graph.AddShape(Shape.Sphere) as SphereNode;
sphere.Translation = new Vector3(0.0f, 3.0f, 0.0f);
sphere.Material = new DiffuseLightMaterial(new ConstantTexture(new Vector3(3.0f, 3.0f, 3.0f)));
sphere.Radius = 1.0f;
sphere = scene_graph.AddShape(Shape.Sphere) as SphereNode;
sphere.Translation = new Vector3(-4.0f, 1.0f, 0.0f);
sphere.Material = new LambertianMaterial(new ConstantTexture(new Vector3(0.4f, 0.2f, 0.1f)));
sphere.Radius = 1.0f;
sphere = scene_graph.AddShape(Shape.Sphere) as SphereNode;
sphere.Translation = new Vector3(4.0f, 1.0f, 0.0f);
sphere.Material = new MetalMaterial(new ConstantTexture(new Vector3(0.7f, 0.6f, 0.5f)), 0.0f);
sphere.Radius = 1.0f;
for (int z = -10; z <= 10; ++z)
{
for (int x = -10; x <= 10; ++x)
{
float choose_material = MathHelper.RandomFloat();
Vector3 translation = new Vector3(x + 0.9f * MathHelper.RandomFloat(), 0.2f, z + 0.9f * MathHelper.RandomFloat());
if (Vector3.Distance(translation, new Vector3(4.0f, 0.2f, 0.0f)) > 0.9f)
{
SphereNode ball = scene_graph.AddShape(Shape.Sphere) as SphereNode;
ball.Translation = translation;
ball.Radius = 0.2f;
if (choose_material < 0.8f)
{
ball.Material = new LambertianMaterial(new ConstantTexture(new Vector3(MathHelper.RandomFloat() * MathHelper.RandomFloat(), MathHelper.RandomFloat() * MathHelper.RandomFloat(), MathHelper.RandomFloat() * MathHelper.RandomFloat())));
}
else if (choose_material < 0.95f)
{
ball.Material = new MetalMaterial(new ConstantTexture(new Vector3(1.0f + MathHelper.RandomFloat(), 1.0f + MathHelper.RandomFloat(), 1.0f + MathHelper.RandomFloat()) * 0.5f), 0.5f * MathHelper.RandomFloat());
}
else
{
ball.Material = new DielectricMaterial(1.5f);
}
}
}
}
ICameraNode camera = scene_graph.AddCamera();
camera.View.Width = 1200;
camera.View.Height = 800;
camera.SetProjection(20.0f);
camera.Translation = new Vector3(13.0f, 2.0f, 3.0f);
camera.Rotation = MathHelper.LookAt(camera.Translation, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
Image image = scene_graph.Render(camera, 50, 50);
PPMImageSaver saver = new PPMImageSaver();
string path = args.Length > 0 ? args[0] : "output.ppm";
saver.Save(image, path);
}
}
}