public Triangle(Mesh owner, int index0, int index1, int index2, Material material) { this.owner = owner; index = new int[3]; index[0] = index0; index[1] = index1; index[2] = index2; Point3 v0 = owner.getVertex(index0); Point3 v1 = owner.getVertex(index1); Point3 v2 = owner.getVertex(index2); if (!owner.existsNormals()) { Vector3 e0 = new Vector3(), e1 = new Vector3(); e0.sub(v1, v0); e1.sub(v2, v0); norm = new Vector3(); norm.cross(e0, e1); } a = v0.x - v1.x; b = v0.y - v1.y; c = v0.z - v1.z; d = v0.x - v2.x; e = v0.y - v2.y; f = v0.z - v2.z; this.setMaterial(material); }
/** * Calculate the BRDF value for this material at the intersection described in record. Returns the * BRDF color in outColor. * * @param outColor Space for the output color * @param scene The scene * @param lights The lights * @param toEye Vector pointing towards the eye * @param record The intersection record, which hold the location, normal, etc. * @param depth The depth of recursive calls. You can ignore this parameter. * @param contribution The contribution of the current ray. You can ignore this parameter. * @param internal You can ignore this parameter. */ public void shade( Color outColor, Scene scene, ArrayList<Light> lights, Vector3 toEye, IntersectionRecord record, int depth, double contribution, boolean internal) { for (Light light : lights) { if (!isShadowed(scene, light, record)) { L.sub(light.position, record.location); L.normalize(); double dot = L.dot(record.normal); if (dot > 0) { outColor.r += dot * light.intensity.r * diffuseColor.r; outColor.g += dot * light.intensity.g * diffuseColor.g; outColor.b += dot * light.intensity.b * diffuseColor.b; } H.set(L); H.add(toEye); H.normalize(); dot = H.dot(record.normal); if (dot > 0) { dot = Math.pow(dot, exponent); outColor.r += dot * light.intensity.r * specularColor.r; outColor.g += dot * light.intensity.g * specularColor.g; outColor.b += dot * light.intensity.b * specularColor.b; } } } }