Exemplo n.º 1
0
func MakeTextureFromImageFile(filePath string) gl.Uint {
	var file, err = os.Open(filePath)
	var img image.Image
	var tex gl.Uint
	if err != nil {
		panic(err)
	}
	defer file.Close()
	img, _, err = image.Decode(file)
	if err != nil {
		panic(err)
	}
	w, h := img.Bounds().Dx(), img.Bounds().Dy()
	sw, sh := gl.Sizei(w), gl.Sizei(h)
	rgba := image.NewRGBA(image.Rect(0, 0, w, h))
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			rgba.Set(x, y, img.At(x, h-1-y))
		}
	}
	gl.GenTextures(1, &tex)
	gl.BindTexture(gl.TEXTURE_2D, tex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexStorage2D(gl.TEXTURE_2D, 1, gl.RGBA8, sw, sh)
	gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, sw, sh, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&rgba.Pix[0]))
	gl.BindTexture(gl.TEXTURE_2D, 0)
	PanicIfErrors("MakeTextureFromImageFile(%v)", filePath)
	return tex
}
Exemplo n.º 2
0
func MakeTextureFromImageFloatsFile(filePath string, w, h int) gl.Uint {
	var file, err = os.Open(filePath)
	var tex gl.Uint
	var pix = make([]gl.Float, w*h*3)
	var fVal float32
	var raw []uint8
	var buf *bytes.Buffer
	var i int
	if err != nil {
		panic(err)
	}
	defer file.Close()
	raw, err = ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}
	buf = bytes.NewBuffer(raw)
	for i = 0; (err == nil) && (i < len(pix)); i++ {
		if err = binary.Read(buf, binary.LittleEndian, &fVal); err == io.EOF {
			err = nil
			break
		} else if err == nil {
			pix[i] = gl.Float(fVal)
		}
	}
	if err != nil {
		panic(err)
	}
	sw, sh := gl.Sizei(w), gl.Sizei(h)
	gl.GenTextures(1, &tex)
	gl.BindTexture(gl.TEXTURE_2D, tex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexStorage2D(gl.TEXTURE_2D, 1, gl.RGB16F, sw, sh)
	gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, sw, sh, gl.RGB, gl.FLOAT, gl.Pointer(&pix[0]))
	gl.BindTexture(gl.TEXTURE_2D, 0)
	PanicIfErrors("MakeTextureFromImageFloatsFile(%v)", filePath)
	return tex
}