Advertisement
Guest User

Functions of functions

a guest
May 4th, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 4.81 KB | None | 0 0
  1. using Images, Colors, ImageView, LinearAlgebra, Gtk.ShortNames, StaticArrays, BenchmarkTools, ForwardDiff, FixedPointNumbers, Profile, StatProfilerHTML
  2.  
  3. function ray_source(x::Real,y::Real,Pos::SVector{3},Direction::SVector{3},
  4.         maxx::Int64,maxy::Int64,Sensorxv::SVector{3},Sensoryv::SVector{3})
  5.   (SVector{3}(Pos+(maxx/2-x)/maxx*Sensorxv+(maxy/2-y)/maxy*Sensoryv),SVector{3}(normalize(Direction)))
  6. end
  7.  
  8. const x = 800
  9. const y = 800
  10.  
  11. Picff  = Array{RGB{Normed{UInt8,8}}}(undef,x,y)
  12. PicTos = Array{RGB{Normed{UInt8,8}}}(undef,x,y)
  13. PicExp = Array{RGB{Normed{UInt8,8}}}(undef,x,y)
  14.  
  15. const CamPosition = SVector{3}(-10.0,10.0,20.0)
  16. const Normal = SVector{3}(10.0,-10.0,-20.0)
  17. const SclX = 60.0
  18. const SclY = 60.0
  19.  
  20. const Ortho1 = SVector{3}(  0.0, -0.8944271909999159, 0.4472135954999579)
  21. const Ortho2 = SVector{3}( 0.9128709291752769, 0.18257418583505536, 0.3651483716701107 )
  22.  
  23.  
  24. function union(SDFs...)
  25.    return (Pos::SVector{3}) -> minimum(map(x->x(Pos),SDFs))
  26. end
  27.  
  28. function trans(Sdf,Vec)
  29.   return (Pos::SVector{3})->(Sdf(Pos-Vec))
  30. end
  31.  
  32. function plane(Normal::SVector{3},Shader)
  33.   return (Pos::SVector{3}) -> (dot(Pos,Normal),rand(),Shader)
  34. end
  35.  
  36. function sphere(Radius::Float64,Shader)
  37.   a = rand() #Needed for strict ordering since my lambdas/shaders can't be compared
  38.   return (Pos::SVector{3}) -> return (norm(Pos)-Radius,a,Shader)
  39. end
  40.  
  41. function unicolor(Color::RGB)
  42.   return (Pos,Normal,Sdf,Step,Inital)->Color
  43. end
  44.  
  45. function checkerbox(BSize,Shader1,Shader2)
  46.   return (Pos::SVector{3},Normal::SVector{3},Sdf,Step,Inital) -> if xor(mod(Pos[1],2*BSize)>BSize,mod(Pos[2],2*BSize)>BSize,mod(Pos[3],2*BSize)>BSize)   Shader1(Pos,Normal,Sdf,Step,Inital) else Shader2(Pos,Normal,Sdf,Step,Inital) end
  47. end
  48.  
  49. function gradient(Pos::SVector{3},Normal,Sdf,Step::Float64,Inital)
  50.   normalize([(Sdf(Pos+SVector{3}(Step,0.0,0.0))[1] - Sdf(Pos)[1])/Step,
  51.              (Sdf(Pos+SVector{3}(0.0,Step,0.0))[1] - Sdf(Pos)[1])/Step,
  52.              (Sdf(Pos+SVector{3}(0.0,0.0,Step))[1] - Sdf(Pos)[1])/Step])
  53. end
  54.  
  55. function lambertian(LightDir,Shader)
  56.   return (Pos::SVector{3},Normal,Sdf,Step,Inital)->(Shader(Pos,Normal,Sdf,Step,Inital)*max(0,dot(gradient(Pos,Normal,Sdf,Step,Inital),LightDir)))::RGB{Normed{UInt8,8}}
  57. end
  58.  
  59. function spot(LightPos,Brightness,Shader)
  60.   return (Pos::SVector{3},Normal::SVector{3},Sdf,Step,Inital)->(Shader(Pos,Normal,Sdf,Step,Inital)*min(1,max(0.1,Brightness*dot(gradient(Pos,Normal,Sdf,Step,Inital),normalize(LightPos-Pos)))))
  61. end
  62.  
  63.  
  64.  
  65.  
  66. const Red   = unicolor(RGB{Normed{UInt8,8}}(0.8,0.0,0.0))
  67. const Green = unicolor(RGB{Normed{UInt8,8}}(0.0,0.9,0.0))
  68. const Blue  = unicolor(RGB{Normed{UInt8,8}}(0.0,0.0,0.7))
  69. const Black = unicolor(RGB{Normed{UInt8,8}}(0.0,0.0,0.0))
  70. const White = unicolor(RGB{Normed{UInt8,8}}(1.0,1.0,1.0))
  71. const Grey = unicolor(RGB{Normed{UInt8,8}}(0.5,0.5,0.5))
  72.  
  73. function scene_ff(Pos)  
  74.    union(trans(sphere(3.0,spot(SVector{3,Float64}(0,0,0),1.5,Red)),SVector{3,Float64}(0,20,0)),
  75.          trans(sphere(12.0,spot(SVector{3,Float64}(0,0,0),1.8,Blue)),SVector{3,Float64}(12,9,-4)),
  76.          trans(sphere(9.0,spot(SVector{3,Float64}(0,0,0),1.0,Green)),SVector{3,Float64}(3,-13,0)),
  77.          sphere(1.0,White),
  78.          trans(sphere(0.5,White),SVector{3,Float64}(-20,0,0)),
  79.          trans(sphere(1.0,Red),SVector{3,Float64}(-18,0,0)),
  80.          plane(SVector{3,Float64}(0.0,0.0,1.0),checkerbox(10,Grey,Black)))(Pos)
  81. end
  82.  
  83.  
  84.  
  85. function raymarch(Pos::SVector{3},Normal::SVector{3},Sdf,FbSDF,renderdist)
  86. Normal=normalize(Normal);
  87. stepsize=1.0;
  88. Start=Pos;
  89. Strecke=0.0;
  90. while (stepsize > 0.001)
  91.    stepsize=Sdf(Pos)[1];
  92.    Pos+=Normal*stepsize;
  93.    Strecke+=stepsize
  94.       if Strecke > renderdist
  95.          return FbSDF(Pos,Normal,Sdf,stepsize,Start)
  96.       end
  97.    end
  98.    return Sdf(Pos)[3](Pos,Normal,Sdf,stepsize,Start)
  99. end
  100.  
  101. for h=-10.0:0.5:10.0
  102.   for i=-10.0:0.5:10.0
  103.     for j=-10.0:0.5:10.0
  104.       scene_ff(SVector{3,Float64}(h,i,j))
  105.     end
  106.   end
  107. end
  108. ##  40.602 ms (1309499 allocations: 178.49 MiB)
  109.  
  110.  
  111. for i=1:x
  112.   @profile for j=1:y
  113.      lpos, lnormal = ray_source(Float64(i),Float64(j),CamPosition,Normal,x,y,SclX * Ortho1,SclY * Ortho2)
  114.      Picff[i,j]=raymarch(lpos,lnormal,scene_ff,Black,500.0)
  115.   end
  116. end
  117.  
  118. statprofilehtml()
  119. #=
  120. @btime for i=1:x
  121.   for j=1:y
  122.      lpos, lnormal = ray_source(Float64(i),Float64(j),$CamPosition,$Normal,x,y,$SclX * $Ortho1,$SclY * $Ortho2)
  123.      Picff[i,j]=raymarch(lpos,lnormal,$scene_ff,$Black,500.0)
  124.   end
  125. end
  126. ##  5.077 s (133328084 allocations: 16.42 GiB)
  127.  
  128. guidict = imshow(Picff);
  129.  
  130. if (!isinteractive())
  131.  
  132.     # Create a condition object
  133.     c = Condition()
  134.  
  135.     # Get the window
  136.     win = guidict["gui"]["window"]
  137.  
  138.     # Notify the condition object when the window closes
  139.     signal_connect(win, :destroy) do widget
  140.         notify(c)
  141.     end
  142.  
  143.     # Wait for the notification before proceeding ...
  144.     wait(c)
  145. end
  146. =#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement