Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double intersect(ray *r, double *current_color, int level){
- double t;
- t = getIntersectingT(r);
- if(t<=0){
- return -1;
- }
- if(level == 0)
- {
- return t;
- }
- vector3 interSectionPoint;
- interSectionPoint = r->start + r->dir*t;
- double colorAt[3];
- //getColor
- for(int i = 0;i<3;i++){
- colorAt[i] = color[i];
- }
- //setColor
- for(int i = 0;i<3;i++){
- current_color[i] = colorAt[i]*co_efficients[0];
- }
- //normalize
- vector3 normal;
- normal = getNormal(interSectionPoint);
- double sum_normal = sqrt(pow(normal.x,2)+pow(normal.y,2)+pow(normal.z,2));
- normal.normalize();
- for(int i = 0;i<lights.size();i++){
- vector3 lightDir;
- vector3 lightStart;
- lightDir = lights.at(i) - interSectionPoint;
- double sum = sqrt(pow(lightDir.x,2)+pow(lightDir.y,2)+pow(lightDir.z,2));
- lightDir.normalize();
- lightStart = interSectionPoint + lightDir;
- ray *newL = new ray(lightStart,lightDir);
- //ray newL(,tempDir);
- bool set_flag = false;
- for(int j = 0;j<objects.size();j++){
- double tempT;
- tempT = objects.at(j)->getIntersectingT(newL);
- if(tempT>0 && tempT<sum){
- set_flag = true;
- break;
- }
- }
- if(set_flag!=true){
- double lambertValue = dot_product(lightDir,normal);
- if(lambertValue<0){
- lambertValue = 0;
- }
- vector3 a;
- vector3 tempB;
- a = r->start - interSectionPoint ;
- a.normalize();
- tempB = (normal*(dot_product(lightDir,normal)*2)) - lightDir;
- tempB.normalize();
- double phongValue = dot_product(a,tempB);
- if(phongValue<0){
- phongValue = 0;
- }
- //cout<<"Lmabert value is "<<lambertValue<<"phong value is "<<phongValue<<endl;
- for(int p= 0;p<3;p++){
- current_color[p]+=lambertValue*co_efficients[1]*colorAt[p];
- current_color[p]+= pow(phongValue,Shine)*co_efficients[2]*colorAt[p];
- }
- }
- if(level<recursion_level)
- {
- vector3 reflection= r->dir - (normal*(dot_product(r->dir,normal)*2));
- vector3 newLightStart = interSectionPoint + reflection*1 ;
- ray *reflectionRay = new ray(newLightStart,reflection);
- double nearest = -1;
- double t_min = 999999;
- double dummyColor[3];
- for(int k = 0;k<objects.size();k++){
- double t = objects[k] -> intersect(reflectionRay,dummyColor,0);
- if(t<=0)
- {
- continue;
- }
- if(t<t_min){
- t_min = t;
- nearest = k;
- }
- }
- double reflected_color [3];
- if(nearest!=-1){
- t = objects[nearest]->intersect(reflectionRay,reflected_color,level+1);
- for(int p = 0;p<3;p++){
- current_color[p]+=reflected_color[p]*co_efficients[3];
- }
- }
- for(int h = 0;h<3;h++){
- if(current_color[h]>1){
- current_color[h] = 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement