broken fpscontrols

This commit is contained in:
BENEDEK László 2024-10-12 18:25:54 +02:00
parent 29e8eff82a
commit 298822e197
3 changed files with 101 additions and 20 deletions

View File

@ -39,13 +39,17 @@ vec3 normal;
vec3 material; // specular, roughness, metalic
vec3 position;
float specular;
float roughness;
float metalic;
vec3 calc_light(light_t light) {
vec3 light_dir = normalize(light.position - position);
vec3 view_dir = normalize(view_pos - position);
vec3 reflect_dir = reflect(-light_dir, normal);
float diffuse = max(dot(normal, light_dir), 0.0);
float specular = pow(max(dot(view_dir, reflect_dir), 0.0), 1 - material.y) * material.x;
float specular = pow(max(dot(view_dir, reflect_dir), 0.0), 1 - roughness) * specular;
return (diffuse + specular) * light.intensity * light.color / max(pow(length(light.position - position), 2), 1);
}
@ -53,8 +57,13 @@ 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 * 2) - 1;
normal = (texture(s_normal, tex).rgb * 0.5) + 0.5;
material = texture(s_material, tex).xyz;
{
specular = material.x;
roughness = material.y;
metalic = material.z;
}
position = texture(s_position, tex).xyz;
vec3 light_result;

29
main.go
View File

@ -10,6 +10,7 @@ import (
"git.tek.govt.hu/dowerx/opengl-deferred/types/light"
"git.tek.govt.hu/dowerx/opengl-deferred/types/shader"
"git.tek.govt.hu/dowerx/opengl-deferred/types/texture"
"git.tek.govt.hu/dowerx/opengl-deferred/utils/fpscontrols"
"github.com/go-gl/gl/v4.6-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl32"
@ -88,7 +89,7 @@ func main() {
defer screenShader.Delete()
// geometry
cube, err := geometry.LoadOBJ("assets/geometries/cube.obj")
cube, err := geometry.LoadOBJ("assets/geometries/suzanne.obj")
if err != nil {
panic(err)
}
@ -133,7 +134,8 @@ func main() {
// lights
lights := []light.Light{
{Color: [3]float32{1, 1, 1}, Position: mgl32.Vec3{3, 3, 3}, Intensity: 10, Type: light.POINT},
{Color: [3]float32{0, 1, 0}, Position: mgl32.Vec3{3, 3, 3}, Intensity: 3, Type: light.POINT},
{Color: [3]float32{1, 0, 1}, Position: mgl32.Vec3{3, 3, -3}, Intensity: 15, Type: light.POINT},
}
// gbuffer
@ -145,20 +147,13 @@ func main() {
gbuff.Bind()
// transformations
projection := mgl32.Perspective(mgl32.DegToRad(FOV), float32(WIDTH)/HEIGHT, 0.1, 10)
viewPositon := mgl32.Vec3{3, 3, 3}
view := mgl32.LookAtV(viewPositon, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
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)
angle := 0.0
previousTime := glfw.GetTime()
for !window.ShouldClose() {
// rotate
time := glfw.GetTime()
elapsed := time - previousTime
previousTime = time
angle += elapsed
model := mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})
glfw.PollEvents()
controls.Update()
// first pass
// bind textures
@ -177,7 +172,7 @@ func main() {
// transfer uniforms
deferredShader.Use()
deferredShader.Mat4("projection", projection)
deferredShader.Mat4("view", view)
deferredShader.Mat4("view", controls.ViewMatrix())
deferredShader.Mat4("model", model)
deferredShader.Int("s_albedo", 0)
@ -200,7 +195,8 @@ func main() {
screenShader.Int("s_normal", 2)
screenShader.Int("s_material", 3)
screenShader.Int("s_position", 4)
screenShader.Vec3("view_pos", &viewPositon[0])
pos := controls.Position()
screenShader.Vec3("view_pos", &pos[0])
for _, light := range lights {
screenShader.Light(&light)
@ -209,7 +205,6 @@ func main() {
screen.Draw()
window.SwapBuffers()
glfw.PollEvents()
}
defer fmt.Println("exiting...")

View File

@ -0,0 +1,77 @@
package fpscontrols
import (
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl32"
)
type FPSControls struct {
lookSpeed float32
moveSpeed float32
position mgl32.Vec3
rotation mgl32.Vec2
currentMove mgl32.Vec3
currentLook mgl32.Vec2
lastMousePos mgl32.Vec2
}
var instance *FPSControls
func Get(lookSpeed, moveSpeed float32, position mgl32.Vec3, rotation mgl32.Vec2, window *glfw.Window) *FPSControls {
if instance != nil {
return instance
}
instance = &FPSControls{
moveSpeed: moveSpeed,
lookSpeed: lookSpeed,
position: position,
rotation: rotation,
}
window.SetKeyCallback(func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
var val float32
switch action {
case glfw.Repeat:
return
case glfw.Press:
val = 1
case glfw.Release:
val = 0
}
switch key {
case glfw.KeyW:
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.KeySpace:
instance.currentMove[1] = -val
case glfw.KeyLeftShift:
instance.currentMove[1] = val
}
})
window.SetCursorPosCallback(func(window *glfw.Window, x float64, y float64) {
pos := mgl32.Vec2{float32(x), float32(y)}
instance.currentLook = instance.lastMousePos.Sub(pos)
instance.lastMousePos = pos
})
return instance
}
func (c *FPSControls) forward() mgl32.Vec3 {
}
func (c *FPSControls) View() mgl32.Mat4 {
}