Raymarcher mostly working, need to fix specular still
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
#import "math.kl"
|
||||
|
||||
Epsilon :: 0.00001
|
||||
Screen : *U32
|
||||
X : S64
|
||||
Y : S64
|
||||
Epsilon :: 0.00001
|
||||
Screen : *U32
|
||||
X : S64
|
||||
Y : S64
|
||||
TotalTime: F64
|
||||
LightPos := Vec3{2,4,2}
|
||||
|
||||
SphereSDF :: (pos: Vec3): F32
|
||||
result := Vec3_Length(pos) - 1.0
|
||||
@@ -14,8 +16,14 @@ Raymarcher_Update :: ()
|
||||
forward := Vec3{0, 0, -1}
|
||||
side := Vec3_Normalize(Vec3_Cross(forward, up))
|
||||
|
||||
light_pos := Vec3{0, 4, 0}
|
||||
LightPos.x = cosf(TotalTime->F32)*4
|
||||
LightPos.y = sinf(TotalTime->F32)*4
|
||||
|
||||
ambient_color := Vec3{0.2,0.2,0.2}
|
||||
diffuse_color := Vec3{0.7,0.2,0.2}
|
||||
specular_color := Vec3{1,1,1}
|
||||
eye := Vec3{0, 0, 2}
|
||||
light_intensity :: 1.2
|
||||
|
||||
Xf := 1 / X->F32
|
||||
Yf := 1 / Y->F32
|
||||
@@ -25,14 +33,13 @@ Raymarcher_Update :: ()
|
||||
uv := Vec3{x->F32 * Xf * 2 - 1, y->F32 * Yf * 2 - 1, 1.0}
|
||||
uv.x *= ratio
|
||||
dir := Vec3_Normalize(Vec3{Vec3_Dot(side, uv), Vec3_Dot(up, uv), Vec3_Dot(forward, uv)})
|
||||
pos := Vec3{0, 0, 2}
|
||||
|
||||
t: F32
|
||||
end: F32 = 100.0
|
||||
hit := true
|
||||
p: Vec3
|
||||
for i := 0, i < 255, i+=1
|
||||
p = Vec3_Add(pos, Vec3_MulF32(dir, t))
|
||||
p = Vec3_Add(eye, Vec3_MulF32(dir, t))
|
||||
|
||||
distance := SphereSDF(p)
|
||||
if distance < Epsilon
|
||||
@@ -50,9 +57,28 @@ Raymarcher_Update :: ()
|
||||
SphereSDF({p.x, p.y, p.z + Epsilon}) - SphereSDF({p.x, p.y, p.z - Epsilon}),
|
||||
})
|
||||
|
||||
diffuse := Vec3_Dot(normal, Vec3_Negate())
|
||||
light_to_point := Vec3_Normalize(Vec3_Sub(LightPos, p))
|
||||
eye_to_point := Vec3_Normalize(Vec3_Sub(eye, p))
|
||||
reflected_light := Vec3_Normalize(Vec3_Reflect(Vec3_Negate(light_to_point), normal))
|
||||
|
||||
Screen[x + y*X] = Vec3_ConvertToARGB(normal)
|
||||
ambient :: 0.2
|
||||
diffuse := Vec3_Dot(normal, light_to_point)
|
||||
|
||||
color := Vec3_MulF32(ambient_color, ambient)
|
||||
if diffuse > Epsilon
|
||||
color = Vec3_Add(color, Vec3_MulF32(diffuse_color, diffuse))
|
||||
|
||||
specular := Vec3_Dot(reflected_light, eye_to_point)
|
||||
if specular > Epsilon
|
||||
specular = specular*specular*specular*specular
|
||||
color = Vec3_Add(color, Vec3_MulF32(specular_color, specular*0.2))
|
||||
color = Vec3_MulF32(color, light_intensity)
|
||||
|
||||
// Gamma correction
|
||||
color.x = sqrtf(color.x)
|
||||
color.y = sqrtf(color.y)
|
||||
color.z = sqrtf(color.z)
|
||||
Screen[x + y*X] = Vec3_ConvertToARGB(color)
|
||||
|
||||
else;; Screen[x + y*X] = 0
|
||||
|
||||
@@ -147,7 +173,6 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
|
||||
requested_time_per_frame := 1.0 / 60.0
|
||||
frame_start_time := Time()
|
||||
frame_number: S64
|
||||
total_time: F64
|
||||
for AppIsRunning
|
||||
msg: MSG
|
||||
for PeekMessageW(&msg, window, 0, 0, PM_REMOVE) > 0
|
||||
@@ -163,6 +188,8 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
|
||||
|
||||
|
||||
frame_time := Time() - frame_start_time
|
||||
frame_number += 1
|
||||
TotalTime += frame_time
|
||||
if frame_time < requested_time_per_frame
|
||||
if good_scheduling
|
||||
time_to_sleep := (requested_time_per_frame - frame_time) * 1000
|
||||
@@ -176,5 +203,3 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
|
||||
new_frame_time = Time() - frame_start_time
|
||||
|
||||
frame_time = new_frame_time
|
||||
frame_number += 1
|
||||
total_time += frame_time
|
||||
Reference in New Issue
Block a user