Got some movement and boundaries. Bad, but works.
parent
8a448d5978
commit
4bafcb2957
2
go.mod
2
go.mod
|
@ -5,7 +5,7 @@ go 1.17
|
|||
require (
|
||||
github.com/go-gl/gl v0.0.0-20210813123233-e4099ee2221f
|
||||
github.com/go-gl/mathgl v1.0.0
|
||||
github.com/veandco/go-sdl2 v0.4.9
|
||||
github.com/veandco/go-sdl2 v0.4.10
|
||||
)
|
||||
|
||||
require golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -2,8 +2,8 @@ github.com/go-gl/gl v0.0.0-20210813123233-e4099ee2221f h1:s0O46d8fPwk9kU4k1jj76w
|
|||
github.com/go-gl/gl v0.0.0-20210813123233-e4099ee2221f/go.mod h1:wjpnOv6ONl2SuJSxqCPVaPZibGFdSci9HFocT9qtVYM=
|
||||
github.com/go-gl/mathgl v1.0.0 h1:t9DznWJlXxxjeeKLIdovCOVJQk/GzDEL7h/h+Ro2B68=
|
||||
github.com/go-gl/mathgl v1.0.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ=
|
||||
github.com/veandco/go-sdl2 v0.4.9 h1:Fc0/Xj8JToXO7qia77Vc1M6bP7iufE+vzsLNah70Y6M=
|
||||
github.com/veandco/go-sdl2 v0.4.9/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=
|
||||
github.com/veandco/go-sdl2 v0.4.10 h1:8QoD2bhWl7SbQDflIAUYWfl9Vq+mT8/boJFAUzAScgY=
|
||||
github.com/veandco/go-sdl2 v0.4.10/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=
|
||||
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f h1:FO4MZ3N56GnxbqxGKqh+YTzUWQ2sDwtFQEZgLOxh9Jc=
|
||||
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
|
152
main.go
152
main.go
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
|
@ -18,12 +17,15 @@ import (
|
|||
)
|
||||
|
||||
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||||
var openglMode = flag.Bool("opengl", true, "Draw game using OpenGL rendering")
|
||||
var sdlMode = flag.Bool("sdl", false, "Draw game using SDL rendering")
|
||||
|
||||
const GameTitle = "Carpy Breakout"
|
||||
const LogFile = "output.log"
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if *cpuprofile != "" {
|
||||
f, err := os.Create(*cpuprofile)
|
||||
if err != nil {
|
||||
|
@ -34,10 +36,7 @@ func main() {
|
|||
}
|
||||
|
||||
setupLogging()
|
||||
run()
|
||||
}
|
||||
|
||||
func run() {
|
||||
// TODO can potentially rework stuff to not need LockOSThread()
|
||||
// here. SDL has the built-in sdl.Do(), but I would need to create
|
||||
// my own similar thing for OpenGL. Essentially they both need to
|
||||
|
@ -45,6 +44,14 @@ func run() {
|
|||
// goroutine.
|
||||
runtime.LockOSThread()
|
||||
|
||||
if *sdlMode {
|
||||
runSDL()
|
||||
} else if *openglMode {
|
||||
runOpenGL()
|
||||
}
|
||||
}
|
||||
|
||||
func runOpenGL() {
|
||||
err := sdl.Init(sdl.INIT_EVERYTHING)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -57,12 +64,11 @@ func run() {
|
|||
|
||||
sdl.DisableScreenSaver()
|
||||
|
||||
sdl.GLSetAttribute(sdl.GL_MULTISAMPLESAMPLES, 2) // Smooth
|
||||
|
||||
// Capture the mouse for movement
|
||||
sdl.SetRelativeMouseMode(true)
|
||||
|
||||
// Init and configure global settings
|
||||
sdl.GLSetAttribute(sdl.GL_MULTISAMPLESAMPLES, 2) // Smooth
|
||||
gl.Init()
|
||||
|
||||
gameWindow, err := game_window.NewGameWindow(GameTitle)
|
||||
|
@ -75,7 +81,7 @@ func run() {
|
|||
sunLight.GLInit(gameWindow.GLProgram)
|
||||
|
||||
camera := gl_objects.NewCamera(gameWindow.GLProgram)
|
||||
camera.Position = mgl32.Vec3{0, 0, 30}
|
||||
camera.Position = mgl32.Vec3{0, 0, 60}
|
||||
gameWindow.SetCamera(camera)
|
||||
gameWindow.AddObject(camera)
|
||||
|
||||
|
@ -87,16 +93,20 @@ func run() {
|
|||
// TODO bounding box
|
||||
|
||||
// Brick Targets
|
||||
targets := breakout.NewTargets(5, 4, mgl32.Vec3{-10, 4, 0})
|
||||
targets := breakout.NewTargets(4, 5, mgl32.Vec3{-8, 4, 0})
|
||||
targets.GLInit(gameWindow.GLProgram)
|
||||
for _, brick := range targets.Bricks {
|
||||
gameWindow.AddObject(brick)
|
||||
}
|
||||
positiveBoundary := mgl32.Vec3{8, 16, 0}
|
||||
negativeBoundary := mgl32.Vec3{-8, -16, 0}
|
||||
gameWindow.PositiveBoundary = positiveBoundary
|
||||
gameWindow.NegativeBoundary = negativeBoundary
|
||||
|
||||
// Paddle
|
||||
paddleMaterial := gl_objects.NewMaterial()
|
||||
paddleMaterial.Color = mgl32.Vec3{0, 1, 0}
|
||||
paddle := gl_objects.NewBox(6.0, 1.0, 4.0, mgl32.Vec3{0, -10, 0}, paddleMaterial)
|
||||
paddle := gl_objects.NewBox(6.0, 1.0, 4.0, mgl32.Vec3{0, -6, 0}, paddleMaterial)
|
||||
paddle.GLInit(gameWindow.GLProgram)
|
||||
gameWindow.AddObject(paddle)
|
||||
gameWindow.SetPaddle(paddle)
|
||||
|
@ -104,7 +114,7 @@ func run() {
|
|||
// Ball
|
||||
ballMaterial := gl_objects.NewMaterial()
|
||||
ballMaterial.Color = mgl32.Vec3{1, 1, 1}
|
||||
ball := gl_objects.NewBox(1, 1, 1, mgl32.Vec3{-8, -2, 0}, ballMaterial)
|
||||
ball := gl_objects.NewBox(1, 1, 1, mgl32.Vec3{-8, 0, 0}, ballMaterial)
|
||||
ball.GLInit(gameWindow.GLProgram)
|
||||
gameWindow.AddObject(ball)
|
||||
|
||||
|
@ -114,16 +124,15 @@ func run() {
|
|||
gl.DepthFunc(gl.LESS)
|
||||
gl.LineWidth(2.0)
|
||||
|
||||
running := true
|
||||
for running {
|
||||
for gameWindow.IsRunning() {
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||
|
||||
gameWindow.Update()
|
||||
gameWindow.Draw()
|
||||
gameWindow.GLDraw()
|
||||
|
||||
gameWindow.SDLWindow.GLSwap()
|
||||
|
||||
running = handleEvents(running, gameWindow)
|
||||
gameWindow.HandleEvents()
|
||||
sdl.Delay(16)
|
||||
}
|
||||
}
|
||||
|
@ -138,116 +147,7 @@ func setupLogging() {
|
|||
log.Println("=== START ===")
|
||||
}
|
||||
|
||||
func handleEvents(running bool, gameWindow *game_window.GameWindow) bool {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch t := event.(type) {
|
||||
case *sdl.WindowEvent:
|
||||
gameWindow.WindowProjection()
|
||||
case *sdl.QuitEvent:
|
||||
log.Println("Quiting")
|
||||
running = false
|
||||
case *sdl.MouseMotionEvent:
|
||||
if t.XRel != 0 || t.YRel != 0 {
|
||||
if gameWindow.IsFreelookOn() {
|
||||
gameWindow.Camera.Rotate(t.XRel, t.YRel, 0)
|
||||
} else {
|
||||
gameWindow.MovePaddle(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",
|
||||
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
|
||||
case *sdl.MouseWheelEvent:
|
||||
fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
|
||||
t.Timestamp, t.Type, t.Which, t.X, t.Y)
|
||||
case *sdl.KeyboardEvent:
|
||||
if t.Keysym.Sym == sdl.K_ESCAPE {
|
||||
log.Println("Esc quitting")
|
||||
running = false
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_f {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.ToggleFullscreen()
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_l {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.ToggleWireframe()
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_k {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.ToggleFreelook()
|
||||
}
|
||||
}
|
||||
// Movement
|
||||
if gameWindow.IsFreelookOn() {
|
||||
if t.Keysym.Sym == sdl.K_w {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetZVelocity(1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetZVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_a {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetXVelocity(-1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetXVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_s {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetZVelocity(-1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetZVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_d {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetXVelocity(1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetXVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_q {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetYVelocity(-1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetYVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_e {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetYVelocity(1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetYVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_z {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetRotationVelocity(-1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetRotationVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_x {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetRotationVelocity(1)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetRotationVelocity(0)
|
||||
}
|
||||
}
|
||||
if t.Keysym.Sym == sdl.K_LSHIFT {
|
||||
if t.Type == sdl.KEYDOWN {
|
||||
gameWindow.Camera.SetFastMoveVelocity(2)
|
||||
} else if t.Type == sdl.KEYUP {
|
||||
gameWindow.Camera.SetFastMoveVelocity(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return running
|
||||
func runSDL() {
|
||||
// I really need to think through how to separate the physical objects from how they are drawn.
|
||||
log.Println("TODO runSDL()")
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ type Brick struct {
|
|||
}
|
||||
|
||||
func NewBrick(
|
||||
brickWidth,
|
||||
brickHeight,
|
||||
brickWidth float32,
|
||||
brickHeight float32,
|
||||
brickDepth float32,
|
||||
position mgl32.Vec3,
|
||||
material *gl_objects.Material) *Brick {
|
||||
|
@ -25,9 +25,15 @@ func NewBrick(
|
|||
return &brick
|
||||
}
|
||||
|
||||
func (b *Brick) Draw() {
|
||||
func (b *Brick) Update() {
|
||||
if !b.broken {
|
||||
b.Box.Draw()
|
||||
b.Box.Update()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Brick) GLDraw() {
|
||||
if !b.broken {
|
||||
b.Box.GLDraw()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,8 +45,8 @@ func (b *Brick) ToggleWireframe() {
|
|||
b.Box.ToggleWireframe()
|
||||
}
|
||||
|
||||
func (b *Brick) Update() {
|
||||
func (b *Brick) SDLDraw() {
|
||||
if !b.broken {
|
||||
b.Box.Update()
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,12 +34,11 @@ func NewTargets(numRows, numColumns int, lowerLeftPos mgl32.Vec3) *Targets {
|
|||
}
|
||||
|
||||
n := 0
|
||||
|
||||
for i := 0; i < numColumns; i++ {
|
||||
for j := 0; j < numRows; j++ {
|
||||
for i := 0; i < numRows; i++ {
|
||||
for j := 0; j < numColumns; j++ {
|
||||
position := lowerLeftPos.Add(mgl32.Vec3{
|
||||
float32(j) + float32(j)*brickWidth,
|
||||
float32(i) + float32(i)*brickHeight,
|
||||
float32(j) * brickWidth,
|
||||
float32(i) * brickHeight,
|
||||
0})
|
||||
targets.Bricks[n] = NewBrick(brickWidth, brickHeight, brickDepth, position, materials[i])
|
||||
n++
|
||||
|
@ -55,8 +54,8 @@ func (t *Targets) GLInit(glProgram uint32) {
|
|||
}
|
||||
}
|
||||
|
||||
func (t *Targets) Draw() {
|
||||
func (t *Targets) GLDraw() {
|
||||
for _, brick := range t.Bricks {
|
||||
brick.Draw()
|
||||
brick.GLDraw()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package game_window
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
gl "github.com/go-gl/gl/v3.1/gles2"
|
||||
|
@ -17,21 +18,28 @@ const DefaultWindowFlags uint32 = sdl.WINDOW_SHOWN | sdl.WINDOW_RESIZABLE | sdl.
|
|||
const FullscreenWindowFlags uint32 = DefaultWindowFlags | sdl.WINDOW_FULLSCREEN_DESKTOP
|
||||
|
||||
type GameWindow struct {
|
||||
SDLWindow *sdl.Window
|
||||
GLContext *sdl.GLContext
|
||||
GLProgram uint32
|
||||
|
||||
GLObjects []gl_objects.GLObject
|
||||
Camera *gl_objects.Camera
|
||||
Paddle *gl_objects.Box
|
||||
SDLWindow *sdl.Window
|
||||
GLContext *sdl.GLContext
|
||||
GLProgram uint32
|
||||
GLObjects []gl_objects.GLObject
|
||||
Camera *gl_objects.Camera
|
||||
Paddle *gl_objects.Box
|
||||
PositiveBoundary mgl32.Vec3
|
||||
NegativeBoundary mgl32.Vec3
|
||||
|
||||
running bool
|
||||
fullscreen bool
|
||||
wireframe bool
|
||||
freelook bool
|
||||
}
|
||||
|
||||
func NewGameWindow(title string) (*GameWindow, error) {
|
||||
gameWindow := GameWindow{}
|
||||
gameWindow := GameWindow{
|
||||
running: true,
|
||||
fullscreen: false,
|
||||
wireframe: false,
|
||||
freelook: false,
|
||||
}
|
||||
|
||||
window, err := sdl.CreateWindow(
|
||||
title,
|
||||
|
@ -92,6 +100,14 @@ func (w *GameWindow) IsFreelookOn() bool {
|
|||
return w.freelook
|
||||
}
|
||||
|
||||
func (w *GameWindow) IsRunning() bool {
|
||||
return w.running
|
||||
}
|
||||
|
||||
func (w *GameWindow) StopRunning() {
|
||||
w.running = false
|
||||
}
|
||||
|
||||
func (w *GameWindow) WindowProjection() {
|
||||
width, height := w.SDLWindow.GLGetDrawableSize()
|
||||
projection := mgl32.Perspective(mgl32.DegToRad(45.0), float32(width)/float32(height), 0.01, 1000.0)
|
||||
|
@ -100,10 +116,6 @@ func (w *GameWindow) WindowProjection() {
|
|||
gl.Viewport(0, 0, width, height)
|
||||
}
|
||||
|
||||
func (w *GameWindow) MovePaddle(x, y int32) {
|
||||
log.Printf("Moving paddle: %v, %v\n", x, y)
|
||||
}
|
||||
|
||||
func (w *GameWindow) SetPaddle(paddle *gl_objects.Box) {
|
||||
w.Paddle = paddle
|
||||
}
|
||||
|
@ -130,10 +142,12 @@ func (w *GameWindow) ToggleWireframe() {
|
|||
w.wireframe = !w.wireframe
|
||||
}
|
||||
|
||||
func (w *GameWindow) Draw() {
|
||||
func (w *GameWindow) GLDraw() {
|
||||
model := mgl32.Ident4()
|
||||
// Probably can't do concurrent drawing due to OpenGL
|
||||
for _, o := range w.GLObjects {
|
||||
o.Draw()
|
||||
gl_helpers.SetUniformMatrix4f(w.GLProgram, "model", &model)
|
||||
o.GLDraw()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,4 +166,165 @@ func (w *GameWindow) Update() {
|
|||
for _, o := range w.GLObjects {
|
||||
o.Update()
|
||||
}
|
||||
|
||||
w.ensurePaddleBoundary()
|
||||
}
|
||||
|
||||
func (w *GameWindow) HandleEvents() {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch e := event.(type) {
|
||||
case *sdl.WindowEvent:
|
||||
w.WindowProjection()
|
||||
case *sdl.QuitEvent:
|
||||
log.Println("Quiting")
|
||||
w.StopRunning()
|
||||
case *sdl.MouseMotionEvent:
|
||||
w.HandleMouseMovementEvent(e)
|
||||
case *sdl.MouseButtonEvent:
|
||||
fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
|
||||
e.Timestamp, e.Type, e.Which, e.X, e.Y, e.Button, e.State)
|
||||
case *sdl.MouseWheelEvent:
|
||||
fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
|
||||
e.Timestamp, e.Type, e.Which, e.X, e.Y)
|
||||
case *sdl.KeyboardEvent:
|
||||
w.HandleKeyboardEvent(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GameWindow) HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
||||
if e.Keysym.Sym == sdl.K_ESCAPE {
|
||||
log.Println("Esc quitting")
|
||||
w.StopRunning()
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_f {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.ToggleFullscreen()
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_l {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.ToggleWireframe()
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_k {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.ToggleFreelook()
|
||||
}
|
||||
}
|
||||
|
||||
// Movement
|
||||
if w.IsFreelookOn() {
|
||||
w.handleFreelookMovement(e)
|
||||
} else {
|
||||
w.handleGameKeyboardMovement(e)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GameWindow) handleFreelookMovement(e *sdl.KeyboardEvent) {
|
||||
if e.Keysym.Sym == sdl.K_w {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetZVelocity(1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetZVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_a {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetXVelocity(-1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetXVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_s {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetZVelocity(-1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetZVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_d {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetXVelocity(1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetXVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_q {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetYVelocity(-1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetYVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_e {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetYVelocity(1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetYVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_z {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetRotationVelocity(-1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetRotationVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_x {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetRotationVelocity(1)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetRotationVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_LSHIFT {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Camera.SetFastMoveVelocity(2)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Camera.SetFastMoveVelocity(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GameWindow) HandleMouseMovementEvent(e *sdl.MouseMotionEvent) {
|
||||
if w.IsFreelookOn() {
|
||||
if e.XRel != 0 || e.YRel != 0 {
|
||||
w.Camera.Rotate(e.XRel, e.YRel, 0)
|
||||
}
|
||||
} else {
|
||||
w.handleGameMouseMovement(e)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GameWindow) handleGameKeyboardMovement(e *sdl.KeyboardEvent) {
|
||||
if e.Keysym.Sym == sdl.K_a {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Paddle.SetXVelocity(-0.5)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Paddle.SetXVelocity(0)
|
||||
}
|
||||
}
|
||||
if e.Keysym.Sym == sdl.K_d {
|
||||
if e.Type == sdl.KEYDOWN {
|
||||
w.Paddle.SetXVelocity(0.5)
|
||||
} else if e.Type == sdl.KEYUP {
|
||||
w.Paddle.SetXVelocity(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GameWindow) handleGameMouseMovement(e *sdl.MouseMotionEvent) {
|
||||
if e.XRel != 0 || e.YRel != 0 {
|
||||
v := float32(e.XRel) / 50.0
|
||||
w.Paddle.SetXVelocity(v)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GameWindow) ensurePaddleBoundary() {
|
||||
if w.Paddle.Translation.X() < w.NegativeBoundary.X() {
|
||||
w.Paddle.Translation[0] = w.NegativeBoundary.X()
|
||||
} else if w.Paddle.Translation.X() > w.PositiveBoundary.X() {
|
||||
w.Paddle.Translation[0] = w.PositiveBoundary.X()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -131,9 +131,6 @@ func UpdateVertexAttribs(glProgram uint32, vertexSize int32) {
|
|||
texCoordAttrib := uint32(gl.GetAttribLocation(glProgram, gl.Str("vertTexCoord\x00")))
|
||||
gl.EnableVertexAttribArray(texCoordAttrib)
|
||||
gl.VertexAttribPointerWithOffset(texCoordAttrib, 2, gl.FLOAT, false, stride, uintptr(6*sizeofFloat))
|
||||
|
||||
model := mgl32.Ident4()
|
||||
SetUniformMatrix4f(glProgram, "model", &model)
|
||||
}
|
||||
|
||||
func GLStr(str string) *uint8 {
|
||||
|
|
|
@ -11,9 +11,14 @@ import (
|
|||
|
||||
type Box struct {
|
||||
Size int32
|
||||
Width float32
|
||||
Height float32
|
||||
Depth float32
|
||||
Faces []*Face
|
||||
Position mgl32.Vec3
|
||||
Translation mgl32.Vec3
|
||||
Velocity mgl32.Vec3
|
||||
Rotation mgl32.Vec3
|
||||
RotationVelocity mgl32.Vec3
|
||||
VertexArray []float32
|
||||
Material *Material
|
||||
GLProgram uint32
|
||||
|
@ -30,8 +35,12 @@ func NewBox(
|
|||
// height = y axis (up)
|
||||
|
||||
box := Box{
|
||||
Material: material,
|
||||
Size: 6,
|
||||
Size: 6,
|
||||
Width: width,
|
||||
Depth: depth,
|
||||
Height: height,
|
||||
Translation: position,
|
||||
Material: material,
|
||||
}
|
||||
|
||||
topPos := mgl32.Vec3{position.X(), position.Y() + height/2.0, position.Z()}
|
||||
|
@ -82,28 +91,31 @@ func (b *Box) GLInit(glProgram uint32) {
|
|||
}
|
||||
|
||||
func (b *Box) Update() {
|
||||
b.Rotation = mgl32.Vec3{b.Rotation.X(), b.Rotation.Y() + 0.01, b.Rotation.Z()}
|
||||
return
|
||||
b.Rotation = b.Rotation.Add(b.RotationVelocity)
|
||||
b.Translation = b.Translation.Add(b.Velocity)
|
||||
}
|
||||
|
||||
func (b *Box) Draw() {
|
||||
func (b *Box) GLDraw() {
|
||||
// Apply rotation
|
||||
model := mgl32.HomogRotate3D(b.Rotation.Y(), mgl32.Vec3{0, 1, 0})
|
||||
gl_helpers.SetUniformMatrix4f(b.GLProgram, "model", &model)
|
||||
rotate := mgl32.HomogRotate3D(b.Rotation.Y(), mgl32.Vec3{0, 1, 0})
|
||||
|
||||
// Apply translation
|
||||
// TODO
|
||||
translate := mgl32.Translate3D(b.Translation.X(), b.Translation.Y(), b.Translation.Z())
|
||||
|
||||
b.Material.Draw(b.GLProgram)
|
||||
model := rotate.Add(translate)
|
||||
|
||||
gl_helpers.SetUniformMatrix4f(b.GLProgram, "model", &model)
|
||||
|
||||
b.Material.GLDraw(b.GLProgram)
|
||||
|
||||
if b.Wireframe {
|
||||
b.drawOutline()
|
||||
b.glDrawLineLoop()
|
||||
} else {
|
||||
b.drawTriangles()
|
||||
b.glDrawTriangles()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Box) drawTriangles() {
|
||||
func (b *Box) glDrawTriangles() {
|
||||
gl_helpers.SetUniformInt(b.GLProgram, "lightsOn", 1)
|
||||
|
||||
gl.BindVertexArray(b.GLVertexArrayId)
|
||||
|
@ -118,10 +130,10 @@ func (b *Box) drawTriangles() {
|
|||
gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3) // 6 sides, 2 triangles, 3 points each
|
||||
}
|
||||
|
||||
func (b *Box) drawOutline() {
|
||||
func (b *Box) glDrawLineLoop() {
|
||||
gl_helpers.SetUniformInt(b.GLProgram, "lightsOn", 0)
|
||||
for _, face := range b.Faces {
|
||||
face.DrawLineLoop(b.GLProgram)
|
||||
face.GLDrawLineLoop(b.GLProgram)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -150,3 +162,31 @@ func (b *Box) ToggleWireframe() {
|
|||
log.Println("Wireframe toggled: ", b.Wireframe)
|
||||
b.Wireframe = !b.Wireframe
|
||||
}
|
||||
|
||||
// func (b *Box) SDLDraw(renderer *sdl.Renderer) {
|
||||
// rect := &sdl.Rect{
|
||||
// X: int32(b.Translation.X()),
|
||||
// Y: int32(b.Translation.Y()),
|
||||
// W: int32(b.Width),
|
||||
// H: int32(b.Height),
|
||||
// }
|
||||
|
||||
// // TODO sdl.Do()
|
||||
// renderer.DrawRect(rect)
|
||||
// }
|
||||
|
||||
func (b *Box) SetXVelocity(v float32) {
|
||||
b.Velocity[0] = v
|
||||
}
|
||||
|
||||
func (b *Box) SetYVelocity(v float32) {
|
||||
b.Velocity[1] = v
|
||||
}
|
||||
|
||||
func (b *Box) SetZVelocity(v float32) {
|
||||
b.Velocity[2] = v
|
||||
}
|
||||
|
||||
func (b *Box) SetYRotationVelocity(v float32) {
|
||||
b.RotationVelocity[1] = v
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ func (c *Camera) GetCenter() mgl32.Vec3 {
|
|||
return c.Position.Add(c.Direction)
|
||||
}
|
||||
|
||||
func (c *Camera) Draw() {
|
||||
func (c *Camera) GLDraw() {
|
||||
cameraMatrix := mgl32.LookAtV(c.Position, c.GetCenter(), c.Up)
|
||||
gl_helpers.SetUniformMatrix4f(c.GLProgram, "camera", &cameraMatrix)
|
||||
gl_helpers.SetUniformVec3f(c.GLProgram, "cameraPos", c.Position)
|
||||
|
|
|
@ -134,7 +134,7 @@ func (f *Face) GetLineLoopVertexArray() []float32 {
|
|||
return a
|
||||
}
|
||||
|
||||
func (f *Face) DrawLineLoop(glProgram uint32) {
|
||||
func (f *Face) GLDrawLineLoop(glProgram uint32) {
|
||||
gl.BindVertexArray(f.GLLineLoopVertexArrayId)
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, f.GLLineLoopVertexBufferId)
|
||||
|
||||
|
|
|
@ -2,6 +2,6 @@ package gl_objects
|
|||
|
||||
type GLObject interface {
|
||||
Update()
|
||||
Draw()
|
||||
GLDraw()
|
||||
ToggleWireframe()
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func NewMaterial() *Material {
|
|||
return &m
|
||||
}
|
||||
|
||||
func (m *Material) Draw(glProgram uint32) {
|
||||
func (m *Material) GLDraw(glProgram uint32) {
|
||||
gl_helpers.SetUniformInt(glProgram, "material.textureDiffuse", m.TextureDiffuse)
|
||||
gl_helpers.SetUniformInt(glProgram, "material.textureSpecular", m.TextureSpecular)
|
||||
|
||||
|
|
Loading…
Reference in New Issue