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
// TODO(dmac) Move into gg helpers package
func newImageTexture(path string) *gg.Texture {
	img := js.Global.Get("Image").New()
	img.Set("src", path)
	img.Set("crossOrigin", "")
	c := make(chan struct{})
	img.Call("addEventListener", "load", func() { close(c) }, false)
	<-c

	tex := gg.CreateTexture()
	gg.ActiveTexture(gg.TEXTURE0)
	gg.BindTexture(gg.TEXTURE_2D, tex)
	gg.TexImage2D(
		gg.TEXTURE_2D,
		0,
		gg.RGBA,
		-1, -1, -1, // These args are ignored in the webgl backend.
		gg.RGBA,
		gg.UNSIGNED_BYTE,
		img,
	)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_WRAP_S, gg.CLAMP_TO_EDGE)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_WRAP_T, gg.CLAMP_TO_EDGE)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_MAG_FILTER, gg.LINEAR)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_MIN_FILTER, gg.LINEAR)

	return tex
}
Example #3
0
func newImageTexture(filename string) (*gg.Texture, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	img, err := png.Decode(f)
	if err != nil {
		return nil, err
	}

	var buf []byte
	for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
		for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
			r, g, b, a := img.At(x, y).RGBA()
			buf = append(buf, byte(r/256))
			buf = append(buf, byte(g/256))
			buf = append(buf, byte(b/256))
			buf = append(buf, byte(a/256))
		}
	}
	tex := gg.CreateTexture()
	gg.ActiveTexture(gg.TEXTURE0)
	gg.Enable(gg.TEXTURE_2D)
	gg.BindTexture(gg.TEXTURE_2D, tex)
	gg.TexImage2D(
		gg.TEXTURE_2D,
		0,
		gg.RGBA,
		img.Bounds().Dx(),
		img.Bounds().Dy(),
		0,
		gg.RGBA,
		gg.UNSIGNED_BYTE,
		buf,
	)

	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_WRAP_S, gg.CLAMP_TO_EDGE)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_WRAP_T, gg.CLAMP_TO_EDGE)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_MAG_FILTER, gg.LINEAR)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_MIN_FILTER, gg.LINEAR)

	return tex, nil
}
Example #4
0
File: tetris.js.go Project: dmac/gg
func NewTextureFromImage(img *js.Object) *gg.Texture {
	tex := gg.CreateTexture()
	gg.ActiveTexture(gg.TEXTURE0)
	gg.BindTexture(gg.TEXTURE_2D, tex)
	gg.TexImage2D(
		gg.TEXTURE_2D,
		0,
		gg.RGBA,
		-1, -1, -1, // ignored by gg/webgl
		gg.RGBA,
		gg.UNSIGNED_BYTE,
		img,
	)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_WRAP_S, gg.CLAMP_TO_EDGE)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_WRAP_T, gg.CLAMP_TO_EDGE)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_MAG_FILTER, gg.LINEAR)
	gg.TexParameteri(gg.TEXTURE_2D, gg.TEXTURE_MIN_FILTER, gg.LINEAR)

	return tex
}
Example #5
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
}