53c56ee8adfed744f3effd4af411ecc654dd001f
[MicroTrace.git] / MicroTrace.cxx
1 #include <string>
2
3 #include "Vec3f.hxx"
4 #include "Sphere.hxx"
5 #include "Triangle.hxx"
6 #include "InfinitePlane.hxx"
7
8 #include "Image.hxx"
9 #include "PerspectiveCamera.hxx"
10 #include "FlatShader.hxx"
11 #include "EyeLightShader.hxx"
12 #include "ReflectiveEyeLightShader.hxx"
13 #include "Scene.hxx"
14
15 void RenderFrameFlat(const std::string& fileName)
16 {
17 /* Scene definition */
18
19 Scene scene;
20
21 /* Flat shaders */
22 FlatShader shd1(&scene, Vec3f(1,0,0)); // red surface
23 FlatShader shd2(&scene, Vec3f(0,1,0)); // green surface
24 FlatShader shd3(&scene, Vec3f(0,0,1)); // blue surface
25 FlatShader shd4(&scene, Vec3f(1,1,0)); // yellow surface
26 FlatShader shd5(&scene, Vec3f(0,1,1)); // cyan surface
27 FlatShader shd6(&scene, Vec3f(1,1,1)); // white surface
28
29 /* scene objects */
30 Sphere s1(Vec3f(-2,1.7,0), 2, &shd1);
31 Sphere s2(Vec3f(1,-1,1), 2.2, &shd2);
32 Sphere s3(Vec3f(3,0.8,-2), 2, &shd3);
33 InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4);
34
35
36 Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5);
37
38 /* add to scene */
39 scene.Add(&s1);
40 scene.Add(&s2);
41 scene.Add(&s3);
42 scene.Add(&p1);
43 scene.Add(&t1);
44
45
46 Image img(scene.camera()->resX(),scene.camera()->resY()); // image array
47 Ray ray; // primary ray
48
49 for(int y = 0; y < scene.camera()->resY(); y++)
50 {
51 for (int x = 0; x < scene.camera()->resX(); x++)
52 {
53
54 /* Initialize your ray here */
55
56 scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray
57
58 Vec3f col = scene.RayTrace(ray);
59
60 img(x,y) = col; // store pixel color
61 //std::cerr << "Main: Image color = " << img(x,y) << std::endl;
62 }
63 }
64 img.WritePPM(fileName); // write final image
65 }
66
67
68 void RenderFrameEyeLight(const std::string& fileName)
69 {
70 /* Scene definition */
71
72 Scene scene;
73
74 /* Flat shaders */
75 EyeLightShader shd1(&scene, Vec3f(1,0,0)); // red surface
76 EyeLightShader shd2(&scene, Vec3f(0,1,0)); // green surface
77 EyeLightShader shd3(&scene, Vec3f(0,0,1)); // blue surface
78 EyeLightShader shd4(&scene, Vec3f(1,1,0)); // yellow surface
79 EyeLightShader shd5(&scene, Vec3f(0,1,1)); // cyan surface
80 EyeLightShader shd6(&scene, Vec3f(1,1,1)); // white surface
81
82 /* scene objects */
83 Sphere s1(Vec3f(-2,1.7,0), 2, &shd1);
84 Sphere s2(Vec3f(1,-1,1), 2.2, &shd2);
85 Sphere s3(Vec3f(3,0.8,-2), 2, &shd3);
86 InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4);
87
88
89 Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5);
90
91 /* add to scene */
92 scene.Add(&s1);
93 scene.Add(&s2);
94 scene.Add(&s3);
95 scene.Add(&p1);
96 scene.Add(&t1);
97
98 Image img(scene.camera()->resX(),scene.camera()->resY()); // image array
99 Ray ray; // primary ray
100
101 for(int y = 0; y < scene.camera()->resY(); y++)
102 {
103 for (int x = 0; x < scene.camera()->resX(); x++)
104 {
105
106 /* Initialize your ray here */
107
108 scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray
109
110 Vec3f col = scene.RayTrace(ray);
111
112 img(x,y) = col; // store pixel color
113 //std::cerr << "Main: Image color = " << img(x,y) << std::endl;
114 }
115 }
116 img.WritePPM(fileName); // write final image
117 }
118
119 void RenderFrameReflectiveEyeLight(const std::string& fileName)
120 {
121 /* Scene definition */
122
123 Scene scene;
124
125 /* Flat shaders */
126 ReflectiveEyeLightShader shd1(&scene, 1.0, Vec3f(1,0,0)); // red surface
127 ReflectiveEyeLightShader shd2(&scene, 1.0, Vec3f(0.0,1.0,0.0)); // green surface
128 ReflectiveEyeLightShader shd3(&scene, 1.0, Vec3f(0,0,1)); // blue surface
129 ReflectiveEyeLightShader shd4(&scene, 0.8, Vec3f(1,1,0)); // yellow surface
130 ReflectiveEyeLightShader shd5(&scene, 1.0, Vec3f(0,1,1)); // cyan surface
131 ReflectiveEyeLightShader shd6(&scene, 0.0, Vec3f(1,1,1)); // white surface
132
133 /* scene objects */
134 Sphere s1(Vec3f(-2,1.7,0), 2, &shd1);
135 Sphere s2(Vec3f(0.4,-1,1), 2.0, &shd2);
136 Sphere s3(Vec3f(3,0.8,-2), 2, &shd3);
137 InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4);
138
139
140 Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5);
141
142 /* add to scene */
143 scene.Add(&s1);
144 scene.Add(&s2);
145 scene.Add(&s3);
146 scene.Add(&p1);
147 scene.Add(&t1);
148
149
150 Image img(scene.camera()->resX(),scene.camera()->resY()); // image array
151 Ray ray; // primary ray
152
153 for(int y = 0; y < scene.camera()->resY(); y++)
154 {
155 for (int x = 0; x < scene.camera()->resX(); x++)
156 {
157
158 /* Initialize your ray here */
159
160 scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray
161
162 Vec3f col = scene.RayTrace(ray);
163
164 img(x,y) = col; // store pixel color
165 //std::cerr << "Main: Image color = " << img(x,y) << std::endl;
166 }
167 }
168 img.WritePPM(fileName); // write final image
169 }
170
171
172
173 #define RESX 640 // image x-resolution
174 #define RESY 480 // image y-resolution
175
176 int main(int, char**)
177 {
178 RenderFrameFlat("flatshaded.ppm");
179 RenderFrameEyeLight("eyelight.ppm");
180 RenderFrameReflectiveEyeLight("reflective.ppm");
181 }
This page took 0.064285 seconds and 3 git commands to generate.