mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Compare commits
2 Commits
1d71715cb4
...
f13db47918
| Author | SHA1 | Date | |
|---|---|---|---|
| f13db47918 | |||
| dcfe254052 |
@ -41,7 +41,6 @@ func Run(g Game, w *Window, rend renderer.Render, ui nmageimgui.ImguiInfo) {
|
||||
|
||||
for isRunning {
|
||||
|
||||
//PERF: Cache these
|
||||
width, height = w.SDLWin.GetSize()
|
||||
fbWidth, fbHeight = w.SDLWin.GLGetDrawableSize()
|
||||
|
||||
|
||||
7
go.mod
7
go.mod
@ -8,7 +8,7 @@ require github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6
|
||||
|
||||
require (
|
||||
github.com/bloeys/assimp-go v0.4.4
|
||||
github.com/bloeys/gglm v0.49.0
|
||||
github.com/bloeys/gglm v0.50.0
|
||||
)
|
||||
|
||||
require (
|
||||
@ -16,4 +16,7 @@ require (
|
||||
github.com/mandykoh/prism v0.35.1
|
||||
)
|
||||
|
||||
require github.com/mandykoh/go-parallel v0.1.0 // indirect
|
||||
require (
|
||||
github.com/mandykoh/go-parallel v0.1.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
|
||||
)
|
||||
|
||||
6
go.sum
6
go.sum
@ -2,8 +2,8 @@ github.com/AllenDang/cimgui-go v0.0.0-20230720025235-f2ff398a66b2 h1:3HA/5qD8Rim
|
||||
github.com/AllenDang/cimgui-go v0.0.0-20230720025235-f2ff398a66b2/go.mod h1:iNfbIyOBN8k3XScMxULbrwYbPsXEAUD0Jb6UwrspQb8=
|
||||
github.com/bloeys/assimp-go v0.4.4 h1:Yn5e/RpE0Oes0YMBy8O7KkwAO4R/RpgrZPJCt08dVIU=
|
||||
github.com/bloeys/assimp-go v0.4.4/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0=
|
||||
github.com/bloeys/gglm v0.49.0 h1:YtbyHpszYhjnxw7KVV0LaCdBktRMqfGx/i37EMomxsE=
|
||||
github.com/bloeys/gglm v0.49.0/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
|
||||
github.com/bloeys/gglm v0.50.0 h1:DlGLp9z8KMNx+hNR6PjnPmC0HjDRC19QwAKL1iwhOxs=
|
||||
github.com/bloeys/gglm v0.50.0/go.mod h1:5s2U/NiOrtJyrSup1j8wK+QOBmGIO03ub0LHMvuNSK8=
|
||||
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 h1:zDw5v7qm4yH7N8C8uWd+8Ii9rROdgWxQuGoJ9WDXxfk=
|
||||
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw=
|
||||
github.com/mandykoh/go-parallel v0.1.0 h1:7vJMNMC4dsbgZdkAb2A8tV5ENY1v7VxIO1wzQWZoT8k=
|
||||
@ -15,6 +15,8 @@ github.com/veandco/go-sdl2 v0.4.35/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofe
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
|
||||
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
|
||||
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
|
||||
165
main.go
165
main.go
@ -82,18 +82,14 @@ func (d *DirLight) GetProjViewMat() gglm.Mat4 {
|
||||
return *projMat.Mul(&viewMat)
|
||||
}
|
||||
|
||||
// Check https://wiki.ogre3d.org/tiki-index.php?page=-Point+Light+Attenuation for values
|
||||
// Based on: https://lisyarus.github.io/blog/posts/point-light-attenuation.html
|
||||
type PointLight struct {
|
||||
Pos gglm.Vec3
|
||||
DiffuseColor gglm.Vec3
|
||||
SpecularColor gglm.Vec3
|
||||
|
||||
// @TODO
|
||||
Radius float32
|
||||
|
||||
Constant float32
|
||||
Linear float32
|
||||
Quadratic float32
|
||||
Falloff float32
|
||||
|
||||
FarPlane float32
|
||||
}
|
||||
@ -200,8 +196,8 @@ var (
|
||||
frameTimesMsIndex int = 0
|
||||
frameTimesMs []float32 = make([]float32, 0, FRAME_TIME_MS_SAMPLES)
|
||||
|
||||
camSpeed float32 = 15
|
||||
mouseSensitivity float32 = 0.5
|
||||
camMoveSpeed float32 = 15
|
||||
camRotSpeed float32 = 0.5
|
||||
|
||||
window engine.Window
|
||||
|
||||
@ -278,38 +274,32 @@ var (
|
||||
Pos: gglm.NewVec3(0, 2, -2),
|
||||
DiffuseColor: gglm.NewVec3(1, 0, 0),
|
||||
SpecularColor: gglm.NewVec3(1, 1, 1),
|
||||
// These values are for 50m range
|
||||
Constant: 1.0,
|
||||
Linear: 0.09,
|
||||
Quadratic: 0.032,
|
||||
|
||||
Falloff: 1.0,
|
||||
Radius: 20,
|
||||
FarPlane: 25,
|
||||
},
|
||||
{
|
||||
Pos: gglm.NewVec3(0, -5, 0),
|
||||
DiffuseColor: gglm.NewVec3(0, 1, 0),
|
||||
SpecularColor: gglm.NewVec3(1, 1, 1),
|
||||
Constant: 1.0,
|
||||
Linear: 0.09,
|
||||
Quadratic: 0.032,
|
||||
Falloff: 1.0,
|
||||
Radius: 20,
|
||||
FarPlane: 25,
|
||||
},
|
||||
{
|
||||
Pos: gglm.NewVec3(5, 0, 0),
|
||||
DiffuseColor: gglm.NewVec3(1, 1, 1),
|
||||
SpecularColor: gglm.NewVec3(1, 1, 1),
|
||||
Constant: 1.0,
|
||||
Linear: 0.09,
|
||||
Quadratic: 0.032,
|
||||
Falloff: 1.0,
|
||||
Radius: 20,
|
||||
FarPlane: 25,
|
||||
},
|
||||
{
|
||||
Pos: gglm.NewVec3(-3, 4, 3),
|
||||
DiffuseColor: gglm.NewVec3(1, 1, 1),
|
||||
SpecularColor: gglm.NewVec3(1, 1, 1),
|
||||
Constant: 1.0,
|
||||
Linear: 0.09,
|
||||
Quadratic: 0.032,
|
||||
Falloff: 1.0,
|
||||
Radius: 10,
|
||||
FarPlane: 25,
|
||||
},
|
||||
}
|
||||
@ -412,9 +402,10 @@ func (g *Game) handleWindowEvents(e sdl.Event) {
|
||||
|
||||
g.WinWidth = e.Data1
|
||||
g.WinHeight = e.Data2
|
||||
cam.AspectRatio = float32(g.WinWidth) / float32(g.WinHeight)
|
||||
|
||||
cam.AspectRatio = float32(g.WinWidth) / float32(g.WinHeight)
|
||||
cam.Update()
|
||||
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
}
|
||||
}
|
||||
@ -749,20 +740,15 @@ func (g *Game) updateLights() {
|
||||
groundMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
|
||||
palleteMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
|
||||
|
||||
whiteMat.SetUnifFloat32(indexString+".constant", p.Constant)
|
||||
containerMat.SetUnifFloat32(indexString+".constant", p.Constant)
|
||||
groundMat.SetUnifFloat32(indexString+".constant", p.Constant)
|
||||
palleteMat.SetUnifFloat32(indexString+".constant", p.Constant)
|
||||
whiteMat.SetUnifFloat32(indexString+".falloff", p.Falloff)
|
||||
containerMat.SetUnifFloat32(indexString+".falloff", p.Falloff)
|
||||
groundMat.SetUnifFloat32(indexString+".falloff", p.Falloff)
|
||||
palleteMat.SetUnifFloat32(indexString+".falloff", p.Falloff)
|
||||
|
||||
whiteMat.SetUnifFloat32(indexString+".linear", p.Linear)
|
||||
containerMat.SetUnifFloat32(indexString+".linear", p.Linear)
|
||||
groundMat.SetUnifFloat32(indexString+".linear", p.Linear)
|
||||
palleteMat.SetUnifFloat32(indexString+".linear", p.Linear)
|
||||
|
||||
whiteMat.SetUnifFloat32(indexString+".quadratic", p.Quadratic)
|
||||
containerMat.SetUnifFloat32(indexString+".quadratic", p.Quadratic)
|
||||
groundMat.SetUnifFloat32(indexString+".quadratic", p.Quadratic)
|
||||
palleteMat.SetUnifFloat32(indexString+".quadratic", p.Quadratic)
|
||||
whiteMat.SetUnifFloat32(indexString+".radius", p.Radius)
|
||||
containerMat.SetUnifFloat32(indexString+".radius", p.Radius)
|
||||
groundMat.SetUnifFloat32(indexString+".radius", p.Radius)
|
||||
palleteMat.SetUnifFloat32(indexString+".radius", p.Radius)
|
||||
|
||||
whiteMat.SetUnifFloat32(indexString+".farPlane", p.FarPlane)
|
||||
containerMat.SetUnifFloat32(indexString+".farPlane", p.FarPlane)
|
||||
@ -831,10 +817,6 @@ func (g *Game) Update() {
|
||||
g.updateCameraPos()
|
||||
|
||||
g.showDebugWindow()
|
||||
|
||||
if input.KeyClicked(sdl.K_F4) {
|
||||
logging.InfoLog.Printf("Pos: %s; Forward: %s; |Forward|: %f\n", cam.Pos.String(), cam.Forward.String(), cam.Forward.Mag())
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) showDebugWindow() {
|
||||
@ -886,7 +868,7 @@ func (g *Game) showDebugWindow() {
|
||||
// Ambient light
|
||||
imgui.Text("Ambient Light")
|
||||
|
||||
if imgui.DragFloat3("Ambient Color", &ambientColor.Data) {
|
||||
if imgui.ColorEdit3("Ambient Color", &ambientColor.Data) {
|
||||
whiteMat.SetUnifVec3("ambientColor", &ambientColor)
|
||||
containerMat.SetUnifVec3("ambientColor", &ambientColor)
|
||||
groundMat.SetUnifVec3("ambientColor", &ambientColor)
|
||||
@ -907,14 +889,14 @@ func (g *Game) showDebugWindow() {
|
||||
palleteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
|
||||
}
|
||||
|
||||
if imgui.DragFloat3("Diffuse Color", &dirLight.DiffuseColor.Data) {
|
||||
if imgui.ColorEdit3("Diffuse Color", &dirLight.DiffuseColor.Data) {
|
||||
whiteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
|
||||
containerMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
|
||||
groundMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
|
||||
palleteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
|
||||
}
|
||||
|
||||
if imgui.DragFloat3("Specular Color", &dirLight.SpecularColor.Data) {
|
||||
if imgui.ColorEdit3("Specular Color", &dirLight.SpecularColor.Data) {
|
||||
whiteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
|
||||
containerMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
|
||||
groundMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
|
||||
@ -956,24 +938,53 @@ func (g *Game) showDebugWindow() {
|
||||
indexString := "pointLights[" + indexNumString + "]"
|
||||
|
||||
if imgui.DragFloat3("Pos", &pl.Pos.Data) {
|
||||
whiteMat.SetUnifVec3(indexString+".pos", &pl.Pos)
|
||||
containerMat.SetUnifVec3(indexString+".pos", &pl.Pos)
|
||||
groundMat.SetUnifVec3(indexString+".pos", &pl.Pos)
|
||||
palleteMat.SetUnifVec3(indexString+".pos", &pl.Pos)
|
||||
|
||||
posStr := indexString + ".pos"
|
||||
|
||||
whiteMat.SetUnifVec3(posStr, &pl.Pos)
|
||||
containerMat.SetUnifVec3(posStr, &pl.Pos)
|
||||
groundMat.SetUnifVec3(posStr, &pl.Pos)
|
||||
palleteMat.SetUnifVec3(posStr, &pl.Pos)
|
||||
}
|
||||
|
||||
if imgui.DragFloat3("Diffuse Color", &pl.DiffuseColor.Data) {
|
||||
whiteMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
|
||||
containerMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
|
||||
groundMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
|
||||
palleteMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
|
||||
if imgui.ColorEdit3("Diffuse Color", &pl.DiffuseColor.Data) {
|
||||
|
||||
diffStr := indexString + ".diffuseColor"
|
||||
|
||||
whiteMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
|
||||
containerMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
|
||||
groundMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
|
||||
palleteMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
|
||||
}
|
||||
|
||||
if imgui.DragFloat3("Specular Color", &pl.SpecularColor.Data) {
|
||||
whiteMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
|
||||
containerMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
|
||||
groundMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
|
||||
palleteMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
|
||||
if imgui.ColorEdit3("Specular Color", &pl.SpecularColor.Data) {
|
||||
|
||||
specularStr := indexString + ".specularColor"
|
||||
|
||||
whiteMat.SetUnifVec3(specularStr, &pl.SpecularColor)
|
||||
containerMat.SetUnifVec3(specularStr, &pl.SpecularColor)
|
||||
groundMat.SetUnifVec3(specularStr, &pl.SpecularColor)
|
||||
palleteMat.SetUnifVec3(specularStr, &pl.SpecularColor)
|
||||
}
|
||||
|
||||
if imgui.DragFloatV("Falloff", &pl.Falloff, 0.1, 0.001, 100, "%.3f", imgui.SliderFlagsNone) {
|
||||
|
||||
falloffStr := indexString + ".falloff"
|
||||
|
||||
whiteMat.SetUnifFloat32(falloffStr, pl.Falloff)
|
||||
containerMat.SetUnifFloat32(falloffStr, pl.Falloff)
|
||||
groundMat.SetUnifFloat32(falloffStr, pl.Falloff)
|
||||
palleteMat.SetUnifFloat32(falloffStr, pl.Falloff)
|
||||
}
|
||||
|
||||
if imgui.DragFloatV("Radius", &pl.Radius, 0.2, 0, 500, "%.3f", imgui.SliderFlagsNone) {
|
||||
|
||||
falloffStr := indexString + ".radius"
|
||||
|
||||
whiteMat.SetUnifFloat32(falloffStr, pl.Radius)
|
||||
containerMat.SetUnifFloat32(falloffStr, pl.Radius)
|
||||
groundMat.SetUnifFloat32(falloffStr, pl.Radius)
|
||||
palleteMat.SetUnifFloat32(falloffStr, pl.Radius)
|
||||
}
|
||||
|
||||
imgui.TreePop()
|
||||
@ -1012,34 +1023,39 @@ func (g *Game) showDebugWindow() {
|
||||
palleteMat.SetUnifVec3(indexString+".dir", &l.Dir)
|
||||
}
|
||||
|
||||
if imgui.DragFloat3("Diffuse Color", &l.DiffuseColor.Data) {
|
||||
if imgui.ColorEdit3("Diffuse Color", &l.DiffuseColor.Data) {
|
||||
whiteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
|
||||
containerMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
|
||||
groundMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
|
||||
palleteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
|
||||
}
|
||||
|
||||
if imgui.DragFloat3("Specular Color", &l.SpecularColor.Data) {
|
||||
if imgui.ColorEdit3("Specular Color", &l.SpecularColor.Data) {
|
||||
whiteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
|
||||
containerMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
|
||||
groundMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
|
||||
palleteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
|
||||
}
|
||||
|
||||
if imgui.DragFloat("Inner Cutoff Radians", &l.InnerCutoffRad) {
|
||||
if imgui.DragFloatRange2V(
|
||||
"Cutoff Radians",
|
||||
&l.InnerCutoffRad,
|
||||
&l.OuterCutoffRad,
|
||||
0.1,
|
||||
0,
|
||||
0,
|
||||
"%.3f",
|
||||
"%.3f",
|
||||
imgui.SliderFlagsNone,
|
||||
) {
|
||||
|
||||
cos := l.InnerCutoffCos()
|
||||
|
||||
whiteMat.SetUnifFloat32(indexString+".innerCutoff", cos)
|
||||
containerMat.SetUnifFloat32(indexString+".innerCutoff", cos)
|
||||
groundMat.SetUnifFloat32(indexString+".innerCutoff", cos)
|
||||
palleteMat.SetUnifFloat32(indexString+".innerCutoff", cos)
|
||||
}
|
||||
|
||||
if imgui.DragFloat("Outer Cutoff Radians", &l.OuterCutoffRad) {
|
||||
|
||||
cos := l.OuterCutoffCos()
|
||||
|
||||
cos = l.OuterCutoffCos()
|
||||
whiteMat.SetUnifFloat32(indexString+".outerCutoff", cos)
|
||||
containerMat.SetUnifFloat32(indexString+".outerCutoff", cos)
|
||||
groundMat.SetUnifFloat32(indexString+".outerCutoff", cos)
|
||||
@ -1084,11 +1100,15 @@ func (g *Game) updateCameraLookAround() {
|
||||
return
|
||||
}
|
||||
|
||||
const MAX_MOUSE_MOVE = 300
|
||||
mouseX = gglm.Clamp(mouseX, -MAX_MOUSE_MOVE, MAX_MOUSE_MOVE)
|
||||
mouseY = gglm.Clamp(mouseY, -MAX_MOUSE_MOVE, MAX_MOUSE_MOVE)
|
||||
|
||||
// Yaw
|
||||
yaw += float32(mouseX) * mouseSensitivity * timing.DT()
|
||||
yaw += float32(mouseX) * camRotSpeed * timing.DT()
|
||||
|
||||
// Pitch
|
||||
pitch += float32(-mouseY) * mouseSensitivity * timing.DT()
|
||||
pitch += float32(-mouseY) * camRotSpeed * timing.DT()
|
||||
if pitch > 1.5 {
|
||||
pitch = 1.5
|
||||
}
|
||||
@ -1097,7 +1117,6 @@ func (g *Game) updateCameraLookAround() {
|
||||
pitch = -1.5
|
||||
}
|
||||
|
||||
// Update cam forward
|
||||
cam.UpdateRotation(pitch, yaw)
|
||||
|
||||
updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
|
||||
@ -1114,21 +1133,21 @@ func (g *Game) updateCameraPos() {
|
||||
|
||||
// Forward and backward
|
||||
if input.KeyDown(sdl.K_w) {
|
||||
cam.Pos.Add(cam.Forward.Clone().Scale(camSpeed * camSpeedScale * timing.DT()))
|
||||
cam.Pos.Add(cam.Forward.Clone().Scale(camMoveSpeed * camSpeedScale * timing.DT()))
|
||||
update = true
|
||||
} else if input.KeyDown(sdl.K_s) {
|
||||
cam.Pos.Add(cam.Forward.Clone().Scale(-camSpeed * camSpeedScale * timing.DT()))
|
||||
cam.Pos.Add(cam.Forward.Clone().Scale(-camMoveSpeed * camSpeedScale * timing.DT()))
|
||||
update = true
|
||||
}
|
||||
|
||||
// Left and right
|
||||
if input.KeyDown(sdl.K_d) {
|
||||
cross := gglm.Cross(&cam.Forward, &cam.WorldUp)
|
||||
cam.Pos.Add(cross.Normalize().Scale(camSpeed * camSpeedScale * timing.DT()))
|
||||
cam.Pos.Add(cross.Normalize().Scale(camMoveSpeed * camSpeedScale * timing.DT()))
|
||||
update = true
|
||||
} else if input.KeyDown(sdl.K_a) {
|
||||
cross := gglm.Cross(&cam.Forward, &cam.WorldUp)
|
||||
cam.Pos.Add(cross.Normalize().Scale(-camSpeed * camSpeedScale * timing.DT()))
|
||||
cam.Pos.Add(cross.Normalize().Scale(-camMoveSpeed * camSpeedScale * timing.DT()))
|
||||
update = true
|
||||
}
|
||||
|
||||
|
||||
@ -35,9 +35,8 @@ struct PointLight {
|
||||
vec3 pos;
|
||||
vec3 diffuseColor;
|
||||
vec3 specularColor;
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
float falloff;
|
||||
float radius;
|
||||
float farPlane;
|
||||
};
|
||||
uniform PointLight pointLights[NUM_POINT_LIGHTS];
|
||||
@ -162,9 +161,8 @@ struct PointLight {
|
||||
vec3 pos;
|
||||
vec3 diffuseColor;
|
||||
vec3 specularColor;
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
float falloff;
|
||||
float radius;
|
||||
float farPlane;
|
||||
};
|
||||
uniform PointLight pointLights[NUM_POINT_LIGHTS];
|
||||
@ -272,10 +270,60 @@ float CalcPointShadow(int lightIndex, vec3 worldLightPos, vec3 tangentLightDir,
|
||||
return shadow;
|
||||
}
|
||||
|
||||
//
|
||||
// The following point light attenuation formulas
|
||||
// are from https://lisyarus.github.io/blog/posts/point-light-attenuation.html
|
||||
//
|
||||
// I found them more intuitive than the standard implementation and it also ensures
|
||||
// we have zero light at the selected distance.
|
||||
//
|
||||
float sqr(float x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
||||
// This version doesn't have a harsh cutoff at radius
|
||||
float AttenuateNoCusp(float dist, float radius, float falloff)
|
||||
{
|
||||
// Since we only use this as attenuation and max intensity defines
|
||||
// the max output value, anything more than 1 would increase
|
||||
// the output of the light, which I don't think makes sense for
|
||||
// our attenuation purposes.
|
||||
//
|
||||
// Seems to me this can be done simply by increasing color values above 255.
|
||||
//
|
||||
// Forcing to 1 for now.
|
||||
#define MAX_INTENSITY 1
|
||||
|
||||
float s = dist / radius;
|
||||
|
||||
if (s >= 1.0)
|
||||
return 0.0;
|
||||
|
||||
float s2 = sqr(s);
|
||||
|
||||
return MAX_INTENSITY * sqr(1 - s2) / (1 + falloff * s2);
|
||||
}
|
||||
|
||||
// This version has a harsh/immediate cutoff at radius
|
||||
float AttenuateCusp(float dist, float radius, float falloff)
|
||||
{
|
||||
#define MAX_INTENSITY 1
|
||||
|
||||
float s = dist / radius;
|
||||
|
||||
if (s >= 1.0)
|
||||
return 0.0;
|
||||
|
||||
float s2 = sqr(s);
|
||||
|
||||
return MAX_INTENSITY * sqr(1 - s2) / (1 + falloff * s);
|
||||
}
|
||||
|
||||
vec3 CalcPointLight(PointLight pointLight, int lightIndex)
|
||||
{
|
||||
// Ignore unset lights
|
||||
if (pointLight.constant == 0){
|
||||
// Ignore inactive lights
|
||||
if (pointLight.radius == 0){
|
||||
return vec3(0);
|
||||
}
|
||||
|
||||
@ -293,7 +341,7 @@ vec3 CalcPointLight(PointLight pointLight, int lightIndex)
|
||||
|
||||
// Attenuation
|
||||
float distToLight = length(tangentLightPos - tangentFragPos);
|
||||
float attenuation = 1 / (pointLight.constant + pointLight.linear * distToLight + pointLight.quadratic * (distToLight * distToLight));
|
||||
float attenuation = AttenuateNoCusp(distToLight, pointLight.radius, pointLight.falloff);
|
||||
|
||||
// Shadow
|
||||
float shadow = CalcPointShadow(lightIndex, pointLight.pos, tangentLightDir, pointLight.farPlane);
|
||||
|
||||
Reference in New Issue
Block a user