package opengl // #cgo LDFLAGS: -framework OpenGL // // #include import "C" import ( "fmt" "github.com/hajimehoshi/go-ebiten/graphics" ) type RenderTarget struct { framebuffer C.GLuint width int height int flipY bool } func createFramebuffer(nativeTexture C.GLuint) C.GLuint { framebuffer := C.GLuint(0) C.glGenFramebuffers(1, &framebuffer) C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer) C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0, C.GL_TEXTURE_2D, nativeTexture, 0) if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) != C.GL_FRAMEBUFFER_COMPLETE { panic("creating framebuffer failed") } // Set this framebuffer opaque because alpha values on a target might be // confusing. C.glClearColor(0, 0, 0, 1) C.glClear(C.GL_COLOR_BUFFER_BIT) return framebuffer } func (r *RenderTarget) setAsViewport() { C.glFlush() C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(r.framebuffer)) err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) if err != C.GL_FRAMEBUFFER_COMPLETE { panic(fmt.Sprintf("glBindFramebuffer failed: %d", err)) } C.glBlendFuncSeparate(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA, C.GL_ZERO, C.GL_ONE) width := graphics.AdjustSizeForTexture(r.width) height := graphics.AdjustSizeForTexture(r.height) C.glViewport(0, 0, C.GLsizei(width), C.GLsizei(height)) } func (r *RenderTarget) projectionMatrix() [4][4]float64 { width := graphics.AdjustSizeForTexture(r.width) height := graphics.AdjustSizeForTexture(r.height) matrix := graphics.OrthoProjectionMatrix(0, width, 0, height) if r.flipY { matrix[1][1] *= -1 matrix[1][3] += float64(r.height) / float64(graphics.AdjustSizeForTexture(r.height)) * 2 } return matrix } func (r *RenderTarget) dispose() { C.glDeleteFramebuffers(1, &r.framebuffer) }