Got some rotations, but they're wrong
parent
11ba8d4913
commit
d624077dd1
42
main.go
42
main.go
|
@ -12,8 +12,8 @@ import (
|
|||
gl "github.com/go-gl/gl/v3.1/gles2"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
|
||||
"gitea.wisellama.rocks/carpy-breakout/pkg/glhelpers"
|
||||
"gitea.wisellama.rocks/carpy-breakout/pkg/globjects"
|
||||
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_helpers"
|
||||
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_objects"
|
||||
)
|
||||
|
||||
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||||
|
@ -77,6 +77,8 @@ func run() {
|
|||
}
|
||||
defer sdl.GLDeleteContext(context)
|
||||
|
||||
sdl.SetRelativeMouseMode(true)
|
||||
|
||||
width, height := window.GetSize()
|
||||
|
||||
gl.Init()
|
||||
|
@ -86,7 +88,7 @@ func run() {
|
|||
gl.Viewport(0, 0, width, height)
|
||||
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
|
||||
|
||||
program, err := glhelpers.NewProgram(vertexShader, fragmentShader)
|
||||
program, err := gl_helpers.NewProgram(vertexShader, fragmentShader)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -96,15 +98,15 @@ func run() {
|
|||
projectionUniform := gl.GetUniformLocation(program, gl.Str("projection\x00"))
|
||||
gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])
|
||||
|
||||
camera := globjects.NewCamera()
|
||||
camera.Position = mgl32.Vec3{0, 0, -10}
|
||||
camera := gl_objects.NewCamera()
|
||||
camera.Position = mgl32.Vec3{0, 0, 10}
|
||||
camera.Draw(program)
|
||||
|
||||
modelUniform := gl.GetUniformLocation(program, gl.Str("model\x00"))
|
||||
|
||||
//gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00"))
|
||||
|
||||
texture, err := glhelpers.NewTexture("square.png")
|
||||
texture, err := gl_helpers.NewTexture("square.png")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -166,12 +168,12 @@ func run() {
|
|||
}
|
||||
|
||||
func setupLogging() {
|
||||
file, err := os.OpenFile(LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// file, err := os.OpenFile(LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
|
||||
log.SetOutput(file)
|
||||
// log.SetOutput(file)
|
||||
log.Println("=== START ===")
|
||||
}
|
||||
|
||||
|
@ -186,7 +188,7 @@ func drawSDLRenderer(renderer *sdl.Renderer) {
|
|||
renderer.Present()
|
||||
}
|
||||
|
||||
func handleEvents(running bool, window *sdl.Window, camera *globjects.Camera) bool {
|
||||
func handleEvents(running bool, window *sdl.Window, camera *gl_objects.Camera) bool {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch t := event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
|
@ -195,8 +197,8 @@ func handleEvents(running bool, window *sdl.Window, camera *globjects.Camera) bo
|
|||
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})
|
||||
if t.XRel != 0 || t.YRel != 0 {
|
||||
camera.Rotate(t.XRel, t.YRel)
|
||||
}
|
||||
case *sdl.MouseButtonEvent:
|
||||
fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
|
||||
|
@ -217,27 +219,27 @@ func handleEvents(running bool, window *sdl.Window, camera *globjects.Camera) bo
|
|||
}
|
||||
if t.Keysym.Sym == sdl.K_w {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
camera.SetZVelocity(1)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
camera.SetXVelocity(1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
camera.SetXVelocity(0)
|
||||
}
|
||||
|
@ -258,14 +260,14 @@ func handleEvents(running bool, window *sdl.Window, camera *globjects.Camera) bo
|
|||
}
|
||||
if t.Keysym.Sym == sdl.K_z {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
camera.SetRotationVelocity(-1)
|
||||
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)
|
||||
camera.SetRotationVelocity(-1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
camera.SetRotationVelocity(0)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package glhelpers
|
||||
package gl_helpers
|
||||
|
||||
// https://github.com/go-gl/example
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
package gl_objects
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
gl "github.com/go-gl/gl/v3.1/gles2"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
|
||||
"gitea.wisellama.rocks/carpy-breakout/pkg/math_helpers"
|
||||
)
|
||||
|
||||
var OriginalDirection mgl32.Vec3 = mgl32.Vec3{0, 0, -1}
|
||||
|
||||
type Camera struct {
|
||||
Position mgl32.Vec3
|
||||
Direction mgl32.Vec3
|
||||
Up mgl32.Vec3
|
||||
Velocity mgl32.Vec3
|
||||
Movestep float32
|
||||
RotationVelocity float32
|
||||
Rotation mgl32.Vec3
|
||||
RotateStep float32
|
||||
}
|
||||
|
||||
func NewCamera() (Camera) {
|
||||
camera := Camera{}
|
||||
camera.Up = mgl32.Vec3{0, 1, 0}
|
||||
camera.Direction = OriginalDirection
|
||||
camera.Movestep = 0.1
|
||||
camera.RotateStep = 0.05
|
||||
return camera
|
||||
}
|
||||
|
||||
func (c *Camera) GetCenter() mgl32.Vec3 {
|
||||
return c.Position.Add(c.Direction)
|
||||
}
|
||||
|
||||
func (c *Camera) Draw(program uint32) {
|
||||
cameraMatrix := mgl32.LookAtV(c.Position, c.GetCenter(), c.Up)
|
||||
cameraUniform := gl.GetUniformLocation(program, gl.Str("camera\x00"))
|
||||
gl.UniformMatrix4fv(cameraUniform, 1, false, &cameraMatrix[0])
|
||||
}
|
||||
|
||||
func (c *Camera) Update() {
|
||||
c.Rotate(0, 0)
|
||||
c.Move(c.Velocity.Mul(c.Movestep))
|
||||
}
|
||||
|
||||
func (c *Camera) Rotate(mouseX, mouseY int32) {
|
||||
// X mouse movement = rotation about the Y axis
|
||||
// Y mouse movement = rotation about the X axis
|
||||
|
||||
// X Rotation (pitch) - mouse up/down
|
||||
c.Rotation[0] = c.RotateStep * -1 * float32(mouseY) * .01
|
||||
xRotate := mgl32.HomogRotate3DX(c.Rotation[0])
|
||||
c.Up = mgl32.TransformNormal(c.Up, xRotate).Normalize()
|
||||
c.Direction = mgl32.TransformNormal(c.Direction, xRotate).Normalize()
|
||||
|
||||
// Y Rotation (yaw) - mouse left/right
|
||||
c.Rotation[1] = c.RotateStep * -1 * float32(mouseX) * .01
|
||||
yRotate := mgl32.HomogRotate3DY(c.Rotation[1])
|
||||
c.Up = mgl32.TransformNormal(c.Up, yRotate).Normalize()
|
||||
c.Direction = mgl32.TransformNormal(c.Direction, yRotate).Normalize()
|
||||
|
||||
// Z Rotation (roll) - keyboard z/x
|
||||
c.Rotation[2] = c.RotateStep * c.RotationVelocity
|
||||
zRotate := mgl32.HomogRotate3DZ(c.Rotation[2])
|
||||
c.Up = mgl32.TransformNormal(c.Up, zRotate).Normalize()
|
||||
c.Direction = mgl32.TransformNormal(c.Direction, zRotate).Normalize()
|
||||
}
|
||||
|
||||
func (c *Camera) Move(vec mgl32.Vec3) {
|
||||
// Rotate our movement vector to apply in our current direction
|
||||
rotation := math_helpers.RotationMatrix(vec, c.Direction)
|
||||
t := mgl32.TransformCoordinate(vec, rotation)
|
||||
log.Println(vec)
|
||||
log.Println(t)
|
||||
|
||||
// Then translate
|
||||
translation := mgl32.Translate3D(t.X(), t.Y(), t.Z())
|
||||
c.Position = mgl32.TransformCoordinate(c.Position, 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
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package gl_objects
|
||||
|
||||
type globject interface {
|
||||
Draw(uint32)
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
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
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package globjects
|
||||
|
||||
type globject interface {
|
||||
draw(uint32)
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package math_helpers
|
||||
|
||||
import (
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
)
|
||||
|
||||
func RotationMatrix(a, b mgl32.Vec3) mgl32.Mat4 {
|
||||
axis := a.Cross(b)
|
||||
angle := a.Dot(b)
|
||||
if angle == 0 || axis.LenSqr() == 0 {
|
||||
return mgl32.Ident4()
|
||||
} else {
|
||||
return mgl32.HomogRotate3D(angle, axis)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue