468942589f140f78c5dd3497d2c0837b8b3f7cfd
[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 // shoot four rays for antialiasing
56 scene.camera()->InitRay(x+0.5,y+0.5, ray); // initialize ray
57 Vec3f col1 = scene.RayTrace(ray);
58 scene.camera()->InitRay(x-0.5,y+0.5, ray); // initialize ray
59 Vec3f col2 = scene.RayTrace(ray);
60 scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray
61 Vec3f col3 = scene.RayTrace(ray);
62 scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray
63 Vec3f col4 = scene.RayTrace(ray);
64
65 img(x,y) = (col1 + col2 + col3 + col4) / 4.0; // store pixel color
66 //std::cerr << "Main: Image color = " << img(x,y) << std::endl;
67 }
68 }
69 img.WritePPM(fileName); // write final image
70 }
71
72
73 void RenderFrameEyeLight(const std::string& fileName)
74 {
75 /* Scene definition */
76
77 Scene scene;
78
79 /* Flat shaders */
80 EyeLightShader shd1(&scene, Vec3f(1,0,0)); // red surface
81 EyeLightShader shd2(&scene, Vec3f(0,1,0)); // green surface
82 EyeLightShader shd3(&scene, Vec3f(0,0,1)); // blue surface
83 EyeLightShader shd4(&scene, Vec3f(1,1,0)); // yellow surface
84 EyeLightShader shd5(&scene, Vec3f(0,1,1)); // cyan surface
85 EyeLightShader shd6(&scene, Vec3f(1,1,1)); // white surface
86
87 /* scene objects */
88 Sphere s1(Vec3f(-2,1.7,0), 2, &shd1);
89 Sphere s2(Vec3f(1,-1,1), 2.2, &shd2);
90 Sphere s3(Vec3f(3,0.8,-2), 2, &shd3);
91 InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4);
92
93
94 Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5);
95
96 /* add to scene */
97 scene.Add(&s1);
98 scene.Add(&s2);
99 scene.Add(&s3);
100 scene.Add(&p1);
101 scene.Add(&t1);
102
103 Image img(scene.camera()->resX(),scene.camera()->resY()); // image array
104 Ray ray; // primary ray
105
106 for(int y = 0; y < scene.camera()->resY(); y++)
107 {
108 for (int x = 0; x < scene.camera()->resX(); x++)
109 {
110
111 /* Initialize your ray here */
112 // shoot four rays for antialiasing
113 scene.camera()->InitRay(x+0.5,y+0.5, ray); // initialize ray
114 Vec3f col1 = scene.RayTrace(ray);
115 scene.camera()->InitRay(x-0.5,y+0.5, ray); // initialize ray
116 Vec3f col2 = scene.RayTrace(ray);
117 scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray
118 Vec3f col3 = scene.RayTrace(ray);
119 scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray
120 Vec3f col4 = scene.RayTrace(ray);
121
122 img(x,y) = (col1 + col2 + col3 + col4) / 4.0; // store pixel color
123 }
124 }
125 img.WritePPM(fileName); // write final image
126 }
127
128 void RenderFrameReflectiveEyeLight(const std::string& fileName)
129 {
130 /* Scene definition */
131
132 Scene scene;
133
134 /* Flat shaders */
135 ReflectiveEyeLightShader shd1(&scene, 1.0, Vec3f(1,0,0)); // red surface
136 ReflectiveEyeLightShader shd2(&scene, 1.0, Vec3f(0.0,1.0,0.0)); // green surface
137 ReflectiveEyeLightShader shd3(&scene, 1.0, Vec3f(0,0,1)); // blue surface
138 ReflectiveEyeLightShader shd4(&scene, 0.8, Vec3f(1,1,0)); // yellow surface
139 ReflectiveEyeLightShader shd5(&scene, 1.0, Vec3f(0,1,1)); // cyan surface
140 ReflectiveEyeLightShader shd6(&scene, 0.0, Vec3f(1,1,1)); // white surface
141
142 /* scene objects */
143 Sphere s1(Vec3f(-2,1.7,0), 2, &shd1);
144 Sphere s2(Vec3f(0.4,-1,1), 2.0, &shd2);
145 Sphere s3(Vec3f(3,0.8,-2), 2, &shd3);
146 InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4);
147
148
149 Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5);
150
151 /* add to scene */
152 scene.Add(&s1);
153 scene.Add(&s2);
154 scene.Add(&s3);
155 scene.Add(&p1);
156 scene.Add(&t1);
157
158
159 Image img(scene.camera()->resX(),scene.camera()->resY()); // image array
160 Ray ray; // primary ray
161
162 for(int y = 0; y < scene.camera()->resY(); y++)
163 {
164 for (int x = 0; x < scene.camera()->resX(); x++)
165 {
166 /* Initialize your ray here */ //int x = 319, y = 59;
167 // shoot four rays for antialiasing
168 scene.camera()->InitRay(x+0.5,y+0.5, ray); // initialize ray
169 Vec3f col1 = scene.RayTrace(ray);
170 scene.camera()->InitRay(x-0.5,y+0.5, ray); // initialize ray
171 Vec3f col2 = scene.RayTrace(ray);
172 scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray
173 Vec3f col3 = scene.RayTrace(ray);
174 scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray
175 Vec3f col4 = scene.RayTrace(ray);
176
177 img(x,y) = (col1 + col2 + col3 + col4) / 4.0; // store pixel color
178 //std::cerr << "Main: Image color = " << img(x,y) << std::endl;
179 }
180 }
181 img.WritePPM(fileName); // write final image
182 }
183
184
185
186 #define RESX 640 // image x-resolution
187 #define RESY 480 // image y-resolution
188
189 int main(int, char**)
190 {
191 //RenderFrameFlat("flatshaded.ppm");
192 //RenderFrameEyeLight("eyelight.ppm");
193 RenderFrameReflectiveEyeLight("reflective.ppm");
194 }
This page took 0.054245 seconds and 3 git commands to generate.