// TexImage2D writes a 2D texture image. // // http://www.khronos.org/opengles/sdk/docs/man3/html/glTexImage2D.xhtml func TexImage2D(target Enum, level int, width, height int, format Enum, ty Enum, data []byte) { p := unsafe.Pointer(nil) if len(data) > 0 { p = gl.Ptr(&data[0]) } gl.TexImage2D(uint32(target), int32(level), int32(format), int32(width), int32(height), 0, uint32(format), uint32(ty), p) }
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) { var texture Texture if err := c.runOnContextThread(func() error { var t uint32 gl.GenTextures(1, &t) // TODO: Use gl.IsTexture if t <= 0 { return errors.New("opengl: creating texture failed") } gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) texture = Texture(t) return nil }); err != nil { return 0, err } if err := c.BindTexture(texture); err != nil { return 0, err } if err := c.runOnContextThread(func() error { gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(filter)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(filter)) var p interface{} if pixels != nil { p = pixels } gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p)) return nil }); err != nil { return 0, err } return texture, nil }
func newChipset(file string, tilesize int) chipset { imgFile, err := os.Open(file) if err != nil { log.Fatalf("texture %q not found on disk: %v\n", file, err) } img, _, err := image.Decode(imgFile) if err != nil { panic(err) } rgba := image.NewRGBA(img.Bounds()) draw.Draw(rgba, rgba.Bounds(), img, image.ZP, draw.Src) var texture uint32 gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix)) return chipset{width: rgba.Bounds().Dx(), height: rgba.Bounds().Dy(), handle: texture, tilesize: tilesize} }
func LataaKuvat() { file, err := os.Open("tiles.png") if err != nil { panic("En saa avattua kuvatiedostoa: " + err.Error()) } kuva, _, err := image.Decode(file) if err != nil { panic("Kuva on rikki: " + err.Error()) } var texture uint32 gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexImage2D(gl.TEXTURE_2D, 0, 4, int32(kuva.Bounds().Dx()), int32(kuva.Bounds().Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&kuva.(*image.RGBA).Pix[0]), ) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.Enable(gl.TEXTURE_2D) }
func loadTexture(fileName string) (uint32, error) { imgFile, err := os.Open("./res/textures/" + fileName) if err != nil { return 0, err } imgCfg, _, err := image.DecodeConfig(imgFile) if err != nil { return 0, err } _, err = imgFile.Seek(0, 0) if err != nil { return 0, err } w, h := int32(imgCfg.Width), int32(imgCfg.Height) img, _, err := image.Decode(imgFile) if err != nil { return 0, err } buffer := make([]byte, w*h*4) index := 0 for y := 0; y < int(h); y++ { for x := 0; x < int(w); x++ { pixel := img.At(x, y).(color.NRGBA) buffer[index] = pixel.R buffer[index+1] = pixel.G buffer[index+2] = pixel.B buffer[index+3] = pixel.A index += 4 } } var texture uint32 gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA8, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(buffer)) return texture, nil }
func (ctx *DrawContext) createHudTexture(o *orrery.Orrery, frametime time.Duration) (uint32, [2]int, error) { lines := []string{} if o.Paused { lines = append(lines, "PAUSED") } if ctx.verbose { lines = append(lines, []string{ "WASD: Move, 1: Toggle wireframe, H: Toggle HUD verbosity, Q: Quit", "Mouse Wheel: Move fast, Mouse Btn #1: Spawn particle, V: Spawn 10 particles", "Space: Reset camera, P: Toggle pause", }...) } lines = append(lines, []string{ fmt.Sprintf(` α: %0.2f θ: %0.2f`, ctx.cam.alpha, ctx.cam.theta), fmt.Sprintf(` x: %0.2f y: %0.2f z: %0.2f`, ctx.cam.Pos.X, ctx.cam.Pos.Y, ctx.cam.Pos.Z), fmt.Sprintf(` Last frame time: %s`, frametime), }...) if ctx.verbose { particles := o.Particles() lines = append(lines, fmt.Sprintf(`#P: %d`, len(particles))) for i, p := range particles { p.L.Lock() l := fmt.Sprintf(` π %d: %s`, i, p) p.L.Unlock() lines = append(lines, l) } } var txt uint32 gl.GenTextures(1, &txt) gl.BindTexture(gl.TEXTURE_2D, txt) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_BASE_LEVEL, 0) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAX_LEVEL, 0) bg := color.RGBA{0, 0, 0, 0} fg := color.RGBA{0, 255, 255, 255} img, err := ctx.txt.RenderMultiline(lines, 12.5, bg, fg) if err != nil { return 0, [2]int{0, 0}, err } v := reflect.ValueOf(img.Pix) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(img.Bounds().Dx()), int32(img.Bounds().Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(v.Index(0).UnsafeAddr())) return txt, [2]int{img.Bounds().Dx(), img.Bounds().Dy()}, nil }
func main() { runtime.LockOSThread() if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(800, 600, "fontstash example", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() glfw.SwapInterval(1) gl.Init() data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf")) if err != nil { panic(err) } gl.Enable(gl.TEXTURE_2D) tmpBitmap := make([]byte, 512*512) cdata, err, _, tmpBitmap := truetype.BakeFontBitmap(data, 0, 32, tmpBitmap, 512, 512, 32, 96) var ftex uint32 gl.GenTextures(1, &ftex) gl.BindTexture(gl.TEXTURE_2D, ftex) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, 512, 512, 0, gl.ALPHA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tmpBitmap[0])) gl.ClearColor(0.3, 0.3, 0.32, 1.) for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 800, 600, 0, 0, 1) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Disable(gl.DEPTH_TEST) gl.Color4ub(255, 255, 255, 255) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) my_print(100, 100, "The quick brown fox jumps over the fence", ftex, cdata) window.SwapBuffers() glfw.PollEvents() } }
func (*backend) TexImage2D( target gg.Enum, level int, internalFormat gg.Enum, width, height, border int, format, typ gg.Enum, data interface{}, ) { gl.TexImage2D( gl.TEXTURE_2D, int32(level), gl.RGBA, int32(width), int32(height), int32(border), uint32(format), uint32(typ), gl.Ptr(data), ) }
func NewTexture() *Texture { texture := createTexture() gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, textureSize, textureSize, 0, gl.RGBA, gl.UNSIGNED_BYTE, nil) gl.BindTexture(gl.TEXTURE_2D, 0) t := Texture{} t.texture = texture t.lookup = make(map[string]int) t.ch = make(chan string, 1024) return &t }
func LoadTexture(path string) { //fmt.Printf("Trying to load texture %q: ", path) // Open the file file, err := os.Open(path) if err != nil { fmt.Println(os.Getwd()) log.Fatal(err) } defer file.Close() // Decode the image img, _, err := image.Decode(file) if err != nil { log.Fatal(err) } bounds := img.Bounds() //fmt.Printf("Loaded %vx%v texture.\n", bounds.Dx(), bounds.Dy()) var format int var pixPointer *uint8 switch img := img.(type) { case *image.RGBA: format = gl.RGBA pixPointer = &img.Pix[0] case *image.NRGBA: format = gl.RGBA pixPointer = &img.Pix[0] case *image.Gray: format = gl.ALPHA pixPointer = &img.Pix[0] default: log.Fatalf("LoadTexture: Unsupported type %T.\n", img) } var texture uint32 gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.GENERATE_MIPMAP, gl.TRUE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, -0.5) gl.TexImage2D(gl.TEXTURE_2D, 0, int32(format), int32(bounds.Dx()), int32(bounds.Dy()), 0, uint32(format), gl.UNSIGNED_BYTE, gl.Ptr(pixPointer)) CheckGLError() }
func newTexture(rgba image.RGBA) *uint32 { var texture1 uint32 gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &texture1) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix)) return &texture1 }
func (atlas *FontAtlas) draw(rendered *image.RGBA, b Bounds) { var texture uint32 gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rendered.Bounds().Dx()), int32(rendered.Bounds().Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rendered.Pix)) gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) gl.Begin(gl.QUADS) { gl.TexCoord2f(0, 0) gl.Vertex2f(b.Min.X, b.Min.Y) gl.TexCoord2f(1, 0) gl.Vertex2f(b.Max.X, b.Min.Y) gl.TexCoord2f(1, 1) gl.Vertex2f(b.Max.X, b.Max.Y) gl.TexCoord2f(0, 1) gl.Vertex2f(b.Min.X, b.Max.Y) } gl.End() gl.Disable(gl.BLEND) gl.DeleteTextures(1, &texture) gl.Disable(gl.TEXTURE_2D) }
func NewGLImageFromImage(img image.Image) (*glImage, error) { var rgba *image.RGBA if asRGBA, ok := img.(*image.RGBA); ok { rgba = asRGBA } else { rgba = image.NewRGBA(img.Bounds()) if rgba.Stride != rgba.Rect.Size().X*4 { return nil, errors.New("unsupported stride") } draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src) } var tex uint32 gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &tex) gl.BindTexture(gl.TEXTURE_2D, tex) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Bounds().Dx()), int32(rgba.Bounds().Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix), ) return &glImage{ tex, img.Bounds().Dx(), img.Bounds().Dy(), 0.0, 0.0, 1.0, 1.0, }, nil }
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) { var t uint32 gl.GenTextures(1, &t) if t < 0 { return 0, errors.New("glGenTexture failed") } gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.BindTexture(gl.TEXTURE_2D, t) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(filter)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(filter)) var p interface{} if pixels != nil { p = pixels } gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p)) return Texture(t), nil }
func (c *Context) TexImage2D(target, level, internalFormat, format, kind int, data interface{}) { var pix []uint8 width := 0 height := 0 if data == nil { pix = nil } else { switch img := data.(type) { case *image.NRGBA: width = img.Bounds().Dx() height = img.Bounds().Dy() pix = img.Pix case *image.RGBA: width = img.Bounds().Dx() height = img.Bounds().Dy() pix = img.Pix default: panic(fmt.Errorf("Image type unsupported: %T", img)) } } gl.TexImage2D(uint32(target), int32(level), int32(internalFormat), int32(width), int32(height), int32(0), uint32(format), uint32(kind), gl.Ptr(pix)) }
func NewTexture(img *image.RGBA, opts ...TextureOption) (*Texture, error) { if img.Stride != img.Rect.Size().X*4 { return nil, fmt.Errorf("unsupported stride in texture image") } opt := textureOption{ filterMin: LINEAR, filterMag: LINEAR, wrap_s: REPEAT, wrap_t: REPEAT, } for _, o := range opts { o(&opt) } var id uint32 gl.GenTextures(1, &id) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, id) defer gl.BindTexture(gl.TEXTURE_2D, 0) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(opt.filterMin)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(opt.filterMag)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, int32(opt.wrap_s)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, int32(opt.wrap_t)) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(img.Rect.Size().X), int32(img.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(img.Pix)) return &Texture{ id: id, size: img.Rect.Size(), }, nil }
func (atlas *FontAtlas) upload() { if !atlas.Dirty { return } atlas.Dirty = false gl.Enable(gl.TEXTURE_2D) if atlas.Texture != 0 { gl.DeleteTextures(1, &atlas.Texture) atlas.Texture = 0 } gl.GenTextures(1, &atlas.Texture) gl.BindTexture(gl.TEXTURE_2D, atlas.Texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(atlas.Image.Rect.Size().X), int32(atlas.Image.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(atlas.Image.Pix)) if err := gl.GetError(); err != 0 { log.Println(err) } gl.Disable(gl.TEXTURE_2D) }
func newTexture(file string) uint32 { imgFile, err := os.Open(file) if err != nil { panic(err) } img, _, err := image.Decode(imgFile) if err != nil { panic(err) } rgba := image.NewRGBA(img.Bounds()) if rgba.Stride != rgba.Rect.Size().X*4 { panic("unsupported stride") } draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src) var texture uint32 gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix)) return texture }
func New(cachew, cacheh int) *Stash { stash := &Stash{} // Create data for clearing the textures stash.emptyData = make([]byte, cachew*cacheh) // Create first texture for the cache stash.tw = cachew stash.th = cacheh stash.itw = 1 / float64(cachew) stash.ith = 1 / float64(cacheh) gl.Enable(gl.TEXTURE_2D) stash.ttTextures = make([]*Texture, 1) stash.ttTextures[0] = &Texture{} gl.GenTextures(1, &stash.ttTextures[0].id) gl.BindTexture(gl.TEXTURE_2D, stash.ttTextures[0].id) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, int32(cachew), int32(cacheh), 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) return stash }
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 }
func (v *Video) Render() { for running { select { case buf := <-v.videoTick: gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(v.prog) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(v.texture, gl.TEXTURE_2D) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 240, 224, 0, gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, gl.Ptr(buf)) gl.DrawArrays(gl.TRIANGLES, 0, 6) if v.screen != nil { sdl.GL_SwapBuffers() v.fpsmanager.FramerateDelay() } case ev := <-sdl.Events: switch e := ev.(type) { case sdl.ResizeEvent: v.ResizeEvent(e.W, e.H) case sdl.QuitEvent: os.Exit(0) case sdl.KeyboardEvent: switch e.Keysym.Sym { case sdl.K_ESCAPE: running = false case sdl.K_r: // Trigger reset interrupt if e.Type == sdl.KEYDOWN { // cpu.RequestInterrupt(InterruptReset) } case sdl.K_l: if e.Type == sdl.KEYDOWN { nes.LoadGameState() } case sdl.K_s: if e.Type == sdl.KEYDOWN { nes.SaveGameState() } case sdl.K_i: if e.Type == sdl.KEYDOWN { nes.AudioEnabled = !nes.AudioEnabled } case sdl.K_p: if e.Type == sdl.KEYDOWN { nes.TogglePause() } case sdl.K_d: if e.Type == sdl.KEYDOWN { jsHandler.ReloadFile(debugfile) } case sdl.K_m: if e.Type == sdl.KEYDOWN { nes.Handler.Handle("debug-mode") } case sdl.K_BACKSLASH: if e.Type == sdl.KEYDOWN { nes.Pause() nes.StepFrame() } case sdl.K_1: if e.Type == sdl.KEYDOWN { v.ResizeEvent(256, 240) } case sdl.K_2: if e.Type == sdl.KEYDOWN { v.ResizeEvent(512, 480) } case sdl.K_3: if e.Type == sdl.KEYDOWN { v.ResizeEvent(768, 720) } case sdl.K_4: if e.Type == sdl.KEYDOWN { v.ResizeEvent(1024, 960) } } switch e.Type { case sdl.KEYDOWN: nes.Pads[0].KeyDown(e, 0) case sdl.KEYUP: nes.Pads[0].KeyUp(e, 0) } } } } }
func setTexture(im *image.RGBA) { size := im.Rect.Size() gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(size.X), int32(size.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(im.Pix)) }
// TexImage2D writes a 2D texture image. // // http://www.khronos.org/opengles/sdk/docs/man3/html/glTexImage2D.xhtml func TexImage2D(target Enum, level int, width, height int, format Enum, ty Enum, p unsafe.Pointer) { gl.TexImage2D(uint32(target), int32(level), int32(format), int32(width), int32(height), 0, uint32(format), uint32(ty), p) }
// loadFont loads the given font data. This does not deal with font scaling. // Scaling should be handled by the independent Bitmap/Truetype loaders. // We therefore expect the supplied image and charset to already be adjusted // to the correct font scale. // // The image should hold a sprite sheet, defining the graphical layout for // every glyph. The config describes font metadata. func loadFont(img *image.RGBA, config *FontConfig) (f *Font, err error) { f = new(Font) f.config = config // Resize image to next power-of-two. img = Pow2Image(img).(*image.RGBA) ib := img.Bounds() // Create the texture itself. It will contain all glyphs. // Individual glyph-quads display a subset of this texture. gl.GenTextures(1, &f.texture) gl.BindTexture(gl.TEXTURE_2D, f.texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(ib.Dx()), int32(ib.Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(img.Pix)) // Create display lists for each glyph. f.listbase = gl.GenLists(int32(len(config.Glyphs))) texWidth := float32(ib.Dx()) texHeight := float32(ib.Dy()) for index, glyph := range config.Glyphs { // Update max glyph bounds. if glyph.Width > f.maxGlyphWidth { f.maxGlyphWidth = glyph.Width } if glyph.Height > f.maxGlyphHeight { f.maxGlyphHeight = glyph.Height } // Quad width/height vw := float32(glyph.Width) vh := float32(glyph.Height) // Texture coordinate offsets. tx1 := float32(glyph.X) / texWidth ty1 := float32(glyph.Y) / texHeight tx2 := (float32(glyph.X) + vw) / texWidth ty2 := (float32(glyph.Y) + vh) / texHeight // Advance width (or height if we render top-to-bottom) adv := float32(glyph.Advance) gl.NewList(f.listbase+uint32(index), gl.COMPILE) { gl.Begin(gl.QUADS) { gl.TexCoord2f(tx1, ty2) gl.Vertex2f(0, 0) gl.TexCoord2f(tx2, ty2) gl.Vertex2f(vw, 0) gl.TexCoord2f(tx2, ty1) gl.Vertex2f(vw, vh) gl.TexCoord2f(tx1, ty1) gl.Vertex2f(0, vh) } gl.End() switch config.Dir { case LeftToRight: gl.Translatef(adv, 0, 0) case RightToLeft: gl.Translatef(-adv, 0, 0) case TopToBottom: gl.Translatef(0, -adv, 0) } } gl.EndList() } err = checkGLError() return }