clean up
This commit is contained in:
parent
f5c912c489
commit
a7f8b46910
8 changed files with 28 additions and 52 deletions
115
src/ray_marching.go
Normal file
115
src/ray_marching.go
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
package main
|
||||
|
||||
// RIGHT : x
|
||||
// FORWARD : Y
|
||||
// UP : Z
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
const MAX_DIST = 1000.0
|
||||
const MAX_STEP = 1000
|
||||
const EPS = 0.01
|
||||
const WIDTH = 500
|
||||
const HEIGHT = 500
|
||||
const PI = math.Pi
|
||||
|
||||
var lightPos Vector3
|
||||
var scene SmoothUnionSDF
|
||||
|
||||
// radius, theta, phi
|
||||
var screenSpherical Vector3
|
||||
var screenPhysicalSize float64
|
||||
var cameraPos Vector3
|
||||
|
||||
func init() {
|
||||
lightPos = Vector3{0, -200, 800}
|
||||
|
||||
cameraPos = Vector3{0, -10, 30}
|
||||
screenSpherical = Vector3{10, PI/2 + 0.2, PI / 2}
|
||||
screenPhysicalSize = 5
|
||||
|
||||
sphereMat := RED_MAT
|
||||
sphereMat.specularFac = 0.5
|
||||
sphere := Sphere{Vector3{0, 100, 5}, 10, sphereMat}
|
||||
plane := Plane{Vector3{0, 0, 1}, 0, WHITE_GREY_GRID_MAT}
|
||||
|
||||
scene = SmoothUnionSDF{sphere, plane, 2}
|
||||
|
||||
}
|
||||
|
||||
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
|
||||
lightVec := (lightPos.Sub(point)).Normalized()
|
||||
reflectLight := Reflect(lightVec, normal)
|
||||
eyeVec := (cameraPos.Sub(point)).Normalized()
|
||||
ambiant := mat.ambiantColor.GetColor(point).Scale(0.1)
|
||||
|
||||
diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec))
|
||||
diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower)
|
||||
|
||||
specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp)
|
||||
specular := mat.specularColor.GetColor(point).Scale(specularPower)
|
||||
|
||||
return ambiant.Add(diffuse).Add(specular)
|
||||
}
|
||||
|
||||
func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
|
||||
p := origin
|
||||
for range MAX_STEP {
|
||||
dist, mat := scene.Distance(p)
|
||||
if dist < EPS {
|
||||
normal := Gradient(scene, p, EPS).Normalized()
|
||||
return true, phongShading(p, normal, mat)
|
||||
}
|
||||
if dist > MAX_DIST {
|
||||
break
|
||||
}
|
||||
p = p.Add(direction.Scale(dist))
|
||||
}
|
||||
return false, GREY
|
||||
}
|
||||
|
||||
func genImage() *rl.Image {
|
||||
screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian()
|
||||
screenCenter := cameraPos.Add(screenCartesian)
|
||||
|
||||
image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black)
|
||||
|
||||
for pixel_x := range WIDTH {
|
||||
for pixel_y := range HEIGHT {
|
||||
sx := screenPhysicalSize * (float64(pixel_x)/WIDTH - 0.5)
|
||||
sy := screenPhysicalSize * (float64(pixel_y)/WIDTH - 0.5)
|
||||
|
||||
origin := screenCenter.Add(phi_vec.Scale(-sx)).Add(theta_vec.Scale(sy))
|
||||
direction := (origin.Sub(cameraPos)).Normalized()
|
||||
|
||||
hit, color_vec := rayMarch(origin, direction)
|
||||
var fr, fg, fb float64
|
||||
if hit {
|
||||
fr, fg, fb = color_vec.Unpack()
|
||||
} else {
|
||||
fr, fg, fb = 0, 0, 0
|
||||
}
|
||||
fr, fg, fb = Clamp(fr, 0, 255), Clamp(fg, 0, 255), Clamp(fb, 0, 255)
|
||||
r, g, b := uint8(fr), uint8(fg), uint8(fb)
|
||||
rl.ImageDrawPixel(image, int32(pixel_x), int32(pixel_y), rl.Color{R: r, G: g, B: b, A: 255})
|
||||
}
|
||||
}
|
||||
|
||||
return image
|
||||
}
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(WIDTH, HEIGHT, "raymarching")
|
||||
texture := rl.LoadTextureFromImage(genImage())
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.Black)
|
||||
rl.DrawTexture(texture, 0, 0, rl.White)
|
||||
rl.EndDrawing()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue