Exemplo n.º 1
0
void cbody_update (Darray_CBody *bodies, double dt)
{
    calc_forces (bodies);

    //First, we change each body's velocity, using the forces calculated a line ago
    CBody *planets = bodies->data;
    for (int i=0; i<bodies->len; i++) {
        Vector2d acc;
        acc = (Vector2d){planets[i].fnet.x/planets[i].mass, planets[i].fnet.y/planets[i].mass};
        planets[i].vel = vectadd(planets[i].vel, vfmult(acc, dt));
    }

    //And then we change their positions based on their new velocities
    for (int i=0; i<bodies->len; i++) {
        planets[i].pos = vectadd(planets[i].pos, vfmult(planets[i].vel, dt));
    }
}
Exemplo n.º 2
0
vector tracing(vector *rayorigin,vector *raydir,int depth){

double tn=IN;
sphere *s=NULL;
double t0=IN,t1=IN;
for(int i=0;i<6;i++){

if(sphere_ray_inter(sp[i],*rayorigin,*raydir,&t0,&t1)){

if(t0<0)t0=t1;

if(t0<tn){

tn=t0;
//if(i==2)printf("\nshan\n");
s=&sp[i];
}
}
}

vector x;
vector z;

vec(&x,2,2,2);
vec(&z,0.5,0.5,0.5);

if(s==NULL)return x;
//else return s->color;

vector color=c;
//printf("%f\n",tn);
//getchar();
vector *tee=vectmult(tn,raydir);
vector *pointhit=vectadd(rayorigin,tee); 
vector *nhit=vectsub(pointhit,&(s->centre));
nhit=normalize(nhit); 

double bias=1e-4;
bool inside=false;

if(dotproduct(raydir,nhit)>0){
nhit=negative(nhit);
inside=true;
}

if((s->transparency>0||s->reflectivity>0)&&depth<maxdepth){

//tee=negative(raydir);

double ratio=-(dotproduct(raydir,nhit));
double fresnel=mixture(pow(1-ratio,3),1,0.1);

double m=dotproduct(raydir,nhit);

//Source : http://www.cs.rpi.edu/~cutler/classes/advancedgraphics/F05/lectures/13_ray_tracing.pdf

tee=vectmult(m*2,nhit);
vector *refldir=vectsub(raydir,tee);
refldir=normalize(refldir);
tee=vectmult(bias,nhit);
tee=vectadd(pointhit,tee);
vector reflection=tracing(tee,refldir,depth+1);
//tee=vectmult(fresnel,&reflection);
//return *tee;

//Source : http://steve.hollasch.net/cgindex/render/refraction.txt

vector refraction=c;
vector *tee1;

if(s->transparency){

double io=1.1,eta=(inside)?io:1/io; 

//tee=negative(nhit);

double cosine=-(dotproduct(nhit,raydir));
double k=1-eta*eta*(1-cosine*cosine);

tee=vectmult((eta*cosine-sqrt(k)),nhit);
tee1=vectmult(eta,raydir);
vector *refrdir=vectadd(tee,tee1);
refrdir=normalize(refrdir);
tee=vectmult(bias,nhit);
tee=vectsub(pointhit,tee);
refraction=tracing(tee,refrdir,depth+1);
//return refraction;
}

tee=vectmult(fresnel,&reflection);
tee1=vectmult(((1-fresnel)*s->transparency),&refraction);
tee=vectadd(tee,tee1);
tee=crossproduct(&(s->color),tee);
color=*tee;
//return *tee;  
}
//return *tee;

else {
 
for(int i=0;i<6;i++){

if(sp[i].emcolor.x>0){
 
vector transmission;

vec(&transmission,1,1,1);
vector *lightdir=vectsub(&(sp[i].centre),pointhit);
lightdir=normalize(lightdir);

for(int j=0;j<6;j++){

if (i != j){

tee=vectmult(bias,nhit);
tee=vectadd(pointhit,tee);
if(sphere_ray_inter(sp[j],*tee,*lightdir,&t0,&t1)){
transmission=c;
break;
}
}
}

tee=crossproduct(&transmission,&(s->color));
tee=crossproduct(&(sp[i].emcolor),tee);
tee=vectmult((max(double(0),dotproduct(nhit,lightdir))),tee);
tee=vectadd(&color,tee);
color=*tee;
}
}
}
  //  return sphere->surfaceColor;

return (*(vectadd(&color,&(s->emcolor))));
}