Пример #1
0
Файл: texture.go Проект: dmac/gg
func NewSprite(vertices []float32, program *gg.Program, texture *gg.Texture) (*Sprite, error) {
	buf := new(bytes.Buffer)
	if err := binary.Write(buf, binary.LittleEndian, vertices); err != nil {
		return nil, err
	}
	pvbo := gg.CreateBuffer()
	gg.BindBuffer(gg.ARRAY_BUFFER, pvbo)
	gg.BufferData(gg.ARRAY_BUFFER, buf.Bytes(), gg.STATIC_DRAW)

	texVertices := []float32{
		0, 0,
		0, 1,
		1, 1,
		1, 0,
	}
	buf.Reset()
	if err := binary.Write(buf, binary.LittleEndian, texVertices); err != nil {
		return nil, err
	}
	tvbo := gg.CreateBuffer()
	gg.BindBuffer(gg.ARRAY_BUFFER, tvbo)
	gg.BufferData(gg.ARRAY_BUFFER, buf.Bytes(), gg.STATIC_DRAW)

	return &Sprite{
		pvbo:    pvbo,
		tvbo:    tvbo,
		program: program,
		tex:     texture,
	}, nil
}
Пример #2
0
Файл: texture.go Проект: 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)
}
Пример #3
0
func NewTriangle(vertices []float32, program *gg.Program) (*Triangle, error) {
	buf := new(bytes.Buffer)
	if err := binary.Write(buf, binary.LittleEndian, vertices); err != nil {
		return nil, err
	}
	vbo := gg.CreateBuffer()
	gg.BindBuffer(gg.ARRAY_BUFFER, vbo)
	gg.BufferData(gg.ARRAY_BUFFER, buf.Bytes(), gg.STATIC_DRAW)
	return &Triangle{
		vbo:     vbo,
		program: program,
	}, nil
}
Пример #4
0
Файл: tetris.go Проект: dmac/gg
func NewSprite(width, height float32, program *gg.Program, texture *gg.Texture) *Sprite {
	s := &Sprite{
		scale:   1,
		width:   width,
		height:  height,
		tex:     texture,
		program: program,
	}

	pvertices := []float32{
		0, 0, 0,
		0, s.height, 0,
		s.width, s.height, 0,
		s.width, 0, 0,
	}
	buf := new(bytes.Buffer)
	if err := binary.Write(buf, binary.LittleEndian, pvertices); err != nil {
		panic(err)
	}
	s.pbuf = gg.CreateBuffer()
	gg.BindBuffer(gg.ARRAY_BUFFER, s.pbuf)
	gg.BufferData(gg.ARRAY_BUFFER, buf.Bytes(), gg.STATIC_DRAW)

	tvertices := []float32{
		0, 0,
		0, 1,
		1, 1,
		1, 0,
	}
	buf.Reset()
	if err := binary.Write(buf, binary.LittleEndian, tvertices); err != nil {
		panic(err)
	}
	s.tbuf = gg.CreateBuffer()
	gg.BindBuffer(gg.ARRAY_BUFFER, s.tbuf)
	gg.BufferData(gg.ARRAY_BUFFER, buf.Bytes(), gg.STATIC_DRAW)
	return s
}
Пример #5
0
Файл: tetris.go Проект: 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
}
Пример #6
0
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)
}