Advertisement
Guest User

Hard coded variant

a guest
May 4th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.92 KB | None | 0 0
  1. using Images, Colors, ImageView, LinearAlgebra, Gtk.ShortNames, StaticArrays, BenchmarkTools, ForwardDiff, FixedPointNumbers
  2.  
  3. ## 1.750 ns (0 allocations: 0 bytes)
  4. function scene(Pos::SVector{3})
  5.   norm(Pos-SVector{3,Float64}(1.0,1.0,1.0))-50.0
  6. end
  7.  
  8.  
  9. ## 66.728 ns (3 allocations: 128 bytes)
  10. function ray_source(x::Real,y::Real,Pos::SVector{3},Direction::SVector{3},
  11.         maxx::Int64,maxy::Int64,Sensorxv::SVector{3},Sensoryv::SVector{3})
  12.   (SVector{3}(Pos+(maxx/2-x)/maxx*Sensorxv+(maxy/2-y)/maxy*Sensoryv),SVector{3}(normalize(Direction)))
  13. end
  14.  
  15. ##  17.810 ns (0 allocations: 0 bytes)
  16. function gradient(scene,Pos::SVector{3})
  17.   ForwardDiff.gradient(scene,Pos)
  18. end
  19.  
  20. ## 46.718 ns (0 allocations: 0 bytes)
  21. function raymarch(scene,Pos::SVector{3,Float64},Normal::SVector{3,Float64},renderlim::Float64)
  22. stepsize=1.0;
  23. Start=Pos;
  24. Strecke=0.0;
  25.  while (stepsize > 0.001)
  26.    stepsize=scene(Pos);
  27.    Pos+=Normal*stepsize;
  28.    Strecke+=stepsize
  29.    if Strecke > renderlim
  30.      return Gray{Normed{UInt8,8}}(0.25)
  31.    end
  32.  end
  33.   Light = clamp(dot(gradient(scene,Pos),normalize(SVector(-1,-1,0))),zero(Normed{UInt8,8})
  34. ,one(Normed{UInt8,8}) ##shader not modular
  35. )
  36.   return Gray{Normed{UInt8,8}}(Light)
  37. end
  38.  
  39. function run_it(y,x)
  40. ##y = 1920
  41. ##x = 1080
  42.  
  43. myPic = Array{Gray{Normed{UInt8,8}}}(undef,x,y)
  44.  
  45. SclY = 200.0
  46. SclX = x/y *SclY
  47. CamPosition = normalize(SVector{3,Float64}(1,-1,1))
  48. Center = SVector{3,Float64}(0,0,0)
  49. Normal = SVector{3,Float64}(normalize(Center-CamPosition))
  50. Helper = SVector{3,Float64}(Center + SVector{3,Float64}(1,0,0))
  51. Foot = SVector{3,Float64}(normalize(Helper-CamPosition) )
  52. Ortho2 = SVector{3,Float64}(cross(Normal,Foot))
  53. Ortho1 = SVector{3,Float64}(cross(Normal,Ortho2))
  54.  
  55.  
  56.  
  57. for i=1:x
  58.   for j=1:y
  59.      lpos, lnormal = ray_source(Float64(i),Float64(j),CamPosition,Normal,x,y,SclX*Ortho1,SclY*Ortho2)
  60.      myPic[i,j]=raymarch(scene,lpos,lnormal,500.0) ## this allocates
  61.   end
  62. end
  63. myPic
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement