Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int amb=0;
  5. int diff=1;
  6. int spec=2;
  7. int reflect=3;
  8. extern int recur_lvl;
  9.  
  10. #define piece 180
  11. #define pi (2*acos(0.0))
  12.  
  13. class vector3{
  14. public:
  15.     double x,y,z;
  16.  
  17.     vector3(){
  18.         x=y=z=0;
  19.     }
  20.  
  21.     vector3(double a,double b,double c){
  22.         this->x=a;
  23.         this->y=b;
  24.         this->z=c;
  25.     }
  26.     vector3 operator+(vector3 v){
  27.         vector3 t(this->x+v.x,this->y+v.y,this->z+v.z);
  28.         return t;
  29.     }
  30.     vector3 operator-(vector3 v){
  31.         vector3 t(this->x-v.x,this->y-v.y,this->z-v.z);
  32.         return t;
  33.     }
  34.     void normalizeVect(){
  35.         double denom=sqrt(x*x+y*y+z*z);
  36.         this->x/=denom;
  37.         this->y/=denom;
  38.         this->z/=denom;
  39.     }
  40.     vector3 operator*(double a){
  41.         vector3 t(this->x*a,this->y*a,this->z*a);
  42.         return t;
  43.     }
  44.     vector3 operator*(vector3 v){
  45.         vector3 t(this->x*v.x,this->y*v.y,this->z*v.z);
  46.         return t;
  47.     }
  48.  
  49. };
  50.  
  51. vector3 CamPos,L,R,U;
  52.  
  53. double dotMult(vector3 a,vector3 b){
  54.     double res=a.x*b.x+a.y*b.y+a.z*b.z;
  55.     return res;
  56. }
  57.  
  58. vector3 crossMult(vector3 a,vector3 b){
  59.     vector3 res;
  60.     res.x=a.y*b.z-a.z*b.y;
  61.     res.y=b.x*a.z-a.x*b.z;
  62.     res.z=a.x*b.y-b.x*a.y;
  63.     return res;
  64. }
  65.  
  66.  
  67. class Ray{
  68. public:
  69.     vector3 start;
  70.     vector3 dir;
  71.     Ray(vector3 st,vector3 d){
  72.         start=st;
  73.         d.normalizeVect();
  74.         dir=d;
  75.     }
  76.     //double intersectObj(Ray* ray,double* colorArr,int lvl){}
  77. };
  78.  
  79.  
  80. class object{
  81. public:
  82.     vector3 reference_point;
  83.     double height, width, length;
  84.     int Shine;
  85.     double source_fact=1.0;
  86.     double color[3];
  87.     double co_efficients[4];
  88.     object(){ }
  89.     virtual void draw(){}
  90.  
  91.     vector3 reflectionFind(Ray *ray,vector3 norm){
  92.         vector3 vect=ray->dir-norm*dotMult(ray->dir,norm)*2;
  93.         vect.normalizeVect();
  94.         return vect;
  95.     }
  96.     void setColor(double a,double b,double c){
  97.         color[0]=a;
  98.         color[1]=b;
  99.         color[2]=c;
  100.     }
  101.     void setShine(int sh){
  102.         Shine=sh;
  103.     }
  104.     void setCoEfficients(double a,double b,double c,double d){
  105.         co_efficients[0]=a;
  106.         co_efficients[1]=b;
  107.         co_efficients[2]=c;
  108.         co_efficients[3]=d;
  109.     }
  110.     virtual double getIntersectingT(Ray* ray){
  111.         return -1;
  112.     }
  113.     virtual double intersectObj(Ray* ray,double* colorArr,int checker){
  114.         return -1;
  115.     }
  116. };
  117. ///variables
  118. extern vector <object*> objects;
  119. extern vector <vector3> lights;
  120.  
  121.  
  122.  
  123. class sphere: public object{
  124. public:
  125.     sphere(vector3 center,double radius){
  126.         reference_point=center;
  127.         length=radius;
  128.     }
  129.     void draw(){
  130.     //write codes for drawing sphere
  131.         glPushMatrix();{
  132.             glColor3f(color[0],color[1],color[2]);
  133.             glTranslatef(reference_point.x,reference_point.y,reference_point.z);
  134.             glutSolidSphere(length,100,100);
  135.         }glPopMatrix();
  136.     }
  137.     vector3 normalFind(vector3 v){
  138.         vector3 res=v-reference_point;
  139.         res.normalizeVect();
  140.         return res;
  141.     }
  142.     double getIntersectingT(Ray *ray){
  143.         vector3 root=ray->start-reference_point;
  144.         double a,b,c,d,t1,t2;
  145.         a=dotMult(ray->dir,ray->dir);
  146.         b=dotMult(root,ray->dir);
  147.         b*=2.0;
  148.         c=dotMult(root,root)-length*length;
  149.         d=(b*b)-(4*a*c);
  150.         cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl;
  151.         if(d<0)return -1;
  152.         else{
  153.             t1=(-b+sqrt(d))/(2.0*a);
  154.             t2=(-b-sqrt(d))/(2.0*a);
  155.             return (t1<t2)?t1:t2;
  156.         }
  157.     }
  158.  
  159.     double intersectObj(Ray* ray,double* colorArr,int lvl){
  160.         double t = this->getIntersectingT(ray);
  161.  
  162.         if(t<=0)return -1;
  163.         if(lvl==0)return t;
  164.  
  165.         vector3 intersectionPoint = ray->start+ ray->dir*t;
  166.  
  167.         for(int f=0;f<3;f++)colorArr[f]=colorArr[f]*this->co_efficients[amb];
  168.  
  169.         vector3 normal = normalFind(intersectionPoint);
  170.         vector3 reflection = reflectionFind(ray,normal);
  171.  
  172.         for(int i=0;i<lights.size();i++){
  173.             vector3 direct = lights[i] - intersectionPoint;
  174.             direct.normalizeVect();
  175.  
  176.             double rayLength = sqrt(dotMult(direct,direct));
  177.             vector3 start = intersectionPoint + direct*1.0;
  178.  
  179.             Ray *tRay = new Ray(start,direct);
  180.  
  181.             bool obscured = false;
  182.  
  183.             for(int j=0;j<objects.size();j++){
  184.                 double inVal = objects[j]->getIntersectingT(tRay);
  185.                 if(inVal<0 || inVal>rayLength) {
  186.                     continue;
  187.                 }
  188.  
  189.                 if(obscured==false)obscured = true;
  190.                 break;
  191.             }
  192.             if(!obscured){
  193.                 double lambert = dotMult(tRay->dir,normal);
  194.                 double base=dotMult(ray->dir,reflection);
  195.                 double phong =  pow(base,Shine);
  196.  
  197.                 if(lambert<0)lambert=0;
  198.                 if(phong<0)phong=0;
  199.  
  200.                 for(int k=0;k<3;k++){
  201.                      colorArr[k]+= source_fact*lambert*co_efficients[diff]*this->color[k];
  202.                      colorArr[k]+= source_fact*phong*co_efficients[spec]*this->color[k];
  203.                 }
  204.  
  205.             }
  206.             if(lvl<recur_lvl){
  207.  
  208.                 vector3 start = intersectionPoint+reflection*1.0;
  209.                 Ray *reflectionRay = new Ray(start,reflection);
  210.  
  211.                 int closest = -1;
  212.                 double reflectedcolor[3];
  213.                 double min_t = 999999999;
  214.  
  215.                 for(int k=0;k<objects.size();k++){
  216.                     double t = objects[k]->intersectObj(reflectionRay,reflectedcolor,0);
  217.                     if(t<=0)continue;
  218.  
  219.                     if(t<min_t){
  220.                         min_t = t;
  221.                         closest = k;
  222.                     }
  223.                 }
  224.                 if(closest!=-1){
  225.                     double t = objects[closest]->intersectObj(reflectionRay,reflectedcolor,lvl+1);
  226.                     for (int z=0; z<3; z++) {
  227.                         colorArr[z] += reflectedcolor[z] * co_efficients[reflect];
  228.                         if(colorArr[z]>1)colorArr[z]=1;
  229.                         if(colorArr[z]<0)colorArr[z]=0;
  230.                     }
  231.                 }
  232.  
  233.             }
  234.  
  235.         }
  236.         return t;
  237.     }
  238.  
  239. };
  240.  
  241. class Floor: public object{
  242. public:
  243.     Floor(){}
  244.     Floor(double floorWidth,double tileWidth){
  245.         vector3 obj(-floorWidth/2.0, -floorWidth/2.0,0);
  246.         reference_point=obj;
  247.         length=tileWidth;
  248.     }
  249.  
  250.     void draw(){
  251.         double white=0;
  252.         double i=reference_point.x,j=reference_point.y,k=0;
  253.         double end1=-reference_point.x,end2=-reference_point.y;
  254.         while(i<end1){
  255.             while(j<end2){
  256.                 glColor3f(white,white,white);
  257.                 glBegin(GL_QUADS);{
  258.                     glVertex3f(i,j,k);
  259.                     glVertex3f(i+length,j,k);
  260.                     glVertex3f(i+length,j+length,k);
  261.                     glVertex3f(i,j+length,k);
  262.                 }glEnd();
  263.                 white=1-white;
  264.                 j+=length;
  265.             }
  266.             white=1-white;
  267.             j=reference_point.y;
  268.             i+=length;
  269.         }
  270.  
  271.     }
  272.     vector3 normalFind(vector3 v){
  273.         vector3 res(0,0,1);
  274.         return res;
  275.     }
  276.     double getIntersectingT(Ray* ray) {
  277.  
  278.         vector3 normal = normalFind(reference_point);
  279.  
  280.         return (-(dotMult(normal,ray->start)/dotMult(normal, ray->dir)));
  281.     }
  282. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement