working fpscontrols
This commit is contained in:
parent
298822e197
commit
92b9e651ac
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -10,4 +10,4 @@ void main() {
|
||||
gl_Position = vec4(_pos.xy, 0, 1);
|
||||
pos = _pos;
|
||||
tex = _tex;
|
||||
}
|
||||
}
|
||||
|
29
main.go
29
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])
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user