Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class general : public object{
- public:
- double A,B,C,D,E,F,G,H,I,J,length,height,width;
- general(double a,double b,double c,double d,double e,double f,double g,double h,double i,double j,vector3 temp,double len,double hei,double wid)
- {
- A = a;
- B = b;
- C = c;
- D = d;
- E = e;
- F = f;
- G = g;
- H = h;
- I = i;
- J = j;
- reference_point = temp;
- length = len;
- height = hei;
- width = wid;
- }
- void draw(){
- //write codes for drawing sphere
- }
- double * getColorAt(vector3 tempVector){
- double r[3];
- for(int i = 0;i<3;i++){
- r[i] = color[i];
- }
- return r;
- }
- vector3 getNormal(vector3 tempInterSectionPoint)
- {
- double i,j,k;
- i = 2*A*tempInterSectionPoint.x + D*tempInterSectionPoint.y + F*tempInterSectionPoint.z + G;
- j = 2*B*tempInterSectionPoint.y + D*tempInterSectionPoint.x + E*tempInterSectionPoint.z + H;
- k = 2*C*tempInterSectionPoint.z + E*tempInterSectionPoint.y + F*tempInterSectionPoint.x + I;
- vector3 finalVector(i,j,k);
- return finalVector;
- }
- double getIntersectingT(ray *r)
- {
- double a,b,c,d,t,t1,t2;
- vector3 for_t;
- a = A * r->dir.x * r->dir.x + B * r->dir.y * r->dir.y + C * r->dir.z * r->dir.z + D * r->dir.x * r->dir.y + E * r->dir.y * r->dir.z + F * r->dir.x * r->dir.z;
- b = 2 * ( r->start.x * r->dir.y * A + r->start.y * r->dir.y * B + r->start.z * r->dir.z * C ) + D * ( r->start.x * r->dir.y + r->dir.x * r->start.y)+ E * ( r->start.y * r->dir.z + r->dir.y * r->start.z ) + F * ( r->start.x * r->dir.z + r->dir.x * r->start.z ) + G * r->dir.x + H * r->dir.y + I * r->dir.z;
- c = A * r->start.x * r->start.x + B * r->start.y * r->start.y + C * r->start.z * r->start.z + D * r->start.x * r->start.y + E * r->start.y * r->start.z + F * r->start.x * r->start.z+ G * r->start.x + H * r->start.y + I * r->start.z + J;
- d = b*b - 4 *a*c;
- if(d<0){
- return -1;
- }
- else
- {
- d = sqrt(d);
- t1 = (-b+d)/(2*a);
- t2 = (-b-d)/(2*a);
- //double t = min(t1,t2);
- }
- vector3 interSectionPoint1,interSectionPoint2;
- interSectionPoint1 = r->start + r->dir*t1;
- interSectionPoint2 = r->start + r->dir*t2;
- if(length == 0 && width == 0 && height ==0)
- {
- t = min(t1,t2);
- return t;
- }
- else
- {
- if(isValid(reference_point,interSectionPoint1,length,0) && isValid(reference_point,interSectionPoint1,width,1) && isValid(reference_point,interSectionPoint1,height,2))
- {
- if(isValid(reference_point,interSectionPoint2,length,0) && isValid(reference_point,interSectionPoint2,width,1) && isValid(reference_point,interSectionPoint2,height,2))
- {
- t = min(t1,t2);
- return t;
- }
- else
- {
- return t1;
- }
- }
- else
- {
- if(isValid(reference_point,interSectionPoint2,length,0) && isValid(reference_point,interSectionPoint2,width,1) && isValid(reference_point,interSectionPoint2,height,2))
- {
- return t2;
- }
- else
- return -1;
- }
- }
- }
- 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;
- }
- }
- }
- }
- return t;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement