Exemplo n.º 1
0
// calculates the color for the current pixel
//************************************
// Method:    calcPixelColor
// FullName:  calcPixelColor
// Access:    public 
// Returns:   vec3
// Qualifier:
// Parameter: const ray & r
// Parameter: hitable * world
// Parameter: int depth
// Parameter: shadingmodel shading
//************************************
vec3 calcPixelColor(const ray& r, hitable* world, int depth, shadingmodel shading)
{
	hit_record rec;

	// shade hit color
	if (world->hit(r, 0.001f, FLT_MAX, rec))
	{
		switch(shading)
		{
			case normals:	
						{	return 0.5f * vec3(rec.normal.x + 1.f, rec.normal.y + 1.f, rec.normal.z + 1.f);
							break;						
						}
			case shaded:
						{	ray scattered;
							vec3 attenuation;
							if (depth < 50 && rec.mtlPtr->scatter(r, rec, attenuation, scattered))
							{
								return attenuation * calcPixelColor(scattered, world, depth + 1, shaded);
							}
							else
								return vec3(0.f, 0.f, 0.f);
						}

			default:	{	return 0.5f * vec3(rec.normal.x + 1.f, rec.normal.y + 1.f, rec.normal.z + 1.f);
							break;						
						}
		}
	}
	// shade background color
	else
	{
		// normalize (i.e. make unit vector) ray direction, -1.0 < (B.x, B.y, B.z) < 1.0
		vec3 unitDirection = r.Direction();
		unitDirection.Normalize();

		// calculate ray parameter, t, scaled to 0.0 < t < 1.0
		float t = 0.5f * (unitDirection.y + 1.f);
		
		// calculate color of pixel at parameter t, where
		// @t == 0.0 :: color == white
		// @t == 1.0 :: color == blue
		// i.e. a LERP between white and blue
		// LERP'ed color = (1-t) * startValue + t * EndValue
		// ---> LERP'ed color = (1-t) * <white> + t * <blue>
		return (1.f - t) * vec3(1.f, 1.f, 1.f) + t * vec3(0.5f, 0.7f, 1.f);
	}
}