big big commit - rework whole pixelgl

This commit is contained in:
faiface 2017-03-06 00:28:52 +01:00
parent e9d6e33b8a
commit 69a3c17cfc
4 changed files with 345 additions and 321 deletions

View File

@ -1,6 +1,7 @@
package pixelgl package pixelgl
import ( import (
"fmt"
"image/color" "image/color"
"github.com/faiface/glhf" "github.com/faiface/glhf"
@ -10,30 +11,33 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Canvas is basically a Picture that you can draw onto. //TODO: make Canvas a Picture
// Canvas is an off-screen rectangular BasicTarget that you can draw onto.
// //
// Canvas supports TrianglesPosition, TrianglesColor and TrianglesTexture. // It supports TrianglesPosition, TrianglesColor, TrianglesPicture and PictureColor.
type Canvas struct { type Canvas struct {
f *glhf.Frame f *glhf.Frame
s *glhf.Shader s *glhf.Shader
copyVs *glhf.VertexSlice bounds pixel.Rect
smooth bool smooth bool
drawTd pixel.TrianglesDrawer
pic *pixel.GLPicture
mat mgl32.Mat3 mat mgl32.Mat3
col mgl32.Vec4 col mgl32.Vec4
bnd mgl32.Vec4
} }
// NewCanvas creates a new fully transparent Canvas with specified dimensions in pixels. // NewCanvas creates a new empty, fully transparent Canvas with given bounds. If the smooth flag
func NewCanvas(width, height float64, smooth bool) *Canvas { // set, then stretched Pictures will be smoothed and will not be drawn pixely onto this Canvas.
c := &Canvas{smooth: smooth} func NewCanvas(bounds pixel.Rect, smooth bool) *Canvas {
c := &Canvas{
smooth: smooth,
mat: mgl32.Ident3(),
col: mgl32.Vec4{1, 1, 1, 1},
}
mainthread.Call(func() { mainthread.Call(func() {
var err error var err error
c.f = glhf.NewFrame(int(width), int(height), smooth)
c.s, err = glhf.NewShader( c.s, err = glhf.NewShader(
canvasVertexFormat, canvasVertexFormat,
canvasUniformFormat, canvasUniformFormat,
@ -41,162 +45,179 @@ func NewCanvas(width, height float64, smooth bool) *Canvas {
canvasFragmentShader, canvasFragmentShader,
) )
if err != nil { if err != nil {
panic(errors.Wrap(err, "failed to create canvas")) panic(errors.Wrap(err, "failed to create Canvas, there's a bug in the shader"))
} }
c.copyVs = glhf.MakeVertexSlice(c.s, 6, 6)
c.copyVs.Begin()
c.copyVs.SetVertexData([]float32{
-1, -1, 1, 1, 1, 1, 0, 0,
1, -1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1,
-1, -1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
-1, 1, 1, 1, 1, 1, 0, 1,
})
c.copyVs.End()
}) })
white := pixel.NRGBA{R: 1, G: 1, B: 1, A: 1} c.SetBounds(bounds)
c.drawTd = pixel.TrianglesDrawer{Triangles: &pixel.TrianglesData{
{Position: pixel.V(-1, -1), Color: white, Picture: pixel.V(0, 0)},
{Position: pixel.V(1, -1), Color: white, Picture: pixel.V(1, 0)},
{Position: pixel.V(1, 1), Color: white, Picture: pixel.V(1, 1)},
{Position: pixel.V(-1, -1), Color: white, Picture: pixel.V(0, 0)},
{Position: pixel.V(1, 1), Color: white, Picture: pixel.V(1, 1)},
{Position: pixel.V(-1, 1), Color: white, Picture: pixel.V(0, 1)},
}}
c.pic = nil
c.mat = mgl32.Ident3()
c.col = mgl32.Vec4{1, 1, 1, 1}
c.bnd = mgl32.Vec4{0, 0, 1, 1}
return c return c
} }
// SetSize resizes the Canvas. The original content will be stretched to fit the new size. // MakeTriangles creates a specialized copy of the supplied Triangles that draws onto this Canvas.
func (c *Canvas) SetSize(width, height float64) {
if pixel.V(width, height) == pixel.V(c.Size()) {
return
}
mainthread.Call(func() {
oldF := c.f
c.f = glhf.NewFrame(int(width), int(height), c.smooth)
c.f.Begin()
c.s.Begin()
c.s.SetUniformAttr(canvasTransformMat3, mgl32.Ident3())
c.s.SetUniformAttr(canvasMaskColorVec4, mgl32.Vec4{1, 1, 1, 1})
c.s.SetUniformAttr(canvasBoundsVec4, mgl32.Vec4{0, 0, 1, 1})
oldF.Texture().Begin()
c.copyVs.Begin()
c.copyVs.Draw()
c.copyVs.End()
oldF.Texture().End()
c.s.End()
c.f.End()
})
}
// Size returns the width and the height of the Canvas in pixels.
func (c *Canvas) Size() (width, height float64) {
return float64(c.f.Width()), float64(c.f.Height())
}
// Content returns a Picture that contains the content of this Canvas. The returned Picture changes
// as you draw onto the Canvas, so there is no real need to call this method more than once (but it
// might be beneficial to your code to do so).
func (c *Canvas) Content() *pixel.GLPicture {
return pixel.PictureFromTexture(c.f.Texture())
}
// Clear fills the whole Canvas with one specified color.
func (c *Canvas) Clear(col color.Color) {
mainthread.CallNonBlock(func() {
c.f.Begin()
col := pixel.NRGBAModel.Convert(col).(pixel.NRGBA)
glhf.Clear(float32(col.R), float32(col.G), float32(col.B), float32(col.A))
c.f.End()
})
}
// Draw draws the content of the Canvas onto another Target. If no transform is applied, the content
// is fully stretched to fit the Target.
func (c *Canvas) Draw(t pixel.Target) {
c.drawTd.Draw(t)
}
// MakeTriangles returns Triangles that draw onto this Canvas.
func (c *Canvas) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
gt := NewGLTriangles(c.s, t).(*glTriangles)
return &canvasTriangles{
c: c,
glTriangles: gt,
}
}
// SetPicture sets a Picture that will be used in further draw operations.
// //
// This does not set the Picture that this Canvas draws onto, don't confuse it. // TrianglesPosition, TrianglesColor and TrianglesPicture are supported.
func (c *Canvas) SetPicture(p *pixel.GLPicture) { func (c *Canvas) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
if p != nil { return &canvasTriangles{
min := pictureBounds(p, pixel.V(0, 0)) GLTriangles: NewGLTriangles(c.s, t),
max := pictureBounds(p, pixel.V(1, 1)) c: c,
c.bnd = mgl32.Vec4{
float32(min.X()), float32(min.Y()),
float32(max.X()), float32(max.Y()),
}
} }
c.pic = p
} }
// SetTransform sets the transformations used in further draw operations. // MakePicture create a specialized copy of the supplied Picture that draws onto this Canvas.
//
// PictureColor is supported.
func (c *Canvas) MakePicture(p pixel.Picture) pixel.TargetPicture {
pd := pixel.PictureDataFromPicture(p)
pixels := make([]uint8, 4*len(pd.Pix))
for i := range pd.Pix {
pixels[i*4+0] = uint8(pd.Pix[i].R * 255)
pixels[i*4+1] = uint8(pd.Pix[i].G * 255)
pixels[i*4+2] = uint8(pd.Pix[i].B * 255)
pixels[i*4+3] = uint8(pd.Pix[i].A * 255)
}
var tex *glhf.Texture
mainthread.Call(func() {
tex = glhf.NewTexture(pd.Stride, len(pd.Pix)/pd.Stride, c.smooth, pixels)
})
return &canvasPicture{
tex: tex,
bounds: pd.Rect,
c: c,
}
}
// SetTransform sets a set of Transforms that every position in triangles will be put through.
func (c *Canvas) SetTransform(t ...pixel.Transform) { func (c *Canvas) SetTransform(t ...pixel.Transform) {
c.mat = transformToMat(t...) c.mat = transformToMat(t...)
} }
// SetMaskColor sets the mask color used in further draw operations. // SetColorMask sets a color that every color in triangles or a picture will be multiplied by.
func (c *Canvas) SetMaskColor(col color.Color) { func (c *Canvas) SetColorMask(col color.Color) {
if col == nil { nrgba := pixel.NRGBA{R: 1, G: 1, B: 1, A: 1}
col = pixel.NRGBA{R: 1, G: 1, B: 1, A: 1} if col != nil {
nrgba = pixel.NRGBAModel.Convert(col).(pixel.NRGBA)
} }
nrgba := pixel.NRGBAModel.Convert(col).(pixel.NRGBA) c.col = mgl32.Vec4{
r := float32(nrgba.R) float32(nrgba.R),
g := float32(nrgba.G) float32(nrgba.G),
b := float32(nrgba.B) float32(nrgba.B),
a := float32(nrgba.A) float32(nrgba.A),
c.col = mgl32.Vec4{r, g, b, a} }
}
// SetBounds resizes the Canvas to the new bounds. Old content will be preserved.
func (c *Canvas) SetBounds(bounds pixel.Rect) {
if c.Bounds() == bounds {
return
}
mainthread.Call(func() {
oldF := c.f
_, _, w, h := discreteBounds(bounds)
c.f = glhf.NewFrame(w, h, c.smooth)
// preserve old content
if oldF != nil {
relBounds := c.bounds
relBounds.Pos -= bounds.Pos
ox, oy, ow, oh := discreteBounds(relBounds)
oldF.Blit(
c.f,
ox, oy, ox+ow, oy+oh,
ox, oy, ox+ow, oy+oh,
)
}
c.s.Begin()
orig := bounds.Center()
c.s.SetUniformAttr(canvasOrig, mgl32.Vec2{
float32(orig.X()),
float32(orig.Y()),
})
c.s.SetUniformAttr(canvasSize, mgl32.Vec2{
float32(w),
float32(h),
})
c.s.End()
})
c.bounds = bounds
}
// Bounds returns the rectangular bounds of the Canvas.
func (c *Canvas) Bounds() pixel.Rect {
return c.bounds
}
// SetSmooth sets whether the stretched Pictures drawn onto this Canvas should be drawn smooth or
// pixely.
func (c *Canvas) SetSmooth(smooth bool) {
c.smooth = smooth
}
// Smooth returns whether the stretched Pictures drawn onto this Canvas are set to be drawn smooth
// or pixely.
func (c *Canvas) Smooth() bool {
return c.smooth
}
// Clear fill the whole Canvas with a single color.
func (c *Canvas) Clear(color color.Color) {
nrgba := pixel.NRGBAModel.Convert(color).(pixel.NRGBA)
mainthread.CallNonBlock(func() {
c.f.Begin()
glhf.Clear(
float32(nrgba.R),
float32(nrgba.G),
float32(nrgba.B),
float32(nrgba.A),
)
c.f.End()
})
} }
type canvasTriangles struct { type canvasTriangles struct {
*GLTriangles
c *Canvas c *Canvas
*glTriangles
} }
func (ct *canvasTriangles) Draw() { func (ct *canvasTriangles) draw(cp *canvasPicture) {
// avoid possible race condition // save the current state vars to avoid race condition
pic := ct.c.pic
mat := ct.c.mat mat := ct.c.mat
col := ct.c.col col := ct.c.col
bnd := ct.c.bnd
mainthread.CallNonBlock(func() { mainthread.CallNonBlock(func() {
ct.c.f.Begin() ct.c.f.Begin()
ct.c.s.Begin() ct.c.s.Begin()
ct.c.s.SetUniformAttr(canvasTransformMat3, mat) ct.c.s.SetUniformAttr(canvasTransform, mat)
ct.c.s.SetUniformAttr(canvasMaskColorVec4, col) ct.c.s.SetUniformAttr(canvasColorMask, col)
ct.c.s.SetUniformAttr(canvasBoundsVec4, bnd)
if pic != nil { if cp == nil {
pic.Texture().Begin() ct.vs.Begin()
ct.glTriangles.Draw() ct.vs.Draw()
pic.Texture().End() ct.vs.End()
} else { } else {
ct.glTriangles.Draw() cp.tex.Begin()
ct.c.s.SetUniformAttr(canvasTexSize, mgl32.Vec2{
float32(cp.tex.Width()),
float32(cp.tex.Height()),
})
if cp.tex.Smooth() != ct.c.smooth {
cp.tex.SetSmooth(ct.c.smooth)
}
ct.vs.Begin()
ct.vs.Draw()
ct.vs.End()
cp.tex.End()
} }
ct.c.s.End() ct.c.s.End()
@ -204,28 +225,64 @@ func (ct *canvasTriangles) Draw() {
}) })
} }
const ( func (ct *canvasTriangles) Draw() {
canvasPositionVec2 int = iota ct.draw(nil)
canvasColorVec4 }
canvasTextureVec2
)
var canvasVertexFormat = glhf.AttrFormat{ type canvasPicture struct {
canvasPositionVec2: {Name: "position", Type: glhf.Vec2}, tex *glhf.Texture
canvasColorVec4: {Name: "color", Type: glhf.Vec4}, bounds pixel.Rect
canvasTextureVec2: {Name: "texture", Type: glhf.Vec2},
c *Canvas
}
func (cp *canvasPicture) Bounds() pixel.Rect {
return cp.bounds
}
func (cp *canvasPicture) Slice(r pixel.Rect) pixel.Picture {
return &canvasPicture{
bounds: r,
c: cp.c,
}
}
func (cp *canvasPicture) Draw(t pixel.TargetTriangles) {
ct := t.(*canvasTriangles)
if cp.c != ct.c {
panic(fmt.Errorf("%T.Draw: TargetTriangles generated by different Canvas", cp))
}
ct.draw(cp)
} }
const ( const (
canvasMaskColorVec4 int = iota canvasPosition int = iota
canvasTransformMat3 canvasColor
canvasBoundsVec4 canvasTexture
canvasIntensity
)
var canvasVertexFormat = glhf.AttrFormat{
canvasPosition: {Name: "position", Type: glhf.Vec2},
canvasColor: {Name: "color", Type: glhf.Vec4},
canvasTexture: {Name: "texture", Type: glhf.Vec2},
canvasIntensity: {Name: "intensity", Type: glhf.Float},
}
const (
canvasTransform int = iota
canvasColorMask
canvasTexSize
canvasOrig
canvasSize
) )
var canvasUniformFormat = glhf.AttrFormat{ var canvasUniformFormat = glhf.AttrFormat{
{Name: "maskColor", Type: glhf.Vec4}, canvasTransform: {Name: "transform", Type: glhf.Mat3},
{Name: "transform", Type: glhf.Mat3}, canvasColorMask: {Name: "colorMask", Type: glhf.Vec4},
{Name: "bounds", Type: glhf.Vec4}, canvasTexSize: {Name: "texSize", Type: glhf.Vec2},
canvasOrig: {Name: "orig", Type: glhf.Vec2},
canvasSize: {Name: "size", Type: glhf.Vec2},
} }
var canvasVertexShader = ` var canvasVertexShader = `
@ -234,16 +291,23 @@ var canvasVertexShader = `
in vec2 position; in vec2 position;
in vec4 color; in vec4 color;
in vec2 texture; in vec2 texture;
in float intensity;
out vec4 Color; out vec4 Color;
out vec2 Texture; out vec2 Texture;
out float Intensity;
uniform mat3 transform; uniform mat3 transform;
uniform vec2 orig;
uniform vec2 size;
void main() { void main() {
gl_Position = vec4((transform * vec3(position.x, position.y, 1.0)).xy, 0.0, 1.0); vec2 transPos = (transform * vec3(position, 1.0)).xy;
vec2 normPos = (transPos - orig) / (size/2);
gl_Position = vec4(normPos, 0.0, 1.0);
Color = color; Color = color;
Texture = texture; Texture = texture;
Intensity = intensity;
} }
` `
@ -252,23 +316,22 @@ var canvasFragmentShader = `
in vec4 Color; in vec4 Color;
in vec2 Texture; in vec2 Texture;
in float Intensity;
out vec4 color; out vec4 color;
uniform vec4 maskColor; uniform vec4 colorMask;
uniform vec4 bounds; uniform vec2 texSize;
uniform sampler2D tex; uniform sampler2D tex;
void main() { void main() {
vec2 boundsMin = bounds.xy; if (Intensity == 0) {
vec2 boundsMax = bounds.zw; color = colorMask * Color;
if (Texture == vec2(-1, -1)) {
color = maskColor * Color;
} else { } else {
float tx = boundsMin.x * (1 - Texture.x) + boundsMax.x * Texture.x; vec2 t = Texture / texSize;
float ty = boundsMin.y * (1 - Texture.y) + boundsMax.y * Texture.y; color = vec4(0, 0, 0, 0);
color = maskColor * Color * texture(tex, vec2(tx, ty)); color += (1 - Intensity) * colorMask * Color;
color += Intensity * colorMask * Color * texture(tex, t);
} }
} }
` `

View File

@ -2,7 +2,6 @@ package pixelgl
import ( import (
"fmt" "fmt"
"math"
"github.com/faiface/glhf" "github.com/faiface/glhf"
"github.com/faiface/mainthread" "github.com/faiface/mainthread"
@ -29,9 +28,9 @@ var (
// //
// Only draw the Triangles using the provided Shader. // Only draw the Triangles using the provided Shader.
func NewGLTriangles(shader *glhf.Shader, t pixel.Triangles) *GLTriangles { func NewGLTriangles(shader *glhf.Shader, t pixel.Triangles) *GLTriangles {
var gt *glTriangles var gt *GLTriangles
mainthread.Call(func() { mainthread.Call(func() {
gt = &glTriangles{ gt = &GLTriangles{
vs: glhf.MakeVertexSlice(shader, 0, t.Len()), vs: glhf.MakeVertexSlice(shader, 0, t.Len()),
shader: shader, shader: shader,
} }
@ -66,7 +65,8 @@ func (gt *GLTriangles) SetLen(len int) {
gt.data = append(gt.data, gt.data = append(gt.data,
0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
float32(math.Inf(+1)), float32(math.Inf(+1)), 0, 0,
0,
) )
} }
} }
@ -77,7 +77,7 @@ func (gt *GLTriangles) SetLen(len int) {
// Slice returns a sub-Triangles of this GLTriangles in range [i, j). // Slice returns a sub-Triangles of this GLTriangles in range [i, j).
func (gt *GLTriangles) Slice(i, j int) pixel.Triangles { func (gt *GLTriangles) Slice(i, j int) pixel.Triangles {
return &glTriangles{ return &GLTriangles{
vs: gt.vs.Slice(i, j), vs: gt.vs.Slice(i, j),
data: gt.data[i*gt.vs.Stride() : j*gt.vs.Stride()], data: gt.data[i*gt.vs.Stride() : j*gt.vs.Stride()],
shader: gt.shader, shader: gt.shader,
@ -86,7 +86,7 @@ func (gt *GLTriangles) Slice(i, j int) pixel.Triangles {
func (gt *GLTriangles) updateData(t pixel.Triangles) { func (gt *GLTriangles) updateData(t pixel.Triangles) {
// glTriangles short path // glTriangles short path
if t, ok := t.(*glTriangles); ok { if t, ok := t.(*GLTriangles); ok {
copy(gt.data, t.data) copy(gt.data, t.data)
return return
} }
@ -98,6 +98,7 @@ func (gt *GLTriangles) updateData(t pixel.Triangles) {
px, py = (*t)[i].Position.XY() px, py = (*t)[i].Position.XY()
col = (*t)[i].Color col = (*t)[i].Color
tx, ty = (*t)[i].Picture.XY() tx, ty = (*t)[i].Picture.XY()
in = (*t)[i].Intensity
) )
gt.data[i*gt.vs.Stride()+0] = float32(px) gt.data[i*gt.vs.Stride()+0] = float32(px)
gt.data[i*gt.vs.Stride()+1] = float32(py) gt.data[i*gt.vs.Stride()+1] = float32(py)
@ -107,6 +108,7 @@ func (gt *GLTriangles) updateData(t pixel.Triangles) {
gt.data[i*gt.vs.Stride()+5] = float32(col.A) gt.data[i*gt.vs.Stride()+5] = float32(col.A)
gt.data[i*gt.vs.Stride()+6] = float32(tx) gt.data[i*gt.vs.Stride()+6] = float32(tx)
gt.data[i*gt.vs.Stride()+7] = float32(ty) gt.data[i*gt.vs.Stride()+7] = float32(ty)
gt.data[i*gt.vs.Stride()+8] = float32(in)
} }
return return
} }
@ -129,15 +131,16 @@ func (gt *GLTriangles) updateData(t pixel.Triangles) {
} }
if t, ok := t.(pixel.TrianglesPicture); ok { if t, ok := t.(pixel.TrianglesPicture); ok {
for i := 0; i < gt.Len(); i++ { for i := 0; i < gt.Len(); i++ {
tx, ty := t.Picture(i).XY() pic, intensity := t.Picture(i)
gt.data[i*gt.vs.Stride()+6] = float32(tx) gt.data[i*gt.vs.Stride()+6] = float32(pic.X())
gt.data[i*gt.vs.Stride()+7] = float32(ty) gt.data[i*gt.vs.Stride()+7] = float32(pic.Y())
gt.data[i*gt.vs.Stride()+8] = float32(intensity)
} }
} }
} }
func (gt *GLTriangles) submitData() { func (gt *GLTriangles) submitData() {
data := gt.data // avoid race condition data := append([]float32{}, gt.data...) // avoid race condition
mainthread.CallNonBlock(func() { mainthread.CallNonBlock(func() {
gt.vs.Begin() gt.vs.Begin()
dataLen := len(data) / gt.vs.Stride() dataLen := len(data) / gt.vs.Stride()
@ -187,8 +190,9 @@ func (gt *GLTriangles) Color(i int) pixel.NRGBA {
} }
// Picture returns the Picture property of the i-th vertex. // Picture returns the Picture property of the i-th vertex.
func (gt *GLTriangles) Picture(i int) pixel.Vec { func (gt *GLTriangles) Picture(i int) (pic pixel.Vec, intensity float64) {
tx := gt.data[i*gt.vs.Stride()+6] tx := gt.data[i*gt.vs.Stride()+6]
ty := gt.data[i*gt.vs.Stride()+7] ty := gt.data[i*gt.vs.Stride()+7]
return pixel.V(float64(tx), float64(ty)) intensity = float64(gt.data[i*gt.vs.Stride()+8])
return pixel.V(float64(tx), float64(ty)), intensity
} }

View File

@ -1,31 +1,12 @@
package pixelgl package pixelgl
import ( import (
"math"
"github.com/faiface/pixel" "github.com/faiface/pixel"
"github.com/go-gl/mathgl/mgl32" "github.com/go-gl/mathgl/mgl32"
) )
func clamp(x, low, high float64) float64 {
if x < low {
return low
}
if x > high {
return high
}
return x
}
func lerp(x float64, a, b pixel.Vec) pixel.Vec {
return a.Scaled(1-x) + b.Scaled(x)
}
func lerp2d(x, a, b pixel.Vec) pixel.Vec {
return pixel.V(
lerp(x.X(), a, b).X(),
lerp(x.Y(), a, b).Y(),
)
}
func transformToMat(t ...pixel.Transform) mgl32.Mat3 { func transformToMat(t ...pixel.Transform) mgl32.Mat3 {
mat := mgl32.Ident3() mat := mgl32.Ident3()
for i := range t { for i := range t {
@ -34,10 +15,10 @@ func transformToMat(t ...pixel.Transform) mgl32.Mat3 {
return mat return mat
} }
func pictureBounds(p *pixel.GLPicture, v pixel.Vec) pixel.Vec { func discreteBounds(bounds pixel.Rect) (x, y, w, h int) {
w, h := float64(p.Texture().Width()), float64(p.Texture().Height()) x0 := int(math.Floor(bounds.Pos.X()))
a := p.Bounds().Pos y0 := int(math.Floor(bounds.Pos.Y()))
b := p.Bounds().Pos + p.Bounds().Size x1 := int(math.Ceil(bounds.Pos.X() + bounds.Size.X()))
u := lerp2d(v, a, b) y1 := int(math.Ceil(bounds.Pos.Y() + bounds.Size.Y()))
return pixel.V(u.X()/w, u.Y()/h) return x0, y0, x1 - x0, y1 - y0
} }

View File

@ -2,7 +2,7 @@ package pixelgl
import ( import (
"image/color" "image/color"
"math"
"runtime" "runtime"
"github.com/faiface/glhf" "github.com/faiface/glhf"
@ -16,16 +16,13 @@ import (
// chosen in such a way, that you usually only need to set a few of them - defaults (zeros) should // chosen in such a way, that you usually only need to set a few of them - defaults (zeros) should
// usually be sensible. // usually be sensible.
// //
// Note that you always need to set the width and the height of a window. // Note that you always need to set the Bounds of the Window.
type WindowConfig struct { type WindowConfig struct {
// Title at the top of a window. // Title at the top of the Window
Title string Title string
// Width of a window in pixels. // Bounds specify the bounds of the Window in pixels.
Width float64 Bounds pixel.Rect
// Height of a window in pixels.
Height float64
// If set to nil, a window will be windowed. Otherwise it will be fullscreen on the // If set to nil, a window will be windowed. Otherwise it will be fullscreen on the
// specified monitor. // specified monitor.
@ -46,23 +43,22 @@ type WindowConfig struct {
// Whether a window is maximized. // Whether a window is maximized.
Maximized bool Maximized bool
// VSync (vertical synchronization) synchronizes window's framerate with the framerate // VSync (vertical synchronization) synchronizes window's framerate with the framerate of
// of the monitor. // the monitor.
VSync bool VSync bool
// Number of samples for multi-sample anti-aliasing (edge-smoothing). Usual values // Number of samples for multi-sample anti-aliasing (edge-smoothing). Usual values are 0, 2,
// are 0, 2, 4, 8 (powers of 2 and not much more than this). // 4, 8 (powers of 2 and not much more than this).
MSAASamples int MSAASamples int
} }
// Window is a window handler. Use this type to manipulate a window (input, drawing, ...). // Window is a window handler. Use this type to manipulate a window (input, drawing, ...).
type Window struct { type Window struct {
window *glfw.Window window *glfw.Window
config WindowConfig
canvas *Canvas bounds pixel.Rect
canvasVs *glhf.VertexSlice canvas *Canvas
shader *glhf.Shader vsync bool
// need to save these to correctly restore a fullscreen window // need to save these to correctly restore a fullscreen window
restore struct { restore struct {
@ -80,13 +76,15 @@ var currentWindow *Window
// NewWindow creates a new Window with it's properties specified in the provided config. // NewWindow creates a new Window with it's properties specified in the provided config.
// //
// If Window creation fails, an error is returned (e.g. due to unavailable graphics device). // If Window creation fails, an error is returned (e.g. due to unavailable graphics device).
func NewWindow(config WindowConfig) (*Window, error) { func NewWindow(cfg WindowConfig) (*Window, error) {
bool2int := map[bool]int{ bool2int := map[bool]int{
true: glfw.True, true: glfw.True,
false: glfw.False, false: glfw.False,
} }
w := &Window{config: config} w := &Window{
bounds: cfg.Bounds,
}
err := mainthread.CallErr(func() error { err := mainthread.CallErr(func() error {
var err error var err error
@ -96,21 +94,22 @@ func NewWindow(config WindowConfig) (*Window, error) {
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Resizable, bool2int[config.Resizable]) glfw.WindowHint(glfw.Resizable, bool2int[cfg.Resizable])
glfw.WindowHint(glfw.Visible, bool2int[!config.Hidden]) glfw.WindowHint(glfw.Visible, bool2int[!cfg.Hidden])
glfw.WindowHint(glfw.Decorated, bool2int[!config.Undecorated]) glfw.WindowHint(glfw.Decorated, bool2int[!cfg.Undecorated])
glfw.WindowHint(glfw.Focused, bool2int[!config.Unfocused]) glfw.WindowHint(glfw.Focused, bool2int[!cfg.Unfocused])
glfw.WindowHint(glfw.Maximized, bool2int[config.Maximized]) glfw.WindowHint(glfw.Maximized, bool2int[cfg.Maximized])
glfw.WindowHint(glfw.Samples, config.MSAASamples) glfw.WindowHint(glfw.Samples, cfg.MSAASamples)
var share *glfw.Window var share *glfw.Window
if currentWindow != nil { if currentWindow != nil {
share = currentWindow.window share = currentWindow.window
} }
_, _, width, height := discreteBounds(cfg.Bounds)
w.window, err = glfw.CreateWindow( w.window, err = glfw.CreateWindow(
int(config.Width), width,
int(config.Height), height,
config.Title, cfg.Title,
nil, nil,
share, share,
) )
@ -122,38 +121,18 @@ func NewWindow(config WindowConfig) (*Window, error) {
w.begin() w.begin()
w.end() w.end()
w.shader, err = glhf.NewShader(
windowVertexFormat,
windowUniformFormat,
windowVertexShader,
windowFragmentShader,
)
if err != nil {
return err
}
w.canvasVs = glhf.MakeVertexSlice(w.shader, 6, 6)
w.canvasVs.Begin()
w.canvasVs.SetVertexData([]float32{
-1, -1, 0, 0,
1, -1, 1, 0,
1, 1, 1, 1,
-1, -1, 0, 0,
1, 1, 1, 1,
-1, 1, 0, 1,
})
w.canvasVs.End()
return nil return nil
}) })
if err != nil { if err != nil {
return nil, errors.Wrap(err, "creating window failed") return nil, errors.Wrap(err, "creating window failed")
} }
w.initInput() w.SetVSync(cfg.VSync)
w.SetMonitor(config.Fullscreen)
w.canvas = NewCanvas(config.Width, config.Height, false) w.initInput()
w.SetMonitor(cfg.Fullscreen)
w.canvas = NewCanvas(cfg.Bounds, false)
w.Update() w.Update()
runtime.SetFinalizer(w, (*Window).Destroy) runtime.SetFinalizer(w, (*Window).Destroy)
@ -168,29 +147,40 @@ func (w *Window) Destroy() {
}) })
} }
// Clear clears the Window with a color.
func (w *Window) Clear(c color.Color) {
w.canvas.Clear(c)
}
// Update swaps buffers and polls events. // Update swaps buffers and polls events.
func (w *Window) Update() { func (w *Window) Update() {
w.canvas.SetSize(w.Size()) mainthread.Call(func() {
wi, hi := w.window.GetSize()
w.bounds.Size = pixel.V(float64(wi), float64(hi))
// fractional positions end up covering more pixels with less size
if w.bounds.X() != math.Floor(w.bounds.X()) {
w.bounds.Size -= pixel.V(1, 0)
}
if w.bounds.Y() != math.Floor(w.bounds.Y()) {
w.bounds.Size -= pixel.V(0, 1)
}
})
w.canvas.SetBounds(w.Bounds())
mainthread.Call(func() { mainthread.Call(func() {
w.begin() w.begin()
glhf.Clear(0, 0, 0, 0) glhf.Bounds(0, 0, w.canvas.f.Width(), w.canvas.f.Height())
w.shader.Begin()
w.canvas.f.Texture().Begin()
w.canvasVs.Begin()
w.canvasVs.Draw()
w.canvasVs.End()
w.canvas.f.Texture().End()
w.shader.End()
if w.config.VSync { glhf.Clear(0, 0, 0, 0)
w.canvas.f.Begin()
w.canvas.f.Blit(
nil,
0, 0, w.canvas.f.Width(), w.canvas.f.Height(),
0, 0, w.canvas.f.Width(), w.canvas.f.Height(),
)
w.canvas.f.End()
if w.vsync {
glfw.SwapInterval(1) glfw.SwapInterval(1)
} else {
glfw.SwapInterval(0)
} }
w.window.SwapBuffers() w.window.SwapBuffers()
w.end() w.end()
@ -225,22 +215,19 @@ func (w *Window) SetTitle(title string) {
}) })
} }
// SetSize resizes the client area of the Window to the specified size in pixels. In case of a // SetBounds sets the bounds of the Window in pixels. Bounds can be fractional, but the size will be
// fullscreen Window, it changes the resolution of that Window. // changed in the next Update to a real possible size of the Window.
func (w *Window) SetSize(width, height float64) { func (w *Window) SetBounds(bounds pixel.Rect) {
w.bounds = bounds
mainthread.Call(func() { mainthread.Call(func() {
w.window.SetSize(int(width), int(height)) _, _, width, height := discreteBounds(bounds)
w.window.SetSize(width, height)
}) })
} }
// Size returns the size of the client area of the Window (the part you can draw on). // Bounds returns the current bounds of the Window.
func (w *Window) Size() (width, height float64) { func (w *Window) Bounds() pixel.Rect {
mainthread.Call(func() { return w.bounds
wi, hi := w.window.GetSize()
width = float64(wi)
height = float64(hi)
})
return width, height
} }
// Show makes the Window visible if it was hidden. // Show makes the Window visible if it was hidden.
@ -351,6 +338,16 @@ func (w *Window) Restore() {
}) })
} }
// SetVSync sets whether the Window should synchronize with the monitor refresh rate.
func (w *Window) SetVSync(vsync bool) {
w.vsync = vsync
}
// VSync returns whether the Window is set to synchronize with the monitor refresh rate.
func (w *Window) VSync() bool {
return w.vsync
}
// Note: must be called inside the main thread. // Note: must be called inside the main thread.
func (w *Window) begin() { func (w *Window) begin() {
if currentWindow != w { if currentWindow != w {
@ -365,7 +362,7 @@ func (w *Window) end() {
// nothing, really // nothing, really
} }
// MakeTriangles generates a specialized copy of the supplied triangles that will draw onto this // MakeTriangles generates a specialized copy of the supplied Triangles that will draw onto this
// Window. // Window.
// //
// Window supports TrianglesPosition, TrianglesColor and TrianglesTexture. // Window supports TrianglesPosition, TrianglesColor and TrianglesTexture.
@ -373,9 +370,11 @@ func (w *Window) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
return w.canvas.MakeTriangles(t) return w.canvas.MakeTriangles(t)
} }
// SetPicture sets a Picture that will be used in subsequent drawings onto the Window. // MakePicture generates a specialized copy of the supplied Picture that will draw onto this Window.
func (w *Window) SetPicture(p *pixel.GLPicture) { //
w.canvas.SetPicture(p) // Window support PictureColor.
func (w *Window) MakePicture(p pixel.Picture) pixel.TargetPicture {
return w.canvas.MakePicture(p)
} }
// SetTransform sets a global transformation matrix for the Window. // SetTransform sets a global transformation matrix for the Window.
@ -385,47 +384,24 @@ func (w *Window) SetTransform(t ...pixel.Transform) {
w.canvas.SetTransform(t...) w.canvas.SetTransform(t...)
} }
// SetMaskColor sets a global mask color for the Window. // SetColorMask sets a global color mask for the Window.
func (w *Window) SetMaskColor(c color.Color) { func (w *Window) SetColorMask(c color.Color) {
w.canvas.SetMaskColor(c) w.canvas.SetColorMask(c)
} }
const ( // SetSmooth sets whether the stretched Pictures drawn onto this Window should be drawn smooth or
windowPositionVec2 = iota // pixely.
windowTextureVec2 func (w *Window) SetSmooth(smooth bool) {
) w.canvas.SetSmooth(smooth)
var windowVertexFormat = glhf.AttrFormat{
windowPositionVec2: {Name: "position", Type: glhf.Vec2},
windowTextureVec2: {Name: "texture", Type: glhf.Vec2},
} }
var windowUniformFormat = glhf.AttrFormat{} // Smooth returns whether the stretched Pictures drawn onto this Window are set to be drawn smooth
// or pixely.
var windowVertexShader = ` func (w *Window) Smooth() bool {
#version 330 core return w.canvas.Smooth()
in vec2 position;
in vec2 texture;
out vec2 Texture;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
Texture = texture;
} }
`
var windowFragmentShader = ` // Clear clears the Window with a color.
#version 330 core func (w *Window) Clear(c color.Color) {
w.canvas.Clear(c)
in vec2 Texture;
out vec4 color;
uniform sampler2D tex;
void main() {
color = texture(tex, Texture);
} }
`