Posted By

joaoalberto on 02/17/19


Tagged


Versions (?)

ray-tracer reformaté


 / Published in: C
 

reformaté pour plus de visibilité, en 49 lignes de code

  1. #include <stdlib.h>
  2. // card > aek.ppm
  3. #include <stdio.h>
  4. #include <math.h>
  5. typedef int i;
  6. typedef float f;
  7. struct v{
  8. f x,y,z;
  9. v operator+(v r){ return v(x+r.x,y+r.y,z+r.z); }
  10. v operator*(f r){ return v(x*r,y*r,z*r); }
  11. f operator%(v r){ return x*r.x+y*r.y+z*r.z; }
  12. v(){} v operator^(v r){return v(y*r.z-z*r.y,z*r.x-x*r.z,x*r.y-y*r.x); }
  13. v(f a,f b,f c){ x=a;y=b;z=c; }
  14. v operator!(){ return*this*(1/sqrt(*this%*this)); }
  15. };
  16. i G[]={247570,280596,280600,249748,18578,18577,231184,16,16};
  17. f R(){ return(f)rand()/RAND_MAX; }
  18. i T(v o,v d,f&t,v&n){ t=1e9;i m=0;f p=-o.z/d.z;if(.01<p)t=p,n=v(0,0,1),m=1;
  19. for(i k=19;k--;)
  20. for(i j=9;j--;)
  21. if(G[j]&1<<k){
  22. v p=o+v(-k,0,-j-4);f b=p%d,c=p%p-1,q=b*b-c;if(q>0){f s=-b-sqrt(q);if(s<t&&s>.01)t=s,n=!(p+d*t),m=2;}
  23. }
  24. return m;
  25. }
  26. v S(v o,v d){
  27. f t;v n;i m=T(o,d,t,n);
  28. if(!m) return v(.7,.6,1)*pow(1-d.z,4);
  29. v h=o+d*t,l=!(v(9+R(),9+R(),16)+h*-1),r=d+n*(n%d*-2);
  30. f b=l%n;if(b<0||T(h,l,t,n))b=0;
  31. f p=pow(l%r*(b>0),99);
  32. if(m&1){
  33. h=h*.2;
  34. return((i)(ceil(h.x)+ceil(h.y))&1?v(3,1,1):v(3,3,3))*(b*.2+.1);
  35. }
  36. return v(p,p,p)+S(h,r)*.5;
  37. }
  38. i main(){
  39. printf("P6 512 512 255 ");
  40. v g=!v (-6,-16,0),a=!(v(0,0,1)^g)*.002,b=!(g^a)*.002,c=(a+b)*-256+g;
  41. for(i y=512;y--;)
  42. for(i x=512;x--;){
  43. v p(13,13,13);
  44. for(i r=64;r--;){
  45. v t=a*(R()-.5)*99+b*(R()-.5)*99;
  46. p=S(v(17,16,8)+t,!(t*-1+(a*(R()+x)+b*(y+R())+c)*16))*3.5+p;}
  47. printf("%c%c%c",(i)p.x,(i)p.y,(i)p.z);
  48. }
  49. }

Report this snippet  

You need to login to post a comment.