From 92b9e651ac03eae797dcb172d2a98054e8cc8daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Tue, 15 Oct 2024 16:52:21 +0200 Subject: [PATCH] working fpscontrols --- assets/geometries/plane.obj | 10 ++-- assets/shader/deferred/deferred.fs | 3 +- assets/shader/deferred/deferred.vs | 4 +- assets/shader/screen/screen.fs | 4 +- assets/shader/screen/screen.vs | 2 +- main.go | 29 ++++++++--- types/shader/shader.go | 2 +- utils/fpscontrols/fpscontrols.go | 82 ++++++++++++++++++++++++++---- 8 files changed, 107 insertions(+), 29 deletions(-) diff --git a/assets/geometries/plane.obj b/assets/geometries/plane.obj index c19b668..92de423 100644 --- a/assets/geometries/plane.obj +++ b/assets/geometries/plane.obj @@ -2,11 +2,11 @@ # www.blender.org mtllib plane.mtl o Plane -v -1.000000 -1.000000 -0.000000 -v 1.000000 -1.000000 -0.000000 -v -1.000000 1.000000 0.000000 -v 1.000000 1.000000 0.000000 -vn -0.0000 -0.0000 1.0000 +v -10.000000 0.000000 10.000000 +v 10.000000 0.000000 10.000000 +v -10.000000 0.000000 -10.000000 +v 10.000000 0.000000 -10.000000 +vn -0.0000 1.0000 -0.0000 vt 0.999900 0.000100 vt 0.999900 0.999900 vt 0.000100 0.000100 diff --git a/assets/shader/deferred/deferred.fs b/assets/shader/deferred/deferred.fs index 5e5e2c6..f7dc3c9 100644 --- a/assets/shader/deferred/deferred.fs +++ b/assets/shader/deferred/deferred.fs @@ -18,9 +18,8 @@ layout (location = 3) out vec3 position; void main() { position = pos; albedo = texture(s_albedo, tex).rgb; - normal = texture(s_normal, tex).rgb; + normal = normalize(texture(s_normal, tex).rgb * 2.0 - 1.0); material.x = texture(s_specular, tex).x; material.y = texture(s_roughness, tex).y; material.z = texture(s_metalic, tex).z; } - diff --git a/assets/shader/deferred/deferred.vs b/assets/shader/deferred/deferred.vs index d102aee..9183fe1 100644 --- a/assets/shader/deferred/deferred.vs +++ b/assets/shader/deferred/deferred.vs @@ -15,6 +15,6 @@ out vec2 tex; void main() { gl_Position = projection * view * model * vec4(_pos, 1.0); pos = vec3(model * vec4(_pos, 1.0)); - norm = _norm; + norm = mat3(transpose(inverse(model))) * _norm; tex = _tex; -} \ No newline at end of file +} diff --git a/assets/shader/screen/screen.fs b/assets/shader/screen/screen.fs index ae44f87..b8b00c4 100644 --- a/assets/shader/screen/screen.fs +++ b/assets/shader/screen/screen.fs @@ -57,7 +57,7 @@ vec3 calc_light(light_t light) { void main() { albedo = texture(s_albedo, tex).rgb; depth = texture(s_depth, tex).x; - normal = (texture(s_normal, tex).rgb * 0.5) + 0.5; + normal = texture(s_normal, tex).rgb; material = texture(s_material, tex).xyz; { specular = material.x; @@ -71,4 +71,4 @@ void main() { light_result += calc_light(lights[i]); } FragColor = vec4(albedo * light_result, 1); -} \ No newline at end of file +} diff --git a/assets/shader/screen/screen.vs b/assets/shader/screen/screen.vs index d28abfa..27ddbeb 100644 --- a/assets/shader/screen/screen.vs +++ b/assets/shader/screen/screen.vs @@ -10,4 +10,4 @@ void main() { gl_Position = vec4(_pos.xy, 0, 1); pos = _pos; tex = _tex; -} \ No newline at end of file +} diff --git a/main.go b/main.go index 457b54d..2c2811d 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,8 @@ import ( ) const ( - WIDTH = 800 - HEIGHT = 600 + WIDTH = 1600 + HEIGHT = 900 TITLE = "opengl-deferred" FOV = 45.0 ) @@ -89,12 +89,24 @@ func main() { defer screenShader.Delete() // geometry - cube, err := geometry.LoadOBJ("assets/geometries/suzanne.obj") + var geometries []geometry.Geometry + + cube, err := geometry.LoadOBJ("assets/geometries/cube.obj") if err != nil { panic(err) } defer cube.Delete() + geometries = append(geometries, cube) + + plane, err := geometry.LoadOBJ("assets/geometries/plane.obj") + if err != nil { + panic(err) + } + defer plane.Delete() + + geometries = append(geometries, plane) + screen, err := geometry.Screen() if err != nil { panic(err) @@ -134,7 +146,7 @@ func main() { // lights lights := []light.Light{ - {Color: [3]float32{0, 1, 0}, Position: mgl32.Vec3{3, 3, 3}, Intensity: 3, Type: light.POINT}, + {Color: [3]float32{0, 1, 0}, Position: mgl32.Vec3{3, 3, 3}, Intensity: 15, Type: light.POINT}, {Color: [3]float32{1, 0, 1}, Position: mgl32.Vec3{3, 3, -3}, Intensity: 15, Type: light.POINT}, } @@ -149,7 +161,7 @@ func main() { // transformations projection := mgl32.Perspective(mgl32.DegToRad(FOV), float32(WIDTH)/HEIGHT, 0.1, 1000) model := mgl32.Ident4() - controls := fpscontrols.New(1, 0.1, mgl32.Vec3{0, 0, -10}, 0, 0, window) + controls := fpscontrols.Get(10, 10, mgl32.Vec3{0, 0, -10}, mgl32.Vec2{0, 0}, window) for !window.ShouldClose() { glfw.PollEvents() @@ -172,7 +184,7 @@ func main() { // transfer uniforms deferredShader.Use() deferredShader.Mat4("projection", projection) - deferredShader.Mat4("view", controls.ViewMatrix()) + deferredShader.Mat4("view", controls.View()) deferredShader.Mat4("model", model) deferredShader.Int("s_albedo", 0) @@ -180,7 +192,9 @@ func main() { deferredShader.Int("s_specular", 2) deferredShader.Int("s_roughness", 3) deferredShader.Int("s_metalic", 4) - cube.Draw() + for _, g := range geometries { + g.Draw() + } // second pass gl.Disable(gl.DEPTH_TEST) @@ -195,6 +209,7 @@ func main() { screenShader.Int("s_normal", 2) screenShader.Int("s_material", 3) screenShader.Int("s_position", 4) + pos := controls.Position() screenShader.Vec3("view_pos", &pos[0]) diff --git a/types/shader/shader.go b/types/shader/shader.go index 45568d1..21a120a 100644 --- a/types/shader/shader.go +++ b/types/shader/shader.go @@ -71,7 +71,7 @@ func (s *Shader) Light(l *light.Light) error { func compileShader(source string, shaderType uint32) (uint32, error) { shader := gl.CreateShader(shaderType) - csource, free := gl.Strs(source) + csource, free := gl.Strs(source + "\x00") gl.ShaderSource(shader, 1, csource, nil) free() gl.CompileShader(shader) diff --git a/utils/fpscontrols/fpscontrols.go b/utils/fpscontrols/fpscontrols.go index fa5762f..04783b6 100644 --- a/utils/fpscontrols/fpscontrols.go +++ b/utils/fpscontrols/fpscontrols.go @@ -1,6 +1,8 @@ package fpscontrols import ( + "math" + "github.com/go-gl/glfw/v3.3/glfw" "github.com/go-gl/mathgl/mgl32" ) @@ -15,10 +17,32 @@ type FPSControls struct { currentMove mgl32.Vec3 currentLook mgl32.Vec2 lastMousePos mgl32.Vec2 + + view mgl32.Mat4 + + lastTime float64 } var instance *FPSControls +func (c *FPSControls) Update() { + time := glfw.GetTime() + + deltaTime := time - c.lastTime + c.lastTime = time + + forward, right, up := c.directions() + + globalUp := mgl32.Vec3{0, 1, 0} + move := right.Mul(c.currentMove.X()).Add(globalUp.Mul(c.currentMove.Y())).Add(forward.Mul(c.currentMove.Z())) + c.position = c.position.Add(move.Mul(float32(deltaTime) * c.moveSpeed)) + + c.rotation = c.rotation.Add(c.currentLook.Mul(float32(deltaTime) * c.lookSpeed)) + c.currentLook = mgl32.Vec2{} + c.view = mgl32.LookAtV(c.position, c.position.Add(forward), up) + +} + func Get(lookSpeed, moveSpeed float32, position mgl32.Vec3, rotation mgl32.Vec2, window *glfw.Window) *FPSControls { if instance != nil { return instance @@ -45,33 +69,73 @@ func Get(lookSpeed, moveSpeed float32, position mgl32.Vec3, rotation mgl32.Vec2, switch key { case glfw.KeyW: - instance.currentMove[2] = -val - case glfw.KeyS: instance.currentMove[2] = val + case glfw.KeyS: + instance.currentMove[2] = -val case glfw.KeyA: - instance.currentMove[0] = -val - case glfw.KeyD: instance.currentMove[0] = val + case glfw.KeyD: + instance.currentMove[0] = -val case glfw.KeySpace: - instance.currentMove[1] = -val - case glfw.KeyLeftShift: instance.currentMove[1] = val + case glfw.KeyLeftShift: + instance.currentMove[1] = -val } }) - window.SetCursorPosCallback(func(window *glfw.Window, x float64, y float64) { + window.SetCursorPosCallback(func(window *glfw.Window, y float64, x float64) { pos := mgl32.Vec2{float32(x), float32(y)} instance.currentLook = instance.lastMousePos.Sub(pos) + instance.currentLook[0] *= -1 instance.lastMousePos = pos }) + window.SetInputMode(glfw.CursorMode, glfw.CursorDisabled) + return instance } -func (c *FPSControls) forward() mgl32.Vec3 { +func (c *FPSControls) directions() (mgl32.Vec3, mgl32.Vec3, mgl32.Vec3) { + rad := mgl32.Vec2{ + mgl32.DegToRad(c.rotation.X()), + mgl32.DegToRad(c.rotation.Y()), + } + sin := mgl32.Vec2{ + float32(math.Sin(float64(rad.X()))), + float32(math.Sin(float64(rad.Y()))), + } + + cos := mgl32.Vec2{ + float32(math.Cos(float64(rad.X()))), + float32(math.Cos(float64(rad.Y()))), + } + + forward := mgl32.Vec3{ + cos.X() * sin.Y(), + -sin.X(), + cos.X() * cos.Y(), + } + + right := mgl32.Vec3{ + cos.Y(), + 0, + -sin.Y(), + } + + up := forward.Cross(right) + + return forward, right, up } func (c *FPSControls) View() mgl32.Mat4 { - + return c.view +} + +func (c *FPSControls) Position() mgl32.Vec3 { + return c.position +} + +func (c *FPSControls) Rotation() mgl32.Vec2 { + return c.rotation }