mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Camera controls+10,000 rotating cubes+update buffer struct
This commit is contained in:
@ -58,6 +58,9 @@ type Buffer struct {
|
|||||||
Type BufType
|
Type BufType
|
||||||
GLType BufGLType
|
GLType BufGLType
|
||||||
DataTypeInfo
|
DataTypeInfo
|
||||||
|
|
||||||
|
//DataLen is the number of elements in the uploaded to the buffer
|
||||||
|
DataLen int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) Activate() {
|
func (b *Buffer) Activate() {
|
||||||
@ -92,6 +95,7 @@ func (bo *BufferObject) GenBuffer(data []float32, bufUsage BufUsage, bufType Buf
|
|||||||
Type: bufType,
|
Type: bufType,
|
||||||
GLType: bufType.GetBufferGLType(),
|
GLType: bufType.GetBufferGLType(),
|
||||||
DataTypeInfo: GetDataTypeInfo(bufDataType),
|
DataTypeInfo: GetDataTypeInfo(bufDataType),
|
||||||
|
DataLen: int32(len(data)),
|
||||||
}
|
}
|
||||||
bo.SetBuffer(buf)
|
bo.SetBuffer(buf)
|
||||||
|
|
||||||
@ -120,6 +124,7 @@ func (bo *BufferObject) GenBufferUint32(data []uint32, bufUsage BufUsage, bufTyp
|
|||||||
Type: bufType,
|
Type: bufType,
|
||||||
GLType: bufType.GetBufferGLType(),
|
GLType: bufType.GetBufferGLType(),
|
||||||
DataTypeInfo: GetDataTypeInfo(bufDataType),
|
DataTypeInfo: GetDataTypeInfo(bufDataType),
|
||||||
|
DataLen: int32(len(data)),
|
||||||
}
|
}
|
||||||
bo.SetBuffer(buf)
|
bo.SetBuffer(buf)
|
||||||
|
|
||||||
|
|||||||
@ -25,10 +25,16 @@ type mouseMotionState struct {
|
|||||||
YPos int32
|
YPos int32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mouseWheelState struct {
|
||||||
|
XDelta int32
|
||||||
|
YDelta int32
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
keyMap = make(map[sdl.Keycode]*keyState)
|
keyMap = make(map[sdl.Keycode]*keyState)
|
||||||
mouseBtnMap = make(map[int]*mouseBtnState)
|
mouseBtnMap = make(map[int]*mouseBtnState)
|
||||||
mouseMotion = mouseMotionState{}
|
mouseMotion = mouseMotionState{}
|
||||||
|
mouseWheel = mouseWheelState{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func EventLoopStart() {
|
func EventLoopStart() {
|
||||||
@ -46,6 +52,9 @@ func EventLoopStart() {
|
|||||||
|
|
||||||
mouseMotion.XDelta = 0
|
mouseMotion.XDelta = 0
|
||||||
mouseMotion.YDelta = 0
|
mouseMotion.YDelta = 0
|
||||||
|
|
||||||
|
mouseWheel.XDelta = 0
|
||||||
|
mouseWheel.YDelta = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
||||||
@ -61,7 +70,7 @@ func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
|||||||
ks.IsReleasedThisFrame = e.State == sdl.RELEASED && e.Repeat == 0
|
ks.IsReleasedThisFrame = e.State == sdl.RELEASED && e.Repeat == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleMouseEvent(e *sdl.MouseButtonEvent) {
|
func HandleMouseBtnEvent(e *sdl.MouseButtonEvent) {
|
||||||
|
|
||||||
mb := mouseBtnMap[int(e.Button)]
|
mb := mouseBtnMap[int(e.Button)]
|
||||||
if mb == nil {
|
if mb == nil {
|
||||||
@ -75,7 +84,7 @@ func HandleMouseEvent(e *sdl.MouseButtonEvent) {
|
|||||||
mb.IsReleasedThisFrame = e.State == sdl.RELEASED
|
mb.IsReleasedThisFrame = e.State == sdl.RELEASED
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleMouseMotion(e *sdl.MouseMotionEvent) {
|
func HandleMouseMotionEvent(e *sdl.MouseMotionEvent) {
|
||||||
|
|
||||||
mouseMotion.XPos = e.X
|
mouseMotion.XPos = e.X
|
||||||
mouseMotion.YPos = e.Y
|
mouseMotion.YPos = e.Y
|
||||||
@ -84,6 +93,11 @@ func HandleMouseMotion(e *sdl.MouseMotionEvent) {
|
|||||||
mouseMotion.YDelta = e.YRel
|
mouseMotion.YDelta = e.YRel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleMouseWheelEvent(e *sdl.MouseWheelEvent) {
|
||||||
|
mouseWheel.XDelta = e.X
|
||||||
|
mouseWheel.YDelta = e.Y
|
||||||
|
}
|
||||||
|
|
||||||
//GetMousePos returns the window coordinates of the mouse
|
//GetMousePos returns the window coordinates of the mouse
|
||||||
func GetMousePos() (x, y int32) {
|
func GetMousePos() (x, y int32) {
|
||||||
return mouseMotion.XPos, mouseMotion.YPos
|
return mouseMotion.XPos, mouseMotion.YPos
|
||||||
@ -112,6 +126,34 @@ func GetMouseMotionNorm() (xDelta, yDelta int32) {
|
|||||||
return x, y
|
return x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMouseWheelMotion() (xDelta, yDelta int32) {
|
||||||
|
return mouseWheel.XDelta, mouseWheel.YDelta
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetMouseWheelXNorm returns 1 if mouse wheel xDelta > 0, -1 if xDelta < 0, and 0 otherwise
|
||||||
|
func GetMouseWheelXNorm() int32 {
|
||||||
|
|
||||||
|
if mouseWheel.XDelta > 0 {
|
||||||
|
return 1
|
||||||
|
} else if mouseWheel.XDelta < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns 1 if mouse wheel yDelta > 0, -1 if yDelta < 0, and 0 otherwise
|
||||||
|
func GetMouseWheelYNorm() int32 {
|
||||||
|
|
||||||
|
if mouseWheel.YDelta > 0 {
|
||||||
|
return 1
|
||||||
|
} else if mouseWheel.YDelta < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func KeyClicked(kc sdl.Keycode) bool {
|
func KeyClicked(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
ks := keyMap[kc]
|
ks := keyMap[kc]
|
||||||
|
|||||||
59
main.go
59
main.go
@ -53,7 +53,7 @@ var (
|
|||||||
|
|
||||||
imguiInfo *ImguiInfo
|
imguiInfo *ImguiInfo
|
||||||
|
|
||||||
camPos = gglm.NewVec3(0, 0, 10)
|
camPos = gglm.NewVec3(0, 0, -10)
|
||||||
camForward = gglm.NewVec3(0, 0, 1)
|
camForward = gglm.NewVec3(0, 0, 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -109,11 +109,10 @@ func main() {
|
|||||||
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||||
|
|
||||||
//Moves objects into the cameras view
|
//Moves objects into the cameras view
|
||||||
camPos = gglm.NewVec3(0, 0, 10)
|
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
|
|
||||||
//Perspective/Depth
|
//Perspective/Depth
|
||||||
projMat := gglm.Perspective(45*gglm.Deg2Rad, float32(winWidth)/float32(winHeight), 0.1, 20)
|
projMat := gglm.Perspective(45*gglm.Deg2Rad, float32(winWidth)/float32(winHeight), 0.1, 500)
|
||||||
simpleShader.SetUnifMat4("projMat", projMat)
|
simpleShader.SetUnifMat4("projMat", projMat)
|
||||||
|
|
||||||
//Lights
|
//Lights
|
||||||
@ -138,7 +137,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateViewMat() {
|
func updateViewMat() {
|
||||||
targetPos := gglm.NewVec3(0, 0, 0)
|
targetPos := camPos.Clone().Add(camForward)
|
||||||
viewMat := gglm.LookAt(camPos, targetPos, gglm.NewVec3(0, 1, 0))
|
viewMat := gglm.LookAt(camPos, targetPos, gglm.NewVec3(0, 1, 0))
|
||||||
simpleShader.SetUnifMat4("viewMat", &viewMat.Mat4)
|
simpleShader.SetUnifMat4("viewMat", &viewMat.Mat4)
|
||||||
}
|
}
|
||||||
@ -349,6 +348,7 @@ func initImGUI() {
|
|||||||
|
|
||||||
func handleInputs() {
|
func handleInputs() {
|
||||||
|
|
||||||
|
input.EventLoopStart()
|
||||||
imIO := imgui.CurrentIO()
|
imIO := imgui.CurrentIO()
|
||||||
|
|
||||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||||
@ -357,20 +357,10 @@ func handleInputs() {
|
|||||||
|
|
||||||
case *sdl.MouseWheelEvent:
|
case *sdl.MouseWheelEvent:
|
||||||
|
|
||||||
var deltaX, deltaY float32
|
input.HandleMouseWheelEvent(e)
|
||||||
if e.X > 0 {
|
|
||||||
deltaX++
|
|
||||||
} else if e.X < 0 {
|
|
||||||
deltaX--
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.Y > 0 {
|
xDelta, yDelta := input.GetMouseWheelMotion()
|
||||||
deltaY++
|
imIO.AddMouseWheelDelta(float32(xDelta), float32(yDelta))
|
||||||
} else if e.Y < 0 {
|
|
||||||
deltaY--
|
|
||||||
}
|
|
||||||
|
|
||||||
imIO.AddMouseWheelDelta(deltaX, deltaY)
|
|
||||||
|
|
||||||
case *sdl.KeyboardEvent:
|
case *sdl.KeyboardEvent:
|
||||||
input.HandleKeyboardEvent(e)
|
input.HandleKeyboardEvent(e)
|
||||||
@ -385,10 +375,10 @@ func handleInputs() {
|
|||||||
imIO.AddInputCharacters(string(e.Text[:]))
|
imIO.AddInputCharacters(string(e.Text[:]))
|
||||||
|
|
||||||
case *sdl.MouseButtonEvent:
|
case *sdl.MouseButtonEvent:
|
||||||
input.HandleMouseEvent(e)
|
input.HandleMouseBtnEvent(e)
|
||||||
|
|
||||||
case *sdl.MouseMotionEvent:
|
case *sdl.MouseMotionEvent:
|
||||||
input.HandleMouseMotion(e)
|
input.HandleMouseMotionEvent(e)
|
||||||
|
|
||||||
case *sdl.WindowEvent:
|
case *sdl.WindowEvent:
|
||||||
|
|
||||||
@ -454,11 +444,11 @@ var lightColor1 gglm.Vec3 = *gglm.NewVec3(1, 1, 1)
|
|||||||
func runGameLogic() {
|
func runGameLogic() {
|
||||||
|
|
||||||
if input.KeyDown(sdl.K_w) {
|
if input.KeyDown(sdl.K_w) {
|
||||||
camPos.Data[1] += -0.1
|
camPos.Data[1] += 0.1
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
}
|
||||||
if input.KeyDown(sdl.K_s) {
|
if input.KeyDown(sdl.K_s) {
|
||||||
camPos.Data[1] += 0.1
|
camPos.Data[1] -= 0.1
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
}
|
||||||
if input.KeyDown(sdl.K_d) {
|
if input.KeyDown(sdl.K_d) {
|
||||||
@ -466,10 +456,19 @@ func runGameLogic() {
|
|||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
}
|
||||||
if input.KeyDown(sdl.K_a) {
|
if input.KeyDown(sdl.K_a) {
|
||||||
camPos.Data[0] += -0.1
|
camPos.Data[0] -= 0.1
|
||||||
updateViewMat()
|
updateViewMat()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if input.GetMouseWheelYNorm() > 0 {
|
||||||
|
camPos.Data[2] += 1
|
||||||
|
updateViewMat()
|
||||||
|
} else if input.GetMouseWheelYNorm() < 0 {
|
||||||
|
camPos.Data[2] -= 1
|
||||||
|
updateViewMat()
|
||||||
|
}
|
||||||
|
|
||||||
|
modelMat.Rotate(10*timing.DT()*gglm.Deg2Rad, gglm.NewVec3(1, 1, 1).Normalize())
|
||||||
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||||
|
|
||||||
//ImGUI
|
//ImGUI
|
||||||
@ -504,10 +503,6 @@ func runGameLogic() {
|
|||||||
simpleShader.SetUnifVec3("lightColor1", &lightColor1)
|
simpleShader.SetUnifVec3("lightColor1", &lightColor1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if imgui.SliderFloat3("Cam pos", &camPos.Data, -10, 10) {
|
|
||||||
updateViewMat()
|
|
||||||
}
|
|
||||||
|
|
||||||
imgui.Render()
|
imgui.Render()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -522,7 +517,17 @@ func draw() {
|
|||||||
|
|
||||||
//DRAW
|
//DRAW
|
||||||
bo.Activate()
|
bo.Activate()
|
||||||
gl.DrawElements(gl.TRIANGLES, 60, gl.UNSIGNED_INT, gl.PtrOffset(0))
|
tempModelMat := modelMat.Clone()
|
||||||
|
|
||||||
|
for y := 0; y < 100; y++ {
|
||||||
|
for x := 0; x < 100; x++ {
|
||||||
|
simpleShader.SetUnifMat4("modelMat", &tempModelMat.Translate(gglm.NewVec3(-1, 0, 0)).Mat4)
|
||||||
|
gl.DrawElements(gl.TRIANGLES, int32(bo.IndexBuf.DataLen), gl.UNSIGNED_INT, gl.PtrOffset(0))
|
||||||
|
}
|
||||||
|
simpleShader.SetUnifMat4("modelMat", &tempModelMat.Translate(gglm.NewVec3(100, -1, 0)).Mat4)
|
||||||
|
}
|
||||||
|
|
||||||
|
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||||
bo.Deactivate()
|
bo.Deactivate()
|
||||||
|
|
||||||
drawUI()
|
drawUI()
|
||||||
|
|||||||
Reference in New Issue
Block a user