int main(int argc, char **argv) { djb::beckmann beckmann(djb::fresnel::ideal(), false); FILE *pf = fopen("eval_cdf_beckmann.txt", "w"); plot_cdf(beckmann, pf); fclose(pf); djb::tabular beckmann_tab(beckmann, 180); pf = fopen("eval_cdf_beckmann_tab.txt", "w"); plot_cdf(beckmann_tab, pf); fclose(pf); djb::ggx ggx(djb::fresnel::ideal(), false); pf = fopen("eval_cdf_ggx.txt", "w"); plot_cdf(ggx, pf); fclose(pf); djb::tabular ggx_tab(ggx, 180); pf = fopen("eval_cdf_ggx_tab.txt", "w"); plot_cdf(ggx_tab, pf); fclose(pf); return 0; }
Vec3f Ray::getColor(const vector <tinyobj::shape_t> &shapes, const vector <tinyobj::material_t> &materials, Vec3f lightSource) { if (DBG) cout << "[getColor] depth = " << depth << endl; //get triangle that intersect the ray Vec3f triangle[3]; pair <int, int> triangleId = this->getIntersectTriangle(shapes, triangle); DBG && cout << "\t[triangleId] " << triangleId.first << ' ' << triangleId.second << endl; if (triangleId.first == -1) { DBG && cout << "\t[return] no intersection" << endl; return Vec3f(0.0, 0.0, 0.0); } Vec3f reversedDirection = this->direction * -1; if (dot(reversedDirection, getNormalwithRayComes(triangle, this->direction)) < 0) { DBG && cout << "[return] " << endl; return Vec3f(0.0, 0.0, 0.0); } //check if position and lightsource are in different sides of the triangle Vec3f intersection; Vec3f color_direct; unsigned int iMaterial = shapes[triangleId.first].mesh.material_ids[triangleId.second]; if (lineCutTrianglePlane(triangle, this->direction, this->position, lightSource)) { DBG && cout << "\t[message] lineCutTrianglePlane" << endl; color_direct = Vec3f(0.0, 0.0, 0.0); } else { //calculate intersection this->intersect_remake(triangle, intersection); //check reflected ray Ray reflectedRay(intersection, lightSource - intersection, bshRoot, 0, triangleId); if (reflectedRay.canReach(lightSource, shapes) == false) { DBG && cout << "\t[message] reflected ray cannot reach lightsource" << endl; color_direct = Vec3f(0.0, 0.0, 0.0); } else { //calculate color_direct float radian_direct = ggx(this->position, lightSource, intersection, triangle, 1.0, 0.8, 0.8, 2.0); // color_direct = Vec3f( (materials[iMaterial].diffuse[0] + materials[iMaterial].specular[0]) * radian_direct, // (materials[iMaterial].diffuse[1] + materials[iMaterial].specular[1]) * radian_direct, // (materials[iMaterial].diffuse[2] + materials[iMaterial].specular[2]) * radian_direct); color_direct = Vec3f( (materials[iMaterial].diffuse[0]) * radian_direct, (materials[iMaterial].diffuse[1]) * radian_direct, (materials[iMaterial].diffuse[2]) * radian_direct); DBG && cout << "\t[color] " << color_direct << endl; } } Vec3f color_indirect(0.0, 0.0, 0.0); int counter = 0; if (depth < MAX_DEPTH) { for (int iRay = 0; iRay < NUMBER_OF_RAYS; ++iRay) { Ray ray = this->getRandomRay_Sphere(intersection, triangle, depth + 1, triangleId); // if (triangleId.first == 4) // ray = this->getMirrorRay(intersection, triangle, depth + 1, triangleId); // Ray ray = this->getRandomRay_Sphere(intersection, triangle, depth + 1, triangleId); // Ray ray = this->getInConeRay(intersection, triangle, depth + 1, triangleId); // Ray ray = this->getUniformRay_Plane(intersection, triangle, depth + 1, triangleId, iRay, NUMBER_OF_RAYS); // Ray ray = this->getMirrorRay(intersection, triangle, depth + 1, triangleId); Vec3f color = ray.getColor(shapes, materials, lightSource); float cos_theta = dot(ray.direction, getNormalwithRayComes(triangle, this->direction)); Vec3f w = lightSource - intersection; Vec3f w0 = this->position - intersection; Vec3f n = getNormalwithRayComes(triangle, this->direction); w.normalize(); w0.normalize(); n.normalize(); float f_s = brdf_GGX(w, w0, n, 0.8, 0.8); float f_d = f_Lambert(2.0); // float f_d = 0; color_indirect += color * (f_s + f_d) * fabs(cos_theta); // color_indirect += color * (f_s + f_d); counter++; } if (counter > 0) color_indirect /= counter; DBG && cout << "\t[color_indirect] = " << color_indirect << endl; } DBG && cout << "\t[counter] " << counter << endl; DBG && cout << "\t[color] " << color_direct << endl; // color_direct = (color_direct * 0.5) + (color_indirect * 0.5); color_direct += (1.0f * color_indirect); for (int i = 0; i < 3; ++i) color_direct[i] = min(color_direct[i], 1.0f); if (this->depth == 1) DBG && cout << "final color = " << color_direct << endl; // return color_direct + Vec3f(materials[iMaterial].ambient[0], // materials[iMaterial].ambient[1], // materials[iMaterial].ambient[2]); return color_direct; }