// loadVolatile will create the framebuffer and return true if successful func (canvas *Canvas) loadVolatile() bool { canvas.status = gl.FRAMEBUFFER_COMPLETE // glTexImage2D is guaranteed to error in this case. if canvas.width > maxTextureSize || canvas.height > maxTextureSize { canvas.status = gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT return false } canvas.Texture = newTexture(canvas.width, canvas.height, false) //NULL means reserve texture memory, but texels are undefined gl.TexImage2D(gl.TEXTURE_2D, 0, int(canvas.width), int(canvas.height), gl.RGBA, gl.UNSIGNED_BYTE, nil) if gl.GetError() != gl.NO_ERROR { canvas.Texture.Release() canvas.status = gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT return false } canvas.fbo, canvas.status = newFBO(canvas.getHandle()) if canvas.status != gl.FRAMEBUFFER_COMPLETE { if canvas.fbo.Valid() { gl.DeleteFramebuffer(canvas.fbo) canvas.fbo = gl.Framebuffer{} } return false } return true }
// Set the 'default' texture (id 0) as a repeating white pixel. Otherwise, // texture2D calls inside a shader would return black when drawing graphics // primitives, which would create the need to use different "passthrough" // shaders for untextured primitives vs images. func createDefaultTexture() { gl_state.defaultTexture = gl.CreateTexture() bindTexture(gl_state.defaultTexture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) pix := []byte{255, 255, 255, 255} gl.TexImage2D(gl.TEXTURE_2D, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pix)) }
// newImageTexture will generate a texture from an image. It will automatically // upload the image data to the texture. func newImageTexture(img image.Image, mipmaps bool) (*Texture, error) { bounds := img.Bounds() new_texture := newTexture(int32(bounds.Dx()), int32(bounds.Dy()), mipmaps) //generate a uniform image and upload to vram rgba := image.NewRGBA(img.Bounds()) draw.Draw(rgba, bounds, img, image.Point{0, 0}, draw.Src) bindTexture(new_texture.getHandle()) gl.TexImage2D(gl.TEXTURE_2D, 0, bounds.Dx(), bounds.Dy(), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix)) if new_texture.mipmaps { new_texture.generateMipmaps() } return new_texture, nil }