Clamp to border for depth map+rotating cubes

This commit is contained in:
bloeys
2024-04-14 06:14:10 +04:00
parent b718611149
commit 5fa6a06079
2 changed files with 37 additions and 15 deletions

View File

@ -276,8 +276,15 @@ func (fbo *Framebuffer) NewDepthAttachment(
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
// This is so that any sampling outside the depth map gives a full depth value.
// Useful for example when doing shadow maps where we want things outside
// the range of the texture to not show shadow
borderColor := []float32{1, 1, 1, 1}
gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
gl.BindTexture(gl.TEXTURE_2D, 0)
// Attach to fbo

41
main.go
View File

@ -51,23 +51,22 @@ type DirLight struct {
}
var (
dSize float32 = 30
dNear float32 = 1
dFar float32 = 50
dPos = gglm.NewVec3(0, 10, 0)
dirLightSize float32 = 30
dirLightNear float32 = 0.1
dirLightFar float32 = 30
dirLightPos = gglm.NewVec3(0, 10, 0)
)
func (d *DirLight) GetProjViewMat() gglm.Mat4 {
// Some arbitrary position for the directional light
pos := dPos //gglm.NewVec3(0, 10, 0)
pos := dirLightPos
size := dSize //float32(50)
nearClip := dNear //float32(1)
farClip := dFar //float32(50)
size := dirLightSize
nearClip := dirLightNear
farClip := dirLightFar
projMat := gglm.Ortho(-size, size, -size, size, nearClip, farClip).Mat4
// viewMat := gglm.LookAtRH(pos, gglm.NewVec3(0, 0, 0), gglm.NewVec3(0, 1, 0)).Mat4
viewMat := gglm.LookAtRH(pos, pos.Clone().Add(d.Dir.Clone().Scale(10)), gglm.NewVec3(0, 1, 0)).Mat4
return *projMat.Mul(&viewMat)
@ -639,10 +638,10 @@ func (g *Game) showDebugWindow() {
palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
}
imgui.DragFloat("dSize", &dSize)
imgui.DragFloat("dNear", &dNear)
imgui.DragFloat("dFar", &dFar)
imgui.DragFloat3("dPos", &dPos.Data)
imgui.DragFloat3("dPos", &dirLightPos.Data)
imgui.DragFloat("dSize", &dirLightSize)
imgui.DragFloat("dNear", &dirLightNear)
imgui.DragFloat("dFar", &dirLightFar)
imgui.Spacing()
@ -857,6 +856,8 @@ func (g *Game) Render() {
// Culling front faces helps 'peter panning' when
// drawing shadow maps, but works only for solids with a back face (i.e. quads won't cast shadows).
// Check more here: https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping
//
// Some note that this is too troublesome and fails in many cases. Might be better to remove.
gl.CullFace(gl.FRONT)
g.RenderScene(depthMapMat)
gl.CullFace(gl.BACK)
@ -909,6 +910,13 @@ func (g *Game) Render() {
}
}
var (
rotatingCubeSpeedDeg1 float32 = 45
rotatingCubeSpeedDeg2 float32 = 90
rotatingCubeTrMat1 = *gglm.NewTrMatId().Translate(gglm.NewVec3(-4, -1, 4))
rotatingCubeTrMat2 = *gglm.NewTrMatId().Translate(gglm.NewVec3(-1, 0.5, 4))
)
func (g *Game) RenderScene(overrideMat *materials.Material) {
tempModelMatrix := cubeModelMat.Clone()
@ -947,6 +955,13 @@ func (g *Game) RenderScene(overrideMat *materials.Material) {
tempModelMatrix.Translate(gglm.NewVec3(0, -1, -4))
window.Rend.DrawMesh(cubeMesh, tempModelMatrix, cubeMat)
// Rotating cubes
rotatingCubeTrMat1.Rotate(rotatingCubeSpeedDeg1*gglm.Deg2Rad*timing.DT(), gglm.NewVec3(0, 1, 0))
window.Rend.DrawMesh(cubeMesh, &rotatingCubeTrMat1, cubeMat)
rotatingCubeTrMat2.Rotate(rotatingCubeSpeedDeg2*gglm.Deg2Rad*timing.DT(), gglm.NewVec3(1, 1, 0))
window.Rend.DrawMesh(cubeMesh, &rotatingCubeTrMat2, cubeMat)
// Cubes generator
// rowSize := 1
// for y := 0; y < rowSize; y++ {