mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
Add graphics/opengl/texture
This commit is contained in:
parent
37746fc323
commit
af1e77eced
@ -9,9 +9,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||||
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
|
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/texture"
|
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
||||||
"image"
|
"image"
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
@ -21,7 +22,7 @@ type Context struct {
|
|||||||
screenWidth int
|
screenWidth int
|
||||||
screenHeight int
|
screenHeight int
|
||||||
screenScale int
|
screenScale int
|
||||||
textures map[graphics.TextureId]*texture.Texture
|
textures map[graphics.TextureId]*gtexture.Texture
|
||||||
renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget
|
renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget
|
||||||
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
||||||
mainFramebufferTexture *rendertarget.RenderTarget
|
mainFramebufferTexture *rendertarget.RenderTarget
|
||||||
@ -33,7 +34,7 @@ func newContext(screenWidth, screenHeight, screenScale int) *Context {
|
|||||||
screenWidth: screenWidth,
|
screenWidth: screenWidth,
|
||||||
screenHeight: screenHeight,
|
screenHeight: screenHeight,
|
||||||
screenScale: screenScale,
|
screenScale: screenScale,
|
||||||
textures: map[graphics.TextureId]*texture.Texture{},
|
textures: map[graphics.TextureId]*gtexture.Texture{},
|
||||||
renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{},
|
renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{},
|
||||||
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
||||||
}
|
}
|
||||||
@ -47,10 +48,10 @@ func (context *Context) Init() {
|
|||||||
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
|
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
context.mainFramebufferTexture, err = newRenderTargetWithFramebuffer(
|
context.mainFramebufferTexture, err = texture.NewRenderTargetWithFramebuffer(
|
||||||
context.screenWidth*context.screenScale,
|
context.screenWidth*context.screenScale,
|
||||||
context.screenHeight*context.screenScale,
|
context.screenHeight*context.screenScale,
|
||||||
C.GLuint(mainFramebuffer))
|
texture.Framebuffer(mainFramebuffer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("creating main framebuffer failed: " + err.Error())
|
panic("creating main framebuffer failed: " + err.Error())
|
||||||
}
|
}
|
||||||
@ -61,7 +62,7 @@ func (context *Context) Init() {
|
|||||||
panic("initializing the offscreen failed: " + err.Error())
|
panic("initializing the offscreen failed: " + err.Error())
|
||||||
}
|
}
|
||||||
screen := context.renderTargets[context.screenId]
|
screen := context.renderTargets[context.screenId]
|
||||||
C.glBindTexture(C.GL_TEXTURE_2D, screen.Texture().Native().(C.GLuint))
|
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(screen.Texture().Native().(texture.Native)))
|
||||||
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST)
|
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST)
|
||||||
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST)
|
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST)
|
||||||
C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
||||||
@ -92,8 +93,8 @@ func (context *Context) DrawTexture(
|
|||||||
if !ok {
|
if !ok {
|
||||||
panic("invalid texture ID")
|
panic("invalid texture ID")
|
||||||
}
|
}
|
||||||
tex.Draw(func(native interface{}, quads []texture.Quad) {
|
tex.Draw(func(native interface{}, quads []gtexture.Quad) {
|
||||||
shader.DrawTexture(shader.Texture(native.(C.GLuint)),
|
shader.DrawTexture(native.(texture.Native),
|
||||||
context.projectionMatrix, quads,
|
context.projectionMatrix, quads,
|
||||||
geometryMatrix, colorMatrix)
|
geometryMatrix, colorMatrix)
|
||||||
})
|
})
|
||||||
@ -106,8 +107,8 @@ func (context *Context) DrawTextureParts(
|
|||||||
if !ok {
|
if !ok {
|
||||||
panic("invalid texture ID")
|
panic("invalid texture ID")
|
||||||
}
|
}
|
||||||
tex.DrawParts(parts, func(native interface{}, quads []texture.Quad) {
|
tex.DrawParts(parts, func(native interface{}, quads []gtexture.Quad) {
|
||||||
shader.DrawTexture(shader.Texture(native.(C.GLuint)),
|
shader.DrawTexture(native.(texture.Native),
|
||||||
context.projectionMatrix, quads,
|
context.projectionMatrix, quads,
|
||||||
geometryMatrix, colorMatrix)
|
geometryMatrix, colorMatrix)
|
||||||
})
|
})
|
||||||
@ -125,7 +126,7 @@ func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
|
|||||||
func (context *Context) setOffscreen(renderTarget *rendertarget.RenderTarget) {
|
func (context *Context) setOffscreen(renderTarget *rendertarget.RenderTarget) {
|
||||||
C.glFlush()
|
C.glFlush()
|
||||||
|
|
||||||
framebuffer := renderTarget.Framebuffer().(C.GLuint)
|
framebuffer := C.GLuint(renderTarget.Framebuffer().(texture.Framebuffer))
|
||||||
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
|
||||||
err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER)
|
err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER)
|
||||||
if err != C.GL_FRAMEBUFFER_COMPLETE {
|
if err != C.GL_FRAMEBUFFER_COMPLETE {
|
||||||
@ -178,7 +179,7 @@ func (context *Context) flush() {
|
|||||||
|
|
||||||
func (context *Context) NewRenderTarget(width, height int) (
|
func (context *Context) NewRenderTarget(width, height int) (
|
||||||
graphics.RenderTargetId, error) {
|
graphics.RenderTargetId, error) {
|
||||||
renderTarget, err := newRenderTarget(width, height)
|
renderTarget, err := texture.NewRenderTarget(width, height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@ -198,7 +199,7 @@ func (context *Context) NewRenderTarget(width, height int) (
|
|||||||
|
|
||||||
func (context *Context) NewTextureFromImage(img image.Image) (
|
func (context *Context) NewTextureFromImage(img image.Image) (
|
||||||
graphics.TextureId, error) {
|
graphics.TextureId, error) {
|
||||||
texture, err := texture.NewFromImage(img, createFromImage)
|
texture, err := texture.NewFromImage(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,8 @@ package shader
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/texture"
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||||
|
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -161,9 +162,7 @@ func use(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatr
|
|||||||
return program
|
return program
|
||||||
}
|
}
|
||||||
|
|
||||||
type Texture C.GLuint
|
func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gtexture.Quad,
|
||||||
|
|
||||||
func DrawTexture(native Texture, projectionMatrix [16]float32, quads []texture.Quad,
|
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
if !initialized {
|
if !initialized {
|
||||||
initialize()
|
initialize()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package opengl
|
package texture
|
||||||
|
|
||||||
// #cgo LDFLAGS: -framework OpenGL
|
// #cgo LDFLAGS: -framework OpenGL
|
||||||
//
|
//
|
||||||
@ -6,12 +6,14 @@ package opengl
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
|
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/texture"
|
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
||||||
"image"
|
"image"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) C.GLuint {
|
type Native C.GLuint
|
||||||
|
|
||||||
|
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) Native {
|
||||||
nativeTexture := C.GLuint(0)
|
nativeTexture := C.GLuint(0)
|
||||||
|
|
||||||
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
|
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
|
||||||
@ -33,7 +35,7 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) C.GLui
|
|||||||
C.GLsizei(textureWidth), C.GLsizei(textureHeight),
|
C.GLsizei(textureWidth), C.GLsizei(textureHeight),
|
||||||
0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr)
|
0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr)
|
||||||
|
|
||||||
return nativeTexture
|
return Native(nativeTexture)
|
||||||
}
|
}
|
||||||
|
|
||||||
func create(textureWidth, textureHeight int) (interface{}, error) {
|
func create(textureWidth, textureHeight int) (interface{}, error) {
|
||||||
@ -45,17 +47,19 @@ func createFromImage(img *image.NRGBA) (interface{}, error) {
|
|||||||
return createNativeTexture(size.X, size.Y, img.Pix), nil
|
return createNativeTexture(size.X, size.Y, img.Pix), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRenderTarget(width, height int) (*rendertarget.RenderTarget, error) {
|
type Framebuffer C.GLuint
|
||||||
texture, err := texture.New(width, height, create)
|
|
||||||
|
func NewRenderTarget(width, height int) (*rendertarget.RenderTarget, error) {
|
||||||
|
texture, err := gtexture.New(width, height, create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
framebuffer := createFramebuffer(texture.Native().(C.GLuint))
|
framebuffer := createFramebuffer(C.GLuint(texture.Native().(Native)))
|
||||||
return rendertarget.NewWithFramebuffer(texture, framebuffer), nil
|
return rendertarget.NewWithFramebuffer(texture, Framebuffer(framebuffer)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRenderTargetWithFramebuffer(width, height int, framebuffer C.GLuint) (*rendertarget.RenderTarget, error) {
|
func NewRenderTargetWithFramebuffer(width, height int, framebuffer Framebuffer) (*rendertarget.RenderTarget, error) {
|
||||||
texture, err := texture.New(width, height, create)
|
texture, err := gtexture.New(width, height, create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -79,3 +83,7 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
|
|||||||
|
|
||||||
return framebuffer
|
return framebuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewFromImage(img image.Image) (*gtexture.Texture, error) {
|
||||||
|
return gtexture.NewFromImage(img, createFromImage)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user