Example #1
0
func (p *glParams) renderCreateTexture(texType nvgTextureType, w, h int, flags ImageFlags, data []byte) int {
	if nearestPow2(w) != w || nearestPow2(h) != h {
		if (flags&ImageRepeatX) != 0 || (flags&ImageRepeatY) != 0 {
			dumpLog("Repeat X/Y is not supported for non power-of-two textures (%d x %d)\n", w, h)
			flags &= ^(ImageRepeatY | ImageRepeatX)
		}
		if (flags & ImageGenerateMipmaps) != 0 {
			dumpLog("Mip-maps is not support for non power-of-two textures (%d x %d)\n", w, h)
			flags &= ^ImageGenerateMipmaps
		}
	}
	tex := p.context.allocTexture()
	tex.tex = gl.CreateTexture()
	tex.width = w
	tex.height = h
	tex.texType = texType
	tex.flags = flags

	p.context.bindTexture(&tex.tex)
	gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)

	if texType == nvgTextureRGBA {
		data = prepareTextureBuffer(data, w, h, 4)
		gl.TexImage2D(gl.TEXTURE_2D, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, data)
	} else {
		data = prepareTextureBuffer(data, w, h, 1)
		gl.TexImage2D(gl.TEXTURE_2D, 0, w, h, gl.LUMINANCE, gl.UNSIGNED_BYTE, data)
	}

	if (flags & ImageGenerateMipmaps) != 0 {
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
	} else {
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	}
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	if (flags & ImageRepeatX) != 0 {
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	} else {
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	}

	if (flags & ImageRepeatY) != 0 {
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	} else {
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	}

	gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)

	if (flags & ImageGenerateMipmaps) != 0 {
		gl.GenerateMipmap(gl.TEXTURE_2D)
	}

	p.context.checkError("create tex")
	p.context.bindTexture(&gl.Texture{})

	return tex.id
}
Example #2
0
func (t *texture) newContext() *textureContext {
	var fmt gl.Enum
	var data []byte
	var pma bool

	switch ty := t.image.(type) {
	case *image.RGBA:
		fmt = gl.RGBA
		data = ty.Pix
		pma = true
	case *image.NRGBA:
		fmt = gl.RGBA
		data = ty.Pix
	case *image.Alpha:
		fmt = gl.ALPHA
		data = ty.Pix
	default:
		panic("Unsupported image type")
	}

	texture := gl.CreateTexture()
	gl.BindTexture(gl.TEXTURE_2D, texture)
	w, h := t.SizePixels().WH()
	gl.TexImage2D(gl.TEXTURE_2D, 0, w, h, fmt, gl.UNSIGNED_BYTE, data)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.BindTexture(gl.TEXTURE_2D, gl.Texture{})
	checkError()

	globalStats.textureContextCount.inc()
	return &textureContext{
		texture:    texture,
		sizePixels: t.Size(),
		flipY:      t.flipY,
		pma:        pma,
	}
}