Beispiel #1
0
Datei: cube.go Projekt: aiju/gl
func main() {
	sdl.Init(sdl.INIT_VIDEO)
	sdl.SetVideoMode(800, 600, 32, sdl.OPENGL|sdl.DOUBLEBUF|sdl.HWSURFACE)
	gl.Init()
	gl.Enable(gl.DEPTH_TEST)
	gl.Viewport(0, 0, 800, 600)
	tick := time.Tick(time.Second / 50)
	timer := 0.0
	posbuf := gl.NewBuffer(gl.ARRAY_BUFFER, Vertices, gl.STATIC_DRAW)
	prog, err := gl.MakeProgram([]string{vertexShader}, []string{fragmentShader})
	if err != nil {
		fmt.Println(err)
		return
	}
	f, err := os.Open("glenda.png")
	if err != nil {
		fmt.Println(err)
		return
	}
	img, _, err := image.Decode(f)
	if err != nil {
		fmt.Println(err)
		return
	}
	tex := gl.NewTexture2D(img, 0)
	for {
		select {
		case ev := <-sdl.Events:
			if _, ok := ev.(sdl.QuitEvent); ok {
				return
			}
		case <-tick:
			gl.ClearColor(0, 0, 0, 1)
			gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

			prog.Use()
			mat := gl.Mul4(gl.Frustum(45, 800./600, 0.01, 100), gl.Translate(0, 0, -8), gl.RotX(timer), gl.RotY(2*timer), gl.RotZ(3*timer))
			prog.EnableAttrib("position", posbuf, 0, 3, 5, false)
			prog.EnableAttrib("texcoord", posbuf, 3, 2, 5, false)
			prog.SetUniform("tex", 0)
			prog.SetUniform("matrix", mat)
			tex.Enable(0, gl.TEXTURE_2D)
			gl.DrawArrays(gl.TRIANGLE_STRIP, 0, len(Vertices)/5)
			prog.DisableAttrib("position")
			prog.Unuse()

			sdl.GL_SwapBuffers()
			timer += 1
		}
	}
}
Beispiel #2
0
func AddTex(tex, file string) {
	f, err := os.Open("data/" + file)
	if err != nil {
		log.Fatal(err)
	}
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	t := gl.NewTexture2D(img, 0)
	t.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	t.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	texs[tex] = t
}
Beispiel #3
0
func StartGraphics(id uint32, update chan *State, move chan MoveMsg, quit chan bool) error {
	var err error

	rc := sdl.Init(sdl.INIT_VIDEO)
	if rc == -1 {
		return errors.New(sdl.GetError())
	}
	disp := sdl.SetVideoMode(800, 600, 32, sdl.OPENGL)
	if disp == nil {
		return errors.New(sdl.GetError())
	}
	winscale = gl.Mul4(gl.Translate(-1, 1, 0), gl.Scale(1./400, -1./300, 1))
	gl.Init()
	gl.ClearColor(1, 1, 1, 1)
	s := new(State)
	ls := time.Now()
	quadBuf = gl.NewBuffer(gl.ARRAY_BUFFER, quadData, gl.STATIC_DRAW)
	prog, err = gl.MakeProgram([]string{vertexShader}, []string{fragmentShader})
	if err != nil {
		log.Fatal(err)
	}
	AddTex("glenda", "glenda.png")
	AddTex("tux", "tux.png")
	i := image.NewRGBA(image.Rect(0, 0, 800, 600))
	texs["black"] = gl.NewTexture2D(i, 0)

	tick := time.Tick(time.Second / 50)
	squit := make(chan bool)
	go ProcessInput(move, quit, squit)
	for {
		select {
		case s = <-update:
			ls = time.Now()
		case <-tick:
			s.Advance(time.Now().Sub(ls))
			ls = time.Now()
			gl.Clear(gl.COLOR_BUFFER_BIT)
			RenderScene(s)
			sdl.GL_SwapBuffers()
		case <-squit:
			return nil
		}
	}
	return nil
}