2013-06-19 01:49:54 +02:00
|
|
|
package opengl
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <OpenGL/gl.h>
|
|
|
|
import "C"
|
|
|
|
import (
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
2013-06-19 03:31:44 +02:00
|
|
|
"image"
|
2013-06-15 10:07:14 +02:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-10-18 20:15:25 +02:00
|
|
|
func nextPowerOf2(x uint64) uint64 {
|
2013-06-15 10:07:14 +02:00
|
|
|
x -= 1
|
|
|
|
x |= (x >> 1)
|
|
|
|
x |= (x >> 2)
|
|
|
|
x |= (x >> 4)
|
|
|
|
x |= (x >> 8)
|
|
|
|
x |= (x >> 16)
|
|
|
|
x |= (x >> 32)
|
|
|
|
return x + 1
|
|
|
|
}
|
|
|
|
|
2013-07-06 21:39:56 +02:00
|
|
|
func adjustPixels(width, height int, pixels []uint8) []uint8 {
|
2013-10-18 20:15:25 +02:00
|
|
|
textureWidth := int(nextPowerOf2(uint64(width)))
|
|
|
|
textureHeight := int(nextPowerOf2(uint64(height)))
|
2013-07-06 21:39:56 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:07:14 +02:00
|
|
|
type Texture struct {
|
2013-06-19 16:51:41 +02:00
|
|
|
id C.GLuint
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
textureWidth int
|
2013-06-19 01:49:54 +02:00
|
|
|
textureHeight int
|
2013-07-12 19:04:19 +02:00
|
|
|
framebuffer C.GLuint
|
2013-07-12 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (texture *Texture) ID() graphics.TextureID {
|
|
|
|
return graphics.TextureID(texture.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (texture *Texture) Width() int {
|
|
|
|
return texture.width
|
|
|
|
}
|
|
|
|
|
|
|
|
func (texture *Texture) Height() int {
|
|
|
|
return texture.height
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 03:31:44 +02:00
|
|
|
func createTexture(width, height int, pixels []uint8) *Texture {
|
2013-06-17 16:39:44 +02:00
|
|
|
if pixels != nil {
|
2013-07-06 21:39:56 +02:00
|
|
|
pixels = adjustPixels(width, height, pixels)
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
2013-10-18 20:15:25 +02:00
|
|
|
textureWidth := int(nextPowerOf2(uint64(width)))
|
|
|
|
textureHeight := int(nextPowerOf2(uint64(height)))
|
2013-06-15 10:07:14 +02:00
|
|
|
texture := &Texture{
|
2013-06-19 16:51:41 +02:00
|
|
|
id: 0,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
textureWidth: textureWidth,
|
2013-06-19 01:49:54 +02:00
|
|
|
textureHeight: textureHeight,
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 03:31:44 +02:00
|
|
|
textureID := C.GLuint(0)
|
|
|
|
C.glGenTextures(1, (*C.GLuint)(&textureID))
|
2013-07-12 18:36:01 +02:00
|
|
|
if textureID < 0 {
|
2013-06-19 03:31:44 +02:00
|
|
|
panic("glGenTexture failed")
|
|
|
|
}
|
|
|
|
C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 4)
|
|
|
|
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(textureID))
|
2013-06-19 16:51:41 +02:00
|
|
|
|
2013-06-19 03:31:44 +02:00
|
|
|
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)
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2013-06-19 03:31:44 +02:00
|
|
|
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)
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2013-06-19 03:31:44 +02:00
|
|
|
texture.id = textureID
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
return texture
|
|
|
|
}
|
|
|
|
|
2013-06-23 09:06:32 +02:00
|
|
|
type textureError string
|
|
|
|
|
|
|
|
func (err textureError) Error() string {
|
|
|
|
return "Texture Error: " + string(err)
|
|
|
|
}
|
|
|
|
|
2013-10-17 03:32:16 +02:00
|
|
|
func newRenderTarget(width, height int) *Texture {
|
2013-10-17 04:21:57 +02:00
|
|
|
texture := createTexture(width, height, nil)
|
|
|
|
texture.framebuffer = createFramebuffer(texture.id)
|
|
|
|
return texture
|
2013-06-19 01:49:54 +02:00
|
|
|
}
|
|
|
|
|
2013-06-23 09:06:32 +02:00
|
|
|
func newTextureFromImage(img image.Image) (*Texture, error) {
|
2013-06-19 03:31:44 +02:00
|
|
|
var pix []uint8
|
|
|
|
switch img.(type) {
|
|
|
|
case *image.RGBA:
|
|
|
|
pix = img.(*image.RGBA).Pix
|
|
|
|
case *image.NRGBA:
|
|
|
|
pix = img.(*image.NRGBA).Pix
|
|
|
|
default:
|
2013-06-23 09:06:32 +02:00
|
|
|
return nil, textureError("image format must be RGBA or NRGBA")
|
2013-06-19 03:31:44 +02:00
|
|
|
}
|
|
|
|
size := img.Bounds().Size()
|
2013-06-23 09:06:32 +02:00
|
|
|
return createTexture(size.X, size.Y, pix), nil
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
2013-07-12 18:36:01 +02:00
|
|
|
|
2013-10-11 18:29:19 +02:00
|
|
|
func newRenderTargetWithFramebuffer(width, height int,
|
2013-10-17 03:32:16 +02:00
|
|
|
framebuffer C.GLuint) *Texture {
|
|
|
|
return &Texture{
|
2013-07-12 18:36:01 +02:00
|
|
|
id: 0,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
2013-10-18 20:15:25 +02:00
|
|
|
textureWidth: int(nextPowerOf2(uint64(width))),
|
|
|
|
textureHeight: int(nextPowerOf2(uint64(height))),
|
2013-10-11 18:29:19 +02:00
|
|
|
framebuffer: framebuffer,
|
2013-07-12 18:36:01 +02:00
|
|
|
}
|
2013-10-11 18:29:19 +02:00
|
|
|
}
|
2013-10-17 04:21:57 +02:00
|
|
|
|
|
|
|
func createFramebuffer(textureID 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, textureID, 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
|
|
|
|
}
|