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:
@ -16,8 +16,7 @@ type Camera struct {
|
|||||||
Type Type
|
Type Type
|
||||||
|
|
||||||
Pos gglm.Vec3
|
Pos gglm.Vec3
|
||||||
Target gglm.Vec3
|
Forward gglm.Vec3
|
||||||
// Forward gglm.Vec3
|
|
||||||
WorldUp gglm.Vec3
|
WorldUp gglm.Vec3
|
||||||
|
|
||||||
NearClip float32
|
NearClip float32
|
||||||
@ -35,10 +34,11 @@ type Camera struct {
|
|||||||
ProjMat gglm.Mat4
|
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() {
|
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 {
|
if c.Type == Type_Perspective {
|
||||||
c.ProjMat = *gglm.Perspective(c.Fov, c.AspectRatio, c.NearClip, c.FarClip)
|
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) {
|
func (c *Camera) LookAt(forward, worldUp *gglm.Vec3) {
|
||||||
c.Target = *targetPos
|
c.Forward = *forward
|
||||||
c.WorldUp = *worldUp
|
c.WorldUp = *worldUp
|
||||||
c.Update()
|
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{
|
cam := &Camera{
|
||||||
Type: Type_Perspective,
|
Type: Type_Perspective,
|
||||||
Pos: *pos,
|
Pos: *pos,
|
||||||
// Forward: *gglm.NewVec3(0, 0, 1),
|
Forward: *forward,
|
||||||
Target: *targetPos,
|
|
||||||
WorldUp: *worldUp,
|
WorldUp: *worldUp,
|
||||||
|
|
||||||
NearClip: nearClip,
|
NearClip: nearClip,
|
||||||
@ -73,13 +72,12 @@ func NewPerspective(pos, targetPos, worldUp *gglm.Vec3, nearClip, farClip, fovRa
|
|||||||
return cam
|
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{
|
cam := &Camera{
|
||||||
Type: Type_Orthographic,
|
Type: Type_Orthographic,
|
||||||
Pos: *pos,
|
Pos: *pos,
|
||||||
// Forward: *gglm.NewVec3(0, 0, 0),
|
Forward: *forward,
|
||||||
Target: *targetPos,
|
|
||||||
WorldUp: *worldUp,
|
WorldUp: *worldUp,
|
||||||
|
|
||||||
NearClip: nearClip,
|
NearClip: nearClip,
|
||||||
|
|||||||
48
main.go
48
main.go
@ -35,6 +35,10 @@ import (
|
|||||||
// Frustum culling
|
// Frustum culling
|
||||||
// Material system editor with fields automatically extracted from the shader
|
// Material system editor with fields automatically extracted from the shader
|
||||||
|
|
||||||
|
const (
|
||||||
|
camSpeed float32 = 15
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
window *engine.Window
|
window *engine.Window
|
||||||
|
|
||||||
@ -43,7 +47,7 @@ var (
|
|||||||
simpleMat *materials.Material
|
simpleMat *materials.Material
|
||||||
cubeMesh *meshes.Mesh
|
cubeMesh *meshes.Mesh
|
||||||
|
|
||||||
modelMat = gglm.NewTrMatId()
|
cubeModelMat = gglm.NewTrMatId()
|
||||||
|
|
||||||
lightPos1 = gglm.NewVec3(2, 2, 0)
|
lightPos1 = gglm.NewVec3(2, 2, 0)
|
||||||
lightColor1 = gglm.NewVec3(1, 1, 1)
|
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))
|
scaleMat := gglm.NewScaleMat(gglm.NewVec3(0.25, 0.25, 0.25))
|
||||||
rotMat := gglm.NewRotMat(gglm.NewQuatEuler(gglm.NewVec3(0, 0, 0).AsRad()))
|
rotMat := gglm.NewRotMat(gglm.NewQuatEuler(gglm.NewVec3(0, 0, 0).AsRad()))
|
||||||
|
|
||||||
modelMat.Mul(translationMat.Mul(rotMat.Mul(scaleMat)))
|
cubeModelMat.Mul(translationMat.Mul(rotMat.Mul(scaleMat)))
|
||||||
simpleMat.SetUnifMat4("modelMat", &modelMat.Mat4)
|
simpleMat.SetUnifMat4("modelMat", &cubeModelMat.Mat4)
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
winWidth, winHeight := g.Win.SDLWin.GetSize()
|
winWidth, winHeight := g.Win.SDLWin.GetSize()
|
||||||
cam = camera.NewPerspective(
|
cam = camera.NewPerspective(
|
||||||
gglm.NewVec3(0, 0, -10),
|
gglm.NewVec3(0, 0, -10),
|
||||||
gglm.NewVec3(0, 0, -9),
|
gglm.NewVec3(0, 0, 1),
|
||||||
gglm.NewVec3(0, 1, 0),
|
gglm.NewVec3(0, 1, 0),
|
||||||
0.1, 20,
|
0.1, 20,
|
||||||
45*gglm.Deg2Rad,
|
45*gglm.Deg2Rad,
|
||||||
@ -195,44 +199,46 @@ func (g *OurGame) Update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Camera movement
|
//Camera movement
|
||||||
var camSpeed float32 = 15
|
|
||||||
if input.KeyDown(sdl.K_w) {
|
if input.KeyDown(sdl.K_w) {
|
||||||
cam.Pos.AddY(camSpeed * timing.DT())
|
cam.Pos.Add(cam.WorldUp.Clone().Scale(camSpeed * timing.DT()))
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
} else if input.KeyDown(sdl.K_s) {
|
||||||
if input.KeyDown(sdl.K_s) {
|
cam.Pos.Sub(cam.WorldUp.Clone().Scale(camSpeed * timing.DT()))
|
||||||
cam.Pos.AddY(-camSpeed * timing.DT())
|
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
}
|
||||||
|
|
||||||
if input.KeyDown(sdl.K_d) {
|
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()
|
updateViewMat()
|
||||||
}
|
} else if input.KeyDown(sdl.K_a) {
|
||||||
if input.KeyDown(sdl.K_a) {
|
cam.Pos.Sub(gglm.Cross(&cam.WorldUp, &cam.Forward).Scale(camSpeed * timing.DT()))
|
||||||
cam.Pos.AddX(-camSpeed * timing.DT())
|
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
}
|
||||||
|
|
||||||
if input.GetMouseWheelYNorm() > 0 {
|
if input.GetMouseWheelYNorm() > 0 {
|
||||||
cam.Pos.AddZ(1)
|
cam.Pos.Add(&cam.Forward)
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
} else if input.GetMouseWheelYNorm() < 0 {
|
} else if input.GetMouseWheelYNorm() < 0 {
|
||||||
cam.Pos.AddZ(-1)
|
cam.Pos.Sub(&cam.Forward)
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Rotating cubes
|
//Rotating cubes
|
||||||
if input.KeyDown(sdl.K_SPACE) {
|
if input.KeyDown(sdl.K_SPACE) {
|
||||||
modelMat.Rotate(10*timing.DT()*gglm.Deg2Rad, gglm.NewVec3(1, 1, 1).Normalize())
|
cubeModelMat.Rotate(10*timing.DT()*gglm.Deg2Rad, gglm.NewVec3(1, 1, 1).Normalize())
|
||||||
simpleMat.SetUnifMat4("modelMat", &modelMat.Mat4)
|
simpleMat.SetUnifMat4("modelMat", &cubeModelMat.Mat4)
|
||||||
}
|
}
|
||||||
|
|
||||||
imgui.DragFloat3("Cam Pos", &cam.Pos.Data)
|
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() {
|
func (g *OurGame) Render() {
|
||||||
|
|
||||||
tempModelMat := modelMat.Clone()
|
tempModelMat := cubeModelMat.Clone()
|
||||||
|
|
||||||
rowSize := 100
|
rowSize := 100
|
||||||
for y := 0; y < rowSize; y++ {
|
for y := 0; y < rowSize; y++ {
|
||||||
@ -254,10 +260,6 @@ func (g *OurGame) DeInit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateViewMat() {
|
func updateViewMat() {
|
||||||
target := cam.Pos.Clone()
|
|
||||||
target.AddZ(1)
|
|
||||||
cam.Target = *target
|
|
||||||
cam.Update()
|
cam.Update()
|
||||||
|
|
||||||
simpleMat.SetUnifMat4("viewMat", &cam.ViewMat)
|
simpleMat.SetUnifMat4("viewMat", &cam.ViewMat)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user