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-25 21:15:27 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
|
2013-10-25 02:30:39 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/texture"
|
2013-06-19 03:31:44 +02:00
|
|
|
"image"
|
2013-06-15 10:07:14 +02:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-10-25 02:30:39 +02:00
|
|
|
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) C.GLuint {
|
2013-10-19 11:47:34 +02:00
|
|
|
nativeTexture := C.GLuint(0)
|
2013-10-20 17:17:05 +02:00
|
|
|
|
2013-10-19 11:47:34 +02:00
|
|
|
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
|
|
|
|
if nativeTexture < 0 {
|
2013-06-19 03:31:44 +02:00
|
|
|
panic("glGenTexture failed")
|
|
|
|
}
|
|
|
|
C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 4)
|
2013-10-19 11:47:34 +02:00
|
|
|
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(nativeTexture))
|
2013-06-19 16:51:41 +02:00
|
|
|
|
2013-10-25 02:30:39 +02:00
|
|
|
ptr := unsafe.Pointer(nil)
|
|
|
|
if pixels != nil {
|
|
|
|
ptr = unsafe.Pointer(&pixels[0])
|
|
|
|
}
|
2013-06-19 03:31:44 +02:00
|
|
|
C.glTexImage2D(C.GL_TEXTURE_2D, 0, C.GL_RGBA,
|
|
|
|
C.GLsizei(textureWidth), C.GLsizei(textureHeight),
|
2013-10-25 02:30:39 +02:00
|
|
|
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-10-20 17:17:05 +02:00
|
|
|
return nativeTexture
|
|
|
|
}
|
|
|
|
|
2013-10-25 02:30:39 +02:00
|
|
|
type NativeTextureCreator struct{}
|
|
|
|
|
|
|
|
func (creator *NativeTextureCreator) Create(textureWidth, textureHeight int) (interface{}, error) {
|
|
|
|
return createNativeTexture(textureWidth, textureHeight, nil), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (creator *NativeTextureCreator) CreateFromImage(img *image.NRGBA) (interface{}, error) {
|
|
|
|
size := img.Bounds().Size()
|
|
|
|
return createNativeTexture(size.X, size.Y, img.Pix), nil
|
2013-10-20 17:17:05 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 21:15:27 +02:00
|
|
|
func newRenderTarget(width, height int) (*rendertarget.RenderTarget, error) {
|
2013-10-25 02:30:39 +02:00
|
|
|
texture, err := texture.New(width, height, &NativeTextureCreator{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-10-22 18:08:01 +02:00
|
|
|
framebuffer := createFramebuffer(texture.Native().(C.GLuint))
|
2013-10-25 21:15:27 +02:00
|
|
|
return rendertarget.NewWithFramebuffer(texture, framebuffer), nil
|
2013-10-20 18:13:09 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 21:15:27 +02:00
|
|
|
func newRenderTargetWithFramebuffer(width, height int, framebuffer C.GLuint) (*rendertarget.RenderTarget, error) {
|
2013-10-25 02:30:39 +02:00
|
|
|
texture, err := texture.New(width, height, &NativeTextureCreator{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-10-19 11:47:34 +02:00
|
|
|
}
|
2013-10-25 21:15:27 +02:00
|
|
|
return rendertarget.NewWithFramebuffer(texture, framebuffer), nil
|
2013-10-11 18:29:19 +02:00
|
|
|
}
|
2013-10-17 04:21:57 +02:00
|
|
|
|
2013-10-19 11:47:34 +02:00
|
|
|
func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
|
2013-10-17 04:21:57 +02:00
|
|
|
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,
|
2013-10-19 11:47:34 +02:00
|
|
|
C.GL_TEXTURE_2D, nativeTexture, 0)
|
2013-10-17 04:21:57 +02:00
|
|
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
|
|
|
|
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) !=
|
|
|
|
C.GL_FRAMEBUFFER_COMPLETE {
|
|
|
|
panic("creating framebuffer failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return framebuffer
|
|
|
|
}
|