/* * Calculate reflection color */ color refColor(const intersection *inter, const ray *srcRay) { color cR = BLACK; if (srcRay->depth == MAX_DEPTH) return cR; intersection refInter; vec3 refVec = vector_reflect(srcRay->dir, inter->normal); ray refRay; // Ray from object to other object ray_init(&refRay, inter->position, refVec, EPSILON, 1000, srcRay->depth + 1); int bounceFound = 0; for (int k = 0; k < object_count; ++k) { bounceFound += intersect(&refRay, &(scene[k]), &refInter); } color refCoef = reflectCoef(inter->mat.reflect_coef, inter->normal, srcRay->dir); if (bounceFound) cR = vector_vec_mul(refCoef, vector_add(refColor(&refInter, &refRay), lightColor(&refInter, &refRay))); else cR = vector_vec_mul(refCoef, SKY_COLOR); return cR; }
void raytrace() { vec3 xR = theCamera.xdir; vec3 yR = vector_float_mul(1 / theCamera.aspect, theCamera.ydir); vec3 zR = vector_float_mul(1 / (tanf(0.5 * ((theCamera.fov / 180) * M_PI))), theCamera.zdir); int halfWidth = WIDTH / 2, halfHeight = HEIGHT / 2; for (int i = 0; i < WIDTH; ++i) { for (int j = 0; j < HEIGHT; ++j) { color c = BLACK; float deltas[4][2] = { { .25f, .25f }, { .25f, .75f }, { .75f, .25f }, { .75f, .75f } }; ray rays[4]; for (int n = 0; n < 4; ++n) { // Calculate ray vec3 x = vector_float_mul((i + deltas[n][0] - halfWidth) / halfWidth, xR); vec3 y = vector_float_mul((j + deltas[n][1] - halfHeight) / halfHeight, yR); vec3 d = vector_add(x, y); d = vector_add(d, zR); ray_init(&rays[n], theCamera.position, d); // Calculate intersection intersection inter; int found = 0; for (int k = 0; k < object_count; ++k) { found += (intersect(&rays[n], &(scene[k]), &inter)); // Keep the closest intersection } // Calculate pixel color if (found) { color cD = lightColor(&inter, &rays[n]); color cR = refColor(&inter, &rays[n]); c = vector_add(c, vector_add(cD, cR)); } else c = vector_add(c, SKY_COLOR); } // Mean output_image[j * WIDTH + i] = vector_float_mul(1.0f / 4.0f, c); } // Progress printf("%d%%\n", (int) (((float) i / WIDTH) * 100)); } printf("Done :)\n"); }
void KisOcioDisplayFilterTest::test() { KisExposureGammaCorrectionInterface *egInterface = new KisDumbExposureGammaCorrectionInterface(); OcioDisplayFilter filter(egInterface); QString configFile = TestUtil::fetchDataFileLazy("./psyfiTestingConfig-master/config.ocio"); dbgKrita << ppVar(configFile); Q_ASSERT(QFile::exists(configFile)); OCIO::ConstConfigRcPtr ocioConfig = OCIO::Config::CreateFromFile(configFile.toUtf8()); filter.config = ocioConfig; filter.inputColorSpaceName = ocioConfig->getColorSpaceNameByIndex(0); filter.displayDevice = ocioConfig->getDisplay(1); filter.view = ocioConfig->getView(filter.displayDevice, 0); filter.gamma = 1.0; filter.exposure = 0.0; filter.swizzle = RGBA; filter.blackPoint = 0.0; filter.whitePoint = 1.0; filter.forceInternalColorManagement = false; filter.setLockCurrentColorVisualRepresentation(false); filter.updateProcessor(); dbgKrita << ppVar(filter.inputColorSpaceName); dbgKrita << ppVar(filter.displayDevice); dbgKrita << ppVar(filter.view); dbgKrita << ppVar(filter.gamma); dbgKrita << ppVar(filter.exposure); const KoColorSpace *paintingCS = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Float32BitsColorDepthID.id(), 0); KisImageSP image = utils::createImage(0, QSize(100, 100)); image->convertImageColorSpace(paintingCS, KoColorConversionTransformation::InternalRenderingIntent, KoColorConversionTransformation::InternalConversionFlags); image->waitForDone(); dbgKrita << ppVar(paintingCS) << ppVar(image->root()->firstChild()->colorSpace()); KoCanvasResourceManager *resourceManager = utils::createResourceManager(image, image->root(), ""); KisDisplayColorConverter converter(resourceManager, 0); dbgKrita << ppVar(image->root()->firstChild()); QVariant v; v.setValue(KisNodeWSP(image->root()->firstChild())); resourceManager->setResource(KisCanvasResourceProvider::CurrentKritaNode, v); converter.setDisplayFilter(&filter); dbgKrita << ppVar(converter.paintingColorSpace()); { QColor refColor(255, 128, 0); KoColor realColor = converter.approximateFromRenderedQColor(refColor); QColor roundTripColor = converter.toQColor(realColor); dbgKrita << ppVar(refColor); dbgKrita << ppVar(realColor.colorSpace()) << ppVar(KoColor::toQString(realColor)); dbgKrita << ppVar(roundTripColor); } { KoColor realColor(Qt::red, paintingCS); QColor roundTripColor = converter.toQColor(realColor); dbgKrita << ppVar(realColor.colorSpace()) << ppVar(KoColor::toQString(realColor)); dbgKrita << ppVar(roundTripColor); } }
bool RayTracer::shade(SbVec3f *ray_origin, SbVec3f *ray_direction, SbVec3f *retColor, int recursionDepth, int flag){ float t_value, t_min = 999; float epsilon = 0.01; SbVec3f normal_at_intersection; SbVec3f normal_at_intersection1, actual_ray_direction ; bool should_color = false; SbVec3f color; color[0] = 0.0; color[1] = 0.0; color[2] = 0.0; //Cone *tempCone = new Cone(); for(int k =0; k<objects.size(); k++){ //Object temp1 ; //temp1 = spheres.at(k); Sphere tempSphere; Cube tempCube; Cone tempCone; Object temp; bool intersects = false; int shapetype = 0; shapetype = objects.at(k).shapeType ; if(shapetype == 1){ tempSphere = objects.at(k); intersects = tempSphere.intersection(ray_origin, ray_direction, &t_value); } else if (shapetype ==2){ //std::cout<<"cube"; tempCube = objects.at(k); intersects = tempCube.intersection(ray_origin, ray_direction, &t_value); //temp = (Cube)tempCube; }else{ tempCone = objects.at(k); intersects = tempCone.intersection(ray_origin, ray_direction, &t_value); } if(intersects) { if(t_value < t_min && t_value > 0 && t_value !=999) { t_min = t_value; SbVec3f V = -(*ray_direction); //view vector V.normalize(); SbVec3f point_of_intersection ; if(shapetype == 1){ normal_at_intersection = tempSphere.calculate_normal(ray_origin, ray_direction, t_value); normal_at_intersection.normalize(); // N vector at the point of intersection point_of_intersection = tempSphere.point_of_intersection( ray_origin, ray_direction, t_value); temp = tempSphere; }else if(shapetype == 2){ normal_at_intersection = tempCube.calculate_normal(ray_origin, ray_direction, t_value); normal_at_intersection.normalize(); // N vector at the point of intersection point_of_intersection = tempCube.point_of_intersection( ray_origin, ray_direction, t_value); temp = tempCube; } else{ normal_at_intersection = tempCone.calculate_normal(ray_origin, ray_direction, t_value); normal_at_intersection.normalize(); // N vector at the point of intersection point_of_intersection = tempCone.point_of_intersection( ray_origin, ray_direction, t_value); temp = tempCone; } for(int i = 0; i <3; i++) {// set the ambient color component color[i] = (0.2 * temp.material->ambientColor[0][i] * (1 - temp.transparency )); } //*retColor = color; return true;//ntc // iterate through all the lights and add the diffuse and specular component for(int j = 0; j < lights.size(); j++){ SbVec3f poi; actual_ray_direction = lights.at(j).position - point_of_intersection ; actual_ray_direction.normalize(); poi = point_of_intersection + (epsilon * actual_ray_direction); bool shadowFlag = false; if(shadow_on == 0 || shadow_on == 1) { if(shadow_on == 1) shadowFlag = shadow_ray_intersection(&poi, &actual_ray_direction , j ); //shadowFlag = true; if(!shadowFlag) { SbVec3f L = lights.at(j).position - point_of_intersection; L.normalize(); SbVec3f R; R = (2 * normal_at_intersection.dot(L) * normal_at_intersection) - L; R.normalize(); float NdotL = normal_at_intersection.dot(L); float cos_theta = V.dot(R); for(int i = 0; i <3; i++){ if(NdotL > 0) color[i] += (( NdotL * temp.material->diffuseColor[0][i] * lights.at(j).intensity * lights.at(j).color[i] * (1 - temp.transparency ))); if(cos_theta > 0) color[i] += (( pow(cos_theta, 50) * temp.material->specularColor[0][i]* lights.at(j).intensity * lights.at(j).color[i]) ); } } } else { // soft shadows { //shadowLevel = soft_shadow_ray_intersection(&point_of_intersection, j ); SbVec3f actual_ray_direction, offset_ray_direction; SbVec3f tempu, tempv, tempn; int number_of_shadow_rays; number_of_shadow_rays = NUMBER_OF_SHADOW_RAYS; float epsilon = 0.01; float R = 0.1; actual_ray_direction = lights.at(j).position - point_of_intersection ; actual_ray_direction.normalize(); SbVec3f point = point_of_intersection + (epsilon * actual_ray_direction); calculate_coordinate_system(&tempu, &tempv, &tempn, actual_ray_direction); for(int ir =0; ir< number_of_shadow_rays; ir++){ float du, dv; //float t; du = get_random_number(); dv = get_random_number(); du = R * (du - 0.5); dv = R * (dv - 0.5); offset_ray_direction = actual_ray_direction + (du * tempu) + (dv * tempv); offset_ray_direction.normalize(); //offset_ray_direction = actual_ray_direction - (R/2 * u) - (R/2 * v) + (du * R * u) + (dv *R * v); SbVec3f poi; poi = point + (epsilon * offset_ray_direction); //offset_ray_direction = actual_ray_direction; if(!shadow_ray_intersection(&poi, &offset_ray_direction, j)){ //normal_at_intersection = temp.calculate_normal(&poi, &offset_ray_direction, t_value); //normal_at_intersection.normalize(); SbVec3f V = -1 * (*ray_direction); //view vector V.normalize(); SbVec3f L = offset_ray_direction; L.normalize(); SbVec3f R; R = (2 * normal_at_intersection.dot(L) * normal_at_intersection) - L; R.normalize(); float NdotL = normal_at_intersection.dot(L); float cos_theta = V.dot(R); //if(temp.transparency > 0) std::cout<<"trnas"; for(int i = 0; i <3; i++){ { if(NdotL > 0) color[i] += (( NdotL * temp.material->diffuseColor[0][i] * lights.at(j).intensity * lights.at(j).color[i] * (1 - temp.transparency ))/ number_of_shadow_rays); if(cos_theta > 0) color[i] += (( pow(cos_theta, 50) * temp.material->specularColor[0][i]* lights.at(j).intensity * lights.at(j).color[i]) / number_of_shadow_rays); } } } } } } } SbVec3f refColor(0.0,0.0,0.0); SbVec3f refracColor(0.0,0.0,0.0); // if the current depth of recustion is less than the maximum depth, //reflect the ray and add the color returned dude to the result of reflection //std::cout<<"here"; if(refraction_on && recursionDepth < 2){ if(temp.isTransparent){ SbVec3f T; if(refract(ray_direction, &normal_at_intersection, &T)){ T.normalize(); SbVec3f poi; poi = point_of_intersection + (epsilon * T); shade(&poi, &T, &refracColor, recursionDepth+1); color = color + (temp.transparency * refracColor); } } } if(reflection_on && recursionDepth < 2){ if(temp.isShiny){//} && !temp.isTransparent){ // compute replection of the ray, R1 SbVec3f R1; R1 = reflect(&normal_at_intersection, ray_direction); SbVec3f poi; poi = point_of_intersection + (epsilon * R1); shade(&poi, &R1, &refColor, recursionDepth+1); color = color + ((1 - temp.transparency) * temp.shininess * refColor); } } should_color = true; } } } *retColor = color; return should_color; }
bool RayTracer::shade(SbVec3f *ray_origin, SbVec3f *ray_direction, SbVec3f *retColor, int recursionDepth){ float t_value, t_min = 999; SbVec3f normal_at_intersection; bool should_color = false; SbVec3f color; color[0] = 0.0; color[1] = 0.0; color[2] = 0.0; for(int k =0; k<spheres.size(); k++){ Sphere temp = spheres.at(k); if(temp.intersection(ray_origin, ray_direction, &t_value)) { if(t_value < t_min && t_value > 0 && t_value !=999) { t_min = t_value; SbVec3f V = -(*ray_direction); //view vector V.normalize(); normal_at_intersection = temp.calculate_normal(ray_origin, ray_direction, t_value); normal_at_intersection.normalize(); // N vector at the point of intersection SbVec3f point_of_intersection = temp.point_of_intersection( ray_origin, ray_direction, t_value); for(int i = 0; i <3; i++) {// set the ambient color component color[i] = (0.2 * temp.material->ambientColor[0][i]); } // iterate through all the lights and add the diffuse and specular component for(int j = 0; j < lights.size(); j++){ bool shadowFlag = false; if(shadow_on ) shadowFlag = shadow_ray_intersection(&point_of_intersection, j ); if(!shadowFlag){ SbVec3f L = lights.at(j).position - point_of_intersection; L.normalize(); SbVec3f R; R = (2 * normal_at_intersection.dot(L) * normal_at_intersection) - L; R.normalize(); //SbVec3f H = (V + L);//is using half way vector //H.normalize();//is using half way vector //float cos_theta = H.dot(normal_at_intersection); //is using half way vector float NdotL = normal_at_intersection.dot(L); float cos_theta = V.dot(R); for(int i = 0; i <3; i++){ if(NdotL > 0) color[i] += ( NdotL * temp.material->diffuseColor[0][i] * lights.at(j).intensity * lights.at(j).color[i]); if(cos_theta > 0) color[i] += ( pow(cos_theta, 20) * temp.material->specularColor[0][i]* lights.at(j).intensity * lights.at(j).color[i]); } } } SbVec3f refColor(0.0,0.0,0.0); // if the current depth of recustion is less than the maximum depth, //reflect the ray and add the color returned dude to the result of reflection if(reflection_on && recursionDepth < 4){ if(temp.isShiny){ // compute replection of the ray, R1 SbVec3f R1; R1 = (-2 *(normal_at_intersection.dot(*ray_direction)* normal_at_intersection)) + *ray_direction; R1.normalize(); shade(&point_of_intersection, &R1, &refColor, recursionDepth+1); color = color + (temp.shininess * refColor); } } should_color = true; } } } *retColor = color; return should_color; }