Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double intersect(Ray *r, struct point* current_color, int level)
- {
- //printf("%f %f %f ", r->dir.x, r->dir.y, r->dir.z);
- double t = getT(r);
- //printf("here0\n");
- if(t <= 0) return -1;
- //printf("here1\n");
- if(level == 0) return t;
- //printf("here2\n");
- struct point intersection;
- intersection.x = r->start.x + t * r->dir.x;
- intersection.y = r->start.y + t * r->dir.y;
- intersection.z = r->start.z + t * r->dir.z;
- current_color->x = color[0] * co_efficients[AMBIENT];
- current_color->y = color[1] * co_efficients[AMBIENT];
- current_color->z = color[2] * co_efficients[AMBIENT];
- struct point normal = getNormal(intersection);
- struct point reflection = getReflection(r, normal);
- for(int i = 0; i < lights.size(); i++){
- struct point ldir;
- ldir.x = lights[i].x - intersection.x;
- ldir.y = lights[i].y - intersection.y;
- ldir.z = lights[i].z - intersection.z;
- ldir = normalize(ldir);
- struct point lstart;
- lstart.x = intersection.x + ldir.x * 1.0;
- lstart.y = intersection.y + ldir.y * 1.0;
- lstart.z = intersection.z + ldir.z * 1.0;
- Ray L(lstart, ldir);
- bool obscured = false;
- for(int j = 0; j < objects.size(); j++){
- double possibleobscure = objects[j]->getT(&L);
- //printf("%f ", possibleobscure);
- if(possibleobscure > 0 && abs(possibleobscure) < sqrt(pow(ldir.x,2) + pow(ldir.y,2) + pow(ldir.z,2))){
- obscured = true;
- break;
- }
- }
- //printf("\n");
- if(!obscured){
- //printf("direct");
- L.dir.x = -L.dir.x;
- L.dir.y = -L.dir.y;
- L.dir.z = -L.dir.z;
- double lambert = dotProduct(L.dir, normal);
- double phong = dotProduct(reflection, r->dir);
- //if(lambert > 0 || phong > 0) printf("emon to hobar kotha na\n");
- lambert = lambert > 0?lambert:0;
- phong = phong > 0?phong:0;
- current_color->x += source_factor * color[0] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
- current_color->y += source_factor * color[1] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
- current_color->z += source_factor * color[2] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
- }
- if(level < recursion_level){
- struct point rs;
- rs.x = intersection.x + reflection.x * 1.0;
- rs.y = intersection.y + reflection.y * 1.0;
- rs.z = intersection.z + reflection.z * 1.0;
- //reflection = normalize(reflection);
- Ray reflectionRay(rs, reflection);
- int nearest = -1;
- struct point reflectColor;
- double t_min = 9999;
- for(int k = 0; k < objects.size(); k++){
- double t = objects[k]->getT(&reflectionRay);
- if(t <= 0) continue;
- else if(t < t_min) {
- t_min = t;
- nearest = k;
- }
- }
- if(nearest != -1){
- double t = objects[nearest]->intersect(&reflectionRay, &reflectColor, level + 1);
- current_color->x += reflectColor.x*co_efficients[REFLECTION];
- current_color->y += reflectColor.y*co_efficients[REFLECTION];
- current_color->z += reflectColor.z*co_efficients[REFLECTION];
- }
- }
- }
- return t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement