mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Update camera to use pos+forward vectors to calc target
This commit is contained in:
@ -15,9 +15,8 @@ const (
|
||||
type Camera struct {
|
||||
Type Type
|
||||
|
||||
Pos gglm.Vec3
|
||||
Target gglm.Vec3
|
||||
// Forward gglm.Vec3
|
||||
Pos gglm.Vec3
|
||||
Forward gglm.Vec3
|
||||
WorldUp gglm.Vec3
|
||||
|
||||
NearClip float32
|
||||
@ -35,10 +34,11 @@ type Camera struct {
|
||||
ProjMat gglm.Mat4
|
||||
}
|
||||
|
||||
// Update recalculates view and projection matrices
|
||||
// Update recalculates view matrix and projection matrix.
|
||||
// Should be called whenever a camera parameter changes
|
||||
func (c *Camera) Update() {
|
||||
|
||||
c.ViewMat = gglm.LookAt(&c.Pos, &c.Target, &c.WorldUp).Mat4
|
||||
c.ViewMat = gglm.LookAt(&c.Pos, c.Pos.Clone().Add(&c.Forward), &c.WorldUp).Mat4
|
||||
|
||||
if c.Type == Type_Perspective {
|
||||
c.ProjMat = *gglm.Perspective(c.Fov, c.AspectRatio, c.NearClip, c.FarClip)
|
||||
@ -47,19 +47,18 @@ func (c *Camera) Update() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Camera) LookAt(targetPos, worldUp *gglm.Vec3) {
|
||||
c.Target = *targetPos
|
||||
func (c *Camera) LookAt(forward, worldUp *gglm.Vec3) {
|
||||
c.Forward = *forward
|
||||
c.WorldUp = *worldUp
|
||||
c.Update()
|
||||
}
|
||||
|
||||
func NewPerspective(pos, targetPos, worldUp *gglm.Vec3, nearClip, farClip, fovRadians, aspectRatio float32) *Camera {
|
||||
func NewPerspective(pos, forward, worldUp *gglm.Vec3, nearClip, farClip, fovRadians, aspectRatio float32) *Camera {
|
||||
|
||||
cam := &Camera{
|
||||
Type: Type_Perspective,
|
||||
Pos: *pos,
|
||||
// Forward: *gglm.NewVec3(0, 0, 1),
|
||||
Target: *targetPos,
|
||||
Type: Type_Perspective,
|
||||
Pos: *pos,
|
||||
Forward: *forward,
|
||||
WorldUp: *worldUp,
|
||||
|
||||
NearClip: nearClip,
|
||||
@ -73,13 +72,12 @@ func NewPerspective(pos, targetPos, worldUp *gglm.Vec3, nearClip, farClip, fovRa
|
||||
return cam
|
||||
}
|
||||
|
||||
func NewOrthographic(pos, targetPos, worldUp *gglm.Vec3, nearClip, farClip, left, right, top, bottom float32) *Camera {
|
||||
func NewOrthographic(pos, forward, worldUp *gglm.Vec3, nearClip, farClip, left, right, top, bottom float32) *Camera {
|
||||
|
||||
cam := &Camera{
|
||||
Type: Type_Orthographic,
|
||||
Pos: *pos,
|
||||
// Forward: *gglm.NewVec3(0, 0, 0),
|
||||
Target: *targetPos,
|
||||
Type: Type_Orthographic,
|
||||
Pos: *pos,
|
||||
Forward: *forward,
|
||||
WorldUp: *worldUp,
|
||||
|
||||
NearClip: nearClip,
|
||||
|
||||
48
main.go
48
main.go
@ -35,6 +35,10 @@ import (
|
||||
// Frustum culling
|
||||
// Material system editor with fields automatically extracted from the shader
|
||||
|
||||
const (
|
||||
camSpeed float32 = 15
|
||||
)
|
||||
|
||||
var (
|
||||
window *engine.Window
|
||||
|
||||
@ -43,7 +47,7 @@ var (
|
||||
simpleMat *materials.Material
|
||||
cubeMesh *meshes.Mesh
|
||||
|
||||
modelMat = gglm.NewTrMatId()
|
||||
cubeModelMat = gglm.NewTrMatId()
|
||||
|
||||
lightPos1 = gglm.NewVec3(2, 2, 0)
|
||||
lightColor1 = gglm.NewVec3(1, 1, 1)
|
||||
@ -165,14 +169,14 @@ func (g *OurGame) Init() {
|
||||
scaleMat := gglm.NewScaleMat(gglm.NewVec3(0.25, 0.25, 0.25))
|
||||
rotMat := gglm.NewRotMat(gglm.NewQuatEuler(gglm.NewVec3(0, 0, 0).AsRad()))
|
||||
|
||||
modelMat.Mul(translationMat.Mul(rotMat.Mul(scaleMat)))
|
||||
simpleMat.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||
cubeModelMat.Mul(translationMat.Mul(rotMat.Mul(scaleMat)))
|
||||
simpleMat.SetUnifMat4("modelMat", &cubeModelMat.Mat4)
|
||||
|
||||
// Camera
|
||||
winWidth, winHeight := g.Win.SDLWin.GetSize()
|
||||
cam = camera.NewPerspective(
|
||||
gglm.NewVec3(0, 0, -10),
|
||||
gglm.NewVec3(0, 0, -9),
|
||||
gglm.NewVec3(0, 0, 1),
|
||||
gglm.NewVec3(0, 1, 0),
|
||||
0.1, 20,
|
||||
45*gglm.Deg2Rad,
|
||||
@ -195,44 +199,46 @@ func (g *OurGame) Update() {
|
||||
}
|
||||
|
||||
//Camera movement
|
||||
var camSpeed float32 = 15
|
||||
if input.KeyDown(sdl.K_w) {
|
||||
cam.Pos.AddY(camSpeed * timing.DT())
|
||||
updateViewMat()
|
||||
}
|
||||
if input.KeyDown(sdl.K_s) {
|
||||
cam.Pos.AddY(-camSpeed * timing.DT())
|
||||
cam.Pos.Add(cam.WorldUp.Clone().Scale(camSpeed * timing.DT()))
|
||||
updateViewMat()
|
||||
} else if input.KeyDown(sdl.K_s) {
|
||||
cam.Pos.Sub(cam.WorldUp.Clone().Scale(camSpeed * timing.DT()))
|
||||
updateViewMat()
|
||||
}
|
||||
|
||||
if input.KeyDown(sdl.K_d) {
|
||||
cam.Pos.AddX(camSpeed * timing.DT())
|
||||
cam.Pos.Add(gglm.Cross(&cam.WorldUp, &cam.Forward).Scale(camSpeed * timing.DT()))
|
||||
updateViewMat()
|
||||
}
|
||||
if input.KeyDown(sdl.K_a) {
|
||||
cam.Pos.AddX(-camSpeed * timing.DT())
|
||||
} else if input.KeyDown(sdl.K_a) {
|
||||
cam.Pos.Sub(gglm.Cross(&cam.WorldUp, &cam.Forward).Scale(camSpeed * timing.DT()))
|
||||
updateViewMat()
|
||||
}
|
||||
|
||||
if input.GetMouseWheelYNorm() > 0 {
|
||||
cam.Pos.AddZ(1)
|
||||
cam.Pos.Add(&cam.Forward)
|
||||
updateViewMat()
|
||||
} else if input.GetMouseWheelYNorm() < 0 {
|
||||
cam.Pos.AddZ(-1)
|
||||
cam.Pos.Sub(&cam.Forward)
|
||||
updateViewMat()
|
||||
}
|
||||
|
||||
//Rotating cubes
|
||||
if input.KeyDown(sdl.K_SPACE) {
|
||||
modelMat.Rotate(10*timing.DT()*gglm.Deg2Rad, gglm.NewVec3(1, 1, 1).Normalize())
|
||||
simpleMat.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||
cubeModelMat.Rotate(10*timing.DT()*gglm.Deg2Rad, gglm.NewVec3(1, 1, 1).Normalize())
|
||||
simpleMat.SetUnifMat4("modelMat", &cubeModelMat.Mat4)
|
||||
}
|
||||
|
||||
imgui.DragFloat3("Cam Pos", &cam.Pos.Data)
|
||||
|
||||
if input.KeyClicked(sdl.K_F4) {
|
||||
fmt.Printf("Pos: %s; Forward: %s; Forward*WorldUp: %s\n", cam.Pos.String(), cam.Forward.String(), gglm.Cross(&cam.Forward, &cam.WorldUp))
|
||||
}
|
||||
}
|
||||
|
||||
func (g *OurGame) Render() {
|
||||
|
||||
tempModelMat := modelMat.Clone()
|
||||
tempModelMat := cubeModelMat.Clone()
|
||||
|
||||
rowSize := 100
|
||||
for y := 0; y < rowSize; y++ {
|
||||
@ -254,10 +260,6 @@ func (g *OurGame) DeInit() {
|
||||
}
|
||||
|
||||
func updateViewMat() {
|
||||
target := cam.Pos.Clone()
|
||||
target.AddZ(1)
|
||||
cam.Target = *target
|
||||
cam.Update()
|
||||
|
||||
simpleMat.SetUnifMat4("viewMat", &cam.ViewMat)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user