package opengl // #cgo LDFLAGS: -framework OpenGL // // #include import "C" import ( "github.com/hajimehoshi/go-ebiten/graphics" "image" "unsafe" ) func nextPowerOf2(x uint64) uint64 { x -= 1 x |= (x >> 1) x |= (x >> 2) x |= (x >> 4) x |= (x >> 8) x |= (x >> 16) x |= (x >> 32) return x + 1 } func adjustPixels(width, height int, pixels []uint8) []uint8 { textureWidth := int(nextPowerOf2(uint64(width))) textureHeight := int(nextPowerOf2(uint64(height))) if width == textureWidth && height == textureHeight { return pixels } newPixels := make([]uint8, textureWidth*textureHeight*4) for j := 0; j < height; j++ { copy(newPixels[textureWidth*4*j:], pixels[width*4*j:width*4*j+width*4]) } return newPixels } type Texture struct { id graphics.TextureID native C.GLuint width int height int textureWidth int textureHeight int } type RenderTarget struct { id graphics.RenderTargetID texture *Texture framebuffer C.GLuint } func (texture *Texture) ID() graphics.TextureID { return texture.id } func createTexture(width, height int, pixels []uint8) *Texture { if pixels != nil { pixels = adjustPixels(width, height, pixels) } textureWidth := int(nextPowerOf2(uint64(width))) textureHeight := int(nextPowerOf2(uint64(height))) texture := &Texture{ id: 0, width: width, height: height, textureWidth: textureWidth, textureHeight: textureHeight, } nativeTexture := C.GLuint(0) C.glGenTextures(1, (*C.GLuint)(&nativeTexture)) if nativeTexture < 0 { panic("glGenTexture failed") } C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 4) C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(nativeTexture)) ptr := unsafe.Pointer(nil) if pixels != nil { ptr = unsafe.Pointer(&pixels[0]) } C.glTexImage2D(C.GL_TEXTURE_2D, 0, C.GL_RGBA, C.GLsizei(textureWidth), C.GLsizei(textureHeight), 0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR) C.glBindTexture(C.GL_TEXTURE_2D, 0) texture.native = nativeTexture texture.id = graphics.TextureID(<-newID) return texture } func newRenderTarget(width, height int) *RenderTarget { texture := createTexture(width, height, nil) framebuffer := createFramebuffer(texture.native) return &RenderTarget{ id: graphics.RenderTargetID(<-newID), texture: texture, framebuffer: framebuffer, } } func (renderTarget *RenderTarget) ID() graphics.RenderTargetID { return renderTarget.id } func (renderTarget *RenderTarget) Texture() *Texture { return renderTarget.texture } type textureError string func (err textureError) Error() string { return "Texture Error: " + string(err) } func newTextureFromImage(img image.Image) (*Texture, error) { var pix []uint8 switch img.(type) { case *image.RGBA: pix = img.(*image.RGBA).Pix case *image.NRGBA: pix = img.(*image.NRGBA).Pix default: return nil, textureError("image format must be RGBA or NRGBA") } size := img.Bounds().Size() return createTexture(size.X, size.Y, pix), nil } func newRenderTargetWithFramebuffer(width, height int, framebuffer C.GLuint) *RenderTarget { texture := &Texture{ id: graphics.TextureID(<-newID), width: width, height: height, textureWidth: int(nextPowerOf2(uint64(width))), textureHeight: int(nextPowerOf2(uint64(height))), } return &RenderTarget{ texture: texture, framebuffer: framebuffer, } } func createFramebuffer(nativeTexture C.GLuint) C.GLuint { framebuffer := C.GLuint(0) C.glGenFramebuffers(1, &framebuffer) origFramebuffer := C.GLint(0) C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer) C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer) C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0, C.GL_TEXTURE_2D, nativeTexture, 0) C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer)) if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) != C.GL_FRAMEBUFFER_COMPLETE { panic("creating framebuffer failed") } return framebuffer } var newID chan int func init() { newID = make(chan int) go func() { for i := 0; ; i++ { newID <- i } }() }