Skip to content

Instantly share code, notes, and snippets.

@vladimir-vg
Created September 2, 2012 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimir-vg/3605519 to your computer and use it in GitHub Desktop.
Save vladimir-vg/3605519 to your computer and use it in GitHub Desktop.
package main
type vector struct {
x, y, z float64
}
package main
import "github.com/banthar/gl"
import "github.com/banthar/glu"
type camera struct {
position vector
target vector
up vector
}
func (self *camera) use() {
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
glu.LookAt(
self.position.x, self.position.y, self.position.z,
self.target.x, self.target.y, self.target.z,
self.up.x, self.up.y, self.up.z)
}
package main
import "github.com/banthar/Go-SDL/sdl"
import "github.com/banthar/gl"
import "github.com/banthar/glu"
import "runtime"
import "time"
const screenWidth = 500
const screenHeight = 500
func setupVideo() {
sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
flags := uint32(sdl.OPENGL | sdl.GL_DOUBLEBUFFER)
screen := sdl.SetVideoMode(screenWidth, screenHeight, 24, flags)
if screen == nil {
panic(sdl.GetError())
}
if gl.Init() != 0 {
panic("Could not initialize OpenGL")
}
}
func setupViewport() {
gl.Viewport(0, 0, screenWidth, screenHeight)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
glu.Perspective(45, screenWidth/screenHeight, 0.1, 100)
// gl.Ortho(0, screenWidth, screenHeight, 0, 1, -1)
}
func mainLoop(drawFunc func(), eventFunc func(sdl.Event)) {
drawTicker := time.NewTicker(time.Second / 60)
eventTicker := time.NewTicker(time.Second / 100)
loop:
for {
select {
case <-drawTicker.C:
drawFunc()
case <-eventTicker.C:
for {
if event := sdl.PollEvent(); event != nil {
eventFunc(event)
switch event.(type) {
case *sdl.QuitEvent:
break loop
}
}
}
}
}
}
// this is a comment
func main() {
// needed for proper working with C-bindings.
// probably this line could be removed,
// when bindings package updates.
runtime.LockOSThread()
if sdl.Init(sdl.INIT_VIDEO) != 0 {
panic(sdl.GetError())
}
defer sdl.Quit()
setupVideo()
setupViewport()
sdl.WM_SetCaption("Test", "Moped")
cam := camera{}
cam.position = vector{x:10, y: 10, z: 5}
cam.target = vector{x:0, y: 0, z: 0}
cam.up = vector{x: -1, y: -1, z: 1}
cam.use()
gl.ClearColor(0, 0, 0, 0)
drawFunc := func() {
gl.Clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT)
gl.Color4f(1, 1, 1, 1)
gl.Begin(gl.QUADS)
gl.Vertex3d(-20, 20, 0)
gl.Vertex3d(20, 20, 0)
gl.Vertex3d(20, -20, 0)
gl.Vertex3d(-20, -20, 0)
gl.End()
drawAxis()
sdl.GL_SwapBuffers()
}
eventFunc := func(event sdl.Event) {
}
mainLoop(drawFunc, eventFunc)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment