Various bug fixes + Material setters
This commit is contained in:
parent
e35cca1830
commit
f5c912c489
5 changed files with 53 additions and 22 deletions
40
material.go
40
material.go
|
|
@ -15,7 +15,7 @@ type GridColor struct {
|
||||||
func (grid GridColor) GetColor(p Vector3) Vector3 {
|
func (grid GridColor) GetColor(p Vector3) Vector3 {
|
||||||
ix := int(math.Floor(p.X / grid.size))
|
ix := int(math.Floor(p.X / grid.size))
|
||||||
iy := int(math.Floor(p.Y / grid.size))
|
iy := int(math.Floor(p.Y / grid.size))
|
||||||
iz := int(math.Floor(p.Z / grid.size))
|
iz := int(math.Floor(p.Z/grid.size)) * 0
|
||||||
|
|
||||||
if (ix+iy+iz)%2 == 0 {
|
if (ix+iy+iz)%2 == 0 {
|
||||||
return grid.color1.GetColor(p)
|
return grid.color1.GetColor(p)
|
||||||
|
|
@ -37,14 +37,14 @@ type Material struct {
|
||||||
refractIndice float64
|
refractIndice float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultMaterial(ambiantColor Color) Material {
|
func DefaultMaterial(diffuseColor Color) Material {
|
||||||
return Material{
|
return Material{
|
||||||
ambiantColor: ambiantColor,
|
ambiantColor: Vector3{0, 0, 0},
|
||||||
diffuseColor: Vector3{255, 255, 255},
|
diffuseColor: diffuseColor,
|
||||||
diffuseFac: 0.5,
|
diffuseFac: 1,
|
||||||
specularColor: Vector3{255, 255, 255},
|
specularColor: Vector3{255, 255, 255},
|
||||||
specularFac: 1.0,
|
specularFac: 2,
|
||||||
specularExp: 32.0,
|
specularExp: 35.0,
|
||||||
reflectanceFac: 0.0,
|
reflectanceFac: 0.0,
|
||||||
reflectanceTint: Vector3{1.0, 1.0, 1.0},
|
reflectanceTint: Vector3{1.0, 1.0, 1.0},
|
||||||
refractFac: 0.0,
|
refractFac: 0.0,
|
||||||
|
|
@ -67,6 +67,32 @@ func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setters
|
||||||
|
|
||||||
|
func ChangeAmbiantColor(m *Material, newAmbiantColor Color) {
|
||||||
|
m.ambiantColor = newAmbiantColor
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChangeDiffuseColor(m *Material, newDiffuseColor Color) {
|
||||||
|
m.diffuseColor = newDiffuseColor
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChangeSpecularColor(m *Material, newSpecularColor Color) {
|
||||||
|
m.specularColor = newSpecularColor
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChangeDiffuseFac(m *Material, newDiffuseFac float64) {
|
||||||
|
m.diffuseFac = newDiffuseFac
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChangeSpecularFac(m *Material, newSpecularFac float64) {
|
||||||
|
m.specularFac = newSpecularFac
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChangeSpecularExp(m *Material, newSpecularExp float64) {
|
||||||
|
m.specularExp = newSpecularExp
|
||||||
|
}
|
||||||
|
|
||||||
// Colors
|
// Colors
|
||||||
|
|
||||||
var RED = Vector3{255, 0, 0}
|
var RED = Vector3{255, 0, 0}
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,11 @@ package main
|
||||||
type Sphere struct {
|
type Sphere struct {
|
||||||
center Vector3
|
center Vector3
|
||||||
radius float64
|
radius float64
|
||||||
material Material
|
material *Material
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Sphere) Distance(p Vector3) (float64, Material) {
|
func (s Sphere) Distance(p Vector3) (float64, Material) {
|
||||||
return p.Sub(s.center).Length() - s.radius, s.material
|
return p.Sub(s.center).Length() - s.radius, *s.material
|
||||||
}
|
}
|
||||||
|
|
||||||
// Box
|
// Box
|
||||||
|
|
|
||||||
|
|
@ -13,17 +13,17 @@ import (
|
||||||
const MAX_DIST = 1000.0
|
const MAX_DIST = 1000.0
|
||||||
const MAX_STEP = 1000
|
const MAX_STEP = 1000
|
||||||
const EPS = 0.01
|
const EPS = 0.01
|
||||||
const WIDTH = 250
|
const WIDTH = 500
|
||||||
const HEIGHT = 250
|
const HEIGHT = 500
|
||||||
const PI = math.Pi
|
const PI = math.Pi
|
||||||
|
|
||||||
var lightPos Vector3 = Vector3{0, -200, 600}
|
var sphereMat = RED_MAT
|
||||||
var sphere Sphere = Sphere{Vector3{0, 100, 5}, 10, RED_MAT}
|
var lightPos = Vector3{0, -200, 800}
|
||||||
var plane Plane = Plane{Vector3{0, 0, 1}, 0, WHITE_GREY_GRID_MAT}
|
var sphere = Sphere{Vector3{0, 100, 5}, 10, &sphereMat}
|
||||||
|
var plane = Plane{Vector3{0, 0, 1}, 0, WHITE_GREY_GRID_MAT}
|
||||||
var scene SmoothUnionSDF = SmoothUnionSDF{sphere, plane, 3.5}
|
|
||||||
|
|
||||||
// var scene UnionSDF = UnionSDF{sphere, plane}
|
// var scene UnionSDF = UnionSDF{sphere, plane}
|
||||||
|
var scene = SmoothUnionSDF{sphere, plane, 2}
|
||||||
var cameraPos Vector3 = Vector3{0, -10, 30}
|
var cameraPos Vector3 = Vector3{0, -10, 30}
|
||||||
|
|
||||||
// radius, theta, phi
|
// radius, theta, phi
|
||||||
|
|
@ -32,14 +32,14 @@ var screenPhysicalSize float64 = 5
|
||||||
|
|
||||||
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
|
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
|
||||||
lightVec := (lightPos.Sub(point)).Normalized()
|
lightVec := (lightPos.Sub(point)).Normalized()
|
||||||
reflectLight := Reflect(lightVec.Scale(-1), normal)
|
reflectLight := Reflect(lightVec, normal)
|
||||||
eyeVec := (cameraPos.Sub(point)).Normalized()
|
eyeVec := (cameraPos.Sub(point)).Normalized()
|
||||||
ambiant := mat.ambiantColor.GetColor(point).Scale(0.1)
|
ambiant := mat.ambiantColor.GetColor(point).Scale(0.1)
|
||||||
|
|
||||||
diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec))
|
diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec))
|
||||||
diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower)
|
diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower)
|
||||||
|
|
||||||
specularPower := mat.specularFac * math.Pow(max(0, -reflectLight.Dot(eyeVec)), mat.specularExp)
|
specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp)
|
||||||
specular := mat.specularColor.GetColor(point).Scale(specularPower)
|
specular := mat.specularColor.GetColor(point).Scale(specularPower)
|
||||||
|
|
||||||
return ambiant.Add(diffuse).Add(specular)
|
return ambiant.Add(diffuse).Add(specular)
|
||||||
|
|
@ -100,6 +100,10 @@ func genImage() *rl.Image {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// ChangeDiffuseColor(&sphereMat, Vector3{0, 0, 255})
|
||||||
|
ChangeSpecularFac(&sphereMat, 0.5)
|
||||||
|
ChangeSpecularExp(&sphereMat, 20)
|
||||||
|
// ChangeSpecularColor(&sphereMat, Vector3{0, 0, 255})
|
||||||
rl.InitWindow(WIDTH, HEIGHT, "raymarching")
|
rl.InitWindow(WIDTH, HEIGHT, "raymarching")
|
||||||
texture := rl.LoadTextureFromImage(genImage())
|
texture := rl.LoadTextureFromImage(genImage())
|
||||||
|
|
||||||
|
|
|
||||||
7
sdf.go
7
sdf.go
|
|
@ -140,7 +140,7 @@ func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) {
|
||||||
h := math.Max(k-math.Abs(-d1-d2), 0.0)
|
h := math.Max(k-math.Abs(-d1-d2), 0.0)
|
||||||
d := math.Max(-d1, d2) + h*h*0.25/k
|
d := math.Max(-d1, d2) + h*h*0.25/k
|
||||||
t := SmoothStep(d2-d1, -k, k)
|
t := SmoothStep(d2-d1, -k, k)
|
||||||
mat := MixMat(mat1, mat2, t, p)
|
mat := MixMat(mat2, mat1, t, p)
|
||||||
return d, mat
|
return d, mat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,8 +154,9 @@ func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) {
|
||||||
k := 4 * s.k
|
k := 4 * s.k
|
||||||
d1, mat1 := s.primitive1.Distance(p)
|
d1, mat1 := s.primitive1.Distance(p)
|
||||||
d2, mat2 := s.primitive2.Distance(p)
|
d2, mat2 := s.primitive2.Distance(p)
|
||||||
d := math.Max(k-math.Abs(d1-d2), 0.0)
|
h := math.Max(k-math.Abs(d1-d2), 0.0)
|
||||||
|
d := math.Max(d1, d2) + h*h*0.25/k
|
||||||
t := SmoothStep(d2-d1, -k, k)
|
t := SmoothStep(d2-d1, -k, k)
|
||||||
mat := MixMat(mat1, mat2, t, p)
|
mat := MixMat(mat2, mat1, t, p)
|
||||||
return d, mat
|
return d, mat
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
vec3.go
2
vec3.go
|
|
@ -69,7 +69,7 @@ func (u Vector3) Min(x float64) Vector3 {
|
||||||
// i incident, n normal. Both vector should be normalized
|
// i incident, n normal. Both vector should be normalized
|
||||||
func Reflect(i Vector3, n Vector3) Vector3 {
|
func Reflect(i Vector3, n Vector3) Vector3 {
|
||||||
y := i.Dot(n)
|
y := i.Dot(n)
|
||||||
return (i.Add(n.Scale(2 * y))).Normalized()
|
return (n.Scale(2 * y)).Sub(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Todo : Refract
|
// Todo : Refract
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue