Example #1
0
File: texture.go Project: dmac/gg
func (s *Sprite) Draw() {
	gg.UseProgram(s.program)

	vattrib, err := gg.GetAttribLocation(s.program, "vertex_position")
	if err != nil {
		log.Fatal(err)
	}
	gg.EnableVertexAttribArray(vattrib)
	gg.BindBuffer(gg.ARRAY_BUFFER, s.pvbo)
	gg.VertexAttribPointer(vattrib, 3, gg.FLOAT, false, 0, 0)

	tattrib, err := gg.GetAttribLocation(s.program, "vertex_texture")
	if err != nil {
		log.Fatal(err)
	}
	gg.EnableVertexAttribArray(tattrib)
	gg.BindBuffer(gg.ARRAY_BUFFER, s.tvbo)
	gg.VertexAttribPointer(tattrib, 2, gg.FLOAT, false, 0, 0)

	gg.ActiveTexture(gg.TEXTURE0)
	gg.BindTexture(gg.TEXTURE_2D, s.tex)
	texUniform, err := gg.GetUniformLocation(s.program, "tex_loc")
	if err != nil {
		log.Fatal(err)
	}
	gg.Uniform1i(texUniform, 0)

	gg.DrawArrays(gg.TRIANGLE_FAN, 0, 4)
}
Example #2
0
File: tetris.go Project: dmac/gg
func NewTetris(vertShader, fragShader string) (*Tetris, error) {
	gg.Enable(gg.BLEND)
	gg.BlendFunc(gg.SRC_ALPHA, gg.ONE_MINUS_SRC_ALPHA)

	// Compile the global shader program
	vshader, err := gg.CreateShader([]byte(vertShader), gg.VERTEX_SHADER)
	if err != nil {
		return nil, err
	}
	fshader, err := gg.CreateShader([]byte(fragShader), gg.FRAGMENT_SHADER)
	if err != nil {
		return nil, err
	}
	program := gg.CreateProgram()
	gg.AttachShader(program, vshader)
	gg.AttachShader(program, fshader)
	if err := gg.LinkProgram(program); err != nil {
		return nil, err
	}

	// Set the global projection matrix
	gg.UseProgram(program)
	proj := mgl.Ortho(0, float32(WindowWidth), float32(WindowHeight), 0, 0, 1)
	projUniform, err := gg.GetUniformLocation(program, "proj")
	if err != nil {
		return nil, err
	}
	gg.UniformMatrix4fv(projUniform, proj[:])

	tetris := &Tetris{}

	tetris.textures, err = LoadTextures()
	if err != nil {
		return nil, err
	}

	tetris.bg = NewSprite(WindowWidth, WindowHeight, program, tetris.textures["bg"])
	tetris.board = NewBoard(WidthCells, HeightCells, program, tetris.textures)
	tetris.board.x = Padding
	tetris.board.y = Padding

	rand.Seed(time.Now().Unix())
	tetris.board.current = tetris.board.NewRandomPiece()

	go func() {
		t := time.NewTicker(time.Second)
		for range t.C {
			tetris.mu.Lock()
			if tetris.gameOver {
				return
			}
			tetris.mu.Unlock()
			tetris.HandleInput(inputDown)
		}
	}()

	return tetris, nil
}
Example #3
0
File: tetris.go Project: dmac/gg
func (s *Sprite) Draw() error {
	gg.UseProgram(s.program)

	model := s.transform()
	modelUniform, err := gg.GetUniformLocation(s.program, "model")
	if err != nil {
		return err
	}
	gg.UniformMatrix4fv(modelUniform, model[:])

	gg.ActiveTexture(gg.TEXTURE0)
	gg.BindTexture(gg.TEXTURE_2D, s.tex)
	textureUniform, err := gg.GetUniformLocation(s.program, "tex_loc")
	if err != nil {
		return err
	}
	gg.Uniform1i(textureUniform, 0)

	vattrib, err := gg.GetAttribLocation(s.program, "vertex_position")
	if err != nil {
		return err
	}
	gg.EnableVertexAttribArray(vattrib)
	gg.BindBuffer(gg.ARRAY_BUFFER, s.pbuf)
	gg.VertexAttribPointer(vattrib, 3, gg.FLOAT, false, 0, 0)

	tattrib, err := gg.GetAttribLocation(s.program, "vertex_texture")
	if err != nil {
		return err
	}
	gg.EnableVertexAttribArray(tattrib)
	gg.BindBuffer(gg.ARRAY_BUFFER, s.tbuf)
	gg.VertexAttribPointer(tattrib, 2, gg.FLOAT, false, 0, 0)

	gg.DrawArrays(gg.TRIANGLE_FAN, 0, 4)
	return nil
}
Example #4
0
File: texture.go Project: dmac/gg
func NewScene(vertShader, fragShader string, texture *gg.Texture) (*Scene, error) {
	gg.Enable(gg.DEPTH_TEST)
	gg.Enable(gg.CULL_FACE)
	gg.DepthFunc(gg.LESS)

	gg.Enable(gg.BLEND)
	gg.BlendFunc(gg.SRC_ALPHA, gg.ONE_MINUS_SRC_ALPHA)

	// Compile the global shader program
	vshader, err := gg.CreateShader([]byte(vertShader), gg.VERTEX_SHADER)
	if err != nil {
		return nil, err
	}
	fshader, err := gg.CreateShader([]byte(fragShader), gg.FRAGMENT_SHADER)
	if err != nil {
		return nil, err
	}
	program := gg.CreateProgram()
	gg.AttachShader(program, vshader)
	gg.AttachShader(program, fshader)
	if err := gg.LinkProgram(program); err != nil {
		return nil, err
	}

	// Set the global projection matrix
	gg.UseProgram(program)
	proj := mgl.Ortho(0, float32(WindowWidth), float32(WindowHeight), 0, 0, 1)
	projUniform, err := gg.GetUniformLocation(program, "proj")
	if err != nil {
		return nil, err
	}
	gg.UniformMatrix4fv(projUniform, proj[:])

	vertices := []float32{
		float32(WindowWidth)/2 - 50, float32(WindowHeight)/2 - 50, 0,
		float32(WindowWidth)/2 - 50, float32(WindowHeight)/2 + 50, 0,
		float32(WindowWidth)/2 + 50, float32(WindowHeight)/2 + 50, 0,
		float32(WindowWidth)/2 + 50, float32(WindowHeight)/2 - 50, 0,
	}

	// Initialize sprite
	sprite, err := NewSprite(vertices, program, texture)
	if err != nil {
		return nil, err
	}

	return &Scene{sprite: sprite}, nil
}
Example #5
0
File: triangle.go Project: dmac/gg
func (t *Triangle) Draw() {
	gg.UseProgram(t.program)
	colorUniform, err := gg.GetUniformLocation(t.program, "color")
	if err != nil {
		log.Fatal(err)
	}
	gg.Uniform4f(colorUniform, 1.0, 0.0, 1.0, 1.0)

	vattrib, err := gg.GetAttribLocation(t.program, "vertex_position")
	if err != nil {
		log.Fatal(err)
	}
	gg.EnableVertexAttribArray(vattrib)
	gg.BindBuffer(gg.ARRAY_BUFFER, t.vbo)
	gg.VertexAttribPointer(vattrib, 3, gg.FLOAT, false, 0, 0)
	gg.DrawArrays(gg.TRIANGLE_FAN, 0, 3)
}