Color3 LightShadeModel::calcPhongLighting( Vector3D n, Vector3D v, Vector3D l, Color3 v_color, Color3 ls_clr, Material* obj_mat, UINT colorMask){ Color3 totalColor(0, 0, 0); if (colorMask & MASK_AMBIENT) { Color3 ac = v_color * ls_clr * obj_mat->ambient; totalColor += ac; } Vector3D r = 2 * (l * n) * n - l; if (colorMask & MASK_DIFFUSE) { double diff = l*n; if (diff < 0) diff = 0; Color3 dc = diff * v_color * ls_clr * obj_mat->diffuse; totalColor += dc; } if (colorMask & MASK_SPECULAR) { double spec = r*v; if (spec < 0) spec = 0; spec = pow(spec, double(obj_mat->shininess)); Color3 sc = spec * v_color * ls_clr * obj_mat->specular; totalColor += sc; } return totalColor; }
glm::vec4 BRDF::sampleColor(FastStack<Ray> & rays, const Ray ray, const Intersection & result, Scene * scene) { glm::vec4 position = result.surface->worldTransform * result.surface->samplePosition(result.index, result.coord); glm::vec4 normal = glm::normalize( result.surface->worldTransformIT * result.surface->sampleNormal(result.index, result.coord)); float p = SampleUniform(); glm::vec4 totalColor(0.f, 0.f, 0.f, 0.f); if (p < emissiveProbability) { totalColor = emissiveIntensity * emissiveColor / emissiveProbability; } else if (ray.depth < maxDepth) { glm::vec4 strength = ray.strength * diffuseColor; glm::vec4 outgoing = SampleHemi(normal); Ray reflected(ray.depth + 1, position, outgoing, strength, PAC_EPSILON); rays.push(reflected); } else { totalColor.a = 1.f; } return totalColor * ray.strength; }