Пример #1
0
func (t *Texture) load(path string) int {
	index := t.lru()
	delete(t.lookup, t.reverse[index])
	t.mark(index)
	t.lookup[path] = index
	t.reverse[index] = path
	x := int32((index % textureDim) * 256)
	y := int32((index / textureDim) * 256)
	im := copyImage(t.loadThumbnail(path))
	size := im.Rect.Size()
	gl.TexSubImage2D(
		gl.TEXTURE_2D, 0, x, y, int32(size.X), int32(size.Y),
		gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(im.Pix))
	return index
}
Пример #2
0
// TexSubImage2D writes a subregion of a 2D texture image.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glTexSubImage2D.xhtml
func TexSubImage2D(target Enum, level int, x, y, width, height int, format, ty Enum, data unsafe.Pointer) {
	gl.TexSubImage2D(uint32(target), int32(level), int32(x), int32(y), int32(width), int32(height), uint32(format), uint32(ty), data)
}
Пример #3
0
// TexSubImage2D writes a subregion of a 2D texture image.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glTexSubImage2D.xhtml
func TexSubImage2D(target Enum, level int, x, y, width, height int, format, ty Enum, data []byte) {
	gl.TexSubImage2D(uint32(target), int32(level), int32(x), int32(y), int32(width), int32(height), uint32(format), uint32(ty), gl.Ptr(&data[0]))
}
Пример #4
0
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
	gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
}
Пример #5
0
func (stash *Stash) GetGlyph(fnt *Font, codepoint int, isize int16) *Glyph {
	size := float64(isize) / 10

	// Find code point and size.
	h := hashint(uint(codepoint)) & (HASH_LUT_SIZE - 1)
	for i := fnt.lut[h]; i != -1; i = fnt.glyphs[i].next {
		if fnt.glyphs[i].codepoint == codepoint && (fnt.fType == BMFONT || fnt.glyphs[i].size == isize) {
			return fnt.glyphs[i]
		}
	}
	// Could not find glyph.

	// For bitmap fonts: ignore this glyph.
	if fnt.fType == BMFONT {
		return nil
	}

	// For truetype fonts: create this glyph.
	scale := fnt.font.ScaleForPixelHeight(size)
	g := fnt.font.FindGlyphIndex(codepoint)
	if g == 0 {
		// glyph not found
		return nil
	}
	advance, _ := fnt.font.GetGlyphHMetrics(g)
	x0, y0, x1, y1 := fnt.font.GetGlyphBitmapBox(g, scale, scale)
	gw := x1 - x0
	gh := y1 - y0

	// Check if glyph is larger than maximum texture size
	if gw >= stash.tw || gh >= stash.th {
		return nil
	}

	// Find texture and row where the glyph can be fit.
	rh := (int16(gh) + 7) & ^7
	var tt int
	texture := stash.ttTextures[tt]
	var br *Row
	for br == nil {
		for i := range texture.rows {
			if texture.rows[i].h == rh && int(texture.rows[i].x)+gw+1 <= stash.tw {
				br = texture.rows[i]
			}
		}

		// If no row is found, there are 3 possibilities:
		//  - add new row
		//  - try next texture
		//  - create new texture
		if br == nil {
			var py int16
			// Check that there is enough space.
			if len(texture.rows) > 0 {
				py = texture.rows[len(texture.rows)-1].y + texture.rows[len(texture.rows)-1].h + 1
				if int(py+rh) > stash.th {
					if tt < len(stash.ttTextures)-1 {
						tt++
						texture = stash.ttTextures[tt]
					} else {
						// Create new texture
						gl.Enable(gl.TEXTURE_2D)
						texture = &Texture{}
						gl.GenTextures(1, &texture.id)
						gl.BindTexture(gl.TEXTURE_2D, texture.id)
						gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA,
							int32(stash.tw), int32(stash.th), 0,
							gl.ALPHA, gl.UNSIGNED_BYTE,
							unsafe.Pointer(&stash.emptyData[0]))
						gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
						gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
						gl.Disable(gl.TEXTURE_2D)
						stash.ttTextures = append(stash.ttTextures, texture)
					}
					continue
				}
			}
			// Init and add row
			br = &Row{
				x: 0,
				y: py,
				h: rh,
			}
			texture.rows = append(texture.rows, br)
		}
	}

	// Init glyph.
	glyph := &Glyph{
		codepoint: codepoint,
		size:      isize,
		texture:   texture,
		x0:        int(br.x),
		y0:        int(br.y),
		x1:        int(br.x) + gw,
		y1:        int(br.y) + gh,
		xadv:      scale * float64(advance),
		xoff:      float64(x0),
		yoff:      float64(y0),
		next:      0,
	}
	fnt.glyphs = append(fnt.glyphs, glyph)

	// Advance row location.
	br.x += int16(gw) + 1

	// Insert char to hash lookup.
	glyph.next = fnt.lut[h]
	fnt.lut[h] = len(fnt.glyphs) - 1

	// Rasterize
	bmp := make([]byte, gw*gh)
	bmp = fnt.font.MakeGlyphBitmap(bmp, gw, gh, gw, scale, scale, g)
	if len(bmp) > 0 {
		gl.Enable(gl.TEXTURE_2D)
		// Update texture
		gl.BindTexture(gl.TEXTURE_2D, texture.id)
		gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
		gl.TexSubImage2D(gl.TEXTURE_2D, 0, int32(glyph.x0), int32(glyph.y0),
			int32(gw), int32(gh), gl.ALPHA, gl.UNSIGNED_BYTE,
			unsafe.Pointer(&bmp[0]))
		gl.Disable(gl.TEXTURE_2D)
	}

	return glyph
}
Пример #6
0
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
	_ = c.runOnContextThread(func() error {
		gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
		return nil
	})
}