Got some movement going.

main
Sean Hickey 2021-08-25 01:04:58 -07:00
parent 21f16bf5c3
commit 11ba8d4913
4 changed files with 161 additions and 16 deletions

95
main.go
View File

@ -12,7 +12,8 @@ import (
gl "github.com/go-gl/gl/v3.1/gles2"
"github.com/go-gl/mathgl/mgl32"
"gitea.wisellama.rocks/carpy-breakout/glhelpers"
"gitea.wisellama.rocks/carpy-breakout/pkg/glhelpers"
"gitea.wisellama.rocks/carpy-breakout/pkg/globjects"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
@ -95,16 +96,11 @@ func run() {
projectionUniform := gl.GetUniformLocation(program, gl.Str("projection\x00"))
gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])
camera := mgl32.LookAtV(mgl32.Vec3{3, 3, 3}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
cameraUniform := gl.GetUniformLocation(program, gl.Str("camera\x00"))
gl.UniformMatrix4fv(cameraUniform, 1, false, &camera[0])
camera := globjects.NewCamera()
camera.Position = mgl32.Vec3{0, 0, -10}
camera.Draw(program)
model := mgl32.Ident4()
modelUniform := gl.GetUniformLocation(program, gl.Str("model\x00"))
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
textureUniform := gl.GetUniformLocation(program, gl.Str("tex\x00"))
gl.Uniform1i(textureUniform, 0)
//gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00"))
@ -133,17 +129,26 @@ func run() {
// Configure global settings
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
gl.ClearColor(1.0, 1.0, 1.0, 1.0)
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
model := mgl32.Ident4()
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
running := true
angle := 0.0
var angle float32 = 0.0
var move float32 = 0.0
for running {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
model = mgl32.Ident4()
angle += 0.01
model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})
move += 0.01
camera.Update()
camera.Draw(program)
angle += 0.00
model = mgl32.HomogRotate3D(angle, mgl32.Vec3{0, 1, 0})
gl.UseProgram(program)
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
gl.BindVertexArray(vao)
@ -155,7 +160,7 @@ func run() {
window.GLSwap()
running = handleEvents(running, window)
running = handleEvents(running, window, &camera)
sdl.Delay(16)
}
}
@ -181,7 +186,7 @@ func drawSDLRenderer(renderer *sdl.Renderer) {
renderer.Present()
}
func handleEvents(running bool, window *sdl.Window) bool {
func handleEvents(running bool, window *sdl.Window, camera *globjects.Camera) bool {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
@ -190,6 +195,9 @@ func handleEvents(running bool, window *sdl.Window) bool {
case *sdl.MouseMotionEvent:
fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel)
if t.XRel > 0 || t.YRel > 0 {
camera.Rotate(mgl32.Vec3{float32(t.XRel), float32(t.YRel), 0})
}
case *sdl.MouseButtonEvent:
fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
@ -207,6 +215,61 @@ func handleEvents(running bool, window *sdl.Window) bool {
if t.Keysym.Sym == sdl.K_g {
window.SetFullscreen(DefaultWindowFlags)
}
if t.Keysym.Sym == sdl.K_w {
if t.Type == sdl.KEYDOWN {
camera.SetZVelocity(1)
} else if t.Type == sdl.KEYUP {
camera.SetZVelocity(0)
}
}
if t.Keysym.Sym == sdl.K_a {
if t.Type == sdl.KEYDOWN {
camera.SetXVelocity(1)
} else if t.Type == sdl.KEYUP {
camera.SetXVelocity(0)
}
}
if t.Keysym.Sym == sdl.K_s {
if t.Type == sdl.KEYDOWN {
camera.SetZVelocity(-1)
} else if t.Type == sdl.KEYUP {
camera.SetZVelocity(0)
} }
if t.Keysym.Sym == sdl.K_d {
if t.Type == sdl.KEYDOWN {
camera.SetXVelocity(-1)
} else if t.Type == sdl.KEYUP {
camera.SetXVelocity(0)
}
}
if t.Keysym.Sym == sdl.K_q {
if t.Type == sdl.KEYDOWN {
camera.SetYVelocity(-1)
} else if t.Type == sdl.KEYUP {
camera.SetYVelocity(0)
}
}
if t.Keysym.Sym == sdl.K_e {
if t.Type == sdl.KEYDOWN {
camera.SetYVelocity(1)
} else if t.Type == sdl.KEYUP {
camera.SetYVelocity(0)
}
}
if t.Keysym.Sym == sdl.K_z {
if t.Type == sdl.KEYDOWN {
camera.SetRotationVelocity(-1)
} else if t.Type == sdl.KEYUP {
camera.SetRotationVelocity(0)
}
}
if t.Keysym.Sym == sdl.K_x {
if t.Type == sdl.KEYDOWN {
camera.SetRotationVelocity(1)
} else if t.Type == sdl.KEYUP {
camera.SetRotationVelocity(0)
}
}
}
}

77
pkg/globjects/camera.go Normal file
View File

@ -0,0 +1,77 @@
package globjects
import (
gl "github.com/go-gl/gl/v3.1/gles2"
"github.com/go-gl/mathgl/mgl32"
)
type Camera struct {
Position mgl32.Vec3
Center mgl32.Vec3
Up mgl32.Vec3
Velocity mgl32.Vec3
Movestep float32
RotationVelocity float32
RotationAngles mgl32.Vec3
RotateStep float32
}
func NewCamera() (Camera) {
camera := Camera{}
camera.Up = mgl32.Vec3{0, 1, 0}
camera.Movestep = 0.1
camera.RotateStep = 0.05
return camera
}
func (c *Camera) Draw(program uint32) {
cameraMatrix := mgl32.LookAtV(c.Position, c.Center, c.Up)
cameraUniform := gl.GetUniformLocation(program, gl.Str("camera\x00"))
gl.UniformMatrix4fv(cameraUniform, 1, false, &cameraMatrix[0])
}
func (c *Camera) Update() {
c.Rotate(mgl32.Vec3{0, 0, 0})
c.Move(c.Velocity.Mul(c.Movestep))
}
func (c *Camera) Rotate(vec mgl32.Vec3) {
// X - mouse left right
c.RotationAngles[0] += c.RotateStep * vec[0]
xRotate := mgl32.HomogRotate3D(c.RotationAngles[0], mgl32.Vec3{1, 0, 0})
c.Up = mgl32.TransformNormal(c.Up, xRotate)
//c.Center = mgl32.TransformCoordinate(c.Center, xRotate)
// Y - mouse up down
c.RotationAngles[1] += c.RotateStep * vec[1]
yRotate := mgl32.HomogRotate3D(c.RotationAngles[1], mgl32.Vec3{0, 1, 0})
c.Up = mgl32.TransformNormal(c.Up, yRotate)
//c.Center = mgl32.TransformCoordinate(c.Center, xRotate)
// Z - keyboard
c.RotationAngles[2] = c.RotateStep * c.RotationVelocity
zRotate := mgl32.HomogRotate3D(c.RotationAngles[2], mgl32.Vec3{0, 0, 1})
c.Up = mgl32.TransformNormal(c.Up, zRotate)
}
func (c *Camera) Move(vec mgl32.Vec3) {
translation := mgl32.Translate3D(vec.X(), vec.Y(), vec.Z())
c.Position = mgl32.TransformCoordinate(c.Position, translation)
c.Center = mgl32.TransformCoordinate(c.Center, translation)
}
func (c *Camera) SetXVelocity(v float32) {
c.Velocity[0] = v
}
func (c *Camera) SetYVelocity(v float32) {
c.Velocity[1] = v
}
func (c *Camera) SetZVelocity(v float32) {
c.Velocity[2] = v
}
func (c *Camera) SetRotationVelocity(v float32) {
c.RotationVelocity = v
}

View File

@ -0,0 +1,5 @@
package globjects
type globject interface {
draw(uint32)
}