Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Images, Colors, ImageView, LinearAlgebra, Gtk.ShortNames, StaticArrays, BenchmarkTools, ForwardDiff, FixedPointNumbers
- ## 1.750 ns (0 allocations: 0 bytes)
- function scene(Pos::SVector{3})
- norm(Pos-SVector{3,Float64}(1.0,1.0,1.0))-50.0
- end
- ## 66.728 ns (3 allocations: 128 bytes)
- function ray_source(x::Real,y::Real,Pos::SVector{3},Direction::SVector{3},
- maxx::Int64,maxy::Int64,Sensorxv::SVector{3},Sensoryv::SVector{3})
- (SVector{3}(Pos+(maxx/2-x)/maxx*Sensorxv+(maxy/2-y)/maxy*Sensoryv),SVector{3}(normalize(Direction)))
- end
- ## 17.810 ns (0 allocations: 0 bytes)
- function gradient(scene,Pos::SVector{3})
- ForwardDiff.gradient(scene,Pos)
- end
- ## 46.718 ns (0 allocations: 0 bytes)
- function raymarch(scene,Pos::SVector{3,Float64},Normal::SVector{3,Float64},renderlim::Float64)
- stepsize=1.0;
- Start=Pos;
- Strecke=0.0;
- while (stepsize > 0.001)
- stepsize=scene(Pos);
- Pos+=Normal*stepsize;
- Strecke+=stepsize
- if Strecke > renderlim
- return Gray{Normed{UInt8,8}}(0.25)
- end
- end
- Light = clamp(dot(gradient(scene,Pos),normalize(SVector(-1,-1,0))),zero(Normed{UInt8,8})
- ,one(Normed{UInt8,8}) ##shader not modular
- )
- return Gray{Normed{UInt8,8}}(Light)
- end
- function run_it(y,x)
- ##y = 1920
- ##x = 1080
- myPic = Array{Gray{Normed{UInt8,8}}}(undef,x,y)
- SclY = 200.0
- SclX = x/y *SclY
- CamPosition = normalize(SVector{3,Float64}(1,-1,1))
- Center = SVector{3,Float64}(0,0,0)
- Normal = SVector{3,Float64}(normalize(Center-CamPosition))
- Helper = SVector{3,Float64}(Center + SVector{3,Float64}(1,0,0))
- Foot = SVector{3,Float64}(normalize(Helper-CamPosition) )
- Ortho2 = SVector{3,Float64}(cross(Normal,Foot))
- Ortho1 = SVector{3,Float64}(cross(Normal,Ortho2))
- for i=1:x
- for j=1:y
- lpos, lnormal = ray_source(Float64(i),Float64(j),CamPosition,Normal,x,y,SclX*Ortho1,SclY*Ortho2)
- myPic[i,j]=raymarch(scene,lpos,lnormal,500.0) ## this allocates
- end
- end
- myPic
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement