Glass implemented

This commit is contained in:
Luxdragon 2024-03-03 20:07:26 +01:00
parent 6f54606e85
commit 6287a73f11
2 changed files with 71628 additions and 71610 deletions

143194
image.ppm

File diff suppressed because it is too large Load diff

44
main.go
View file

@ -150,6 +150,13 @@ func Reflect(v Vec3, n Vec3) Vec3 {
return v.Sub(n.Mult(2*Dot(v,n))) return v.Sub(n.Mult(2*Dot(v,n)))
} }
func Refract(uv Vec3, n Vec3, etai_over_etat float32) Vec3 {
cos_theta := float32(math.Min(float64(Dot(uv.Neg(), n)), 1.0))
var r_out_perp Vec3 = (uv.Add(n.Mult(cos_theta))).Mult(etai_over_etat)
var r_out_parallel Vec3 = n.Mult(float32(-math.Sqrt(math.Abs(float64(1.0 - r_out_perp.Length_squared())))))
return r_out_perp.Add(r_out_parallel)
}
const pi float32 = 3.1415926535897932385 const pi float32 = 3.1415926535897932385
func Degrees_to_radians(degrees float32) float32 { func Degrees_to_radians(degrees float32) float32 {
@ -363,33 +370,33 @@ func (hl Hittable) Hit(r *Ray, ray_t Interval, rec *Hit_record) bool {
// =============== MATERIAL ================= // =============== MATERIAL =================
type Material struct { type Material struct {
lambertian bool material int
metal bool
albedo Vec3 albedo Vec3
fuzz float32 fuzz float32
ir float32
} }
func NewMaterial(l bool, m bool, a Vec3, f float32) Material { func NewMaterial(m int, a Vec3, f float32, i float32) Material {
if f >= 1.0 { if f >= 1.0 {
return Material{ return Material{
lambertian: l, material: m,
metal: m,
albedo : a, albedo : a,
fuzz: 1.0, fuzz: 1.0,
ir: i,
} }
} else { } else {
return Material{ return Material{
lambertian: l, material: m,
metal: m,
albedo : a, albedo : a,
fuzz: f, fuzz: f,
ir: i,
} }
} }
} }
func (mat Material) Scatter(r_in *Ray, rec *Hit_record, attenuation *Vec3, scattered *Ray) bool { func (mat Material) Scatter(r_in *Ray, rec *Hit_record, attenuation *Vec3, scattered *Ray) bool {
if (mat.lambertian) { if (mat.material == 0) { //Lambertian
scatter_direction := rec.normal.Add(RandomUnitVector()) scatter_direction := rec.normal.Add(RandomUnitVector())
// Catch degenerate scatter direction // Catch degenerate scatter direction
if scatter_direction.Near_zero() { if scatter_direction.Near_zero() {
@ -398,13 +405,24 @@ func (mat Material) Scatter(r_in *Ray, rec *Hit_record, attenuation *Vec3, scatt
*scattered = *NewRay(rec.p, scatter_direction) *scattered = *NewRay(rec.p, scatter_direction)
*attenuation = mat.albedo *attenuation = mat.albedo
} else if (mat.metal) { } else if (mat.material == 1) { //Metall
var reflected Vec3 = Reflect(Unit_vector(r_in.Direction()), rec.normal) var reflected Vec3 = Reflect(Unit_vector(r_in.Direction()), rec.normal)
*scattered = *NewRay(rec.p, reflected.Add(RandomUnitVector().Mult(mat.fuzz))) *scattered = *NewRay(rec.p, reflected.Add(RandomUnitVector().Mult(mat.fuzz)))
*attenuation = mat.albedo *attenuation = mat.albedo
if !(Dot(scattered.Direction(), rec.normal) > 0) { if !(Dot(scattered.Direction(), rec.normal) > 0) {
return false return false
} }
} else if (mat.material == 2) { //Dielectric
*attenuation = NewColor(1.0, 1.0, 1.0)
var refraction_ration float32
if rec.front_face {
refraction_ration = 1/mat.ir
} else {
refraction_ration = mat.ir
}
unit_direction := Unit_vector(r_in.Direction())
refracted := Refract(unit_direction, rec.normal, refraction_ration)
*scattered = *NewRay(rec.p, refracted)
} }
return true return true
} }
@ -512,10 +530,10 @@ func (cam *Camera) Pixel_sample_square() Vec3 {
// =============== MAIN ===================== // =============== MAIN =====================
func main() { func main() {
// Materials // Materials
material_ground := NewMaterial(true, false, NewColor(0.8, 0.8, 0.0), 0.0) material_ground := NewMaterial(0, NewColor(0.8, 0.8, 0.0), 0.0, 0.0)
material_center := NewMaterial(true, false, NewColor(0.7, 0.3, 0.3), 0.0) material_center := NewMaterial(2, NewColor(0.7, 0.3, 0.3), 0.0, 1.5)
material_left := NewMaterial(false, true, NewColor(0.8, 0.8, 0.8), 0.3) material_left := NewMaterial(2, NewColor(0.8, 0.8, 0.8), 0.3, 1.5)
material_right := NewMaterial(false, true, NewColor(0.8, 0.6, 0.2), 1.0) material_right := NewMaterial(1, NewColor(0.8, 0.6, 0.2), 1.0, 0.0)
// World // World