Advertisement
baksatibi

Untitled

Jul 3rd, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. #!/usr/bin/env bc -lq
  2.  
  3. width=320
  4. height=240
  5. angle=0.785 #pi/4
  6.  
  7. spherepos[0]=width/2; spherepos[1]=height/2; spherepos[2]=140
  8. sphereradius=100
  9. spherecolor[0]=255; spherecolor[1]=170; spherecolor[2]=85
  10.  
  11. lightpos[0]=0; lightpos[1]=0; lightpos[2]=0
  12. campos[0]=width/2; campos[1]=height/2; campos[2]=-((width/2)/(s(angle/2)/c(angle/2)))
  13.  
  14. diffuse=0.75
  15. specular=0.5
  16. ambient=0.25
  17. shininess=10
  18.  
  19. bgcolor[0]=0; bgcolor[1]=0; bgcolor[2]=0
  20.  
  21. scale=4
  22. defaultscale=scale
  23.  
  24. define len(vector[]) {
  25.     return sqrt(vector[0]^2+vector[1]^2+vector[2]^2)
  26. }
  27.  
  28. define clamp(value, min, max) {
  29.     if(value<min) {
  30.         return min
  31.     } else if(value>max) {
  32.         return max
  33.     }
  34.    
  35.     return value
  36. }
  37.  
  38. define printpixel(color[]) {
  39.     scale=0
  40.    
  41.     print "\\\\x0\\\\x",color[2]/1,"\\\\x",color[1]/1,"\\\\x",color[0]/1,"\n"
  42.    
  43.     scale=defaultscale
  44. }
  45.  
  46. define shade(normal[], dir[], lightdir[], diffusecolor[]) {
  47.     auto cosang, half[], l, blinnterm, color[]
  48.    
  49.     cosang=clamp(normal[0]*lightdir[0]+normal[1]*lightdir[1]+normal[2]*lightdir[2], 0, 1)
  50.    
  51.     half[0]=dir[0]+lightdir[0]; half[1]=dir[1]+lightdir[1]; half[2]=dir[2]+lightdir[2]
  52.     l=len(half[])
  53.     half[0]/=l; half[1]/=l; half[2]/=l
  54.    
  55.     blinnterm=0
  56.     if(cosang>0) {
  57.         blinnterm=clamp(normal[0]*half[0]+normal[1]*half[1]+normal[2]*half[2], 0, 1)^shininess
  58.     }
  59.    
  60.     color[0]=clamp(diffusecolor[0]*cosang*diffuse+255*blinnterm*specular+diffusecolor[0]*ambient, 0, 255)
  61.     color[1]=clamp(diffusecolor[1]*cosang*diffuse+255*blinnterm*specular+diffusecolor[1]*ambient, 0, 255)
  62.     color[2]=clamp(diffusecolor[2]*cosang*diffuse+255*blinnterm*specular+diffusecolor[2]*ambient, 0, 255)
  63.    
  64.     null=printpixel(color[])
  65. }
  66.  
  67. define sphere(pos[], radius, origin[], dir[]) {
  68.     auto s[], a, b, c, square, t, t2
  69.    
  70.     s[0]=pos[0]-origin[0]; s[1]=pos[1]-origin[1]; s[2]=pos[2]-origin[2]
  71.    
  72.     a=1 #the expression would be (dir[0]^2+dir[1]^2+dir[2]^2), but it's always 1 because the vector is normalized
  73.     b=-2*(dir[0]*s[0]+dir[1]*s[1]+dir[2]*s[2])
  74.     c=s[0]^2+s[1]^2+s[2]^2-radius^2
  75.    
  76.     square=(b^2)-(4*a*c)
  77.    
  78.     if(square>0) {
  79.         t=(-b+sqrt(square))/(2*a)
  80.         t2=(-b-sqrt(square))/(2*a)
  81.        
  82.         if(t2<t) {
  83.             t=t2
  84.         }
  85.        
  86.         return t
  87.     }
  88.    
  89.     return 0
  90. }
  91.  
  92. define trace(x, y) {
  93.     auto dir[], l, t, is[], normal[], lightdir[]
  94.    
  95.     dir[0]=x-campos[0]; dir[1]=y-campos[1]; dir[2]=0-campos[2]
  96.     l=len(dir[])
  97.     dir[0]/=l; dir[1]/=l; dir[2]/=l
  98.    
  99.     t=sphere(spherepos[], sphereradius, campos[], dir[])
  100.    
  101.     if(t>0) {
  102.         is[0]=t*dir[0]+campos[0]; is[1]=t*dir[1]+campos[1]; is[2]=t*dir[2]+campos[2]
  103.        
  104.         normal[0]=spherepos[0]-is[0]; normal[1]=spherepos[1]-is[1]; normal[2]=spherepos[2]-is[2]
  105.         l=len(normal[])
  106.         normal[0]/=l; normal[1]/=l; normal[2]/=l
  107.        
  108.         lightdir[0]=is[0]-lightpos[0]; lightdir[1]=is[1]-lightpos[1]; lightdir[2]=is[2]-lightpos[2]
  109.         l=len(lightdir[])
  110.         lightdir[0]/=l; lightdir[1]/=l; lightdir[2]/=l
  111.        
  112.         null=shade(normal[], dir[], lightdir[], spherecolor[])
  113.         return
  114.     }
  115.    
  116.     null=printpixel(bgcolor[])
  117. }
  118.  
  119. define mainloop(width, height) {
  120.     auto x, y
  121.    
  122.     for(y=0;y<height;y++) {
  123.         for(x=0;x<width;x++) {
  124.             null=trace(x, y)
  125.         }
  126.     }
  127. }
  128.  
  129. obase=16
  130.  
  131. null=mainloop(width, height)
  132.  
  133. halt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement