Phong does not work yet, but saving here
[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 "PhongShader.hxx"
14 #include "PointLight.hxx"
15 #include "SpotLight.hxx"
16 #include "Scene.hxx"
17
18
19 void RenderFramePhongPointLight(const std::string& fileName)
20 {
21 /* Scene definition */
22 Scene scene;
23
24 /* Flat shaders */
25 PhongShader shd1(&scene, Vec3f(1,0,0),Vec3f(1,0,0),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // red surface
26 PhongShader shd2(&scene, Vec3f(0,1,0),Vec3f(0,1,0),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // green surface
27 PhongShader shd3(&scene, Vec3f(0,0,1),Vec3f(0,0,1),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // blue surface
28 PhongShader shd4(&scene, Vec3f(1,1,0),Vec3f(1,1,0),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // yellow surface
29 PhongShader shd5(&scene, Vec3f(0,1,1),Vec3f(0,1,1),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // cyan surface
30 PhongShader shd6(&scene, Vec3f(1,1,1),Vec3f(1,1,1),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // white surface
31
32 /* scene objects */
33 Sphere s1(Vec3f(-2,1.7,0), 2, &shd1);
34 Sphere s2(Vec3f(1,-1,1), 2.2, &shd2);
35 Sphere s3(Vec3f(3,0.8,-2), 2, &shd3);
36 InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4);
37
38
39 Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5);
40
41 /* add to scene */
42 scene.Add(&s1);
43 scene.Add(&s2);
44 scene.Add(&s3);
45 scene.Add(&p1);
46 scene.Add(&t1);
47
48 /* light sources */
49 Vec3f lightPosition1(4,5,6);
50 Vec3f lightPosition2(-3,5,4);
51 Vec3f pointLightSourceIntensity(50,50,50);
52
53 PointLight pointLight1(&scene, lightPosition1, pointLightSourceIntensity);
54 PointLight pointLight2(&scene, lightPosition2, pointLightSourceIntensity);
55
56 scene.Add(&pointLight1);
57 scene.Add(&pointLight2);
58
59
60 Image img(scene.camera()->resX(),scene.camera()->resY()); // image array
61 Ray ray; // primary ray
62
63 for(int y = 0; y < scene.camera()->resY(); y++)
64 {
65 for (int x = 0; x < scene.camera()->resX(); x++)
66 {
67
68 /* Initialize your ray here */
69
70 scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray
71
72 Vec3f col = scene.RayTrace(ray);
73
74 img(x,y) = col; // store pixel color
75 //std::cerr << "Main: Image color = " << img(x,y) << std::endl;
76 }
77 }
78 img.WritePPM(fileName); // write final image
79 }
80
81 void RenderFramePhongSpotLight(const std::string& fileName)
82 {
83 /* Scene definition */
84 Scene scene;
85
86 /* Flat shaders */
87 PhongShader shd1(&scene, Vec3f(1,0,0),Vec3f(1,0,0),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // red surface
88 PhongShader shd2(&scene, Vec3f(0,1,0),Vec3f(0,1,0),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // green surface
89 PhongShader shd3(&scene, Vec3f(0,0,1),Vec3f(0,0,1),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // blue surface
90 PhongShader shd4(&scene, Vec3f(1,1,0),Vec3f(1,1,0),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // yellow surface
91 PhongShader shd5(&scene, Vec3f(0,1,1),Vec3f(0,1,1),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // cyan surface
92 PhongShader shd6(&scene, Vec3f(1,1,1),Vec3f(1,1,1),Vec3f(1,1,1), 0.1, 0.5, 0.5, 40); // white surface
93
94 /* scene objects */
95 Sphere s1(Vec3f(-2,1.7,0), 2, &shd1);
96 Sphere s2(Vec3f(1,-1,1), 2.2, &shd2);
97 Sphere s3(Vec3f(3,0.8,-2), 2, &shd3);
98 InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4);
99
100
101 Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5);
102
103 /* add to scene */
104 scene.Add(&s1);
105 scene.Add(&s2);
106 scene.Add(&s3);
107 scene.Add(&p1);
108 scene.Add(&t1);
109
110 /* light sources */
111 Vec3f lightPosition1(4,5,6);
112 Vec3f lightPosition2(-3,5,4);
113 Vec3f spotLightSourceIntensity(50,50,50);
114 Vec3f lightDir1 = lightPosition1 * (-1.0f);
115 lightDir1.normalize();
116 Vec3f lightDir2 = lightPosition2 *(-1.0f);
117 lightDir2.normalize();
118 float alpha_min = 15.0f;
119 float alpha_max = 30.0f;
120
121 SpotLight spotLight1(&scene, lightPosition1, lightDir1, spotLightSourceIntensity, alpha_min, alpha_max);
122 SpotLight spotLight2(&scene, lightPosition2, lightDir2, spotLightSourceIntensity, alpha_min, alpha_max);
123
124 scene.Add(&spotLight1);
125 scene.Add(&spotLight2);
126
127
128 Image img(scene.camera()->resX(),scene.camera()->resY()); // image array
129 Ray ray; // primary ray
130
131 for(int y = 0; y < scene.camera()->resY(); y++)
132 {
133 for (int x = 0; x < scene.camera()->resX(); x++)
134 {
135
136 /* Initialize your ray here */
137
138 scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray
139
140 Vec3f col = scene.RayTrace(ray);
141
142 img(x,y) = col; // store pixel color
143 //std::cerr << "Main: Image color = " << img(x,y) << std::endl;
144 }
145 }
146 img.WritePPM(fileName); // write final image
147 }
148
149 #define RESX 640 // image x-resolution
150 #define RESY 480 // image y-resolution
151
152 int main(int, char**)
153 {
154 RenderFramePhongPointLight("phong_point.ppm");
155 RenderFramePhongSpotLight("phong_spot.ppm");
156 }
This page took 0.081339 seconds and 5 git commands to generate.