func TextureFromFile(fname string) *glh.Texture { f, err := os.Open(fname) if err != nil { panic(err) } // All images will be valid img, _, _ := image.Decode(f) width := img.Bounds().Size().X height := img.Bounds().Size().Y t := glh.NewTexture(width, height) t.FromImage(img, 0) return t }
func (block *Block) BuildTexture() { block.tex = glh.NewTexture(1024, 256) block.tex.Init() // TODO: use runtime.SetFinalizer() to clean up/delete the texture? glh.With(block.tex, func() { //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_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) // TODO: Only try and activate anisotropic filtering if it is available gl.TexParameterf(gl.TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MaxAnisotropy) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAX_LEVEL, 1) }) for i := 0; i < 2; i++ { glh.With(&glh.Framebuffer{Texture: block.tex, Level: i}, func() { glh.With(glh.Attrib{gl.COLOR_BUFFER_BIT}, func() { gl.ClearColor(1, 0, 0, 0) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) }) viewport_proj := glh.Compound(glh.Attrib{gl.VIEWPORT_BIT}, glh.Matrix{gl.PROJECTION}) glh.With(viewport_proj, func() { gl.Viewport(0, 0, block.tex.W/(1<<uint(i)), block.tex.H/(1<<uint(i))) gl.LoadIdentity() //gl.Ortho(0, float64(tex.w), 0, float64(tex.h), -1, 1) gl.Ortho(-2, -2+WIDTH, 2, -2, -1, 1) glh.With(glh.Matrix{gl.MODELVIEW}, func() { gl.LoadIdentity() //gl.Hint(gl.LINES, gl.NICEST) //gl.LineWidth(4) /* glh.With(glh.Primitive{gl.LINES}, func() { gl.Color4f(1, 1, 1, 1) gl.Vertex2f(-2, 0) gl.Vertex2f(2, 0) }) */ gl.PointSize(4) glh.With(glh.Matrix{gl.MODELVIEW}, func() { gl.Translated(0, -2, 0) gl.Scaled(1, 4/float64(block.nrecords), 1) block.vertex_data.Render(gl.POINTS) }) /* gl.Color4f(0.5, 0.5, 1, 1) With(Primitive{gl.LINE_LOOP}, func() { b := 0.2 Squared(-2.1+b, -2.1+b, 4.35-b*2, 4.2-b*2) }) */ }) }) }) } //block.img = block.tex.AsImage() if !block.detail_needed { block.vertex_data = nil runtime.GC() } blocks_rendered++ }
func NewCanvas(w, h int) *Canvas { buffer := new(glh.Framebuffer) buffer.Texture = glh.NewTexture(w, h) buffer.Texture.Init() return &Canvas{w, h, buffer} }