Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int amb=0;
- int diff=1;
- int spec=2;
- int reflect=3;
- extern int recur_lvl;
- #define piece 180
- #define pi (2*acos(0.0))
- class vector3{
- public:
- double x,y,z;
- vector3(){
- x=y=z=0;
- }
- vector3(double a,double b,double c){
- this->x=a;
- this->y=b;
- this->z=c;
- }
- vector3 operator+(vector3 v){
- vector3 t(this->x+v.x,this->y+v.y,this->z+v.z);
- return t;
- }
- vector3 operator-(vector3 v){
- vector3 t(this->x-v.x,this->y-v.y,this->z-v.z);
- return t;
- }
- void normalizeVect(){
- double denom=sqrt(x*x+y*y+z*z);
- this->x/=denom;
- this->y/=denom;
- this->z/=denom;
- }
- vector3 operator*(double a){
- vector3 t(this->x*a,this->y*a,this->z*a);
- return t;
- }
- vector3 operator*(vector3 v){
- vector3 t(this->x*v.x,this->y*v.y,this->z*v.z);
- return t;
- }
- };
- vector3 CamPos,L,R,U;
- double dotMult(vector3 a,vector3 b){
- double res=a.x*b.x+a.y*b.y+a.z*b.z;
- return res;
- }
- vector3 crossMult(vector3 a,vector3 b){
- vector3 res;
- res.x=a.y*b.z-a.z*b.y;
- res.y=b.x*a.z-a.x*b.z;
- res.z=a.x*b.y-b.x*a.y;
- return res;
- }
- class Ray{
- public:
- vector3 start;
- vector3 dir;
- Ray(vector3 st,vector3 d){
- start=st;
- d.normalizeVect();
- dir=d;
- }
- //double intersectObj(Ray* ray,double* colorArr,int lvl){}
- };
- class object{
- public:
- vector3 reference_point;
- double height, width, length;
- int Shine;
- double source_fact=1.0;
- double color[3];
- double co_efficients[4];
- object(){ }
- virtual void draw(){}
- vector3 reflectionFind(Ray *ray,vector3 norm){
- vector3 vect=ray->dir-norm*dotMult(ray->dir,norm)*2;
- vect.normalizeVect();
- return vect;
- }
- void setColor(double a,double b,double c){
- color[0]=a;
- color[1]=b;
- color[2]=c;
- }
- void setShine(int sh){
- Shine=sh;
- }
- void setCoEfficients(double a,double b,double c,double d){
- co_efficients[0]=a;
- co_efficients[1]=b;
- co_efficients[2]=c;
- co_efficients[3]=d;
- }
- virtual double getIntersectingT(Ray* ray){
- return -1;
- }
- virtual double intersectObj(Ray* ray,double* colorArr,int checker){
- return -1;
- }
- };
- ///variables
- extern vector <object*> objects;
- extern vector <vector3> lights;
- class sphere: public object{
- public:
- sphere(vector3 center,double radius){
- reference_point=center;
- length=radius;
- }
- void draw(){
- //write codes for drawing sphere
- glPushMatrix();{
- glColor3f(color[0],color[1],color[2]);
- glTranslatef(reference_point.x,reference_point.y,reference_point.z);
- glutSolidSphere(length,100,100);
- }glPopMatrix();
- }
- vector3 normalFind(vector3 v){
- vector3 res=v-reference_point;
- res.normalizeVect();
- return res;
- }
- double getIntersectingT(Ray *ray){
- vector3 root=ray->start-reference_point;
- double a,b,c,d,t1,t2;
- a=dotMult(ray->dir,ray->dir);
- b=dotMult(root,ray->dir);
- b*=2.0;
- c=dotMult(root,root)-length*length;
- d=(b*b)-(4*a*c);
- cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl;
- if(d<0)return -1;
- else{
- t1=(-b+sqrt(d))/(2.0*a);
- t2=(-b-sqrt(d))/(2.0*a);
- return (t1<t2)?t1:t2;
- }
- }
- double intersectObj(Ray* ray,double* colorArr,int lvl){
- double t = this->getIntersectingT(ray);
- if(t<=0)return -1;
- if(lvl==0)return t;
- vector3 intersectionPoint = ray->start+ ray->dir*t;
- for(int f=0;f<3;f++)colorArr[f]=colorArr[f]*this->co_efficients[amb];
- vector3 normal = normalFind(intersectionPoint);
- vector3 reflection = reflectionFind(ray,normal);
- for(int i=0;i<lights.size();i++){
- vector3 direct = lights[i] - intersectionPoint;
- direct.normalizeVect();
- double rayLength = sqrt(dotMult(direct,direct));
- vector3 start = intersectionPoint + direct*1.0;
- Ray *tRay = new Ray(start,direct);
- bool obscured = false;
- for(int j=0;j<objects.size();j++){
- double inVal = objects[j]->getIntersectingT(tRay);
- if(inVal<0 || inVal>rayLength) {
- continue;
- }
- if(obscured==false)obscured = true;
- break;
- }
- if(!obscured){
- double lambert = dotMult(tRay->dir,normal);
- double base=dotMult(ray->dir,reflection);
- double phong = pow(base,Shine);
- if(lambert<0)lambert=0;
- if(phong<0)phong=0;
- for(int k=0;k<3;k++){
- colorArr[k]+= source_fact*lambert*co_efficients[diff]*this->color[k];
- colorArr[k]+= source_fact*phong*co_efficients[spec]*this->color[k];
- }
- }
- if(lvl<recur_lvl){
- vector3 start = intersectionPoint+reflection*1.0;
- Ray *reflectionRay = new Ray(start,reflection);
- int closest = -1;
- double reflectedcolor[3];
- double min_t = 999999999;
- for(int k=0;k<objects.size();k++){
- double t = objects[k]->intersectObj(reflectionRay,reflectedcolor,0);
- if(t<=0)continue;
- if(t<min_t){
- min_t = t;
- closest = k;
- }
- }
- if(closest!=-1){
- double t = objects[closest]->intersectObj(reflectionRay,reflectedcolor,lvl+1);
- for (int z=0; z<3; z++) {
- colorArr[z] += reflectedcolor[z] * co_efficients[reflect];
- if(colorArr[z]>1)colorArr[z]=1;
- if(colorArr[z]<0)colorArr[z]=0;
- }
- }
- }
- }
- return t;
- }
- };
- class Floor: public object{
- public:
- Floor(){}
- Floor(double floorWidth,double tileWidth){
- vector3 obj(-floorWidth/2.0, -floorWidth/2.0,0);
- reference_point=obj;
- length=tileWidth;
- }
- void draw(){
- double white=0;
- double i=reference_point.x,j=reference_point.y,k=0;
- double end1=-reference_point.x,end2=-reference_point.y;
- while(i<end1){
- while(j<end2){
- glColor3f(white,white,white);
- glBegin(GL_QUADS);{
- glVertex3f(i,j,k);
- glVertex3f(i+length,j,k);
- glVertex3f(i+length,j+length,k);
- glVertex3f(i,j+length,k);
- }glEnd();
- white=1-white;
- j+=length;
- }
- white=1-white;
- j=reference_point.y;
- i+=length;
- }
- }
- vector3 normalFind(vector3 v){
- vector3 res(0,0,1);
- return res;
- }
- double getIntersectingT(Ray* ray) {
- vector3 normal = normalFind(reference_point);
- return (-(dotMult(normal,ray->start)/dotMult(normal, ray->dir)));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement