graphics: Add comments

This commit is contained in:
Hajime Hoshi 2017-09-22 01:13:37 +09:00
parent 6145ecee74
commit ceceed951f
3 changed files with 35 additions and 11 deletions

View File

@ -423,14 +423,6 @@ func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
if c.height < 1 { if c.height < 1 {
return errors.New("graphics: height must be equal or more than 1.") return errors.New("graphics: height must be equal or more than 1.")
} }
f := &framebuffer{ c.result.framebuffer = newScreenFramebuffer(c.width, c.height, c.offsetX, c.offsetY)
native: opengl.GetContext().ScreenFramebuffer(),
flipY: true,
width: c.width,
height: c.height,
offsetX: c.offsetX,
offsetY: c.offsetY,
}
c.result.framebuffer = f
return nil return nil
} }

View File

@ -19,6 +19,9 @@ import (
"github.com/hajimehoshi/ebiten/internal/web" "github.com/hajimehoshi/ebiten/internal/web"
) )
// orthoProjectionMatrix returns an orthogonal projection matrix for OpenGL.
//
// The matrix converts the coodinates (left, bottom) - (right, top) to the normalized device coodinates (-1, -1) - (1, 1).
func orthoProjectionMatrix(left, right, bottom, top int) []float32 { func orthoProjectionMatrix(left, right, bottom, top int) []float32 {
e11 := 2 / float32(right-left) e11 := 2 / float32(right-left)
e22 := 2 / float32(top-bottom) e22 := 2 / float32(top-bottom)
@ -33,16 +36,21 @@ func orthoProjectionMatrix(left, right, bottom, top int) []float32 {
} }
} }
// framebuffer is a wrapper of OpenGL's framebuffer.
type framebuffer struct { type framebuffer struct {
native opengl.Framebuffer native opengl.Framebuffer
flipY bool flipY bool
proMatrix []float32 proMatrix []float32
width int width int
height int height int
offsetX float64
offsetY float64 // offsetX and offsetY are translation part of the projection matrix.
// offsetX and offsetY are used for the fullscreen mode.
offsetX float64
offsetY float64
} }
// newFramebufferFromTexture creates a framebuffer from the given texture.
func newFramebufferFromTexture(texture *texture, width, height int) (*framebuffer, error) { func newFramebufferFromTexture(texture *texture, width, height int) (*framebuffer, error) {
native, err := opengl.GetContext().NewFramebuffer(opengl.Texture(texture.native)) native, err := opengl.GetContext().NewFramebuffer(opengl.Texture(texture.native))
if err != nil { if err != nil {
@ -55,8 +63,24 @@ func newFramebufferFromTexture(texture *texture, width, height int) (*framebuffe
}, nil }, nil
} }
// newScreenFramebuffer creates a framebuffer for the screen.
func newScreenFramebuffer(width, height int, offsetX, offsetY float64) *framebuffer {
return &framebuffer{
native: opengl.GetContext().ScreenFramebuffer(),
flipY: true,
width: width,
height: height,
offsetX: offsetX,
offsetY: offsetY,
}
}
// defaultViewportSize is the default size (width or height) of viewport.
//
// defaultViewportSize also represents the maximum size of a framebuffer.
const defaultViewportSize = 4096 const defaultViewportSize = 4096
// viewportSize returns the viewport size of the framebuffer.
func (f *framebuffer) viewportSize() (int, int) { func (f *framebuffer) viewportSize() (int, int) {
// On some browsers, viewport size must be within the framebuffer size. // On some browsers, viewport size must be within the framebuffer size.
// e.g. Edge (#71), Chrome on GPD Pocket (#420) // e.g. Edge (#71), Chrome on GPD Pocket (#420)
@ -68,11 +92,17 @@ func (f *framebuffer) viewportSize() (int, int) {
return defaultViewportSize, defaultViewportSize return defaultViewportSize, defaultViewportSize
} }
// setAsViewport sets the framebuffer as the current viewport.
func (f *framebuffer) setAsViewport() error { func (f *framebuffer) setAsViewport() error {
w, h := f.viewportSize() w, h := f.viewportSize()
return opengl.GetContext().SetViewport(f.native, w, h) return opengl.GetContext().SetViewport(f.native, w, h)
} }
// projectionMatrix returns a projection matrix of the framebuffer.
//
// A projection matrix converts the coodinates on the framebuffer
// (0, 0) - (viewport width, viewport height)
// to the normalized device coodinates (-1, -1) - (1, 1) with adjustment.
func (f *framebuffer) projectionMatrix(height int) []float32 { func (f *framebuffer) projectionMatrix(height int) []float32 {
if f.proMatrix != nil { if f.proMatrix != nil {
return f.proMatrix return f.proMatrix

View File

@ -22,6 +22,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
// Image represents an image that is implemented with OpenGL.
type Image struct { type Image struct {
texture *texture texture *texture
framebuffer *framebuffer framebuffer *framebuffer
@ -29,6 +30,7 @@ type Image struct {
height int height int
} }
// MaxImageSize is the maximum of width/height of an image.
const MaxImageSize = defaultViewportSize const MaxImageSize = defaultViewportSize
func NewImage(width, height int, filter opengl.Filter) *Image { func NewImage(width, height int, filter opengl.Filter) *Image {