func upload(id gl.Uint, data []byte, stride int, w int, h int) { gl.BindTexture(gl.TEXTURE_2D, id) gl.PixelStorei(gl.UNPACK_ROW_LENGTH, gl.Int(stride)) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, gl.Sizei(w), gl.Sizei(h), 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, gl.Pointer(&data[0])) }
func glInit(width, height int) { gl.Init() gl.Enable(gl.TEXTURE_2D) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height)) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, gl.Double(width), gl.Double(height), 0, -1, 1) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.EnableClientState(gl.VERTEX_ARRAY) gl.EnableClientState(gl.COLOR_ARRAY) gl.EnableClientState(gl.TEXTURE_COORD_ARRAY) gl.VertexPointer(2, gl.FLOAT, 0, gl.Pointer(&vs[0])) gl.ColorPointer(3, gl.UNSIGNED_BYTE, 0, gl.Pointer(&cs[0])) gl.TexCoordPointer(2, gl.FLOAT, 0, gl.Pointer(&ts[0])) }
// Sets up anything that wouldn't have been loaded from disk, including // all opengl data, and sets up finalizers for that data. func (d *Dictionary) setupGlStuff() { d.dlists = make(map[string]uint32) d.strs = make(map[string]strBuffer) d.pars = make(map[string]strBuffer) // TODO: This finalizer is untested runtime.SetFinalizer(d, func(d *Dictionary) { render.Queue(func() { for _, v := range d.dlists { gl.DeleteLists(gl.Uint(v), 1) } }) }) render.Queue(func() { gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, (*gl.Uint)(&d.texture)) gl.BindTexture(gl.TEXTURE_2D, gl.Uint(d.texture)) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.ALPHA, gl.Sizei(d.data.Dx), gl.Sizei(d.data.Dy), 0, gl.ALPHA, gl.UNSIGNED_BYTE, gl.Pointer(&d.data.Pix[0])) gl.Disable(gl.TEXTURE_2D) }) }
func handleLoadRequest(req loadRequest) { f, _ := os.Open(req.path) im, _, err := image.Decode(f) f.Close() if err != nil { return } gray := true dx := im.Bounds().Dx() dy := im.Bounds().Dy() for i := 0; i < dx; i++ { for j := 0; j < dy; j++ { r, g, b, _ := im.At(i, j).RGBA() if r != g || g != b { gray = false break } } if !gray { break } } var canvas draw.Image var pix []byte if gray { ga := NewGrayAlpha(im.Bounds()) pix = ga.Pix canvas = ga } else { pix = memory.GetBlock(4 * req.data.dx * req.data.dy) canvas = &image.RGBA{pix, 4 * req.data.dx, im.Bounds()} } draw.Draw(canvas, im.Bounds(), im, image.Point{}, draw.Src) load_mutex.Lock() load_count += len(pix) manual_unlock := false // This prevents us from trying to send too much to opengl in a single // frame. If we go over the threshold then we hold the lock until we're // done sending data to opengl, then other requests will be free to // queue up and they will run on the next frame. if load_count < load_threshold { load_mutex.Unlock() } else { manual_unlock = true } render.Queue(func() { { gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, (*gl.Uint)(&req.data.texture)) gl.BindTexture(gl.TEXTURE_2D, req.data.texture) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) } if gray { gl.TexImage2D( gl.TEXTURE_2D, 0, gl.LUMINANCE_ALPHA, gl.Sizei(req.data.dx), gl.Sizei(req.data.dy), 0, gl.LUMINANCE_ALPHA, gl.BYTE, gl.Pointer(&pix[0])) } else { gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(req.data.dx), gl.Sizei(req.data.dy), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&pix[0])) // gl.TexImage2D(target, level, internalformat, width, height, border, format, type_, pixels) // glu.Build2DMipmaps(gl.TEXTURE_2D, gl.RGBA, req.data.dx, req.data.dy, gl.RGBA, pix) } memory.FreeBlock(pix) if manual_unlock { load_count = 0 load_mutex.Unlock() } }) }