2018-11-04 10:34:36 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
2018-11-04 11:06:13 +01:00
|
|
|
import (
|
2018-11-04 11:36:33 +01:00
|
|
|
"fmt"
|
|
|
|
|
2018-11-04 11:06:13 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/math"
|
|
|
|
)
|
|
|
|
|
2018-11-04 11:36:33 +01:00
|
|
|
func checkSize(width, height int) {
|
|
|
|
if width < 1 {
|
|
|
|
panic(fmt.Sprintf("opengl: width (%d) must be equal or more than 1.", width))
|
|
|
|
}
|
|
|
|
if height < 1 {
|
|
|
|
panic(fmt.Sprintf("opengl: height (%d) must be equal or more than 1.", height))
|
|
|
|
}
|
|
|
|
m := theContext.MaxTextureSize()
|
|
|
|
if width > m {
|
|
|
|
panic(fmt.Sprintf("opengl: width (%d) must be less than or equal to %d", width, m))
|
|
|
|
}
|
|
|
|
if height > m {
|
|
|
|
panic(fmt.Sprintf("opengl: height (%d) must be less than or equal to %d", height, m))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 10:34:36 +01:00
|
|
|
type Image struct {
|
2018-11-04 11:46:20 +01:00
|
|
|
textureNative textureNative
|
2018-11-04 12:00:13 +01:00
|
|
|
framebuffer *framebuffer
|
2018-11-04 11:46:20 +01:00
|
|
|
width int
|
|
|
|
height int
|
2018-11-04 10:34:36 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 11:36:33 +01:00
|
|
|
func NewImage(width, height int) (*Image, error) {
|
|
|
|
i := &Image{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
|
|
|
w := math.NextPowerOf2Int(width)
|
|
|
|
h := math.NextPowerOf2Int(height)
|
|
|
|
checkSize(w, h)
|
|
|
|
t, err := theContext.newTexture(w, h)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-04 11:46:20 +01:00
|
|
|
i.textureNative = t
|
2018-11-04 11:36:33 +01:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewScreenFramebufferImage(width, height int) *Image {
|
|
|
|
checkSize(width, height)
|
|
|
|
i := &Image{
|
2018-11-04 10:34:36 +01:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
2018-11-04 11:36:33 +01:00
|
|
|
// The (default) framebuffer size can't be converted to a power of 2.
|
|
|
|
// On browsers, c.width and c.height are used as viewport size and
|
|
|
|
// Edge can't treat a bigger viewport than the drawing area (#71).
|
2018-11-04 12:00:13 +01:00
|
|
|
i.framebuffer = newScreenFramebuffer(width, height)
|
2018-11-04 11:36:33 +01:00
|
|
|
return i
|
2018-11-04 10:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) IsInvalidated() bool {
|
2018-11-04 11:46:20 +01:00
|
|
|
return !theContext.isTexture(i.textureNative)
|
2018-11-04 10:34:36 +01:00
|
|
|
}
|
2018-11-04 10:44:41 +01:00
|
|
|
|
|
|
|
func (i *Image) Delete() {
|
2018-11-04 12:00:13 +01:00
|
|
|
if i.framebuffer != nil {
|
|
|
|
i.framebuffer.delete()
|
2018-11-04 10:44:41 +01:00
|
|
|
}
|
2018-11-04 11:46:20 +01:00
|
|
|
if i.textureNative != *new(textureNative) {
|
|
|
|
theContext.deleteTexture(i.textureNative)
|
2018-11-04 10:44:41 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-04 11:06:13 +01:00
|
|
|
|
2018-11-05 19:44:43 +01:00
|
|
|
func (i *Image) SetAsDestination() {
|
|
|
|
theOpenGLState.destination = i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) setViewport() error {
|
2018-11-04 11:06:13 +01:00
|
|
|
if err := i.ensureFramebuffer(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-04 12:00:13 +01:00
|
|
|
theContext.setViewport(i.framebuffer)
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Pixels() ([]byte, error) {
|
|
|
|
if err := i.ensureFramebuffer(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-04 12:00:13 +01:00
|
|
|
p, err := theContext.framebufferPixels(i.framebuffer, i.width, i.height)
|
2018-11-04 11:06:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:44:43 +01:00
|
|
|
func (i *Image) projectionMatrix() []float32 {
|
2018-11-04 12:00:13 +01:00
|
|
|
if i.framebuffer == nil {
|
2018-11-04 11:06:13 +01:00
|
|
|
panic("not reached")
|
|
|
|
}
|
2018-11-04 12:00:13 +01:00
|
|
|
return i.framebuffer.projectionMatrix()
|
2018-11-04 11:06:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) ensureFramebuffer() error {
|
2018-11-04 12:00:13 +01:00
|
|
|
if i.framebuffer != nil {
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-04 11:59:27 +01:00
|
|
|
w, h := i.width, i.height
|
2018-11-04 11:46:20 +01:00
|
|
|
f, err := newFramebufferFromTexture(i.textureNative, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
|
2018-11-04 11:06:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-04 12:00:13 +01:00
|
|
|
i.framebuffer = f
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-04 11:42:21 +01:00
|
|
|
|
|
|
|
func (i *Image) TexSubImage2D(p []byte, x, y, width, height int) {
|
2018-11-04 11:46:20 +01:00
|
|
|
theContext.texSubImage2D(i.textureNative, p, x, y, width, height)
|
2018-11-04 11:42:21 +01:00
|
|
|
}
|
2018-11-05 19:44:43 +01:00
|
|
|
|
|
|
|
func (i *Image) SetAsSource() {
|
|
|
|
theOpenGLState.source = i
|
|
|
|
}
|