New lighting object tree: AmbientLight -> PointLight
This commit is contained in:
		
							parent
							
								
									9fef3c33d2
								
							
						
					
					
						commit
						55ef253c10
					
				
					 5 changed files with 99 additions and 32 deletions
				
			
		|  | @ -41,8 +41,10 @@ main(int argc, | |||
|     scene.add_shape(s2); | ||||
|     scene.add_shape(s3); | ||||
| 
 | ||||
|     Light *l1 = new Light(Vector3(500.0, 100.0, 600.0), 1.0); | ||||
|     PointLight *l1 = new PointLight(Vector3(0.0, 240.0, 100.0), Color(1.0, 1.0, 1.0), 1.0); | ||||
|     //Light *l2 = new Light(Vector3(640.0, 240.0, 10000.0), 0.2);
 | ||||
|     scene.add_light(l1); | ||||
|     //scene.add_light(l2);
 | ||||
| 
 | ||||
|     // Render.
 | ||||
|     scene.render(); | ||||
|  |  | |||
							
								
								
									
										83
									
								
								src/light.cc
									
										
									
									
									
								
							
							
						
						
									
										83
									
								
								src/light.cc
									
										
									
									
									
								
							|  | @ -9,38 +9,85 @@ | |||
| #include "light.h" | ||||
| #include "object.h" | ||||
| 
 | ||||
| #pragma mark - Ambient Lights | ||||
| 
 | ||||
| Light::Light() | ||||
|     : Light(1.0) | ||||
| AmbientLight::AmbientLight() | ||||
|     : AmbientLight(Color::White) | ||||
| { } | ||||
| 
 | ||||
| 
 | ||||
| Light::Light(float i) | ||||
|     : Light(Vector3::Zero, i) | ||||
| AmbientLight::AmbientLight(const Color &c) | ||||
|     : AmbientLight(c, 1.0) | ||||
| { } | ||||
| 
 | ||||
| 
 | ||||
| Light::Light(Vector3 o, float i) | ||||
|     : Object(o), | ||||
| AmbientLight::AmbientLight(const Color &c, const float &i) | ||||
|     : color(c), | ||||
|       intensity(i) | ||||
| { } | ||||
| { | ||||
|     _clamp_intensity(); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| /*
 | ||||
|  * Light::get_intensity -- | ||||
|  * Light::set_intensity -- | ||||
|  * | ||||
|  * Get and set the intensity of this light. | ||||
|  */ | ||||
| float | ||||
| Light::get_intensity() | ||||
| const Color & | ||||
| AmbientLight::get_color() | ||||
|     const | ||||
| { | ||||
|     return color; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| const float & | ||||
| AmbientLight::get_intensity() | ||||
|     const | ||||
| { | ||||
|     return intensity; | ||||
| } | ||||
| 
 | ||||
| void | ||||
| Light::set_intensity(float i) | ||||
| 
 | ||||
| Color | ||||
| AmbientLight::compute_color_contribution() | ||||
|     const | ||||
| { | ||||
|     intensity = i; | ||||
|     return color * intensity; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void | ||||
| AmbientLight::_clamp_intensity() | ||||
| { | ||||
|     if (intensity < 0.0) { | ||||
|         intensity = 0.0; | ||||
|     } | ||||
|     else if (intensity > 1.0) { | ||||
|         intensity = 1.0; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #pragma mark - Point Lights | ||||
| 
 | ||||
| PointLight::PointLight() | ||||
|     : AmbientLight(), | ||||
|       Object() | ||||
| { } | ||||
| 
 | ||||
| 
 | ||||
| PointLight::PointLight(const Vector3 &o) | ||||
|     : AmbientLight(), | ||||
|       Object(o) | ||||
| { } | ||||
| 
 | ||||
| 
 | ||||
| PointLight::PointLight(const Vector3 &o, | ||||
|                        const Color &c) | ||||
|     : AmbientLight(c), | ||||
|       Object(o) | ||||
| { } | ||||
| 
 | ||||
| 
 | ||||
| PointLight::PointLight(const Vector3 &o, | ||||
|                        const Color &c, | ||||
|                        const float &i) | ||||
|     : AmbientLight(c, i), | ||||
|       Object(o) | ||||
| { } | ||||
|  |  | |||
							
								
								
									
										33
									
								
								src/light.h
									
										
									
									
									
								
							
							
						
						
									
										33
									
								
								src/light.h
									
										
									
									
									
								
							|  | @ -12,19 +12,36 @@ | |||
| #include "object.h" | ||||
| 
 | ||||
| 
 | ||||
| class Light | ||||
|     : public Object | ||||
| class AmbientLight | ||||
| { | ||||
| public: | ||||
|     Light(); | ||||
|     Light(float i); | ||||
|     Light(Vector3 o, float i); | ||||
|     AmbientLight(); | ||||
|     AmbientLight(const Color &c); | ||||
|     AmbientLight(const Color &c, const float &i); | ||||
| 
 | ||||
|     float get_intensity() const; | ||||
|     void set_intensity(float i); | ||||
|     const Color &get_color() const; | ||||
|     const float &get_intensity() const; | ||||
| 
 | ||||
|     Color compute_color_contribution() const; | ||||
| 
 | ||||
| protected: | ||||
|     Color color; | ||||
|     float intensity; | ||||
| 
 | ||||
| private: | ||||
|     float intensity; | ||||
|     void _clamp_intensity(); | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| class PointLight | ||||
|     : public AmbientLight, | ||||
|       public Object | ||||
| { | ||||
| public: | ||||
|     PointLight(); | ||||
|     PointLight(const Vector3 &o); | ||||
|     PointLight(const Vector3 &o, const Color &c); | ||||
|     PointLight(const Vector3 &o, const Color &c, const float &i); | ||||
| }; | ||||
| 
 | ||||
| #endif | ||||
|  |  | |||
|  | @ -140,7 +140,7 @@ Scene::add_shape(Shape *shape) | |||
|  * Add a light to the scene. | ||||
|  */ | ||||
| void | ||||
| Scene::add_light(Light *light) | ||||
| Scene::add_light(PointLight *light) | ||||
| { | ||||
|     lights.push_back(light); | ||||
| } | ||||
|  | @ -186,7 +186,7 @@ Scene::trace_ray(const Ray &ray, const int depth) | |||
|     Vector3 intersection = ray.parameterize(nearest_t); | ||||
|     Vector3 normal = intersected_shape->compute_normal(intersection); | ||||
| 
 | ||||
|     for (Light *l : lights) { | ||||
|     for (PointLight *l : lights) { | ||||
|         Vector3 light_direction = (intersection - l->get_origin()).normalize(); | ||||
|         float ldotn = light_direction.dot(normal); | ||||
|         out_color.red *= ((ldotn >= 0.0) ? ldotn : 0.0) * l->get_intensity(); | ||||
|  |  | |||
|  | @ -16,7 +16,8 @@ | |||
| #include "basics.h" | ||||
| 
 | ||||
| 
 | ||||
| class Light; | ||||
| class AmbientLight; | ||||
| class PointLight; | ||||
| class Shape; | ||||
| class Writer; | ||||
| 
 | ||||
|  | @ -37,7 +38,7 @@ public: | |||
|     void render(); | ||||
| 
 | ||||
|     void add_shape(Shape *obj); | ||||
|     void add_light(Light *light); | ||||
|     void add_light(PointLight *light); | ||||
| 
 | ||||
| private: | ||||
|     Color trace_ray(const Ray &ray, const int depth); | ||||
|  | @ -50,7 +51,7 @@ private: | |||
| 
 | ||||
|     // Scene objects.
 | ||||
|     std::list<Shape *> shapes; | ||||
|     std::list<Light *> lights; | ||||
|     std::list<PointLight *> lights; | ||||
| 
 | ||||
|     // Rendering stats
 | ||||
|     unsigned int nrays; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue