-
Notifications
You must be signed in to change notification settings - Fork 1
/
lunray.nelua
259 lines (239 loc) · 6.68 KB
/
lunray.nelua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
##[[
DENOISER = true
-- OPENMP = true
-- CARTOONIZED = true
-- EMSCRIPTEN = true
]]
require 'config'
require 'mathx'
require 'vec2'
require 'vec3'
require 'argbcolor'
require 'perlin'
require 'sdl2'
require 'ray'
require 'texture'
require 'material'
require 'sphere'
require 'camera'
require 'window'
local SCREEN_WIDTH <comptime> = 480
local SCREEN_HEIGHT <comptime> = 270
local NUMSAMPLES <comptime> = 1
local MAXDEPTH <comptime> = 8
local APERTURE <comptime> = 0.05
local SCREEN_SCALE <comptime> = 1
local worldsize
local Hitlist = @array(Sphere,500)
local world: Hitlist
local cam: Camera
local window: Window
local pixels: [SCREEN_HEIGHT][SCREEN_WIDTH]argbcolor
## if DENOISER then
local tempixels: [SCREEN_HEIGHT][SCREEN_WIDTH]vec3
local ntem = 0
## end
local lastticks = SDL_GetTicks()
local fps = 0
local quit = false
local function Hitlist_hit(self: *Hitlist, r: Ray, tmin: number, tmax: number): (boolean, Hit)
local rec: Hit
local tnear = tmax
local hashit = false
for i=0,<worldsize do
local hitted, temprec = Sphere.hit(self[i], r, tmin, tnear)
if hitted and temprec.t < tnear then
hashit = true
tnear = temprec.t
rec = temprec
end
end
return hashit, rec
end
local function sky_color(direction: vec3)
local t = 0.5*(direction.y + 1)
return vec3.lerp(vec3{1,1,1}, vec3{0.5, 0.7, 1}, t)
end
local function trace_ray(r: Ray, depth: integer): vec3 <noinline>
local hitted, rec = Hitlist_hit(world, r, 0.001, 1e32)
if hitted then
local emitted = Material.emitted(rec.mat, rec.uv, rec.p)
if depth > MAXDEPTH then
return emitted
end
local forward, attenuation, scattered = Material.scatter(rec.mat, r, rec)
if forward then
return emitted + trace_ray(scattered, depth+1) * attenuation
else
return emitted
end
end
return sky_color(r.direction)
end
local function draw() <noinline>
## if DENOISER then
if ntem == 0 then
for y=0,<SCREEN_HEIGHT do
for x=0,<SCREEN_WIDTH do
tempixels[y][x] = vec3{}
end
end
end
ntem = ntem + 1
## end
local yoff = window.height - 1
##[[if OPENMP then
cemit '#pragma omp parallel for schedule(dynamic)'
end]]
for y=0,<SCREEN_HEIGHT do
for x=0,<SCREEN_WIDTH do
## if NUMSAMPLES.value:tonumber() == 1 then
local v, u
## if DENOISER then
v, u = (y + math.fastrand() - 0.5) / SCREEN_HEIGHT, (x + math.fastrand() - 0.5) / SCREEN_WIDTH
## else
v, u = y / SCREEN_HEIGHT, x / SCREEN_WIDTH
## end
local col = trace_ray(Camera.getray(cam, u, v), 0)
## else
local col: vec3
for i=1,NUMSAMPLES do
local v, u = (y + math.fastrand() - 0.5) / SCREEN_HEIGHT, (x + math.fastrand() - 0.5) / SCREEN_WIDTH
col = col + trace_ray(Camera.getray(cam, u, v), 0)
end
col = col / NUMSAMPLES
## end
col = vec3.sqrt(col) -- gamma correction
col = vec3.min(col, vec3{1,1,1})
## if DENOISER then
tempixels[y][x] = tempixels[y][x] + col
pixels[yoff - y][x] = argbcolor.fromvec3(tempixels[y][x] / ntem)
## else
pixels[yoff - y][x] = argbcolor.fromvec3(col)
## end
end
end
window:uploadpixels(&pixels[0][0])
end
local function refresh()
## if DENOISER then
ntem = 0
## end
end
local function world_init()
world[1] = Sphere{center={ 0,-1000,0}, radius=1000, mat={MATERIALS.LAMBERTIAN, albedo={TEXTURES.CHECKER, diffuse_color={0.1, 0.1, 0.1}, scale=10}}}
world[3] = Sphere{center={ 0, 1,0}, radius=1, mat={MATERIALS.DIELECTRIC, fuzz=1.5}}
world[2] = Sphere{
center={-3,1,0},
radius=1,
mat={
MATERIALS.LAMBERTIAN,
albedo={
TEXTURES.NOISE,
diffuse_color={0.9, 0.1, 0.1},
scale=10
}
}
}
world[4] = Sphere{center={ 3, 1,0}, radius=1, mat={MATERIALS.METAL, albedo={TEXTURES.COLOR, {0.7, 0.6, 0.5}}}}
local i = 5
local NUMSPHERES <comptime> = 3
for a=-NUMSPHERES,<NUMSPHERES do
for b=-NUMSPHERES,<NUMSPHERES do
local choosemat = math.random()
local center = vec3{-4+8*math.random(), 0.2, -4+8*math.random()}
if choosemat < 0.8 then --diffuse
world[i] = Sphere{center=center, radius=0.2, mat={MATERIALS.LAMBERTIAN, albedo={TEXTURES.COLOR, {math.random()*math.random(), math.random()*math.random(), math.random()*math.random()}}}}
elseif choosemat < 0.95 then -- metal
world[i] = Sphere{center=center, radius=0.2, mat={MATERIALS.METAL, albedo={TEXTURES.COLOR, {0.5*(1+math.random()), 0.5*(1+math.random()), 0.5*(1+math.random())}}, fuzz=0.5*math.random()}}
else -- glass
world[i] = Sphere{center=center, radius=0.2, mat={MATERIALS.DIELECTRIC, fuzz=1.5}}
end
i = i + 1
end
end
worldsize = i
for i=0,<worldsize do
Sphere.update(world[i])
end
local lookfrom = vec3{5.1,1.2,2.4}
local lookat = vec3{2.0,1,0.8}
local vup = vec3{0,1,0}
local vfov = 59
local aspect = SCREEN_WIDTH/SCREEN_HEIGHT
local aperture: number = APERTURE
local focusdist: number = 4
cam = Camera.create(lookfrom, lookat, vup, vfov, aspect, aperture, focusdist)
end
local function poll_events() <noinline>
local event: SDL_Event
while SDL_PollEvent(&event) ~= 0 do
switch event.type
case SDL_QUIT then
quit = true
case SDL_KEYDOWN then
local kevent = (@*SDL_KeyboardEvent)(&event)
switch kevent.keysym.sym
case SDLK_UP then
cam:rotate(0.05, 0)
refresh()
case SDLK_DOWN then
cam:rotate(-0.05, 0)
refresh()
case SDLK_RIGHT then
cam:rotate(0, -0.05)
refresh()
case SDLK_LEFT then
cam:rotate(0, 0.05)
refresh()
case SDLK_ESCAPE then
quit = true
case SDLK_w then
cam:translate(0, 0, 0.05)
refresh()
case SDLK_s then
cam:translate(0, 0, -0.05)
refresh()
case SDLK_a then
cam:translate(-0.05, 0, 0)
refresh()
case SDLK_d then
cam:translate(0.05, 0, 0)
refresh()
case SDLK_e then
cam:translate(0, 0.05, 0)
refresh()
case SDLK_q then
cam:translate(0, -0.05, 0)
refresh()
end
end
end
end
local function frame()
local ticks = SDL_GetTicks()
if ticks - lastticks >= 1000 then
print('FPS', fps)
lastticks = ticks
fps = 0
end
poll_events()
draw()
fps = fps + 1
end
local function emframe(context: pointer) <codename 'emframe'>
frame()
end
local function go()
world_init()
window = Window.create(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_SCALE)
## if not EMSCRIPTEN then
repeat
frame()
until quit
window:destroy()
## else
## cemit 'emscripten_set_main_loop_arg(emframe, 0, -1, 1);'
## end
end
go()