Use dep to vendor things

This commit is contained in:
2018-03-18 04:23:13 +00:00
parent d572a19352
commit 1ba64f2aa8
123 changed files with 34229 additions and 0 deletions

73
Gopkg.lock generated Normal file
View File

@@ -0,0 +1,73 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/faiface/glhf"
packages = ["."]
revision = "98c0391c0fd3f0b365cfe5d467ac162b79dfb002"
[[projects]]
branch = "master"
name = "github.com/faiface/mainthread"
packages = ["."]
revision = "8b78f0a41ae388189090ac4506612659fa53082b"
[[projects]]
name = "github.com/faiface/pixel"
packages = [
".",
"imdraw",
"pixelgl"
]
revision = "2d9a739e406f1f7093747c599d844e50c6e92d0e"
version = "v0.7"
[[projects]]
branch = "master"
name = "github.com/go-gl/gl"
packages = ["v3.3-core/gl"]
revision = "eafa86a81d9705ff0b4ab163d26fe74da80e0559"
[[projects]]
branch = "master"
name = "github.com/go-gl/glfw"
packages = ["v3.2/glfw"]
revision = "513e4f2bf85c31fba0fc4907abd7895242ccbe50"
[[projects]]
branch = "master"
name = "github.com/go-gl/mathgl"
packages = ["mgl32"]
revision = "d28d8d903c65929938eaac4953b3eb50a425b5ee"
[[projects]]
name = "github.com/pkg/errors"
packages = ["."]
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0"
[[projects]]
branch = "master"
name = "golang.org/x/image"
packages = [
"colornames",
"math/f32"
]
revision = "f3a9b89b59def9194717c1d0bd4c0d08fa1afa7b"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = [
"html",
"html/atom"
]
revision = "24dd3780ca4f75fed9f321890729414a4b5d3f13"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "20e4628692bb0166d60606bd2f9e88c17bbd484bfe0e1fc0e3478c05a5d5f736"
solver-name = "gps-cdcl"
solver-version = 1

38
Gopkg.toml Normal file
View File

@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
name = "github.com/faiface/pixel"
version = "0.7.0"
[[constraint]]
branch = "master"
name = "golang.org/x/image"
[prune]
go-tests = true
unused-packages = true

21
vendor/github.com/faiface/glhf/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Michal Štrba
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

191
vendor/github.com/faiface/glhf/README.md generated vendored Normal file
View File

@@ -0,0 +1,191 @@
# glhf [![GoDoc](https://godoc.org/github.com/faiface/glhf?status.svg)](http://godoc.org/github.com/faiface/glhf) [![Report card](https://goreportcard.com/badge/github.com/faiface/glhf)](https://goreportcard.com/report/github.com/faiface/glhf)
open**GL** **H**ave **F**un - A Go package that makes life with OpenGL enjoyable.
```
go get github.com/faiface/glhf
```
## Main features
- Garbage collected OpenGL objects
- Dynamically sized vertex slices (vertex arrays are boring)
- Textures, Shaders, Frames (reasonably managed framebuffers)
- Always possible to use standard OpenGL with `glhf`
## Motivation
OpenGL is verbose, it's usage patterns are repetitive and it's manual memory management doesn't fit
Go's design. When making a game development library, it's usually desirable to create some
higher-level abstractions around OpenGL. This library is a take on that.
## Contribute!
The library is young and many features are still missing. If you find a bug, have a proposal or a
feature request, _do an issue_!. If you know how to implement something that's missing, _do a pull
request_.
## Code
The following are parts of the demo program, which can be found in the [examples](https://github.com/faiface/glhf/tree/master/examples/demo).
```go
// ... GLFW window creation and stuff ...
// vertex shader source
var vertexShader = `
#version 330 core
in vec2 position;
in vec2 texture;
out vec2 Texture;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
Texture = texture;
}
`
// fragment shader source
var fragmentShader = `
#version 330 core
in vec2 Texture;
out vec4 color;
uniform sampler2D tex;
void main() {
color = texture(tex, Texture);
}
`
var (
// Here we define a vertex format of our vertex slice. It's actually a basic slice
// literal.
//
// The vertex format consists of names and types of the attributes. The name is the
// name that the attribute is referenced by inside a shader.
vertexFormat = glhf.AttrFormat{
{Name: "position", Type: glhf.Vec2},
{Name: "texture", Type: glhf.Vec2},
}
// Here we declare some variables for later use.
shader *glhf.Shader
texture *glhf.Texture
slice *glhf.VertexSlice
)
// Here we load an image from a file. The loadImage function is not within the library, it
// just loads and returns a image.NRGBA.
gopherImage, err := loadImage("celebrate.png")
if err != nil {
panic(err)
}
// Every OpenGL call needs to be done inside the main thread.
mainthread.Call(func() {
var err error
// Here we create a shader. The second argument is the format of the uniform
// attributes. Since our shader has no uniform attributes, the format is empty.
shader, err = glhf.NewShader(vertexFormat, glhf.AttrFormat{}, vertexShader, fragmentShader)
// If the shader compilation did not go successfully, an error with a full
// description is returned.
if err != nil {
panic(err)
}
// We create a texture from the loaded image.
texture = glhf.NewTexture(
gopherImage.Bounds().Dx(),
gopherImage.Bounds().Dy(),
true,
gopherImage.Pix,
)
// And finally, we make a vertex slice, which is basically a dynamically sized
// vertex array. The length of the slice is 6 and the capacity is the same.
//
// The slice inherits the vertex format of the supplied shader. Also, it should
// only be used with that shader.
slice = glhf.MakeVertexSlice(shader, 6, 6)
// Before we use a slice, we need to Begin it. The same holds for all objects in
// GLHF.
slice.Begin()
// We assign data to the vertex slice. The values are in the order as in the vertex
// format of the slice (shader). Each two floats correspond to an attribute of type
// glhf.Vec2.
slice.SetVertexData([]float32{
-1, -1, 0, 1,
+1, -1, 1, 1,
+1, +1, 1, 0,
-1, -1, 0, 1,
+1, +1, 1, 0,
-1, +1, 0, 0,
})
// When we're done with the slice, we End it.
slice.End()
})
shouldQuit := false
for !shouldQuit {
mainthread.Call(func() {
// ... GLFW stuff ...
// Clear the window.
glhf.Clear(1, 1, 1, 1)
// Here we Begin/End all necessary objects and finally draw the vertex
// slice.
shader.Begin()
texture.Begin()
slice.Begin()
slice.Draw()
slice.End()
texture.End()
shader.End()
// ... GLFW stuff ...
})
}
```
## FAQ
### Which version of OpenGL does GLHF use?
It uses OpenGL 3.3 and uses
[`github.com/go-gl/gl/v3.3-core/gl`](https://github.com/go-gl/gl/tree/master/v3.3-core/gl).
### Why do I have to use `github.com/faiface/mainthread` package with GLHF?
First of all, OpenGL has to be done from one thread and many operating systems require, that the one
thread will be the main thread of your application.
But why that specific package? GLHF uses the `mainthread` package to do the garbage collection of
OpenGL objects, which is super convenient. So in order for it to work correctly, you have to
initialize the `mainthread` package through `mainthread.Run`. However, once you call this function
there is no way to run functions on the main thread, except for through the `mainthread` package.
### Why is the important XY feature not included?
I probably didn't need it yet. If you want that features, create an issue or implement it and do a
pull request.
### Does GLHF create windows for me?
No. You have to use another library for windowing, e.g.
[github.com/go-gl/glfw/v3.2/glfw](https://github.com/go-gl/glfw/tree/master/v3.2/glfw).
### Why no tests?
If you find a way to automatically test OpenGL, I may add tests.

80
vendor/github.com/faiface/glhf/attr.go generated vendored Normal file
View File

@@ -0,0 +1,80 @@
package glhf
// AttrFormat defines names and types of OpenGL attributes (vertex format, uniform format, etc.).
//
// Example:
// AttrFormat{{"position", Vec2}, {"color", Vec4}, {"texCoord": Vec2}}
type AttrFormat []Attr
// Size returns the total size of all attributes of the AttrFormat.
func (af AttrFormat) Size() int {
total := 0
for _, attr := range af {
total += attr.Type.Size()
}
return total
}
// Attr represents an arbitrary OpenGL attribute, such as a vertex attribute or a shader
// uniform attribute.
type Attr struct {
Name string
Type AttrType
}
// AttrType represents the type of an OpenGL attribute.
type AttrType int
// List of all possible attribute types.
const (
Int AttrType = iota
Float
Vec2
Vec3
Vec4
Mat2
Mat23
Mat24
Mat3
Mat32
Mat34
Mat4
Mat42
Mat43
)
// Size returns the size of a type in bytes.
func (at AttrType) Size() int {
switch at {
case Int:
return 4
case Float:
return 4
case Vec2:
return 2 * 4
case Vec3:
return 3 * 4
case Vec4:
return 4 * 4
case Mat2:
return 2 * 2 * 4
case Mat23:
return 2 * 3 * 4
case Mat24:
return 2 * 4 * 4
case Mat3:
return 3 * 3 * 4
case Mat32:
return 3 * 2 * 4
case Mat34:
return 3 * 4 * 4
case Mat4:
return 4 * 4 * 4
case Mat42:
return 4 * 2 * 4
case Mat43:
return 4 * 3 * 4
default:
panic("size of vertex attribute type: invalid type")
}
}

7
vendor/github.com/faiface/glhf/doc.go generated vendored Normal file
View File

@@ -0,0 +1,7 @@
// Package glhf provides abstractions around the basic OpenGL primitives and operations.
//
// All calls should be done from the main thread using "github.com/faiface/mainthread" package.
//
// This package deliberately does not handle nor report trivial OpenGL errors, it's up to you to
// cause none. It does of course report errors like shader compilation error and such.
package glhf

108
vendor/github.com/faiface/glhf/frame.go generated vendored Normal file
View File

@@ -0,0 +1,108 @@
package glhf
import (
"runtime"
"github.com/faiface/mainthread"
"github.com/go-gl/gl/v3.3-core/gl"
)
// Frame is a fixed resolution texture that you can draw on.
type Frame struct {
fb, rf, df binder // framebuffer, read framebuffer, draw framebuffer
tex *Texture
}
// NewFrame creates a new fully transparent Frame with given dimensions in pixels.
func NewFrame(width, height int, smooth bool) *Frame {
f := &Frame{
fb: binder{
restoreLoc: gl.FRAMEBUFFER_BINDING,
bindFunc: func(obj uint32) {
gl.BindFramebuffer(gl.FRAMEBUFFER, obj)
},
},
rf: binder{
restoreLoc: gl.READ_FRAMEBUFFER_BINDING,
bindFunc: func(obj uint32) {
gl.BindFramebuffer(gl.READ_FRAMEBUFFER, obj)
},
},
df: binder{
restoreLoc: gl.DRAW_FRAMEBUFFER_BINDING,
bindFunc: func(obj uint32) {
gl.BindFramebuffer(gl.DRAW_FRAMEBUFFER, obj)
},
},
tex: NewTexture(width, height, smooth, make([]uint8, width*height*4)),
}
gl.GenFramebuffers(1, &f.fb.obj)
f.fb.bind()
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, f.tex.tex.obj, 0)
f.fb.restore()
runtime.SetFinalizer(f, (*Frame).delete)
return f
}
func (f *Frame) delete() {
mainthread.CallNonBlock(func() {
gl.DeleteFramebuffers(1, &f.fb.obj)
})
}
// ID returns the OpenGL framebuffer ID of this Frame.
func (f *Frame) ID() uint32 {
return f.fb.obj
}
// Begin binds the Frame. All draw operations will target this Frame until End is called.
func (f *Frame) Begin() {
f.fb.bind()
}
// End unbinds the Frame. All draw operations will go to whatever was bound before this Frame.
func (f *Frame) End() {
f.fb.restore()
}
// Blit copies rectangle (sx0, sy0, sx1, sy1) in this Frame onto rectangle (dx0, dy0, dx1, dy1) in
// dst Frame.
//
// If the dst Frame is nil, the destination will be the framebuffer 0, which is the screen.
//
// If the sizes of the rectangles don't match, the source will be stretched to fit the destination
// rectangle. The stretch will be either smooth or pixely according to the source Frame's
// smoothness.
func (f *Frame) Blit(dst *Frame, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1 int) {
f.rf.obj = f.fb.obj
if dst != nil {
f.df.obj = dst.fb.obj
} else {
f.df.obj = 0
}
f.rf.bind()
f.df.bind()
filter := gl.NEAREST
if f.tex.smooth {
filter = gl.LINEAR
}
gl.BlitFramebuffer(
int32(sx0), int32(sy0), int32(sx1), int32(sy1),
int32(dx0), int32(dy0), int32(dx1), int32(dy1),
gl.COLOR_BUFFER_BIT, uint32(filter),
)
f.rf.restore()
f.df.restore()
}
// Texture returns the Frame's underlying Texture that the Frame draws on.
func (f *Frame) Texture() *Texture {
return f.tex
}

11
vendor/github.com/faiface/glhf/interface.go generated vendored Normal file
View File

@@ -0,0 +1,11 @@
package glhf
// BeginEnder is an interface for manipulating OpenGL state.
//
// OpenGL is a state machine. Every object can 'enter' it's state and 'leave' it's state. For
// example, you can bind a buffer and unbind a buffer, bind a texture and unbind it, use shader
// and unuse it, and so on.
type BeginEnder interface {
Begin()
End()
}

51
vendor/github.com/faiface/glhf/orphan.go generated vendored Normal file
View File

@@ -0,0 +1,51 @@
package glhf
import "github.com/go-gl/gl/v3.3-core/gl"
// Init initializes OpenGL by loading function pointers from the active OpenGL context.
// This function must be manually run inside the main thread (using "github.com/faiface/mainthread"
// package).
//
// It must be called under the presence of an active OpenGL context, e.g., always after calling
// window.MakeContextCurrent(). Also, always call this function when switching contexts.
func Init() {
err := gl.Init()
if err != nil {
panic(err)
}
gl.Enable(gl.BLEND)
gl.Enable(gl.SCISSOR_TEST)
gl.BlendEquation(gl.FUNC_ADD)
}
// Clear clears the current framebuffer or window with the given color.
func Clear(r, g, b, a float32) {
gl.ClearColor(r, g, b, a)
gl.Clear(gl.COLOR_BUFFER_BIT)
}
// Bounds sets the drawing bounds in pixels. Drawing outside bounds is always discarted.
//
// Calling this function is equivalent to setting viewport and scissor in OpenGL.
func Bounds(x, y, w, h int) {
gl.Viewport(int32(x), int32(y), int32(w), int32(h))
gl.Scissor(int32(x), int32(y), int32(w), int32(h))
}
// BlendFactor represents a source or destination blend factor.
type BlendFactor int
// Here's the list of all blend factors.
const (
One = BlendFactor(gl.ONE)
Zero = BlendFactor(gl.ZERO)
SrcAlpha = BlendFactor(gl.SRC_ALPHA)
DstAlpha = BlendFactor(gl.DST_ALPHA)
OneMinusSrcAlpha = BlendFactor(gl.ONE_MINUS_SRC_ALPHA)
OneMinusDstAlpha = BlendFactor(gl.ONE_MINUS_DST_ALPHA)
)
// BlendFunc sets the source and destination blend factor.
func BlendFunc(src, dst BlendFactor) {
gl.BlendFunc(uint32(src), uint32(dst))
}

224
vendor/github.com/faiface/glhf/shader.go generated vendored Normal file
View File

@@ -0,0 +1,224 @@
package glhf
import (
"fmt"
"runtime"
"github.com/faiface/mainthread"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
// Shader is an OpenGL shader program.
type Shader struct {
program binder
vertexFmt AttrFormat
uniformFmt AttrFormat
uniformLoc []int32
}
// NewShader creates a new shader program from the specified vertex shader and fragment shader
// sources.
//
// Note that vertexShader and fragmentShader parameters must contain the source code, they're
// not filenames.
func NewShader(vertexFmt, uniformFmt AttrFormat, vertexShader, fragmentShader string) (*Shader, error) {
shader := &Shader{
program: binder{
restoreLoc: gl.CURRENT_PROGRAM,
bindFunc: func(obj uint32) {
gl.UseProgram(obj)
},
},
vertexFmt: vertexFmt,
uniformFmt: uniformFmt,
uniformLoc: make([]int32, len(uniformFmt)),
}
var vshader, fshader uint32
// vertex shader
{
vshader = gl.CreateShader(gl.VERTEX_SHADER)
src, free := gl.Strs(vertexShader)
defer free()
length := int32(len(vertexShader))
gl.ShaderSource(vshader, 1, src, &length)
gl.CompileShader(vshader)
var success int32
gl.GetShaderiv(vshader, gl.COMPILE_STATUS, &success)
if success == gl.FALSE {
var logLen int32
gl.GetShaderiv(vshader, gl.INFO_LOG_LENGTH, &logLen)
infoLog := make([]byte, logLen)
gl.GetShaderInfoLog(vshader, logLen, nil, &infoLog[0])
return nil, fmt.Errorf("error compiling vertex shader: %s", string(infoLog))
}
defer gl.DeleteShader(vshader)
}
// fragment shader
{
fshader = gl.CreateShader(gl.FRAGMENT_SHADER)
src, free := gl.Strs(fragmentShader)
defer free()
length := int32(len(fragmentShader))
gl.ShaderSource(fshader, 1, src, &length)
gl.CompileShader(fshader)
var success int32
gl.GetShaderiv(fshader, gl.COMPILE_STATUS, &success)
if success == gl.FALSE {
var logLen int32
gl.GetShaderiv(fshader, gl.INFO_LOG_LENGTH, &logLen)
infoLog := make([]byte, logLen)
gl.GetShaderInfoLog(fshader, logLen, nil, &infoLog[0])
return nil, fmt.Errorf("error compiling fragment shader: %s", string(infoLog))
}
defer gl.DeleteShader(fshader)
}
// shader program
{
shader.program.obj = gl.CreateProgram()
gl.AttachShader(shader.program.obj, vshader)
gl.AttachShader(shader.program.obj, fshader)
gl.LinkProgram(shader.program.obj)
var success int32
gl.GetProgramiv(shader.program.obj, gl.LINK_STATUS, &success)
if success == gl.FALSE {
var logLen int32
gl.GetProgramiv(shader.program.obj, gl.INFO_LOG_LENGTH, &logLen)
infoLog := make([]byte, logLen)
gl.GetProgramInfoLog(shader.program.obj, logLen, nil, &infoLog[0])
return nil, fmt.Errorf("error linking shader program: %s", string(infoLog))
}
}
// uniforms
for i, uniform := range uniformFmt {
loc := gl.GetUniformLocation(shader.program.obj, gl.Str(uniform.Name+"\x00"))
shader.uniformLoc[i] = loc
}
runtime.SetFinalizer(shader, (*Shader).delete)
return shader, nil
}
func (s *Shader) delete() {
mainthread.CallNonBlock(func() {
gl.DeleteProgram(s.program.obj)
})
}
// ID returns the OpenGL ID of this Shader.
func (s *Shader) ID() uint32 {
return s.program.obj
}
// VertexFormat returns the vertex attribute format of this Shader. Do not change it.
func (s *Shader) VertexFormat() AttrFormat {
return s.vertexFmt
}
// UniformFormat returns the uniform attribute format of this Shader. Do not change it.
func (s *Shader) UniformFormat() AttrFormat {
return s.uniformFmt
}
// SetUniformAttr sets the value of a uniform attribute of this Shader. The attribute is
// specified by the index in the Shader's uniform format.
//
// If the uniform attribute does not exist in the Shader, this method returns false.
//
// Supplied value must correspond to the type of the attribute. Correct types are these
// (right-hand is the type of the value):
// Attr{Type: Int}: int32
// Attr{Type: Float}: float32
// Attr{Type: Vec2}: mgl32.Vec2
// Attr{Type: Vec3}: mgl32.Vec3
// Attr{Type: Vec4}: mgl32.Vec4
// Attr{Type: Mat2}: mgl32.Mat2
// Attr{Type: Mat23}: mgl32.Mat2x3
// Attr{Type: Mat24}: mgl32.Mat2x4
// Attr{Type: Mat3}: mgl32.Mat3
// Attr{Type: Mat32}: mgl32.Mat3x2
// Attr{Type: Mat34}: mgl32.Mat3x4
// Attr{Type: Mat4}: mgl32.Mat4
// Attr{Type: Mat42}: mgl32.Mat4x2
// Attr{Type: Mat43}: mgl32.Mat4x3
// No other types are supported.
//
// The Shader must be bound before calling this method.
func (s *Shader) SetUniformAttr(uniform int, value interface{}) (ok bool) {
if s.uniformLoc[uniform] < 0 {
return false
}
switch s.uniformFmt[uniform].Type {
case Int:
value := value.(int32)
gl.Uniform1iv(s.uniformLoc[uniform], 1, &value)
case Float:
value := value.(float32)
gl.Uniform1fv(s.uniformLoc[uniform], 1, &value)
case Vec2:
value := value.(mgl32.Vec2)
gl.Uniform2fv(s.uniformLoc[uniform], 1, &value[0])
case Vec3:
value := value.(mgl32.Vec3)
gl.Uniform3fv(s.uniformLoc[uniform], 1, &value[0])
case Vec4:
value := value.(mgl32.Vec4)
gl.Uniform4fv(s.uniformLoc[uniform], 1, &value[0])
case Mat2:
value := value.(mgl32.Mat2)
gl.UniformMatrix2fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat23:
value := value.(mgl32.Mat2x3)
gl.UniformMatrix2x3fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat24:
value := value.(mgl32.Mat2x4)
gl.UniformMatrix2x4fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat3:
value := value.(mgl32.Mat3)
gl.UniformMatrix3fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat32:
value := value.(mgl32.Mat3x2)
gl.UniformMatrix3x2fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat34:
value := value.(mgl32.Mat3x4)
gl.UniformMatrix3x4fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat4:
value := value.(mgl32.Mat4)
gl.UniformMatrix4fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat42:
value := value.(mgl32.Mat4x2)
gl.UniformMatrix4x2fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat43:
value := value.(mgl32.Mat4x3)
gl.UniformMatrix4x3fv(s.uniformLoc[uniform], 1, false, &value[0])
default:
panic("set uniform attr: invalid attribute type")
}
return true
}
// Begin binds the Shader program. This is necessary before using the Shader.
func (s *Shader) Begin() {
s.program.bind()
}
// End unbinds the Shader program and restores the previous one.
func (s *Shader) End() {
s.program.restore()
}

148
vendor/github.com/faiface/glhf/texture.go generated vendored Normal file
View File

@@ -0,0 +1,148 @@
package glhf
import (
"runtime"
"github.com/faiface/mainthread"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
// Texture is an OpenGL texture.
type Texture struct {
tex binder
width, height int
smooth bool
}
// NewTexture creates a new texture with the specified width and height with some initial
// pixel values. The pixels must be a sequence of RGBA values (one byte per component).
func NewTexture(width, height int, smooth bool, pixels []uint8) *Texture {
tex := &Texture{
tex: binder{
restoreLoc: gl.TEXTURE_BINDING_2D,
bindFunc: func(obj uint32) {
gl.BindTexture(gl.TEXTURE_2D, obj)
},
},
width: width,
height: height,
}
gl.GenTextures(1, &tex.tex.obj)
tex.Begin()
defer tex.End()
// initial data
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(width),
int32(height),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(pixels),
)
borderColor := mgl32.Vec4{0, 0, 0, 0}
gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
tex.SetSmooth(smooth)
runtime.SetFinalizer(tex, (*Texture).delete)
return tex
}
func (t *Texture) delete() {
mainthread.CallNonBlock(func() {
gl.DeleteTextures(1, &t.tex.obj)
})
}
// ID returns the OpenGL ID of this Texture.
func (t *Texture) ID() uint32 {
return t.tex.obj
}
// Width returns the width of the Texture in pixels.
func (t *Texture) Width() int {
return t.width
}
// Height returns the height of the Texture in pixels.
func (t *Texture) Height() int {
return t.height
}
// SetPixels sets the content of a sub-region of the Texture. Pixels must be an RGBA byte sequence.
func (t *Texture) SetPixels(x, y, w, h int, pixels []uint8) {
if len(pixels) != w*h*4 {
panic("set pixels: wrong number of pixels")
}
gl.TexSubImage2D(
gl.TEXTURE_2D,
0,
int32(x),
int32(y),
int32(w),
int32(h),
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(pixels),
)
}
// Pixels returns the content of a sub-region of the Texture as an RGBA byte sequence.
func (t *Texture) Pixels(x, y, w, h int) []uint8 {
pixels := make([]uint8, t.width*t.height*4)
gl.GetTexImage(
gl.TEXTURE_2D,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(pixels),
)
subPixels := make([]uint8, w*h*4)
for i := 0; i < h; i++ {
row := pixels[(i+y)*t.width*4+x*4 : (i+y)*t.width*4+(x+w)*4]
subRow := subPixels[i*w*4 : (i+1)*w*4]
copy(subRow, row)
}
return subPixels
}
// SetSmooth sets whether the Texture should be drawn "smoothly" or "pixely".
//
// It affects how the Texture is drawn when zoomed. Smooth interpolates between the neighbour
// pixels, while pixely always chooses the nearest pixel.
func (t *Texture) SetSmooth(smooth bool) {
t.smooth = smooth
if smooth {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
} else {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
}
}
// Smooth returns whether the Texture is set to be drawn "smooth" or "pixely".
func (t *Texture) Smooth() bool {
return t.smooth
}
// Begin binds the Texture. This is necessary before using the Texture.
func (t *Texture) Begin() {
t.tex.bind()
}
// End unbinds the Texture and restores the previous one.
func (t *Texture) End() {
t.tex.restore()
}

31
vendor/github.com/faiface/glhf/util.go generated vendored Normal file
View File

@@ -0,0 +1,31 @@
package glhf
import "github.com/go-gl/gl/v3.3-core/gl"
type binder struct {
restoreLoc uint32
bindFunc func(uint32)
obj uint32
prev []uint32
}
func (b *binder) bind() *binder {
var prev int32
gl.GetIntegerv(b.restoreLoc, &prev)
b.prev = append(b.prev, uint32(prev))
if b.prev[len(b.prev)-1] != b.obj {
b.bindFunc(b.obj)
}
return b
}
func (b *binder) restore() *binder {
if b.prev[len(b.prev)-1] != b.obj {
b.bindFunc(b.prev[len(b.prev)-1])
}
b.prev = b.prev[:len(b.prev)-1]
return b
}

285
vendor/github.com/faiface/glhf/vertex.go generated vendored Normal file
View File

@@ -0,0 +1,285 @@
package glhf
import (
"fmt"
"runtime"
"github.com/faiface/mainthread"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/pkg/errors"
)
// VertexSlice points to a portion of (or possibly whole) vertex array. It is used as a pointer,
// contrary to Go's builtin slices. This is, so that append can be 'in-place'. That's for the good,
// because Begin/End-ing a VertexSlice would become super confusing, if append returned a new
// VertexSlice.
//
// It also implements all basic slice-like operations: appending, sub-slicing, etc.
//
// Note that you need to Begin a VertexSlice before getting or updating it's elements or drawing it.
// After you're done with it, you need to End it.
type VertexSlice struct {
va *vertexArray
i, j int
}
// MakeVertexSlice allocates a new vertex array with specified capacity and returns a VertexSlice
// that points to it's first len elements.
//
// Note, that a vertex array is specialized for a specific shader and can't be used with another
// shader.
func MakeVertexSlice(shader *Shader, len, cap int) *VertexSlice {
if len > cap {
panic("failed to make vertex slice: len > cap")
}
return &VertexSlice{
va: newVertexArray(shader, cap),
i: 0,
j: len,
}
}
// VertexFormat returns the format of vertex attributes inside the underlying vertex array of this
// VertexSlice.
func (vs *VertexSlice) VertexFormat() AttrFormat {
return vs.va.format
}
// Stride returns the number of float32 elements occupied by one vertex.
func (vs *VertexSlice) Stride() int {
return vs.va.stride / 4
}
// Len returns the length of the VertexSlice (number of vertices).
func (vs *VertexSlice) Len() int {
return vs.j - vs.i
}
// Cap returns the capacity of an underlying vertex array.
func (vs *VertexSlice) Cap() int {
return vs.va.cap - vs.i
}
// SetLen resizes the VertexSlice to length len.
func (vs *VertexSlice) SetLen(len int) {
vs.End() // vs must have been Begin-ed before calling this method
*vs = vs.grow(len)
vs.Begin()
}
// grow returns supplied vs with length changed to len. Allocates new underlying vertex array if
// necessary. The original content is preserved.
func (vs VertexSlice) grow(len int) VertexSlice {
if len <= vs.Cap() {
// capacity sufficient
return VertexSlice{
va: vs.va,
i: vs.i,
j: vs.i + len,
}
}
// grow the capacity
newCap := vs.Cap()
if newCap < 1024 {
newCap += newCap
} else {
newCap += newCap / 4
}
if newCap < len {
newCap = len
}
newVs := VertexSlice{
va: newVertexArray(vs.va.shader, newCap),
i: 0,
j: len,
}
// preserve the original content
newVs.Begin()
newVs.Slice(0, vs.Len()).SetVertexData(vs.VertexData())
newVs.End()
return newVs
}
// Slice returns a sub-slice of this VertexSlice covering the range [i, j) (relative to this
// VertexSlice).
//
// Note, that the returned VertexSlice shares an underlying vertex array with the original
// VertexSlice. Modifying the contents of one modifies corresponding contents of the other.
func (vs *VertexSlice) Slice(i, j int) *VertexSlice {
if i < 0 || j < i || j > vs.va.cap {
panic("failed to slice vertex slice: index out of range")
}
return &VertexSlice{
va: vs.va,
i: vs.i + i,
j: vs.i + j,
}
}
// SetVertexData sets the contents of the VertexSlice.
//
// The data is a slice of float32's, where each vertex attribute occupies a certain number of
// elements. Namely, Float occupies 1, Vec2 occupies 2, Vec3 occupies 3 and Vec4 occupies 4. The
// attribues in the data slice must be in the same order as in the vertex format of this Vertex
// Slice.
//
// If the length of vertices does not match the length of the VertexSlice, this methdo panics.
func (vs *VertexSlice) SetVertexData(data []float32) {
if len(data)/vs.Stride() != vs.Len() {
fmt.Println(len(data)/vs.Stride(), vs.Len())
panic("set vertex data: wrong length of vertices")
}
vs.va.setVertexData(vs.i, vs.j, data)
}
// VertexData returns the contents of the VertexSlice.
//
// The data is in the same format as with SetVertexData.
func (vs *VertexSlice) VertexData() []float32 {
return vs.va.vertexData(vs.i, vs.j)
}
// Draw draws the content of the VertexSlice.
func (vs *VertexSlice) Draw() {
vs.va.draw(vs.i, vs.j)
}
// Begin binds the underlying vertex array. Calling this method is necessary before using the VertexSlice.
func (vs *VertexSlice) Begin() {
vs.va.begin()
}
// End unbinds the underlying vertex array. Call this method when you're done with VertexSlice.
func (vs *VertexSlice) End() {
vs.va.end()
}
type vertexArray struct {
vao, vbo binder
cap int
format AttrFormat
stride int
offset []int
shader *Shader
}
const vertexArrayMinCap = 4
func newVertexArray(shader *Shader, cap int) *vertexArray {
if cap < vertexArrayMinCap {
cap = vertexArrayMinCap
}
va := &vertexArray{
vao: binder{
restoreLoc: gl.VERTEX_ARRAY_BINDING,
bindFunc: func(obj uint32) {
gl.BindVertexArray(obj)
},
},
vbo: binder{
restoreLoc: gl.ARRAY_BUFFER_BINDING,
bindFunc: func(obj uint32) {
gl.BindBuffer(gl.ARRAY_BUFFER, obj)
},
},
cap: cap,
format: shader.VertexFormat(),
stride: shader.VertexFormat().Size(),
offset: make([]int, len(shader.VertexFormat())),
shader: shader,
}
offset := 0
for i, attr := range va.format {
switch attr.Type {
case Float, Vec2, Vec3, Vec4:
default:
panic(errors.New("failed to create vertex array: invalid attribute type"))
}
va.offset[i] = offset
offset += attr.Type.Size()
}
gl.GenVertexArrays(1, &va.vao.obj)
va.vao.bind()
gl.GenBuffers(1, &va.vbo.obj)
defer va.vbo.bind().restore()
emptyData := make([]byte, cap*va.stride)
gl.BufferData(gl.ARRAY_BUFFER, len(emptyData), gl.Ptr(emptyData), gl.DYNAMIC_DRAW)
for i, attr := range va.format {
loc := gl.GetAttribLocation(shader.program.obj, gl.Str(attr.Name+"\x00"))
var size int32
switch attr.Type {
case Float:
size = 1
case Vec2:
size = 2
case Vec3:
size = 3
case Vec4:
size = 4
}
gl.VertexAttribPointer(
uint32(loc),
size,
gl.FLOAT,
false,
int32(va.stride),
gl.PtrOffset(va.offset[i]),
)
gl.EnableVertexAttribArray(uint32(loc))
}
va.vao.restore()
runtime.SetFinalizer(va, (*vertexArray).delete)
return va
}
func (va *vertexArray) delete() {
mainthread.CallNonBlock(func() {
gl.DeleteVertexArrays(1, &va.vao.obj)
gl.DeleteBuffers(1, &va.vbo.obj)
})
}
func (va *vertexArray) begin() {
va.vao.bind()
va.vbo.bind()
}
func (va *vertexArray) end() {
va.vbo.restore()
va.vao.restore()
}
func (va *vertexArray) draw(i, j int) {
gl.DrawArrays(gl.TRIANGLES, int32(i), int32(i+j))
}
func (va *vertexArray) setVertexData(i, j int, data []float32) {
if j-i == 0 {
// avoid setting 0 bytes of buffer data
return
}
gl.BufferSubData(gl.ARRAY_BUFFER, i*va.stride, len(data)*4, gl.Ptr(data))
}
func (va *vertexArray) vertexData(i, j int) []float32 {
if j-i == 0 {
// avoid getting 0 bytes of buffer data
return nil
}
data := make([]float32, (j-i)*va.stride/4)
gl.GetBufferSubData(gl.ARRAY_BUFFER, i*va.stride, len(data)*4, gl.Ptr(data))
return data
}

21
vendor/github.com/faiface/mainthread/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Michal Štrba
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

71
vendor/github.com/faiface/mainthread/README.md generated vendored Normal file
View File

@@ -0,0 +1,71 @@
# mainthread [![GoDoc](https://godoc.org/github.com/faiface/mainthread?status.svg)](http://godoc.org/github.com/faiface/mainthread) [![Report card](https://goreportcard.com/badge/github.com/faiface/mainthread)](https://goreportcard.com/report/github.com/faiface/mainthread)
Package mainthread allows you to run code on the main operating system thread.
`go get github.com/faiface/mainthread`
Operating systems often require, that code which deals with windows and graphics has to run on the
main thread. This is however somehow challenging in Go due to Go's concurrent nature.
This package makes it easily possible.
All you need to do is put your main code into a separate function and call `mainthread.Run` from
your real main, like this:
```go
package main
import (
"fmt"
"github.com/faiface/mainthread"
)
func run() {
// now we can run stuff on the main thread like this
mainthread.Call(func() {
fmt.Println("printing from the main thread")
})
fmt.Println("printing from another thread")
}
func main() {
mainthread.Run(run) // enables mainthread package and runs run in a separate goroutine
}
```
## More functions
If you don't wish to wait until a function finishes running on the main thread, use
`mainthread.CallNonBlock`:
```go
mainthread.CallNonBlock(func() {
fmt.Println("i'm in the main thread")
})
fmt.Println("but imma be likely printed first, cuz i don't wait")
```
If you want to get some value returned from the main thread, you can use `mainthread.CallErr` or
`mainthread.CallVal`:
```go
err := mainthread.CallErr(func() error {
return nil // i don't do nothing wrong
})
val := mainthread.CallVal(func() interface{} {
return 42 // the meaning of life, universe and everything
})
```
If `mainthread.CallErr` or `mainthread.CallVal` aren't sufficient for you, you can just assign
variables from within the main thread:
```go
var x, y int
mainthread.Call(func() {
x, y = 1, 2
})
```
However, be careful with `mainthread.CallNonBlock` when dealing with local variables.

87
vendor/github.com/faiface/mainthread/mainthread.go generated vendored Normal file
View File

@@ -0,0 +1,87 @@
package mainthread
import (
"errors"
"runtime"
)
// CallQueueCap is the capacity of the call queue. This means how many calls to CallNonBlock will not
// block until some call finishes.
//
// The default value is 16 and should be good for 99% usecases.
var CallQueueCap = 16
var (
callQueue chan func()
)
func init() {
runtime.LockOSThread()
}
func checkRun() {
if callQueue == nil {
panic(errors.New("mainthread: did not call Run"))
}
}
// Run enables mainthread package functionality. To use mainthread package, put your main function
// code into the run function (the argument to Run) and simply call Run from the real main function.
//
// Run returns when run (argument) function finishes.
func Run(run func()) {
callQueue = make(chan func(), CallQueueCap)
done := make(chan struct{})
go func() {
run()
done <- struct{}{}
}()
for {
select {
case f := <-callQueue:
f()
case <-done:
return
}
}
}
// CallNonBlock queues function f on the main thread and returns immediately. Does not wait until f
// finishes.
func CallNonBlock(f func()) {
checkRun()
callQueue <- f
}
// Call queues function f on the main thread and blocks until the function f finishes.
func Call(f func()) {
checkRun()
done := make(chan struct{})
callQueue <- func() {
f()
done <- struct{}{}
}
<-done
}
// CallErr queues function f on the main thread and returns an error returned by f.
func CallErr(f func() error) error {
checkRun()
errChan := make(chan error)
callQueue <- func() {
errChan <- f()
}
return <-errChan
}
// CallVal queues function f on the main thread and returns a value returned by f.
func CallVal(f func() interface{}) interface{} {
checkRun()
respChan := make(chan interface{})
callQueue <- func() {
respChan <- f()
}
return <-respChan
}

2
vendor/github.com/faiface/pixel/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,2 @@
test
.vscode

23
vendor/github.com/faiface/pixel/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,23 @@
language: go
sudo: false
addons:
apt:
packages:
- xorg-dev
- libx11-dev
- libxrandr-dev
- libxinerama-dev
- libxcursor-dev
- libxi-dev
- libopenal-dev
- libasound2-dev
go:
- 1.8
- 1.7.4
- tip
install:
- go get -t ./...
script:
- go test -i -race ./...
- go test -v -race ./...

14
vendor/github.com/faiface/pixel/CONTRIBUTING.md generated vendored Normal file
View File

@@ -0,0 +1,14 @@
# Contributing to Pixel
:tada: Hi! I'm really glad you're considering contributing to Pixel! :tada:
## Here are a few ways you can contribute
1. **Make a community example** and place it inside the [examples/community](examples/community) folder.
2. **Add tests**. There only few tests in Pixel at the moment. Take a look at them and make some similar.
3. **Add a small feature or an improvement**. Feel like some small feature is missing? Just make a PR. Be ready that I might reject it, though, if I don't find it particularly appealing.
4. **Join the big development** by joining the discussion at the [Gitter](https://gitter.im/pixellib/Lobby), where we can discuss bigger changes and implement them after that.
## How to make a pull request
Go gives you a nice surprise when attempting to make a PR on Github. The thing is, that when user _xyz_ forks Pixel on Github, it ends up in _github.com/xyz/pixel_, which fucks up your import paths. Here's how you deal with that: https://www.reddit.com/r/golang/comments/2jdcw1/how_do_you_deal_with_github_forking/.

21
vendor/github.com/faiface/pixel/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 Michal Štrba
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

160
vendor/github.com/faiface/pixel/README.md generated vendored Normal file
View File

@@ -0,0 +1,160 @@
# Pixel [![Build Status](https://travis-ci.org/faiface/pixel.svg?branch=master)](https://travis-ci.org/faiface/pixel) [![GoDoc](https://godoc.org/github.com/faiface/pixel?status.svg)](https://godoc.org/github.com/faiface/pixel) [![Go Report Card](https://goreportcard.com/badge/github.com/faiface/pixel)](https://goreportcard.com/report/github.com/faiface/pixel) [![Join the chat at https://gitter.im/pixellib/Lobby](https://badges.gitter.im/pixellib/Lobby.svg)](https://gitter.im/pixellib/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
A hand-crafted 2D game library in Go. Take a look into the [features](#features) to see what it can
do.
```
go get github.com/faiface/pixel
```
See [requirements](#requirements) for the list of libraries necessary for compilation.
## Tutorial
The [Wiki of this repo](https://github.com/faiface/pixel/wiki) contains an extensive tutorial
covering several topics of Pixel. Here's the content of the tutorial parts so far:
- [Creating a Window](https://github.com/faiface/pixel/wiki/Creating-a-Window)
- [Drawing a Sprite](https://github.com/faiface/pixel/wiki/Drawing-a-Sprite)
- [Moving, scaling and rotating with Matrix](https://github.com/faiface/pixel/wiki/Moving,-scaling-and-rotating-with-Matrix)
- [Pressing keys and clicking mouse](https://github.com/faiface/pixel/wiki/Pressing-keys-and-clicking-mouse)
- [Drawing efficiently with Batch](https://github.com/faiface/pixel/wiki/Drawing-efficiently-with-Batch)
- [Drawing shapes with IMDraw](https://github.com/faiface/pixel/wiki/Drawing-shapes-with-IMDraw)
- [Typing text on the screen](https://github.com/faiface/pixel/wiki/Typing-text-on-the-screen)
## Examples
The [examples](https://github.com/faiface/pixel/tree/master/examples) directory contains a few
examples demonstrating Pixel's functionality.
**To run an example**, navigate to it's directory, then `go run` the `main.go` file. For example:
```
$ cd examples/platformer
$ go run main.go
```
Here are some screenshots from the examples!
| [Lights](examples/lights) | [Platformer](examples/platformer) |
| --- | --- |
| ![Lights](examples/lights/screenshot.png) | ![Platformer](examples/platformer/screenshot.png) |
| [Smoke](examples/smoke) | [Typewriter](examples/typewriter) |
| --- | --- |
| ![Smoke](examples/smoke/screenshot.png) | ![Typewriter](examples/typewriter/screenshot.png) |
| [Raycaster](examples/community/raycaster) | [Starfield](examples/community/starfield) |
| --- | --- |
| ![Raycaster](examples/community/raycaster/screenshot.png) | ![Starfield](examples/community/starfield/screenshot.png) |
## Features
Here's the list of the main features in Pixel. Although Pixel is still under heavy development,
**there should be no major breakage in the API.** This is not a 100% guarantee, though.
- Fast 2D graphics
- Sprites
- Primitive shapes with immediate mode style
[IMDraw](https://github.com/faiface/pixel/wiki/Drawing-shapes-with-IMDraw) (circles, rectangles,
lines, ...)
- Optimized drawing with [Batch](https://github.com/faiface/pixel/wiki/Drawing-efficiently-with-Batch)
- Text drawing with [text](https://godoc.org/github.com/faiface/pixel/text) package
- Audio through a separate [Beep](https://github.com/faiface/beep) library.
- Simple and convenient API
- Drawing a sprite to a window is as simple as `sprite.Draw(window, matrix)`
- Wanna know where the center of a window is? `window.Bounds().Center()`
- [...](https://godoc.org/github.com/faiface/pixel)
- Full documentation and tutorial
- Works on Linux, macOS and Windows
- Window creation and manipulation (resizing, fullscreen, multiple windows, ...)
- Keyboard (key presses, text input) and mouse input without events
- Well integrated with the Go standard library
- Use `"image"` package for loading pictures
- Use `"time"` package for measuring delta time and FPS
- Use `"image/color"` for colors, or use Pixel's own `color.Color` format, which supports easy
multiplication and a few more features
- Pixel uses `float64` throughout the library, compatible with `"math"` package
- Geometry transformations with
[Matrix](https://github.com/faiface/pixel/wiki/Moving,-scaling-and-rotating-with-Matrix)
- Moving, scaling, rotating
- Easy camera implementation
- Off-screen drawing to Canvas or any other target (Batch, IMDraw, ...)
- Fully garbage collected, no `Close` or `Dispose` methods
- Full [Porter-Duff](http://ssp.impulsetrain.com/porterduff.html) composition, which enables
- 2D lighting
- Cutting holes into objects
- Much more...
- Pixel let's you draw stuff and do your job, it doesn't impose any particular style or paradigm
- Platform and backend independent [core](https://godoc.org/github.com/faiface/pixel)
- Core Target/Triangles/Picture pattern makes it easy to create new drawing targets that do
arbitrarily crazy stuff (e.g. graphical effects)
- Small codebase, ~5K lines of code, including the backend [glhf](https://github.com/faiface/glhf)
package
## Missing features
Pixel is in development and still missing few critical features. Here're the most critical ones.
- ~~Audio~~
- ~~Drawing text~~
- Antialiasing (filtering is supported, though)
- ~~Advanced window manipulation (cursor hiding, window icon, ...)~~
- Better support for Hi-DPI displays
- Mobile (and perhaps HTML5?) backend
- More advanced graphical effects (e.g. blur)
- Tests and benchmarks
**Implementing these features will get us to the 1.0 release.** Contribute, so that it's as soon as
possible!
## Requirements
If you're using Windows and having trouble building Pixel, please check [this
guide](https://github.com/faiface/pixel/wiki/Building-Pixel-on-Windows) on the
[wiki](https://github.com/faiface/pixel/wiki).
[PixelGL](https://godoc.org/github.com/faiface/pixel/pixelgl) backend uses OpenGL to render
graphics. Because of that, OpenGL development libraries are needed for compilation. The dependencies
are same as for [GLFW](https://github.com/go-gl/glfw).
The OpenGL version used is **OpenGL 3.3**.
- On macOS, you need Xcode or Command Line Tools for Xcode (`xcode-select --install`) for required
headers and libraries.
- On Ubuntu/Debian-like Linux distributions, you need `libgl1-mesa-dev` and `xorg-dev` packages.
- On CentOS/Fedora-like Linux distributions, you need `libX11-devel libXcursor-devel libXrandr-devel
libXinerama-devel mesa-libGL-devel libXi-devel` packages.
- See [here](http://www.glfw.org/docs/latest/compile.html#compile_deps) for full details.
**The combination of Go 1.8, macOS and latest XCode seems to be problematic** as mentioned in issue
[#7](https://github.com/faiface/pixel/issues/7). This issue is probably not related to Pixel.
**Upgrading to Go 1.8.1 fixes the issue.**
## Contributing
Pixel is in, let's say, mid-stage of development. Many of the important features are here, some are
missing. That's why **contributions are very important and welcome!** All alone, I will be able to
finish the library, but it'll take a lot of time. With your help, it'll take much less. I encourage
everyone to contribute, even with just an idea. Especially welcome are **issues** and **pull
requests**.
**However, I won't accept everything. Pixel is being developed with thought and care.** Each
component was designed and re-designed multiple times. Code and API quality is very important here.
API is focused on simplicity and expressiveness.
When contributing, keep these goals in mind. It doesn't mean that I'll only accept perfect pull
requests. It just means that I might not like your idea. Or that your pull requests could need some
rewriting. That's perfectly fine, don't let it put you off. In the end, we'll just end up with a
better result.
Take a look at [CONTRIBUTING.md](CONTRIBUTING.md) for further information.
For any kind of discussion, feel free to use our
[Gitter](https://gitter.im/pixellib/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
community.
## License
[MIT](LICENSE)

163
vendor/github.com/faiface/pixel/batch.go generated vendored Normal file
View File

@@ -0,0 +1,163 @@
package pixel
import (
"fmt"
"image/color"
)
// Batch is a Target that allows for efficient drawing of many objects with the same Picture.
//
// To put an object into a Batch, just draw it onto it:
// object.Draw(batch)
type Batch struct {
cont Drawer
mat Matrix
col RGBA
}
var _ BasicTarget = (*Batch)(nil)
// NewBatch creates an empty Batch with the specified Picture and container.
//
// The container is where objects get accumulated. Batch will support precisely those Triangles
// properties, that the supplied container supports. If you retain access to the container and
// change it, call Dirty to notify Batch about the change.
//
// Note, that if the container does not support TrianglesColor, color masking will not work.
func NewBatch(container Triangles, pic Picture) *Batch {
b := &Batch{cont: Drawer{Triangles: container, Picture: pic}}
b.SetMatrix(IM)
b.SetColorMask(Alpha(1))
return b
}
// Dirty notifies Batch about an external modification of it's container. If you retain access to
// the Batch's container and change it, call Dirty to notify Batch about the change.
//
// container := &pixel.TrianglesData{}
// batch := pixel.NewBatch(container, nil)
// container.SetLen(10) // container changed from outside of Batch
// batch.Dirty() // notify Batch about the change
func (b *Batch) Dirty() {
b.cont.Dirty()
}
// Clear removes all objects from the Batch.
func (b *Batch) Clear() {
b.cont.Triangles.SetLen(0)
b.cont.Dirty()
}
// Draw draws all objects that are currently in the Batch onto another Target.
func (b *Batch) Draw(t Target) {
b.cont.Draw(t)
}
// SetMatrix sets a Matrix that every point will be projected by.
func (b *Batch) SetMatrix(m Matrix) {
b.mat = m
}
// SetColorMask sets a mask color used in the following draws onto the Batch.
func (b *Batch) SetColorMask(c color.Color) {
if c == nil {
b.col = Alpha(1)
return
}
b.col = ToRGBA(c)
}
// MakeTriangles returns a specialized copy of the provided Triangles that draws onto this Batch.
func (b *Batch) MakeTriangles(t Triangles) TargetTriangles {
bt := &batchTriangles{
tri: t.Copy(),
tmp: MakeTrianglesData(t.Len()),
dst: b,
}
return bt
}
// MakePicture returns a specialized copy of the provided Picture that draws onto this Batch.
func (b *Batch) MakePicture(p Picture) TargetPicture {
if p != b.cont.Picture {
panic(fmt.Errorf("(%T).MakePicture: Picture is not the Batch's Picture", b))
}
bp := &batchPicture{
pic: p,
dst: b,
}
return bp
}
type batchTriangles struct {
tri Triangles
tmp *TrianglesData
dst *Batch
}
func (bt *batchTriangles) Len() int {
return bt.tri.Len()
}
func (bt *batchTriangles) SetLen(len int) {
bt.tri.SetLen(len)
bt.tmp.SetLen(len)
}
func (bt *batchTriangles) Slice(i, j int) Triangles {
return &batchTriangles{
tri: bt.tri.Slice(i, j),
tmp: bt.tmp.Slice(i, j).(*TrianglesData),
dst: bt.dst,
}
}
func (bt *batchTriangles) Update(t Triangles) {
bt.tri.Update(t)
}
func (bt *batchTriangles) Copy() Triangles {
return &batchTriangles{
tri: bt.tri.Copy(),
tmp: bt.tmp.Copy().(*TrianglesData),
dst: bt.dst,
}
}
func (bt *batchTriangles) draw(bp *batchPicture) {
bt.tmp.Update(bt.tri)
for i := range *bt.tmp {
(*bt.tmp)[i].Position = bt.dst.mat.Project((*bt.tmp)[i].Position)
(*bt.tmp)[i].Color = bt.dst.col.Mul((*bt.tmp)[i].Color)
}
cont := bt.dst.cont.Triangles
cont.SetLen(cont.Len() + bt.tri.Len())
added := cont.Slice(cont.Len()-bt.tri.Len(), cont.Len())
added.Update(bt.tri)
added.Update(bt.tmp)
bt.dst.cont.Dirty()
}
func (bt *batchTriangles) Draw() {
bt.draw(nil)
}
type batchPicture struct {
pic Picture
dst *Batch
}
func (bp *batchPicture) Bounds() Rect {
return bp.pic.Bounds()
}
func (bp *batchPicture) Draw(t TargetTriangles) {
bt := t.(*batchTriangles)
if bp.dst != bt.dst {
panic(fmt.Errorf("(%T).Draw: TargetTriangles generated by different Batch", bp))
}
bt.draw(bp)
}

97
vendor/github.com/faiface/pixel/color.go generated vendored Normal file
View File

@@ -0,0 +1,97 @@
package pixel
import "image/color"
// RGBA represents an alpha-premultiplied RGBA color with components within range [0, 1].
//
// The difference between color.RGBA is that the value range is [0, 1] and the values are floats.
type RGBA struct {
R, G, B, A float64
}
// RGB returns a fully opaque RGBA color with the given RGB values.
//
// A common way to construct a transparent color is to create one with RGB constructor, then
// multiply it by a color obtained from the Alpha constructor.
func RGB(r, g, b float64) RGBA {
return RGBA{r, g, b, 1}
}
// Alpha returns a white RGBA color with the given alpha component.
func Alpha(a float64) RGBA {
return RGBA{a, a, a, a}
}
// Add adds color d to color c component-wise and returns the result (the components are not
// clamped).
func (c RGBA) Add(d RGBA) RGBA {
return RGBA{
R: c.R + d.R,
G: c.G + d.G,
B: c.B + d.B,
A: c.A + d.A,
}
}
// Sub subtracts color d from color c component-wise and returns the result (the components
// are not clamped).
func (c RGBA) Sub(d RGBA) RGBA {
return RGBA{
R: c.R - d.R,
G: c.G - d.G,
B: c.B - d.B,
A: c.A - d.A,
}
}
// Mul multiplies color c by color d component-wise (the components are not clamped).
func (c RGBA) Mul(d RGBA) RGBA {
return RGBA{
R: c.R * d.R,
G: c.G * d.G,
B: c.B * d.B,
A: c.A * d.A,
}
}
// Scaled multiplies each component of color c by scale and returns the result (the components
// are not clamped).
func (c RGBA) Scaled(scale float64) RGBA {
return RGBA{
R: c.R * scale,
G: c.G * scale,
B: c.B * scale,
A: c.A * scale,
}
}
// RGBA returns alpha-premultiplied red, green, blue and alpha components of the RGBA color.
func (c RGBA) RGBA() (r, g, b, a uint32) {
r = uint32(0xffff * c.R)
g = uint32(0xffff * c.G)
b = uint32(0xffff * c.B)
a = uint32(0xffff * c.A)
return
}
// ToRGBA converts a color to RGBA format. Using this function is preferred to using RGBAModel, for
// performance (using RGBAModel introduces additional unnecessary allocations).
func ToRGBA(c color.Color) RGBA {
if c, ok := c.(RGBA); ok {
return c
}
r, g, b, a := c.RGBA()
return RGBA{
float64(r) / 0xffff,
float64(g) / 0xffff,
float64(b) / 0xffff,
float64(a) / 0xffff,
}
}
// RGBAModel converts colors to RGBA format.
var RGBAModel = color.ModelFunc(rgbaModel)
func rgbaModel(c color.Color) color.Color {
return ToRGBA(c)
}

65
vendor/github.com/faiface/pixel/compose.go generated vendored Normal file
View File

@@ -0,0 +1,65 @@
package pixel
import "errors"
// ComposeTarget is a BasicTarget capable of Porter-Duff composition.
type ComposeTarget interface {
BasicTarget
// SetComposeMethod sets a Porter-Duff composition method to be used.
SetComposeMethod(ComposeMethod)
}
// ComposeMethod is a Porter-Duff composition method.
type ComposeMethod int
// Here's the list of all available Porter-Duff composition methods. Use ComposeOver for the basic
// alpha blending.
const (
ComposeOver ComposeMethod = iota
ComposeIn
ComposeOut
ComposeAtop
ComposeRover
ComposeRin
ComposeRout
ComposeRatop
ComposeXor
ComposePlus
ComposeCopy
)
// Compose composes two colors together according to the ComposeMethod. A is the foreground, B is
// the background.
func (cm ComposeMethod) Compose(a, b RGBA) RGBA {
var fa, fb float64
switch cm {
case ComposeOver:
fa, fb = 1, 1-a.A
case ComposeIn:
fa, fb = b.A, 0
case ComposeOut:
fa, fb = 1-b.A, 0
case ComposeAtop:
fa, fb = b.A, 1-a.A
case ComposeRover:
fa, fb = 1-b.A, 1
case ComposeRin:
fa, fb = 0, a.A
case ComposeRout:
fa, fb = 0, 1-a.A
case ComposeRatop:
fa, fb = 1-b.A, a.A
case ComposeXor:
fa, fb = 1-b.A, 1-a.A
case ComposePlus:
fa, fb = 1, 1
case ComposeCopy:
fa, fb = 1, 0
default:
panic(errors.New("Compose: invalid ComposeMethod"))
}
return a.Mul(Alpha(fa)).Add(b.Mul(Alpha(fb)))
}

276
vendor/github.com/faiface/pixel/data.go generated vendored Normal file
View File

@@ -0,0 +1,276 @@
package pixel
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
)
// TrianglesData specifies a list of Triangles vertices with three common properties:
// TrianglesPosition, TrianglesColor and TrianglesPicture.
type TrianglesData []struct {
Position Vec
Color RGBA
Picture Vec
Intensity float64
}
// MakeTrianglesData creates TrianglesData of length len initialized with default property values.
//
// Prefer this function to make(TrianglesData, len), because make zeros them, while this function
// does the correct intialization.
func MakeTrianglesData(len int) *TrianglesData {
td := &TrianglesData{}
td.SetLen(len)
return td
}
// Len returns the number of vertices in TrianglesData.
func (td *TrianglesData) Len() int {
return len(*td)
}
// SetLen resizes TrianglesData to len, while keeping the original content.
//
// If len is greater than TrianglesData's current length, the new data is filled with default
// values ((0, 0), white, (0, 0), 0).
func (td *TrianglesData) SetLen(len int) {
if len > td.Len() {
needAppend := len - td.Len()
for i := 0; i < needAppend; i++ {
*td = append(*td, struct {
Position Vec
Color RGBA
Picture Vec
Intensity float64
}{Color: RGBA{1, 1, 1, 1}})
}
}
if len < td.Len() {
*td = (*td)[:len]
}
}
// Slice returns a sub-Triangles of this TrianglesData.
func (td *TrianglesData) Slice(i, j int) Triangles {
s := TrianglesData((*td)[i:j])
return &s
}
func (td *TrianglesData) updateData(t Triangles) {
// fast path optimization
if t, ok := t.(*TrianglesData); ok {
copy(*td, *t)
return
}
// slow path manual copy
if t, ok := t.(TrianglesPosition); ok {
for i := range *td {
(*td)[i].Position = t.Position(i)
}
}
if t, ok := t.(TrianglesColor); ok {
for i := range *td {
(*td)[i].Color = t.Color(i)
}
}
if t, ok := t.(TrianglesPicture); ok {
for i := range *td {
(*td)[i].Picture, (*td)[i].Intensity = t.Picture(i)
}
}
}
// Update copies vertex properties from the supplied Triangles into this TrianglesData.
//
// TrianglesPosition, TrianglesColor and TrianglesTexture are supported.
func (td *TrianglesData) Update(t Triangles) {
if td.Len() != t.Len() {
panic(fmt.Errorf("(%T).Update: invalid triangles length", td))
}
td.updateData(t)
}
// Copy returns an exact independent copy of this TrianglesData.
func (td *TrianglesData) Copy() Triangles {
copyTd := TrianglesData{}
copyTd.SetLen(td.Len())
copyTd.Update(td)
return &copyTd
}
// Position returns the position property of i-th vertex.
func (td *TrianglesData) Position(i int) Vec {
return (*td)[i].Position
}
// Color returns the color property of i-th vertex.
func (td *TrianglesData) Color(i int) RGBA {
return (*td)[i].Color
}
// Picture returns the picture property of i-th vertex.
func (td *TrianglesData) Picture(i int) (pic Vec, intensity float64) {
return (*td)[i].Picture, (*td)[i].Intensity
}
// PictureData specifies an in-memory rectangular area of pixels and implements Picture and
// PictureColor.
//
// Pixels are small rectangles of unit size of form (x, y, x+1, y+1), where x and y are integers.
// PictureData contains and assigns a color to all pixels that are at least partially contained
// within it's Bounds (Rect).
//
// The struct's innards are exposed for convenience, manual modification is at your own risk.
//
// The format of the pixels is color.RGBA and not pixel.RGBA for a very serious reason:
// pixel.RGBA takes up 8x more memory than color.RGBA.
type PictureData struct {
Pix []color.RGBA
Stride int
Rect Rect
}
// MakePictureData creates a zero-initialized PictureData covering the given rectangle.
func MakePictureData(rect Rect) *PictureData {
w := int(math.Ceil(rect.Max.X)) - int(math.Floor(rect.Min.X))
h := int(math.Ceil(rect.Max.Y)) - int(math.Floor(rect.Min.Y))
pd := &PictureData{
Stride: w,
Rect: rect,
}
pd.Pix = make([]color.RGBA, w*h)
return pd
}
func verticalFlip(rgba *image.RGBA) {
bounds := rgba.Bounds()
width := bounds.Dx()
tmpRow := make([]uint8, width*4)
for i, j := 0, bounds.Dy()-1; i < j; i, j = i+1, j-1 {
iRow := rgba.Pix[i*rgba.Stride : i*rgba.Stride+width*4]
jRow := rgba.Pix[j*rgba.Stride : j*rgba.Stride+width*4]
copy(tmpRow, iRow)
copy(iRow, jRow)
copy(jRow, tmpRow)
}
}
// PictureDataFromImage converts an image.Image into PictureData.
//
// The resulting PictureData's Bounds will be the equivalent of the supplied image.Image's Bounds.
func PictureDataFromImage(img image.Image) *PictureData {
var rgba *image.RGBA
if rgbaImg, ok := img.(*image.RGBA); ok {
rgba = rgbaImg
} else {
rgba = image.NewRGBA(img.Bounds())
draw.Draw(rgba, rgba.Bounds(), img, img.Bounds().Min, draw.Src)
}
verticalFlip(rgba)
pd := MakePictureData(R(
float64(rgba.Bounds().Min.X),
float64(rgba.Bounds().Min.Y),
float64(rgba.Bounds().Max.X),
float64(rgba.Bounds().Max.Y),
))
for i := range pd.Pix {
pd.Pix[i].R = rgba.Pix[i*4+0]
pd.Pix[i].G = rgba.Pix[i*4+1]
pd.Pix[i].B = rgba.Pix[i*4+2]
pd.Pix[i].A = rgba.Pix[i*4+3]
}
return pd
}
// PictureDataFromPicture converts an arbitrary Picture into PictureData (the conversion may be
// lossy, because PictureData works with unit-sized pixels).
//
// Bounds are preserved.
func PictureDataFromPicture(pic Picture) *PictureData {
if pd, ok := pic.(*PictureData); ok {
return pd
}
bounds := pic.Bounds()
pd := MakePictureData(bounds)
if pic, ok := pic.(PictureColor); ok {
for y := math.Floor(bounds.Min.Y); y < bounds.Max.Y; y++ {
for x := math.Floor(bounds.Min.X); x < bounds.Max.X; x++ {
// this together with the Floor is a trick to get all of the pixels
at := V(
math.Max(x, bounds.Min.X),
math.Max(y, bounds.Min.Y),
)
col := pic.Color(at)
pd.Pix[pd.Index(at)] = color.RGBA{
R: uint8(col.R * 255),
G: uint8(col.G * 255),
B: uint8(col.B * 255),
A: uint8(col.A * 255),
}
}
}
}
return pd
}
// Image converts PictureData into an image.RGBA.
//
// The resulting image.RGBA's Bounds will be equivalent of the PictureData's Bounds.
func (pd *PictureData) Image() *image.RGBA {
bounds := image.Rect(
int(math.Floor(pd.Rect.Min.X)),
int(math.Floor(pd.Rect.Min.Y)),
int(math.Ceil(pd.Rect.Max.X)),
int(math.Ceil(pd.Rect.Max.Y)),
)
rgba := image.NewRGBA(bounds)
i := 0
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
off := pd.Index(V(float64(x), float64(y)))
rgba.Pix[i*4+0] = pd.Pix[off].R
rgba.Pix[i*4+1] = pd.Pix[off].G
rgba.Pix[i*4+2] = pd.Pix[off].B
rgba.Pix[i*4+3] = pd.Pix[off].A
i++
}
}
verticalFlip(rgba)
return rgba
}
// Index returns the index of the pixel at the specified position inside the Pix slice.
func (pd *PictureData) Index(at Vec) int {
at = at.Sub(pd.Rect.Min.Map(math.Floor))
x, y := int(at.X), int(at.Y)
return y*pd.Stride + x
}
// Bounds returns the bounds of this PictureData.
func (pd *PictureData) Bounds() Rect {
return pd.Rect
}
// Color returns the color located at the given position.
func (pd *PictureData) Color(at Vec) RGBA {
if !pd.Rect.Contains(at) {
return RGBA{0, 0, 0, 0}
}
return ToRGBA(pd.Pix[pd.Index(at)])
}

7
vendor/github.com/faiface/pixel/doc.go generated vendored Normal file
View File

@@ -0,0 +1,7 @@
// Package pixel implements platform and backend agnostic core of the Pixel game development
// library.
//
// It specifies the core Target, Triangles, Picture pattern and implements standard elements, such
// as Sprite, Batch, Vec, Matrix and RGBA in addition to the basic Triangles and Picture
// implementations: TrianglesData and PictureData.
package pixel

96
vendor/github.com/faiface/pixel/drawer.go generated vendored Normal file
View File

@@ -0,0 +1,96 @@
package pixel
// Drawer glues all the fundamental interfaces (Target, Triangles, Picture) into a coherent and the
// only intended usage pattern.
//
// Drawer makes it possible to draw any combination of Triangles and Picture onto any Target
// efficiently.
//
// To create a Drawer, just assign it's Triangles and Picture fields:
//
// d := pixel.Drawer{Triangles: t, Picture: p}
//
// If Triangles is nil, nothing will be drawn. If Picture is nil, Triangles will be drawn without a
// Picture.
//
// Whenever you change the Triangles, call Dirty to notify Drawer that Triangles changed. You don't
// need to notify Drawer about a change of the Picture.
//
// Note, that Drawer caches the results of MakePicture from Targets it's drawn to for each Picture
// it's set to. What it means is that using a Drawer with an unbounded number of Pictures leads to a
// memory leak, since Drawer caches them and never forgets. In such a situation, create a new Drawer
// for each Picture.
type Drawer struct {
Triangles Triangles
Picture Picture
targets map[Target]*drawerTarget
inited bool
}
type drawerTarget struct {
tris TargetTriangles
pics map[Picture]TargetPicture
clean bool
}
func (d *Drawer) lazyInit() {
if !d.inited {
d.targets = make(map[Target]*drawerTarget)
d.inited = true
}
}
// Dirty marks the Triangles of this Drawer as changed. If not called, changes will not be visible
// when drawing.
func (d *Drawer) Dirty() {
d.lazyInit()
for _, t := range d.targets {
t.clean = false
}
}
// Draw efficiently draws Triangles with Picture onto the provided Target.
//
// If Triangles is nil, nothing will be drawn. If Picture is nil, Triangles will be drawn without a
// Picture.
func (d *Drawer) Draw(t Target) {
d.lazyInit()
if d.Triangles == nil {
return
}
dt := d.targets[t]
if dt == nil {
dt = &drawerTarget{
pics: make(map[Picture]TargetPicture),
}
d.targets[t] = dt
}
if dt.tris == nil {
dt.tris = t.MakeTriangles(d.Triangles)
dt.clean = true
}
if !dt.clean {
dt.tris.SetLen(d.Triangles.Len())
dt.tris.Update(d.Triangles)
dt.clean = true
}
if d.Picture == nil {
dt.tris.Draw()
return
}
pic := dt.pics[d.Picture]
if pic == nil {
pic = t.MakePicture(d.Picture)
dt.pics[d.Picture] = pic
}
pic.Draw(dt.tris)
}

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 stephen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

405
vendor/github.com/faiface/pixel/geometry.go generated vendored Normal file
View File

@@ -0,0 +1,405 @@
package pixel
import (
"fmt"
"math"
)
// Clamp returns x clamped to the interval [min, max].
//
// If x is less than min, min is returned. If x is more than max, max is returned. Otherwise, x is
// returned.
func Clamp(x, min, max float64) float64 {
if x < min {
return min
}
if x > max {
return max
}
return x
}
// Vec is a 2D vector type with X and Y coordinates.
//
// Create vectors with the V constructor:
//
// u := pixel.V(1, 2)
// v := pixel.V(8, -3)
//
// Use various methods to manipulate them:
//
// w := u.Add(v)
// fmt.Println(w) // Vec(9, -1)
// fmt.Println(u.Sub(v)) // Vec(-7, 5)
// u = pixel.V(2, 3)
// v = pixel.V(8, 1)
// if u.X < 0 {
// fmt.Println("this won't happen")
// }
// x := u.Unit().Dot(v.Unit())
type Vec struct {
X, Y float64
}
// ZV is a zero vector.
var ZV = Vec{0, 0}
// V returns a new 2D vector with the given coordinates.
func V(x, y float64) Vec {
return Vec{x, y}
}
// Unit returns a vector of length 1 facing the given angle.
func Unit(angle float64) Vec {
return Vec{1, 0}.Rotated(angle)
}
// String returns the string representation of the vector u.
//
// u := pixel.V(4.5, -1.3)
// u.String() // returns "Vec(4.5, -1.3)"
// fmt.Println(u) // Vec(4.5, -1.3)
func (u Vec) String() string {
return fmt.Sprintf("Vec(%v, %v)", u.X, u.Y)
}
// XY returns the components of the vector in two return values.
func (u Vec) XY() (x, y float64) {
return u.X, u.Y
}
// Add returns the sum of vectors u and v.
func (u Vec) Add(v Vec) Vec {
return Vec{
u.X + v.X,
u.Y + v.Y,
}
}
// Sub returns the difference betweeen vectors u and v.
func (u Vec) Sub(v Vec) Vec {
return Vec{
u.X - v.X,
u.Y - v.Y,
}
}
// To returns the vector from u to v. Equivalent to v.Sub(u).
func (u Vec) To(v Vec) Vec {
return Vec{
v.X - u.X,
v.Y - u.Y,
}
}
// Scaled returns the vector u multiplied by c.
func (u Vec) Scaled(c float64) Vec {
return Vec{u.X * c, u.Y * c}
}
// ScaledXY returns the vector u multiplied by the vector v component-wise.
func (u Vec) ScaledXY(v Vec) Vec {
return Vec{u.X * v.X, u.Y * v.Y}
}
// Len returns the length of the vector u.
func (u Vec) Len() float64 {
return math.Hypot(u.X, u.Y)
}
// Angle returns the angle between the vector u and the x-axis. The result is in range [-Pi, Pi].
func (u Vec) Angle() float64 {
return math.Atan2(u.Y, u.X)
}
// Unit returns a vector of length 1 facing the direction of u (has the same angle).
func (u Vec) Unit() Vec {
if u.X == 0 && u.Y == 0 {
return Vec{1, 0}
}
return u.Scaled(1 / u.Len())
}
// Rotated returns the vector u rotated by the given angle in radians.
func (u Vec) Rotated(angle float64) Vec {
sin, cos := math.Sincos(angle)
return Vec{
u.X*cos - u.Y*sin,
u.X*sin + u.Y*cos,
}
}
// Normal returns a vector normal to u. Equivalent to u.Rotated(math.Pi / 2), but faster.
func (u Vec) Normal() Vec {
return Vec{-u.Y, u.X}
}
// Dot returns the dot product of vectors u and v.
func (u Vec) Dot(v Vec) float64 {
return u.X*v.X + u.Y*v.Y
}
// Cross return the cross product of vectors u and v.
func (u Vec) Cross(v Vec) float64 {
return u.X*v.Y - v.X*u.Y
}
// Project returns a projection (or component) of vector u in the direction of vector v.
//
// Behaviour is undefined if v is a zero vector.
func (u Vec) Project(v Vec) Vec {
len := u.Dot(v) / v.Len()
return v.Unit().Scaled(len)
}
// Map applies the function f to both x and y components of the vector u and returns the modified
// vector.
//
// u := pixel.V(10.5, -1.5)
// v := u.Map(math.Floor) // v is Vec(10, -2), both components of u floored
func (u Vec) Map(f func(float64) float64) Vec {
return Vec{
f(u.X),
f(u.Y),
}
}
// Lerp returns a linear interpolation between vectors a and b.
//
// This function basically returns a point along the line between a and b and t chooses which one.
// If t is 0, then a will be returned, if t is 1, b will be returned. Anything between 0 and 1 will
// return the appropriate point between a and b and so on.
func Lerp(a, b Vec, t float64) Vec {
return a.Scaled(1 - t).Add(b.Scaled(t))
}
// Rect is a 2D rectangle aligned with the axes of the coordinate system. It is defined by two
// points, Min and Max.
//
// The invariant should hold, that Max's components are greater or equal than Min's components
// respectively.
type Rect struct {
Min, Max Vec
}
// R returns a new Rect with given the Min and Max coordinates.
//
// Note that the returned rectangle is not automatically normalized.
func R(minX, minY, maxX, maxY float64) Rect {
return Rect{
Min: Vec{minX, minY},
Max: Vec{maxX, maxY},
}
}
// String returns the string representation of the Rect.
//
// r := pixel.R(100, 50, 200, 300)
// r.String() // returns "Rect(100, 50, 200, 300)"
// fmt.Println(r) // Rect(100, 50, 200, 300)
func (r Rect) String() string {
return fmt.Sprintf("Rect(%v, %v, %v, %v)", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
}
// Norm returns the Rect in normal form, such that Max is component-wise greater or equal than Min.
func (r Rect) Norm() Rect {
return Rect{
Min: Vec{
math.Min(r.Min.X, r.Max.X),
math.Min(r.Min.Y, r.Max.Y),
},
Max: Vec{
math.Max(r.Min.X, r.Max.X),
math.Max(r.Min.Y, r.Max.Y),
},
}
}
// W returns the width of the Rect.
func (r Rect) W() float64 {
return r.Max.X - r.Min.X
}
// H returns the height of the Rect.
func (r Rect) H() float64 {
return r.Max.Y - r.Min.Y
}
// Size returns the vector of width and height of the Rect.
func (r Rect) Size() Vec {
return V(r.W(), r.H())
}
// Area returns the area of r. If r is not normalized, area may be negative.
func (r Rect) Area() float64 {
return r.W() * r.H()
}
// Center returns the position of the center of the Rect.
func (r Rect) Center() Vec {
return Lerp(r.Min, r.Max, 0.5)
}
// Moved returns the Rect moved (both Min and Max) by the given vector delta.
func (r Rect) Moved(delta Vec) Rect {
return Rect{
Min: r.Min.Add(delta),
Max: r.Max.Add(delta),
}
}
// Resized returns the Rect resized to the given size while keeping the position of the given
// anchor.
//
// r.Resized(r.Min, size) // resizes while keeping the position of the lower-left corner
// r.Resized(r.Max, size) // same with the top-right corner
// r.Resized(r.Center(), size) // resizes around the center
//
// This function does not make sense for resizing a rectangle of zero area and will panic. Use
// ResizedMin in the case of zero area.
func (r Rect) Resized(anchor, size Vec) Rect {
if r.W()*r.H() == 0 {
panic(fmt.Errorf("(%T).Resize: zero area", r))
}
fraction := Vec{size.X / r.W(), size.Y / r.H()}
return Rect{
Min: anchor.Add(r.Min.Sub(anchor).ScaledXY(fraction)),
Max: anchor.Add(r.Max.Sub(anchor).ScaledXY(fraction)),
}
}
// ResizedMin returns the Rect resized to the given size while keeping the position of the Rect's
// Min.
//
// Sizes of zero area are safe here.
func (r Rect) ResizedMin(size Vec) Rect {
return Rect{
Min: r.Min,
Max: r.Min.Add(size),
}
}
// Contains checks whether a vector u is contained within this Rect (including it's borders).
func (r Rect) Contains(u Vec) bool {
return r.Min.X <= u.X && u.X <= r.Max.X && r.Min.Y <= u.Y && u.Y <= r.Max.Y
}
// Union returns the minimal Rect which covers both r and s. Rects r and s must be normalized.
func (r Rect) Union(s Rect) Rect {
return R(
math.Min(r.Min.X, s.Min.X),
math.Min(r.Min.Y, s.Min.Y),
math.Max(r.Max.X, s.Max.X),
math.Max(r.Max.Y, s.Max.Y),
)
}
// Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized.
//
// If r and s don't overlap, this function returns R(0, 0, 0, 0).
func (r Rect) Intersect(s Rect) Rect {
t := R(
math.Max(r.Min.X, s.Min.X),
math.Max(r.Min.Y, s.Min.Y),
math.Min(r.Max.X, s.Max.X),
math.Min(r.Max.Y, s.Max.Y),
)
if t.Min.X >= t.Max.X || t.Min.Y >= t.Max.Y {
return Rect{}
}
return t
}
// Matrix is a 3x2 affine matrix that can be used for all kinds of spatial transforms, such
// as movement, scaling and rotations.
//
// Matrix has a handful of useful methods, each of which adds a transformation to the matrix. For
// example:
//
// pixel.IM.Moved(pixel.V(100, 200)).Rotated(pixel.ZV, math.Pi/2)
//
// This code creates a Matrix that first moves everything by 100 units horizontally and 200 units
// vertically and then rotates everything by 90 degrees around the origin.
//
// Layout is:
// [0] [2] [4]
// [1] [3] [5]
// 0 0 1 (implicit row)
type Matrix [6]float64
// IM stands for identity matrix. Does nothing, no transformation.
var IM = Matrix{1, 0, 0, 1, 0, 0}
// String returns a string representation of the Matrix.
//
// m := pixel.IM
// fmt.Println(m) // Matrix(1 0 0 | 0 1 0)
func (m Matrix) String() string {
return fmt.Sprintf(
"Matrix(%v %v %v | %v %v %v)",
m[0], m[2], m[4],
m[1], m[3], m[5],
)
}
// Moved moves everything by the delta vector.
func (m Matrix) Moved(delta Vec) Matrix {
m[4], m[5] = m[4]+delta.X, m[5]+delta.Y
return m
}
// ScaledXY scales everything around a given point by the scale factor in each axis respectively.
func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix {
m[4], m[5] = m[4]-around.X, m[5]-around.Y
m[0], m[2], m[4] = m[0]*scale.X, m[2]*scale.X, m[4]*scale.X
m[1], m[3], m[5] = m[1]*scale.Y, m[3]*scale.Y, m[5]*scale.Y
m[4], m[5] = m[4]+around.X, m[5]+around.Y
return m
}
// Scaled scales everything around a given point by the scale factor.
func (m Matrix) Scaled(around Vec, scale float64) Matrix {
return m.ScaledXY(around, V(scale, scale))
}
// Rotated rotates everything around a given point by the given angle in radians.
func (m Matrix) Rotated(around Vec, angle float64) Matrix {
sint, cost := math.Sincos(angle)
m[4], m[5] = m[4]-around.X, m[5]-around.Y
m = m.Chained(Matrix{cost, sint, -sint, cost, 0, 0})
m[4], m[5] = m[4]+around.X, m[5]+around.Y
return m
}
// Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied
// after the transformations of this Matrix.
func (m Matrix) Chained(next Matrix) Matrix {
return Matrix{
next[0]*m[0] + next[2]*m[1],
next[1]*m[0] + next[3]*m[1],
next[0]*m[2] + next[2]*m[3],
next[1]*m[2] + next[3]*m[3],
next[0]*m[4] + next[2]*m[5] + next[4],
next[1]*m[4] + next[3]*m[5] + next[5],
}
}
// Project applies all transformations added to the Matrix to a vector u and returns the result.
//
// Time complexity is O(1).
func (m Matrix) Project(u Vec) Vec {
return Vec{m[0]*u.X + m[2]*u.Y + m[4], m[1]*u.X + m[3]*u.Y + m[5]}
}
// Unproject does the inverse operation to Project.
//
// It turns out that multiplying a vector by the inverse matrix of m can be nearly-accomplished by
// subtracting the translate part of the matrix and multplying by the inverse of the top-left 2x2
// matrix, and the inverse of a 2x2 matrix is simple enough to just be inlined in the computation.
//
// Time complexity is O(1).
func (m Matrix) Unproject(u Vec) Vec {
d := (m[0] * m[3]) - (m[1] * m[2])
u.X, u.Y = (u.X-m[4])/d, (u.Y-m[5])/d
return Vec{u.X*m[3] - u.Y*m[1], u.Y*m[0] - u.X*m[2]}
}

627
vendor/github.com/faiface/pixel/imdraw/imdraw.go generated vendored Normal file
View File

@@ -0,0 +1,627 @@
// Package imdraw implements a basic primitive geometry shape and pictured polygon drawing for Pixel
// with a nice immediate-mode-like API.
package imdraw
import (
"image/color"
"math"
"github.com/faiface/pixel"
)
// IMDraw is an immediate-mode-like shape drawer and BasicTarget. IMDraw supports TrianglesPosition,
// TrianglesColor, TrianglesPicture and PictureColor.
//
// IMDraw, other than a regular BasicTarget, is used to draw shapes. To draw shapes, you first need
// to Push some points to IMDraw:
//
// imd := pixel.NewIMDraw(pic) // use nil pic if you only want to draw primitive shapes
// imd.Push(pixel.V(100, 100))
// imd.Push(pixel.V(500, 100))
//
// Once you have Pushed some points, you can use them to draw a shape, such as a line:
//
// imd.Line(20) // draws a 20 units thick line
//
// Set exported fields to change properties of Pushed points:
//
// imd.Color = pixel.RGB(1, 0, 0)
// imd.Push(pixel.V(200, 200))
// imd.Circle(400, 0)
//
// Here is the list of all available point properties (need to be set before Pushing a point):
// - Color - applies to all
// - Picture - coordinates, only applies to filled polygons
// - Intensity - picture intensity, only applies to filled polygons
// - Precision - curve drawing precision, only applies to circles and ellipses
// - EndShape - shape of the end of a line, only applies to lines and outlines
//
// And here's the list of all shapes that can be drawn (all, except for line, can be filled or
// outlined):
// - Line
// - Polygon
// - Circle
// - Circle arc
// - Ellipse
// - Ellipse arc
type IMDraw struct {
Color color.Color
Picture pixel.Vec
Intensity float64
Precision int
EndShape EndShape
points []point
pool [][]point
matrix pixel.Matrix
mask pixel.RGBA
tri *pixel.TrianglesData
batch *pixel.Batch
}
var _ pixel.BasicTarget = (*IMDraw)(nil)
type point struct {
pos pixel.Vec
col pixel.RGBA
pic pixel.Vec
in float64
precision int
endshape EndShape
}
// EndShape specifies the shape of an end of a line or a curve.
type EndShape int
const (
// NoEndShape leaves a line point with no special end shape.
NoEndShape EndShape = iota
// SharpEndShape is a sharp triangular end shape.
SharpEndShape
// RoundEndShape is a circular end shape.
RoundEndShape
)
// New creates a new empty IMDraw. An optional Picture can be used to draw with a Picture.
//
// If you just want to draw primitive shapes, pass nil as the Picture.
func New(pic pixel.Picture) *IMDraw {
tri := &pixel.TrianglesData{}
im := &IMDraw{
tri: tri,
batch: pixel.NewBatch(tri, pic),
}
im.SetMatrix(pixel.IM)
im.SetColorMask(pixel.Alpha(1))
im.Reset()
return im
}
// Clear removes all drawn shapes from the IM. This does not remove Pushed points.
func (imd *IMDraw) Clear() {
imd.tri.SetLen(0)
imd.batch.Dirty()
}
// Reset restores all point properties to defaults and removes all Pushed points.
//
// This does not affect matrix and color mask set by SetMatrix and SetColorMask.
func (imd *IMDraw) Reset() {
imd.points = imd.points[:0]
imd.Color = pixel.Alpha(1)
imd.Picture = pixel.ZV
imd.Intensity = 0
imd.Precision = 64
imd.EndShape = NoEndShape
}
// Draw draws all currently drawn shapes inside the IM onto another Target.
//
// Note, that IMDraw's matrix and color mask have no effect here.
func (imd *IMDraw) Draw(t pixel.Target) {
imd.batch.Draw(t)
}
// Push adds some points to the IM queue. All Pushed points will have the same properties except for
// the position.
func (imd *IMDraw) Push(pts ...pixel.Vec) {
if _, ok := imd.Color.(pixel.RGBA); !ok {
imd.Color = pixel.ToRGBA(imd.Color)
}
opts := point{
col: imd.Color.(pixel.RGBA),
pic: imd.Picture,
in: imd.Intensity,
precision: imd.Precision,
endshape: imd.EndShape,
}
for _, pt := range pts {
imd.pushPt(pt, opts)
}
}
func (imd *IMDraw) pushPt(pos pixel.Vec, pt point) {
pt.pos = pos
imd.points = append(imd.points, pt)
}
// SetMatrix sets a Matrix that all further points will be transformed by.
func (imd *IMDraw) SetMatrix(m pixel.Matrix) {
imd.matrix = m
imd.batch.SetMatrix(imd.matrix)
}
// SetColorMask sets a color that all further point's color will be multiplied by.
func (imd *IMDraw) SetColorMask(color color.Color) {
imd.mask = pixel.ToRGBA(color)
imd.batch.SetColorMask(imd.mask)
}
// MakeTriangles returns a specialized copy of the provided Triangles that draws onto this IMDraw.
func (imd *IMDraw) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
return imd.batch.MakeTriangles(t)
}
// MakePicture returns a specialized copy of the provided Picture that draws onto this IMDraw.
func (imd *IMDraw) MakePicture(p pixel.Picture) pixel.TargetPicture {
return imd.batch.MakePicture(p)
}
// Line draws a polyline of the specified thickness between the Pushed points.
func (imd *IMDraw) Line(thickness float64) {
imd.polyline(thickness, false)
}
// Rectangle draws a rectangle between each two subsequent Pushed points. Drawing a rectangle
// between two points means drawing a rectangle with sides parallel to the axes of the coordinate
// system, where the two points specify it's two opposite corners.
//
// If the thickness is 0, rectangles will be filled, otherwise will be outlined with the given
// thickness.
func (imd *IMDraw) Rectangle(thickness float64) {
if thickness == 0 {
imd.fillRectangle()
} else {
imd.outlineRectangle(thickness)
}
}
// Polygon draws a polygon from the Pushed points. If the thickness is 0, the convex polygon will be
// filled. Otherwise, an outline of the specified thickness will be drawn. The outline does not have
// to be convex.
//
// Note, that the filled polygon does not have to be strictly convex. The way it's drawn is that a
// triangle is drawn between each two adjacent points and the first Pushed point. You can use this
// property to draw certain kinds of concave polygons.
func (imd *IMDraw) Polygon(thickness float64) {
if thickness == 0 {
imd.fillPolygon()
} else {
imd.polyline(thickness, true)
}
}
// Circle draws a circle of the specified radius around each Pushed point. If the thickness is 0,
// the circle will be filled, otherwise a circle outline of the specified thickness will be drawn.
func (imd *IMDraw) Circle(radius, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(pixel.V(radius, radius), 0, 2*math.Pi)
} else {
imd.outlineEllipseArc(pixel.V(radius, radius), 0, 2*math.Pi, thickness, false)
}
}
// CircleArc draws a circle arc of the specified radius around each Pushed point. If the thickness
// is 0, the arc will be filled, otherwise will be outlined. The arc starts at the low angle and
// continues to the high angle. If low<high, the arc will be drawn counterclockwise. Otherwise it
// will be clockwise. The angles are not normalized by any means.
//
// imd.CircleArc(40, 0, 8*math.Pi, 0)
//
// This line will fill the whole circle 4 times.
func (imd *IMDraw) CircleArc(radius, low, high, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(pixel.V(radius, radius), low, high)
} else {
imd.outlineEllipseArc(pixel.V(radius, radius), low, high, thickness, true)
}
}
// Ellipse draws an ellipse of the specified radius in each axis around each Pushed points. If the
// thickness is 0, the ellipse will be filled, otherwise an ellipse outline of the specified
// thickness will be drawn.
func (imd *IMDraw) Ellipse(radius pixel.Vec, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(radius, 0, 2*math.Pi)
} else {
imd.outlineEllipseArc(radius, 0, 2*math.Pi, thickness, false)
}
}
// EllipseArc draws an ellipse arc of the specified radius in each axis around each Pushed point. If
// the thickness is 0, the arc will be filled, otherwise will be outlined. The arc starts at the low
// angle and continues to the high angle. If low<high, the arc will be drawn counterclockwise.
// Otherwise it will be clockwise. The angles are not normalized by any means.
//
// imd.EllipseArc(pixel.V(100, 50), 0, 8*math.Pi, 0)
//
// This line will fill the whole ellipse 4 times.
func (imd *IMDraw) EllipseArc(radius pixel.Vec, low, high, thickness float64) {
if thickness == 0 {
imd.fillEllipseArc(radius, low, high)
} else {
imd.outlineEllipseArc(radius, low, high, thickness, true)
}
}
func (imd *IMDraw) getAndClearPoints() []point {
points := imd.points
// use one of the existing pools so we don't reallocate as often
if len(imd.pool) > 0 {
pos := len(imd.pool) - 1
imd.points = imd.pool[pos][:0]
imd.pool = imd.pool[:pos]
} else {
imd.points = nil
}
return points
}
func (imd *IMDraw) restorePoints(points []point) {
imd.pool = append(imd.pool, imd.points)
imd.points = points[:0]
}
func (imd *IMDraw) applyMatrixAndMask(off int) {
for i := range (*imd.tri)[off:] {
(*imd.tri)[off+i].Position = imd.matrix.Project((*imd.tri)[off+i].Position)
(*imd.tri)[off+i].Color = imd.mask.Mul((*imd.tri)[off+i].Color)
}
}
func (imd *IMDraw) fillRectangle() {
points := imd.getAndClearPoints()
if len(points) < 2 {
imd.restorePoints(points)
return
}
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 6*(len(points)-1))
for i, j := 0, off; i+1 < len(points); i, j = i+1, j+6 {
a, b := points[i], points[i+1]
c := point{
pos: pixel.V(a.pos.X, b.pos.Y),
col: a.col.Add(b.col).Mul(pixel.Alpha(0.5)),
pic: pixel.V(a.pic.X, b.pic.Y),
in: (a.in + b.in) / 2,
}
d := point{
pos: pixel.V(b.pos.X, a.pos.Y),
col: a.col.Add(b.col).Mul(pixel.Alpha(0.5)),
pic: pixel.V(b.pic.X, a.pic.Y),
in: (a.in + b.in) / 2,
}
for k, p := range [...]point{a, b, c, a, b, d} {
(*imd.tri)[j+k].Position = p.pos
(*imd.tri)[j+k].Color = p.col
(*imd.tri)[j+k].Picture = p.pic
(*imd.tri)[j+k].Intensity = p.in
}
}
imd.applyMatrixAndMask(off)
imd.batch.Dirty()
imd.restorePoints(points)
}
func (imd *IMDraw) outlineRectangle(thickness float64) {
points := imd.getAndClearPoints()
if len(points) < 2 {
imd.restorePoints(points)
return
}
for i := 0; i+1 < len(points); i++ {
a, b := points[i], points[i+1]
mid := a
mid.col = a.col.Add(b.col).Mul(pixel.Alpha(0.5))
mid.in = (a.in + b.in) / 2
imd.pushPt(a.pos, a)
imd.pushPt(pixel.V(a.pos.X, b.pos.Y), mid)
imd.pushPt(b.pos, b)
imd.pushPt(pixel.V(b.pos.X, a.pos.Y), mid)
imd.polyline(thickness, true)
}
imd.restorePoints(points)
}
func (imd *IMDraw) fillPolygon() {
points := imd.getAndClearPoints()
if len(points) < 3 {
imd.restorePoints(points)
return
}
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 3*(len(points)-2))
for i, j := 1, off; i+1 < len(points); i, j = i+1, j+3 {
for k, p := range [...]int{0, i, i + 1} {
tri := &(*imd.tri)[j+k]
tri.Position = points[p].pos
tri.Color = points[p].col
tri.Picture = points[p].pic
tri.Intensity = points[p].in
}
}
imd.applyMatrixAndMask(off)
imd.batch.Dirty()
imd.restorePoints(points)
}
func (imd *IMDraw) fillEllipseArc(radius pixel.Vec, low, high float64) {
points := imd.getAndClearPoints()
for _, pt := range points {
num := math.Ceil(math.Abs(high-low) / (2 * math.Pi) * float64(pt.precision))
delta := (high - low) / num
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 3*int(num))
for i := range (*imd.tri)[off:] {
(*imd.tri)[off+i].Color = pt.col
(*imd.tri)[off+i].Picture = pixel.ZV
(*imd.tri)[off+i].Intensity = 0
}
for i, j := 0.0, off; i < num; i, j = i+1, j+3 {
angle := low + i*delta
sin, cos := math.Sincos(angle)
a := pt.pos.Add(pixel.V(
radius.X*cos,
radius.Y*sin,
))
angle = low + (i+1)*delta
sin, cos = math.Sincos(angle)
b := pt.pos.Add(pixel.V(
radius.X*cos,
radius.Y*sin,
))
(*imd.tri)[j+0].Position = pt.pos
(*imd.tri)[j+1].Position = a
(*imd.tri)[j+2].Position = b
}
imd.applyMatrixAndMask(off)
imd.batch.Dirty()
}
imd.restorePoints(points)
}
func (imd *IMDraw) outlineEllipseArc(radius pixel.Vec, low, high, thickness float64, doEndShape bool) {
points := imd.getAndClearPoints()
for _, pt := range points {
num := math.Ceil(math.Abs(high-low) / (2 * math.Pi) * float64(pt.precision))
delta := (high - low) / num
off := imd.tri.Len()
imd.tri.SetLen(imd.tri.Len() + 6*int(num))
for i := range (*imd.tri)[off:] {
(*imd.tri)[off+i].Color = pt.col
(*imd.tri)[off+i].Picture = pixel.ZV
(*imd.tri)[off+i].Intensity = 0
}
for i, j := 0.0, off; i < num; i, j = i+1, j+6 {
angle := low + i*delta
sin, cos := math.Sincos(angle)
normalSin, normalCos := pixel.V(sin, cos).ScaledXY(radius).Unit().XY()
a := pt.pos.Add(pixel.V(
radius.X*cos-thickness/2*normalCos,
radius.Y*sin-thickness/2*normalSin,
))
b := pt.pos.Add(pixel.V(
radius.X*cos+thickness/2*normalCos,
radius.Y*sin+thickness/2*normalSin,
))
angle = low + (i+1)*delta
sin, cos = math.Sincos(angle)
normalSin, normalCos = pixel.V(sin, cos).ScaledXY(radius).Unit().XY()
c := pt.pos.Add(pixel.V(
radius.X*cos-thickness/2*normalCos,
radius.Y*sin-thickness/2*normalSin,
))
d := pt.pos.Add(pixel.V(
radius.X*cos+thickness/2*normalCos,
radius.Y*sin+thickness/2*normalSin,
))
(*imd.tri)[j+0].Position = a
(*imd.tri)[j+1].Position = b
(*imd.tri)[j+2].Position = c
(*imd.tri)[j+3].Position = c
(*imd.tri)[j+4].Position = b
(*imd.tri)[j+5].Position = d
}
imd.applyMatrixAndMask(off)
imd.batch.Dirty()
if doEndShape {
lowSin, lowCos := math.Sincos(low)
lowCenter := pt.pos.Add(pixel.V(
radius.X*lowCos,
radius.Y*lowSin,
))
normalLowSin, normalLowCos := pixel.V(lowSin, lowCos).ScaledXY(radius).Unit().XY()
normalLow := pixel.V(normalLowCos, normalLowSin).Angle()
highSin, highCos := math.Sincos(high)
highCenter := pt.pos.Add(pixel.V(
radius.X*highCos,
radius.Y*highSin,
))
normalHighSin, normalHighCos := pixel.V(highSin, highCos).ScaledXY(radius).Unit().XY()
normalHigh := pixel.V(normalHighCos, normalHighSin).Angle()
orientation := 1.0
if low > high {
orientation = -1.0
}
switch pt.endshape {
case NoEndShape:
// nothing
case SharpEndShape:
thick := pixel.V(thickness/2, 0).Rotated(normalLow)
imd.pushPt(lowCenter.Add(thick), pt)
imd.pushPt(lowCenter.Sub(thick), pt)
imd.pushPt(lowCenter.Sub(thick.Normal().Scaled(orientation)), pt)
imd.fillPolygon()
thick = pixel.V(thickness/2, 0).Rotated(normalHigh)
imd.pushPt(highCenter.Add(thick), pt)
imd.pushPt(highCenter.Sub(thick), pt)
imd.pushPt(highCenter.Add(thick.Normal().Scaled(orientation)), pt)
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(lowCenter, pt)
imd.fillEllipseArc(pixel.V(thickness/2, thickness/2), normalLow, normalLow-math.Pi*orientation)
imd.pushPt(highCenter, pt)
imd.fillEllipseArc(pixel.V(thickness/2, thickness/2), normalHigh, normalHigh+math.Pi*orientation)
}
}
}
imd.restorePoints(points)
}
func (imd *IMDraw) polyline(thickness float64, closed bool) {
points := imd.getAndClearPoints()
if len(points) == 0 {
imd.restorePoints(points)
return
}
if len(points) == 1 {
// one point special case
points = append(points, points[0])
}
// first point
j, i := 0, 1
ijNormal := points[0].pos.To(points[1].pos).Normal().Unit().Scaled(thickness / 2)
if !closed {
switch points[j].endshape {
case NoEndShape:
// nothing
case SharpEndShape:
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal.Normal()), points[j])
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(pixel.V(thickness/2, thickness/2), ijNormal.Angle(), ijNormal.Angle()+math.Pi)
}
}
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
// middle points
for i := 0; i < len(points); i++ {
j, k := i+1, i+2
closing := false
if j >= len(points) {
j %= len(points)
closing = true
}
if k >= len(points) {
if !closed {
break
}
k %= len(points)
}
jkNormal := points[j].pos.To(points[k].pos).Normal().Unit().Scaled(thickness / 2)
orientation := 1.0
if ijNormal.Cross(jkNormal) > 0 {
orientation = -1.0
}
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.fillPolygon()
switch points[j].endshape {
case NoEndShape:
// nothing
case SharpEndShape:
imd.pushPt(points[j].pos, points[j])
imd.pushPt(points[j].pos.Add(ijNormal.Scaled(orientation)), points[j])
imd.pushPt(points[j].pos.Add(jkNormal.Scaled(orientation)), points[j])
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(pixel.V(thickness/2, thickness/2), ijNormal.Angle(), ijNormal.Angle()-math.Pi)
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(pixel.V(thickness/2, thickness/2), jkNormal.Angle(), jkNormal.Angle()+math.Pi)
}
if !closing {
imd.pushPt(points[j].pos.Add(jkNormal), points[j])
imd.pushPt(points[j].pos.Sub(jkNormal), points[j])
}
// "next" normal becomes previous normal
ijNormal = jkNormal
}
// last point
i, j = len(points)-2, len(points)-1
ijNormal = points[i].pos.To(points[j].pos).Normal().Unit().Scaled(thickness / 2)
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.fillPolygon()
if !closed {
switch points[j].endshape {
case NoEndShape:
// nothing
case SharpEndShape:
imd.pushPt(points[j].pos.Add(ijNormal), points[j])
imd.pushPt(points[j].pos.Sub(ijNormal), points[j])
imd.pushPt(points[j].pos.Add(ijNormal.Normal().Scaled(-1)), points[j])
imd.fillPolygon()
case RoundEndShape:
imd.pushPt(points[j].pos, points[j])
imd.fillEllipseArc(pixel.V(thickness/2, thickness/2), ijNormal.Angle(), ijNormal.Angle()-math.Pi)
}
}
imd.restorePoints(points)
}

133
vendor/github.com/faiface/pixel/interface.go generated vendored Normal file
View File

@@ -0,0 +1,133 @@
package pixel
import "image/color"
// Target is something that can be drawn onto, such as a window, a canvas, and so on.
//
// You can notice, that there are no "drawing" methods in a Target. That's because all drawing
// happens indirectly through Triangles and Picture instances generated via MakeTriangles and
// MakePicture method.
type Target interface {
// MakeTriangles generates a specialized copy of the provided Triangles.
//
// When calling Draw method on the returned TargetTriangles, the TargetTriangles will be
// drawn onto the Target that generated them.
//
// Note, that not every Target has to recognize all possible types of Triangles. Some may
// only recognize TrianglesPosition and TrianglesColor and ignore all other properties (if
// present) when making new TargetTriangles. This varies from Target to Target.
MakeTriangles(Triangles) TargetTriangles
// MakePicture generates a specialized copy of the provided Picture.
//
// When calling Draw method on the returned TargetPicture, the TargetPicture will be drawn
// onto the Target that generated it together with the TargetTriangles supplied to the Draw
// method.
MakePicture(Picture) TargetPicture
}
// BasicTarget is a Target with additional basic adjustment methods.
type BasicTarget interface {
Target
// SetMatrix sets a Matrix that every point will be projected by.
SetMatrix(Matrix)
// SetColorMask sets a color that will be multiplied with the TrianglesColor property of all
// Triangles.
SetColorMask(color.Color)
}
// Triangles represents a list of vertices, where each three vertices form a triangle. (First,
// second and third is the first triangle, fourth, fifth and sixth is the second triangle, etc.)
type Triangles interface {
// Len returns the number of vertices. The number of triangles is the number of vertices
// divided by 3.
Len() int
// SetLen resizes Triangles to len vertices. If Triangles B were obtained by calling Slice
// method on Triangles A, the relationship between A and B is undefined after calling SetLen
// on either one of them.
SetLen(len int)
// Slice returns a sub-Triangles of this Triangles, covering vertices in range [i, j).
//
// If Triangles B were obtained by calling Slice(4, 9) on Triangles A, then A and B must
// share the same underlying data. Modifying B must change the contents of A in range
// [4, 9). The vertex with index 0 at B is the vertex with index 4 in A, and so on.
//
// Returned Triangles must have the same underlying type.
Slice(i, j int) Triangles
// Update copies vertex properties from the supplied Triangles into this Triangles.
//
// Properies not supported by these Triangles should be ignored. Properties not supported by
// the supplied Triangles should be left untouched.
//
// The two Triangles must have the same Len.
Update(Triangles)
// Copy creates an exact independent copy of this Triangles (with the same underlying type).
Copy() Triangles
}
// TargetTriangles are Triangles generated by a Target with MakeTriangles method. They can be drawn
// onto that (no other) Target.
type TargetTriangles interface {
Triangles
// Draw draws Triangles onto an associated Target.
Draw()
}
// TrianglesPosition specifies Triangles with Position property.
type TrianglesPosition interface {
Triangles
Position(i int) Vec
}
// TrianglesColor specifies Triangles with Color property.
type TrianglesColor interface {
Triangles
Color(i int) RGBA
}
// TrianglesPicture specifies Triangles with Picture propery.
//
// The first value returned from Picture method is Picture coordinates. The second one specifies the
// weight of the Picture. Value of 0 means, that Picture should be completely ignored, 1 means that
// is should be fully included and anything in between means anything in between.
type TrianglesPicture interface {
Triangles
Picture(i int) (pic Vec, intensity float64)
}
// Picture represents a rectangular area of raster data, such as a color. It has Bounds which
// specify the rectangle where data is located.
type Picture interface {
// Bounds returns the rectangle of the Picture. All data is located witih this rectangle.
// Querying properties outside the rectangle should return default value of that property.
Bounds() Rect
}
// TargetPicture is a Picture generated by a Target using MakePicture method. This Picture can be drawn onto
// that (no other) Target together with a TargetTriangles generated by the same Target.
//
// The TargetTriangles specify where, shape and how the Picture should be drawn.
type TargetPicture interface {
Picture
// Draw draws the supplied TargetTriangles (which must be generated by the same Target as
// this TargetPicture) with this TargetPicture. The TargetTriangles should utilize the data
// from this TargetPicture in some way.
Draw(TargetTriangles)
}
// PictureColor specifies Picture with Color property, so that every position inside the Picture's
// Bounds has a color.
//
// Positions outside the Picture's Bounds must return full transparent (Alpha(0)).
type PictureColor interface {
Picture
Color(at Vec) RGBA
}

431
vendor/github.com/faiface/pixel/pixelgl/canvas.go generated vendored Normal file
View File

@@ -0,0 +1,431 @@
package pixelgl
import (
"fmt"
"image/color"
"github.com/faiface/glhf"
"github.com/faiface/mainthread"
"github.com/faiface/pixel"
"github.com/go-gl/mathgl/mgl32"
"github.com/pkg/errors"
)
// Canvas is an off-screen rectangular BasicTarget and Picture at the same time, that you can draw
// onto.
//
// It supports TrianglesPosition, TrianglesColor, TrianglesPicture and PictureColor.
type Canvas struct {
gf *GLFrame
shader *glhf.Shader
cmp pixel.ComposeMethod
mat mgl32.Mat3
col mgl32.Vec4
smooth bool
sprite *pixel.Sprite
}
var _ pixel.ComposeTarget = (*Canvas)(nil)
// NewCanvas creates a new empty, fully transparent Canvas with given bounds.
func NewCanvas(bounds pixel.Rect) *Canvas {
c := &Canvas{
gf: NewGLFrame(bounds),
mat: mgl32.Ident3(),
col: mgl32.Vec4{1, 1, 1, 1},
}
c.SetBounds(bounds)
var shader *glhf.Shader
mainthread.Call(func() {
var err error
shader, err = glhf.NewShader(
canvasVertexFormat,
canvasUniformFormat,
canvasVertexShader,
canvasFragmentShader,
)
if err != nil {
panic(errors.Wrap(err, "failed to create Canvas, there's a bug in the shader"))
}
})
c.shader = shader
return c
}
// MakeTriangles creates a specialized copy of the supplied Triangles that draws onto this Canvas.
//
// TrianglesPosition, TrianglesColor and TrianglesPicture are supported.
func (c *Canvas) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
return &canvasTriangles{
GLTriangles: NewGLTriangles(c.shader, t),
dst: c,
}
}
// 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 {
if cp, ok := p.(*canvasPicture); ok {
return &canvasPicture{
GLPicture: cp.GLPicture,
dst: c,
}
}
if gp, ok := p.(GLPicture); ok {
return &canvasPicture{
GLPicture: gp,
dst: c,
}
}
return &canvasPicture{
GLPicture: NewGLPicture(p),
dst: c,
}
}
// SetMatrix sets a Matrix that every point will be projected by.
func (c *Canvas) SetMatrix(m pixel.Matrix) {
// pixel.Matrix is 3x2 with an implicit 0, 0, 1 row after it. So
// [0] [2] [4] [0] [3] [6]
// [1] [3] [5] => [1] [4] [7]
// 0 0 1 0 0 1
// since all matrix ops are affine, the last row never changes, and we don't need to copy it
for i, j := range [...]int{0, 1, 3, 4, 6, 7} {
c.mat[j] = float32(m[i])
}
}
// SetColorMask sets a color that every color in triangles or a picture will be multiplied by.
func (c *Canvas) SetColorMask(col color.Color) {
rgba := pixel.Alpha(1)
if col != nil {
rgba = pixel.ToRGBA(col)
}
c.col = mgl32.Vec4{
float32(rgba.R),
float32(rgba.G),
float32(rgba.B),
float32(rgba.A),
}
}
// SetComposeMethod sets a Porter-Duff composition method to be used in the following draws onto
// this Canvas.
func (c *Canvas) SetComposeMethod(cmp pixel.ComposeMethod) {
c.cmp = cmp
}
// SetBounds resizes the Canvas to the new bounds. Old content will be preserved.
func (c *Canvas) SetBounds(bounds pixel.Rect) {
c.gf.SetBounds(bounds)
if c.sprite == nil {
c.sprite = pixel.NewSprite(nil, pixel.Rect{})
}
c.sprite.Set(c, c.Bounds())
//c.sprite.SetMatrix(pixel.IM.Moved(c.Bounds().Center()))
}
// Bounds returns the rectangular bounds of the Canvas.
func (c *Canvas) Bounds() pixel.Rect {
return c.gf.Bounds()
}
// SetSmooth sets whether stretched Pictures drawn onto this Canvas should be drawn smooth or
// pixely.
func (c *Canvas) SetSmooth(smooth bool) {
c.smooth = smooth
}
// Smooth returns whether stretched Pictures drawn onto this Canvas are set to be drawn smooth or
// pixely.
func (c *Canvas) Smooth() bool {
return c.smooth
}
// must be manually called inside mainthread
func (c *Canvas) setGlhfBounds() {
_, _, bw, bh := intBounds(c.gf.Bounds())
glhf.Bounds(0, 0, bw, bh)
}
// must be manually called inside mainthread
func setBlendFunc(cmp pixel.ComposeMethod) {
switch cmp {
case pixel.ComposeOver:
glhf.BlendFunc(glhf.One, glhf.OneMinusSrcAlpha)
case pixel.ComposeIn:
glhf.BlendFunc(glhf.DstAlpha, glhf.Zero)
case pixel.ComposeOut:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.Zero)
case pixel.ComposeAtop:
glhf.BlendFunc(glhf.DstAlpha, glhf.OneMinusSrcAlpha)
case pixel.ComposeRover:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.One)
case pixel.ComposeRin:
glhf.BlendFunc(glhf.Zero, glhf.SrcAlpha)
case pixel.ComposeRout:
glhf.BlendFunc(glhf.Zero, glhf.OneMinusSrcAlpha)
case pixel.ComposeRatop:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.SrcAlpha)
case pixel.ComposeXor:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.OneMinusSrcAlpha)
case pixel.ComposePlus:
glhf.BlendFunc(glhf.One, glhf.One)
case pixel.ComposeCopy:
glhf.BlendFunc(glhf.One, glhf.Zero)
default:
panic(errors.New("Canvas: invalid compose method"))
}
}
// Clear fills the whole Canvas with a single color.
func (c *Canvas) Clear(color color.Color) {
c.gf.Dirty()
rgba := pixel.ToRGBA(color)
// color masking
rgba = rgba.Mul(pixel.RGBA{
R: float64(c.col[0]),
G: float64(c.col[1]),
B: float64(c.col[2]),
A: float64(c.col[3]),
})
mainthread.CallNonBlock(func() {
c.setGlhfBounds()
c.gf.Frame().Begin()
glhf.Clear(
float32(rgba.R),
float32(rgba.G),
float32(rgba.B),
float32(rgba.A),
)
c.gf.Frame().End()
})
}
// Color returns the color of the pixel over the given position inside the Canvas.
func (c *Canvas) Color(at pixel.Vec) pixel.RGBA {
return c.gf.Color(at)
}
// Texture returns the underlying OpenGL Texture of this Canvas.
//
// Implements GLPicture interface.
func (c *Canvas) Texture() *glhf.Texture {
return c.gf.Texture()
}
// Frame returns the underlying OpenGL Frame of this Canvas.
func (c *Canvas) Frame() *glhf.Frame {
return c.gf.frame
}
// SetPixels replaces the content of the Canvas with the provided pixels. The provided slice must be
// an alpha-premultiplied RGBA sequence of correct length (4 * width * height).
func (c *Canvas) SetPixels(pixels []uint8) {
c.gf.Dirty()
mainthread.Call(func() {
tex := c.Texture()
tex.Begin()
tex.SetPixels(0, 0, tex.Width(), tex.Height(), pixels)
tex.End()
})
}
// Pixels returns an alpha-premultiplied RGBA sequence of the content of the Canvas.
func (c *Canvas) Pixels() []uint8 {
var pixels []uint8
mainthread.Call(func() {
tex := c.Texture()
tex.Begin()
pixels = tex.Pixels(0, 0, tex.Width(), tex.Height())
tex.End()
})
return pixels
}
// Draw draws the content of the Canvas onto another Target, transformed by the given Matrix, just
// like if it was a Sprite containing the whole Canvas.
func (c *Canvas) Draw(t pixel.Target, matrix pixel.Matrix) {
c.sprite.Draw(t, matrix)
}
// DrawColorMask draws the content of the Canvas onto another Target, transformed by the given
// Matrix and multiplied by the given mask, just like if it was a Sprite containing the whole Canvas.
//
// If the color mask is nil, a fully opaque white mask will be used causing no effect.
func (c *Canvas) DrawColorMask(t pixel.Target, matrix pixel.Matrix, mask color.Color) {
c.sprite.DrawColorMask(t, matrix, mask)
}
type canvasTriangles struct {
*GLTriangles
dst *Canvas
}
func (ct *canvasTriangles) draw(tex *glhf.Texture, bounds pixel.Rect) {
ct.dst.gf.Dirty()
// save the current state vars to avoid race condition
cmp := ct.dst.cmp
mat := ct.dst.mat
col := ct.dst.col
smt := ct.dst.smooth
mainthread.CallNonBlock(func() {
ct.dst.setGlhfBounds()
setBlendFunc(cmp)
frame := ct.dst.gf.Frame()
shader := ct.dst.shader
frame.Begin()
shader.Begin()
dstBounds := ct.dst.Bounds()
shader.SetUniformAttr(canvasBounds, mgl32.Vec4{
float32(dstBounds.Min.X),
float32(dstBounds.Min.Y),
float32(dstBounds.W()),
float32(dstBounds.H()),
})
shader.SetUniformAttr(canvasTransform, mat)
shader.SetUniformAttr(canvasColorMask, col)
if tex == nil {
ct.vs.Begin()
ct.vs.Draw()
ct.vs.End()
} else {
tex.Begin()
bx, by, bw, bh := intBounds(bounds)
shader.SetUniformAttr(canvasTexBounds, mgl32.Vec4{
float32(bx),
float32(by),
float32(bw),
float32(bh),
})
if tex.Smooth() != smt {
tex.SetSmooth(smt)
}
ct.vs.Begin()
ct.vs.Draw()
ct.vs.End()
tex.End()
}
shader.End()
frame.End()
})
}
func (ct *canvasTriangles) Draw() {
ct.draw(nil, pixel.Rect{})
}
type canvasPicture struct {
GLPicture
dst *Canvas
}
func (cp *canvasPicture) Draw(t pixel.TargetTriangles) {
ct := t.(*canvasTriangles)
if cp.dst != ct.dst {
panic(fmt.Errorf("(%T).Draw: TargetTriangles generated by different Canvas", cp))
}
ct.draw(cp.GLPicture.Texture(), cp.GLPicture.Bounds())
}
const (
canvasPosition int = iota
canvasColor
canvasTexCoords
canvasIntensity
)
var canvasVertexFormat = glhf.AttrFormat{
canvasPosition: {Name: "position", Type: glhf.Vec2},
canvasColor: {Name: "color", Type: glhf.Vec4},
canvasTexCoords: {Name: "texCoords", Type: glhf.Vec2},
canvasIntensity: {Name: "intensity", Type: glhf.Float},
}
const (
canvasTransform int = iota
canvasColorMask
canvasBounds
canvasTexBounds
)
var canvasUniformFormat = glhf.AttrFormat{
canvasTransform: {Name: "transform", Type: glhf.Mat3},
canvasColorMask: {Name: "colorMask", Type: glhf.Vec4},
canvasBounds: {Name: "bounds", Type: glhf.Vec4},
canvasTexBounds: {Name: "texBounds", Type: glhf.Vec4},
}
var canvasVertexShader = `
#version 330 core
in vec2 position;
in vec4 color;
in vec2 texCoords;
in float intensity;
out vec4 Color;
out vec2 TexCoords;
out float Intensity;
uniform mat3 transform;
uniform vec4 bounds;
void main() {
vec2 transPos = (transform * vec3(position, 1.0)).xy;
vec2 normPos = (transPos - bounds.xy) / bounds.zw * 2 - vec2(1, 1);
gl_Position = vec4(normPos, 0.0, 1.0);
Color = color;
TexCoords = texCoords;
Intensity = intensity;
}
`
var canvasFragmentShader = `
#version 330 core
in vec4 Color;
in vec2 TexCoords;
in float Intensity;
out vec4 color;
uniform vec4 colorMask;
uniform vec4 texBounds;
uniform sampler2D tex;
void main() {
if (Intensity == 0) {
color = colorMask * Color;
} else {
color = vec4(0, 0, 0, 0);
color += (1 - Intensity) * Color;
vec2 t = (TexCoords - texBounds.xy) / texBounds.zw;
color += Intensity * Color * texture(tex, t);
color *= colorMask;
}
}
`

5
vendor/github.com/faiface/pixel/pixelgl/doc.go generated vendored Normal file
View File

@@ -0,0 +1,5 @@
// Package pixelgl implements efficient OpenGL targets and utilities for the Pixel game development
// library, specifically Window and Canvas.
//
// It also contains a few additional utilities to help extend Pixel with OpenGL graphical effects.
package pixelgl

105
vendor/github.com/faiface/pixel/pixelgl/glframe.go generated vendored Normal file
View File

@@ -0,0 +1,105 @@
package pixelgl
import (
"github.com/faiface/glhf"
"github.com/faiface/mainthread"
"github.com/faiface/pixel"
)
// GLFrame is a type that helps implementing OpenGL Targets. It implements most common methods to
// avoid code redundancy. It contains an glhf.Frame that you can draw on.
type GLFrame struct {
frame *glhf.Frame
bounds pixel.Rect
pixels []uint8
dirty bool
}
// NewGLFrame creates a new GLFrame with the given bounds.
func NewGLFrame(bounds pixel.Rect) *GLFrame {
gf := new(GLFrame)
gf.SetBounds(bounds)
return gf
}
// SetBounds resizes the GLFrame to the new bounds.
func (gf *GLFrame) SetBounds(bounds pixel.Rect) {
if bounds == gf.Bounds() {
return
}
mainthread.Call(func() {
oldF := gf.frame
_, _, w, h := intBounds(bounds)
if w <= 0 {
w = 1
}
if h <= 0 {
h = 1
}
gf.frame = glhf.NewFrame(w, h, false)
// preserve old content
if oldF != nil {
ox, oy, ow, oh := intBounds(bounds)
oldF.Blit(
gf.frame,
ox, oy, ox+ow, oy+oh,
ox, oy, ox+ow, oy+oh,
)
}
})
gf.bounds = bounds
gf.pixels = nil
gf.dirty = true
}
// Bounds returns the current GLFrame's bounds.
func (gf *GLFrame) Bounds() pixel.Rect {
return gf.bounds
}
// Color returns the color of the pixel under the specified position.
func (gf *GLFrame) Color(at pixel.Vec) pixel.RGBA {
if gf.dirty {
mainthread.Call(func() {
tex := gf.frame.Texture()
tex.Begin()
gf.pixels = tex.Pixels(0, 0, tex.Width(), tex.Height())
tex.End()
})
gf.dirty = false
}
if !gf.bounds.Contains(at) {
return pixel.Alpha(0)
}
bx, by, bw, _ := intBounds(gf.bounds)
x, y := int(at.X)-bx, int(at.Y)-by
off := y*bw + x
return pixel.RGBA{
R: float64(gf.pixels[off*4+0]) / 255,
G: float64(gf.pixels[off*4+1]) / 255,
B: float64(gf.pixels[off*4+2]) / 255,
A: float64(gf.pixels[off*4+3]) / 255,
}
}
// Frame returns the GLFrame's Frame that you can draw on.
func (gf *GLFrame) Frame() *glhf.Frame {
return gf.frame
}
// Texture returns the underlying Texture of the GLFrame's Frame.
//
// Implements GLPicture interface.
func (gf *GLFrame) Texture() *glhf.Texture {
return gf.frame.Texture()
}
// Dirty marks the GLFrame as changed. Always call this method when you draw onto the GLFrame's
// Frame.
func (gf *GLFrame) Dirty() {
gf.dirty = true
}

98
vendor/github.com/faiface/pixel/pixelgl/glpicture.go generated vendored Normal file
View File

@@ -0,0 +1,98 @@
package pixelgl
import (
"math"
"github.com/faiface/glhf"
"github.com/faiface/mainthread"
"github.com/faiface/pixel"
)
// GLPicture is a pixel.PictureColor with a Texture. All OpenGL Targets should implement and accept
// this interface, because it enables seamless drawing of one to another.
//
// Implementing this interface on an OpenGL Target enables other OpenGL Targets to efficiently draw
// that Target onto them.
type GLPicture interface {
pixel.PictureColor
Texture() *glhf.Texture
}
// NewGLPicture creates a new GLPicture with it's own static OpenGL texture. This function always
// allocates a new texture that cannot (shouldn't) be further modified.
func NewGLPicture(p pixel.Picture) GLPicture {
bounds := p.Bounds()
bx, by, bw, bh := intBounds(bounds)
pixels := make([]uint8, 4*bw*bh)
if pd, ok := p.(*pixel.PictureData); ok {
// PictureData short path
for y := 0; y < bh; y++ {
for x := 0; x < bw; x++ {
rgba := pd.Pix[y*pd.Stride+x]
off := (y*bw + x) * 4
pixels[off+0] = rgba.R
pixels[off+1] = rgba.G
pixels[off+2] = rgba.B
pixels[off+3] = rgba.A
}
}
} else if p, ok := p.(pixel.PictureColor); ok {
for y := 0; y < bh; y++ {
for x := 0; x < bw; x++ {
at := pixel.V(
math.Max(float64(bx+x), bounds.Min.X),
math.Max(float64(by+y), bounds.Min.Y),
)
color := p.Color(at)
off := (y*bw + x) * 4
pixels[off+0] = uint8(color.R * 255)
pixels[off+1] = uint8(color.G * 255)
pixels[off+2] = uint8(color.B * 255)
pixels[off+3] = uint8(color.A * 255)
}
}
}
var tex *glhf.Texture
mainthread.Call(func() {
tex = glhf.NewTexture(bw, bh, false, pixels)
})
gp := &glPicture{
bounds: bounds,
tex: tex,
pixels: pixels,
}
return gp
}
type glPicture struct {
bounds pixel.Rect
tex *glhf.Texture
pixels []uint8
}
func (gp *glPicture) Bounds() pixel.Rect {
return gp.bounds
}
func (gp *glPicture) Texture() *glhf.Texture {
return gp.tex
}
func (gp *glPicture) Color(at pixel.Vec) pixel.RGBA {
if !gp.bounds.Contains(at) {
return pixel.Alpha(0)
}
bx, by, bw, _ := intBounds(gp.bounds)
x, y := int(at.X)-bx, int(at.Y)-by
off := y*bw + x
return pixel.RGBA{
R: float64(gp.pixels[off*4+0]) / 255,
G: float64(gp.pixels[off*4+1]) / 255,
B: float64(gp.pixels[off*4+2]) / 255,
A: float64(gp.pixels[off*4+3]) / 255,
}
}

215
vendor/github.com/faiface/pixel/pixelgl/gltriangles.go generated vendored Normal file
View File

@@ -0,0 +1,215 @@
package pixelgl
import (
"fmt"
"github.com/faiface/glhf"
"github.com/faiface/mainthread"
"github.com/faiface/pixel"
)
// GLTriangles are OpenGL triangles implemented using glhf.VertexSlice.
//
// Triangles returned from this function support TrianglesPosition, TrianglesColor and
// TrianglesPicture. If you need to support more, you can "override" SetLen and Update methods.
type GLTriangles struct {
vs *glhf.VertexSlice
data []float32
shader *glhf.Shader
}
var (
_ pixel.TrianglesPosition = (*GLTriangles)(nil)
_ pixel.TrianglesColor = (*GLTriangles)(nil)
_ pixel.TrianglesPicture = (*GLTriangles)(nil)
)
// NewGLTriangles returns GLTriangles initialized with the data from the supplied Triangles.
//
// Only draw the Triangles using the provided Shader.
func NewGLTriangles(shader *glhf.Shader, t pixel.Triangles) *GLTriangles {
var gt *GLTriangles
mainthread.Call(func() {
gt = &GLTriangles{
vs: glhf.MakeVertexSlice(shader, 0, t.Len()),
shader: shader,
}
})
gt.SetLen(t.Len())
gt.Update(t)
return gt
}
// VertexSlice returns the VertexSlice of this GLTriangles.
//
// You can use it to draw them.
func (gt *GLTriangles) VertexSlice() *glhf.VertexSlice {
return gt.vs
}
// Shader returns the GLTriangles's associated shader.
func (gt *GLTriangles) Shader() *glhf.Shader {
return gt.shader
}
// Len returns the number of vertices.
func (gt *GLTriangles) Len() int {
return len(gt.data) / gt.vs.Stride()
}
// SetLen efficiently resizes GLTriangles to len.
//
// Time complexity is amortized O(1).
func (gt *GLTriangles) SetLen(length int) {
switch {
case length > gt.Len():
needAppend := length - gt.Len()
for i := 0; i < needAppend; i++ {
gt.data = append(gt.data,
0, 0,
1, 1, 1, 1,
0, 0,
0,
)
}
case length < gt.Len():
gt.data = gt.data[:length*gt.vs.Stride()]
default:
return
}
mainthread.CallNonBlock(func() {
gt.vs.Begin()
gt.vs.SetLen(length)
gt.vs.End()
})
}
// Slice returns a sub-Triangles of this GLTriangles in range [i, j).
func (gt *GLTriangles) Slice(i, j int) pixel.Triangles {
return &GLTriangles{
vs: gt.vs.Slice(i, j),
data: gt.data[i*gt.vs.Stride() : j*gt.vs.Stride()],
shader: gt.shader,
}
}
func (gt *GLTriangles) updateData(t pixel.Triangles) {
// glTriangles short path
if t, ok := t.(*GLTriangles); ok {
copy(gt.data, t.data)
return
}
// TrianglesData short path
stride := gt.vs.Stride()
length := gt.Len()
if t, ok := t.(*pixel.TrianglesData); ok {
for i := 0; i < length; i++ {
var (
px, py = (*t)[i].Position.XY()
col = (*t)[i].Color
tx, ty = (*t)[i].Picture.XY()
in = (*t)[i].Intensity
)
d := gt.data[i*stride : i*stride+9]
d[0] = float32(px)
d[1] = float32(py)
d[2] = float32(col.R)
d[3] = float32(col.G)
d[4] = float32(col.B)
d[5] = float32(col.A)
d[6] = float32(tx)
d[7] = float32(ty)
d[8] = float32(in)
}
return
}
if t, ok := t.(pixel.TrianglesPosition); ok {
for i := 0; i < length; i++ {
px, py := t.Position(i).XY()
gt.data[i*stride+0] = float32(px)
gt.data[i*stride+1] = float32(py)
}
}
if t, ok := t.(pixel.TrianglesColor); ok {
for i := 0; i < length; i++ {
col := t.Color(i)
gt.data[i*stride+2] = float32(col.R)
gt.data[i*stride+3] = float32(col.G)
gt.data[i*stride+4] = float32(col.B)
gt.data[i*stride+5] = float32(col.A)
}
}
if t, ok := t.(pixel.TrianglesPicture); ok {
for i := 0; i < length; i++ {
pic, intensity := t.Picture(i)
gt.data[i*stride+6] = float32(pic.X)
gt.data[i*stride+7] = float32(pic.Y)
gt.data[i*stride+8] = float32(intensity)
}
}
}
// Update copies vertex properties from the supplied Triangles into this GLTriangles.
//
// The two Triangles (gt and t) must be of the same len.
func (gt *GLTriangles) Update(t pixel.Triangles) {
if gt.Len() != t.Len() {
panic(fmt.Errorf("(%T).Update: invalid triangles len", gt))
}
gt.updateData(t)
// this code is supposed to copy the vertex data and CallNonBlock the update if
// the data is small enough, otherwise it'll block and not copy the data
if len(gt.data) < 256 { // arbitrary heurestic constant
data := append([]float32{}, gt.data...)
mainthread.CallNonBlock(func() {
gt.vs.Begin()
gt.vs.SetVertexData(data)
gt.vs.End()
})
} else {
mainthread.Call(func() {
gt.vs.Begin()
gt.vs.SetVertexData(gt.data)
gt.vs.End()
})
}
}
// Copy returns an independent copy of this GLTriangles.
//
// The returned Triangles are *GLTriangles as the underlying type.
func (gt *GLTriangles) Copy() pixel.Triangles {
return NewGLTriangles(gt.shader, gt)
}
// Position returns the Position property of the i-th vertex.
func (gt *GLTriangles) Position(i int) pixel.Vec {
px := gt.data[i*gt.vs.Stride()+0]
py := gt.data[i*gt.vs.Stride()+1]
return pixel.V(float64(px), float64(py))
}
// Color returns the Color property of the i-th vertex.
func (gt *GLTriangles) Color(i int) pixel.RGBA {
r := gt.data[i*gt.vs.Stride()+2]
g := gt.data[i*gt.vs.Stride()+3]
b := gt.data[i*gt.vs.Stride()+4]
a := gt.data[i*gt.vs.Stride()+5]
return pixel.RGBA{
R: float64(r),
G: float64(g),
B: float64(b),
A: float64(a),
}
}
// Picture returns the Picture property of the i-th vertex.
func (gt *GLTriangles) Picture(i int) (pic pixel.Vec, intensity float64) {
tx := gt.data[i*gt.vs.Stride()+6]
ty := gt.data[i*gt.vs.Stride()+7]
intensity = float64(gt.data[i*gt.vs.Stride()+8])
return pixel.V(float64(tx), float64(ty)), intensity
}

388
vendor/github.com/faiface/pixel/pixelgl/input.go generated vendored Normal file
View File

@@ -0,0 +1,388 @@
package pixelgl
import (
"github.com/faiface/mainthread"
"github.com/faiface/pixel"
"github.com/go-gl/glfw/v3.2/glfw"
)
// Pressed returns whether the Button is currently pressed down.
func (w *Window) Pressed(button Button) bool {
return w.currInp.buttons[button]
}
// JustPressed returns whether the Button has just been pressed down.
func (w *Window) JustPressed(button Button) bool {
return w.currInp.buttons[button] && !w.prevInp.buttons[button]
}
// JustReleased returns whether the Button has just been released up.
func (w *Window) JustReleased(button Button) bool {
return !w.currInp.buttons[button] && w.prevInp.buttons[button]
}
// Repeated returns whether a repeat event has been triggered on button.
//
// Repeat event occurs repeatedly when a button is held down for some time.
func (w *Window) Repeated(button Button) bool {
return w.currInp.repeat[button]
}
// MousePosition returns the current mouse position in the Window's Bounds.
func (w *Window) MousePosition() pixel.Vec {
return w.currInp.mouse
}
// MouseScroll returns the mouse scroll amount (in both axes) since the last call to Window.Update.
func (w *Window) MouseScroll() pixel.Vec {
return w.currInp.scroll
}
// Typed returns the text typed on the keyboard since the last call to Window.Update.
func (w *Window) Typed() string {
return w.currInp.typed
}
// Button is a keyboard or mouse button. Why distinguish?
type Button int
// List of all mouse buttons.
const (
MouseButton1 = Button(glfw.MouseButton1)
MouseButton2 = Button(glfw.MouseButton2)
MouseButton3 = Button(glfw.MouseButton3)
MouseButton4 = Button(glfw.MouseButton4)
MouseButton5 = Button(glfw.MouseButton5)
MouseButton6 = Button(glfw.MouseButton6)
MouseButton7 = Button(glfw.MouseButton7)
MouseButton8 = Button(glfw.MouseButton8)
MouseButtonLast = Button(glfw.MouseButtonLast)
MouseButtonLeft = Button(glfw.MouseButtonLeft)
MouseButtonRight = Button(glfw.MouseButtonRight)
MouseButtonMiddle = Button(glfw.MouseButtonMiddle)
)
// List of all keyboard buttons.
const (
KeyUnknown = Button(glfw.KeyUnknown)
KeySpace = Button(glfw.KeySpace)
KeyApostrophe = Button(glfw.KeyApostrophe)
KeyComma = Button(glfw.KeyComma)
KeyMinus = Button(glfw.KeyMinus)
KeyPeriod = Button(glfw.KeyPeriod)
KeySlash = Button(glfw.KeySlash)
Key0 = Button(glfw.Key0)
Key1 = Button(glfw.Key1)
Key2 = Button(glfw.Key2)
Key3 = Button(glfw.Key3)
Key4 = Button(glfw.Key4)
Key5 = Button(glfw.Key5)
Key6 = Button(glfw.Key6)
Key7 = Button(glfw.Key7)
Key8 = Button(glfw.Key8)
Key9 = Button(glfw.Key9)
KeySemicolon = Button(glfw.KeySemicolon)
KeyEqual = Button(glfw.KeyEqual)
KeyA = Button(glfw.KeyA)
KeyB = Button(glfw.KeyB)
KeyC = Button(glfw.KeyC)
KeyD = Button(glfw.KeyD)
KeyE = Button(glfw.KeyE)
KeyF = Button(glfw.KeyF)
KeyG = Button(glfw.KeyG)
KeyH = Button(glfw.KeyH)
KeyI = Button(glfw.KeyI)
KeyJ = Button(glfw.KeyJ)
KeyK = Button(glfw.KeyK)
KeyL = Button(glfw.KeyL)
KeyM = Button(glfw.KeyM)
KeyN = Button(glfw.KeyN)
KeyO = Button(glfw.KeyO)
KeyP = Button(glfw.KeyP)
KeyQ = Button(glfw.KeyQ)
KeyR = Button(glfw.KeyR)
KeyS = Button(glfw.KeyS)
KeyT = Button(glfw.KeyT)
KeyU = Button(glfw.KeyU)
KeyV = Button(glfw.KeyV)
KeyW = Button(glfw.KeyW)
KeyX = Button(glfw.KeyX)
KeyY = Button(glfw.KeyY)
KeyZ = Button(glfw.KeyZ)
KeyLeftBracket = Button(glfw.KeyLeftBracket)
KeyBackslash = Button(glfw.KeyBackslash)
KeyRightBracket = Button(glfw.KeyRightBracket)
KeyGraveAccent = Button(glfw.KeyGraveAccent)
KeyWorld1 = Button(glfw.KeyWorld1)
KeyWorld2 = Button(glfw.KeyWorld2)
KeyEscape = Button(glfw.KeyEscape)
KeyEnter = Button(glfw.KeyEnter)
KeyTab = Button(glfw.KeyTab)
KeyBackspace = Button(glfw.KeyBackspace)
KeyInsert = Button(glfw.KeyInsert)
KeyDelete = Button(glfw.KeyDelete)
KeyRight = Button(glfw.KeyRight)
KeyLeft = Button(glfw.KeyLeft)
KeyDown = Button(glfw.KeyDown)
KeyUp = Button(glfw.KeyUp)
KeyPageUp = Button(glfw.KeyPageUp)
KeyPageDown = Button(glfw.KeyPageDown)
KeyHome = Button(glfw.KeyHome)
KeyEnd = Button(glfw.KeyEnd)
KeyCapsLock = Button(glfw.KeyCapsLock)
KeyScrollLock = Button(glfw.KeyScrollLock)
KeyNumLock = Button(glfw.KeyNumLock)
KeyPrintScreen = Button(glfw.KeyPrintScreen)
KeyPause = Button(glfw.KeyPause)
KeyF1 = Button(glfw.KeyF1)
KeyF2 = Button(glfw.KeyF2)
KeyF3 = Button(glfw.KeyF3)
KeyF4 = Button(glfw.KeyF4)
KeyF5 = Button(glfw.KeyF5)
KeyF6 = Button(glfw.KeyF6)
KeyF7 = Button(glfw.KeyF7)
KeyF8 = Button(glfw.KeyF8)
KeyF9 = Button(glfw.KeyF9)
KeyF10 = Button(glfw.KeyF10)
KeyF11 = Button(glfw.KeyF11)
KeyF12 = Button(glfw.KeyF12)
KeyF13 = Button(glfw.KeyF13)
KeyF14 = Button(glfw.KeyF14)
KeyF15 = Button(glfw.KeyF15)
KeyF16 = Button(glfw.KeyF16)
KeyF17 = Button(glfw.KeyF17)
KeyF18 = Button(glfw.KeyF18)
KeyF19 = Button(glfw.KeyF19)
KeyF20 = Button(glfw.KeyF20)
KeyF21 = Button(glfw.KeyF21)
KeyF22 = Button(glfw.KeyF22)
KeyF23 = Button(glfw.KeyF23)
KeyF24 = Button(glfw.KeyF24)
KeyF25 = Button(glfw.KeyF25)
KeyKP0 = Button(glfw.KeyKP0)
KeyKP1 = Button(glfw.KeyKP1)
KeyKP2 = Button(glfw.KeyKP2)
KeyKP3 = Button(glfw.KeyKP3)
KeyKP4 = Button(glfw.KeyKP4)
KeyKP5 = Button(glfw.KeyKP5)
KeyKP6 = Button(glfw.KeyKP6)
KeyKP7 = Button(glfw.KeyKP7)
KeyKP8 = Button(glfw.KeyKP8)
KeyKP9 = Button(glfw.KeyKP9)
KeyKPDecimal = Button(glfw.KeyKPDecimal)
KeyKPDivide = Button(glfw.KeyKPDivide)
KeyKPMultiply = Button(glfw.KeyKPMultiply)
KeyKPSubtract = Button(glfw.KeyKPSubtract)
KeyKPAdd = Button(glfw.KeyKPAdd)
KeyKPEnter = Button(glfw.KeyKPEnter)
KeyKPEqual = Button(glfw.KeyKPEqual)
KeyLeftShift = Button(glfw.KeyLeftShift)
KeyLeftControl = Button(glfw.KeyLeftControl)
KeyLeftAlt = Button(glfw.KeyLeftAlt)
KeyLeftSuper = Button(glfw.KeyLeftSuper)
KeyRightShift = Button(glfw.KeyRightShift)
KeyRightControl = Button(glfw.KeyRightControl)
KeyRightAlt = Button(glfw.KeyRightAlt)
KeyRightSuper = Button(glfw.KeyRightSuper)
KeyMenu = Button(glfw.KeyMenu)
KeyLast = Button(glfw.KeyLast)
)
// String returns a human-readable string describing the Button.
func (b Button) String() string {
name, ok := buttonNames[b]
if !ok {
return "Invalid"
}
return name
}
var buttonNames = map[Button]string{
MouseButton4: "MouseButton4",
MouseButton5: "MouseButton5",
MouseButton6: "MouseButton6",
MouseButton7: "MouseButton7",
MouseButton8: "MouseButton8",
MouseButtonLeft: "MouseButtonLeft",
MouseButtonRight: "MouseButtonRight",
MouseButtonMiddle: "MouseButtonMiddle",
KeyUnknown: "Unknown",
KeySpace: "Space",
KeyApostrophe: "Apostrophe",
KeyComma: "Comma",
KeyMinus: "Minus",
KeyPeriod: "Period",
KeySlash: "Slash",
Key0: "0",
Key1: "1",
Key2: "2",
Key3: "3",
Key4: "4",
Key5: "5",
Key6: "6",
Key7: "7",
Key8: "8",
Key9: "9",
KeySemicolon: "Semicolon",
KeyEqual: "Equal",
KeyA: "A",
KeyB: "B",
KeyC: "C",
KeyD: "D",
KeyE: "E",
KeyF: "F",
KeyG: "G",
KeyH: "H",
KeyI: "I",
KeyJ: "J",
KeyK: "K",
KeyL: "L",
KeyM: "M",
KeyN: "N",
KeyO: "O",
KeyP: "P",
KeyQ: "Q",
KeyR: "R",
KeyS: "S",
KeyT: "T",
KeyU: "U",
KeyV: "V",
KeyW: "W",
KeyX: "X",
KeyY: "Y",
KeyZ: "Z",
KeyLeftBracket: "LeftBracket",
KeyBackslash: "Backslash",
KeyRightBracket: "RightBracket",
KeyGraveAccent: "GraveAccent",
KeyWorld1: "World1",
KeyWorld2: "World2",
KeyEscape: "Escape",
KeyEnter: "Enter",
KeyTab: "Tab",
KeyBackspace: "Backspace",
KeyInsert: "Insert",
KeyDelete: "Delete",
KeyRight: "Right",
KeyLeft: "Left",
KeyDown: "Down",
KeyUp: "Up",
KeyPageUp: "PageUp",
KeyPageDown: "PageDown",
KeyHome: "Home",
KeyEnd: "End",
KeyCapsLock: "CapsLock",
KeyScrollLock: "ScrollLock",
KeyNumLock: "NumLock",
KeyPrintScreen: "PrintScreen",
KeyPause: "Pause",
KeyF1: "F1",
KeyF2: "F2",
KeyF3: "F3",
KeyF4: "F4",
KeyF5: "F5",
KeyF6: "F6",
KeyF7: "F7",
KeyF8: "F8",
KeyF9: "F9",
KeyF10: "F10",
KeyF11: "F11",
KeyF12: "F12",
KeyF13: "F13",
KeyF14: "F14",
KeyF15: "F15",
KeyF16: "F16",
KeyF17: "F17",
KeyF18: "F18",
KeyF19: "F19",
KeyF20: "F20",
KeyF21: "F21",
KeyF22: "F22",
KeyF23: "F23",
KeyF24: "F24",
KeyF25: "F25",
KeyKP0: "KP0",
KeyKP1: "KP1",
KeyKP2: "KP2",
KeyKP3: "KP3",
KeyKP4: "KP4",
KeyKP5: "KP5",
KeyKP6: "KP6",
KeyKP7: "KP7",
KeyKP8: "KP8",
KeyKP9: "KP9",
KeyKPDecimal: "KPDecimal",
KeyKPDivide: "KPDivide",
KeyKPMultiply: "KPMultiply",
KeyKPSubtract: "KPSubtract",
KeyKPAdd: "KPAdd",
KeyKPEnter: "KPEnter",
KeyKPEqual: "KPEqual",
KeyLeftShift: "LeftShift",
KeyLeftControl: "LeftControl",
KeyLeftAlt: "LeftAlt",
KeyLeftSuper: "LeftSuper",
KeyRightShift: "RightShift",
KeyRightControl: "RightControl",
KeyRightAlt: "RightAlt",
KeyRightSuper: "RightSuper",
KeyMenu: "Menu",
}
func (w *Window) initInput() {
mainthread.Call(func() {
w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
switch action {
case glfw.Press:
w.tempInp.buttons[Button(button)] = true
case glfw.Release:
w.tempInp.buttons[Button(button)] = false
}
})
w.window.SetKeyCallback(func(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if key == glfw.KeyUnknown {
return
}
switch action {
case glfw.Press:
w.tempInp.buttons[Button(key)] = true
case glfw.Release:
w.tempInp.buttons[Button(key)] = false
case glfw.Repeat:
w.tempInp.repeat[Button(key)] = true
}
})
w.window.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
w.tempInp.mouse = pixel.V(
x+w.bounds.Min.X,
(w.bounds.H()-y)+w.bounds.Min.Y,
)
})
w.window.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) {
w.tempInp.scroll.X += xoff
w.tempInp.scroll.Y += yoff
})
w.window.SetCharCallback(func(_ *glfw.Window, r rune) {
w.tempInp.typed += string(r)
})
})
}
// UpdateInput polls window events. Call this function to poll window events
// without swapping buffers. Note that the Update method invokes UpdateInput.
func (w *Window) UpdateInput() {
mainthread.Call(func() {
glfw.PollEvents()
})
w.prevInp = w.currInp
w.currInp = w.tempInp
w.tempInp.repeat = [KeyLast + 1]bool{}
w.tempInp.scroll = pixel.ZV
w.tempInp.typed = ""
}

97
vendor/github.com/faiface/pixel/pixelgl/monitor.go generated vendored Normal file
View File

@@ -0,0 +1,97 @@
package pixelgl
import (
"github.com/faiface/mainthread"
"github.com/go-gl/glfw/v3.2/glfw"
)
// Monitor represents a physical display attached to your computer.
type Monitor struct {
monitor *glfw.Monitor
}
// PrimaryMonitor returns the main monitor (usually the one with the taskbar and stuff).
func PrimaryMonitor() *Monitor {
var monitor *glfw.Monitor
mainthread.Call(func() {
monitor = glfw.GetPrimaryMonitor()
})
return &Monitor{
monitor: monitor,
}
}
// Monitors returns a slice of all currently available monitors.
func Monitors() []*Monitor {
var monitors []*Monitor
mainthread.Call(func() {
for _, monitor := range glfw.GetMonitors() {
monitors = append(monitors, &Monitor{monitor: monitor})
}
})
return monitors
}
// Name returns a human-readable name of the Monitor.
func (m *Monitor) Name() string {
var name string
mainthread.Call(func() {
name = m.monitor.GetName()
})
return name
}
// PhysicalSize returns the size of the display area of the Monitor in millimeters.
func (m *Monitor) PhysicalSize() (width, height float64) {
var wi, hi int
mainthread.Call(func() {
wi, hi = m.monitor.GetPhysicalSize()
})
width = float64(wi)
height = float64(hi)
return
}
// Position returns the position of the upper-left corner of the Monitor in screen coordinates.
func (m *Monitor) Position() (x, y float64) {
var xi, yi int
mainthread.Call(func() {
xi, yi = m.monitor.GetPos()
})
x = float64(xi)
y = float64(yi)
return
}
// Size returns the resolution of the Monitor in pixels.
func (m *Monitor) Size() (width, height float64) {
var mode *glfw.VidMode
mainthread.Call(func() {
mode = m.monitor.GetVideoMode()
})
width = float64(mode.Width)
height = float64(mode.Height)
return
}
// BitDepth returns the number of bits per color of the Monitor.
func (m *Monitor) BitDepth() (red, green, blue int) {
var mode *glfw.VidMode
mainthread.Call(func() {
mode = m.monitor.GetVideoMode()
})
red = mode.RedBits
green = mode.GreenBits
blue = mode.BlueBits
return
}
// RefreshRate returns the refresh frequency of the Monitor in Hz (refreshes/second).
func (m *Monitor) RefreshRate() (rate float64) {
var mode *glfw.VidMode
mainthread.Call(func() {
mode = m.monitor.GetVideoMode()
})
rate = float64(mode.RefreshRate)
return
}

33
vendor/github.com/faiface/pixel/pixelgl/run.go generated vendored Normal file
View File

@@ -0,0 +1,33 @@
package pixelgl
import (
"github.com/faiface/mainthread"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pkg/errors"
)
// Run is essentially the main function of PixelGL. It exists mainly due to the technical
// limitations of OpenGL and operating systems. In short, all graphics and window manipulating calls
// must be done from the main thread. Run makes this possible.
//
// Call this function from the main function of your application. This is necessary, so that Run
// runs on the main thread.
//
// func run() {
// // interact with Pixel and PixelGL from here (even concurrently)
// }
//
// func main() {
// pixel.Run(run)
// }
//
// You can spawn any number of goroutines from your run function and interact with PixelGL
// concurrently. The only condition is that the Run function is called from your main function.
func Run(run func()) {
err := glfw.Init()
if err != nil {
panic(errors.Wrap(err, "failed to initialize GLFW"))
}
defer glfw.Terminate()
mainthread.Run(run)
}

15
vendor/github.com/faiface/pixel/pixelgl/util.go generated vendored Normal file
View File

@@ -0,0 +1,15 @@
package pixelgl
import (
"math"
"github.com/faiface/pixel"
)
func intBounds(bounds pixel.Rect) (x, y, w, h int) {
x0 := int(math.Floor(bounds.Min.X))
y0 := int(math.Floor(bounds.Min.Y))
x1 := int(math.Ceil(bounds.Max.X))
y1 := int(math.Ceil(bounds.Max.Y))
return x0, y0, x1 - x0, y1 - y0
}

426
vendor/github.com/faiface/pixel/pixelgl/window.go generated vendored Normal file
View File

@@ -0,0 +1,426 @@
package pixelgl
import (
"image"
"image/color"
"runtime"
"github.com/faiface/glhf"
"github.com/faiface/mainthread"
"github.com/faiface/pixel"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pkg/errors"
)
// WindowConfig is a structure for specifying all possible properties of a Window. Properties are
// chosen in such a way, that you usually only need to set a few of them - defaults (zeros) should
// usually be sensible.
//
// Note that you always need to set the Bounds of a Window.
type WindowConfig struct {
// Title at the top of the Window.
Title string
// Icon specifies the icon images available to be used by the window. This is usually
// displayed in the top bar of the window or in the task bar of the desktop environment.
//
// If passed one image, it will use that image, if passed an array of images those of or
// closest to the sizes desired by the system are selected. The desired image sizes varies
// depending on platform and system settings. The selected images will be rescaled as
// needed. Good sizes include 16x16, 32x32 and 48x48.
//
// Note: Setting this value doesn't have an effect on OSX. You'll need to set the icon when
// bundling your application for release.
Icon []pixel.Picture
// Bounds specify the bounds of the Window in pixels.
Bounds pixel.Rect
// If set to nil, the Window will be windowed. Otherwise it will be fullscreen on the
// specified Monitor.
Monitor *Monitor
// Whether the Window is resizable.
Resizable bool
// Undecorated Window ommits the borders and decorations (close button, etc.).
Undecorated bool
// VSync (vertical synchronization) synchronizes Window's framerate with the framerate of
// the monitor.
VSync bool
}
// Window is a window handler. Use this type to manipulate a window (input, drawing, etc.).
type Window struct {
window *glfw.Window
bounds pixel.Rect
canvas *Canvas
vsync bool
cursorVisible bool
// need to save these to correctly restore a fullscreen window
restore struct {
xpos, ypos, width, height int
}
prevInp, currInp, tempInp struct {
mouse pixel.Vec
buttons [KeyLast + 1]bool
repeat [KeyLast + 1]bool
scroll pixel.Vec
typed string
}
}
var currWin *Window
// 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).
func NewWindow(cfg WindowConfig) (*Window, error) {
bool2int := map[bool]int{
true: glfw.True,
false: glfw.False,
}
w := &Window{bounds: cfg.Bounds, cursorVisible: true}
err := mainthread.CallErr(func() error {
var err error
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Resizable, bool2int[cfg.Resizable])
glfw.WindowHint(glfw.Decorated, bool2int[!cfg.Undecorated])
var share *glfw.Window
if currWin != nil {
share = currWin.window
}
_, _, width, height := intBounds(cfg.Bounds)
w.window, err = glfw.CreateWindow(
width,
height,
cfg.Title,
nil,
share,
)
if err != nil {
return err
}
// enter the OpenGL context
w.begin()
w.end()
return nil
})
if err != nil {
return nil, errors.Wrap(err, "creating window failed")
}
if len(cfg.Icon) > 0 {
imgs := make([]image.Image, len(cfg.Icon))
for i, icon := range cfg.Icon {
pic := pixel.PictureDataFromPicture(icon)
imgs[i] = pic.Image()
}
mainthread.Call(func() {
w.window.SetIcon(imgs)
})
}
w.SetVSync(cfg.VSync)
w.initInput()
w.SetMonitor(cfg.Monitor)
w.canvas = NewCanvas(cfg.Bounds)
w.Update()
runtime.SetFinalizer(w, (*Window).Destroy)
return w, nil
}
// Destroy destroys the Window. The Window can't be used any further.
func (w *Window) Destroy() {
mainthread.Call(func() {
w.window.Destroy()
})
}
// Update swaps buffers and polls events. Call this method at the end of each frame.
func (w *Window) Update() {
mainthread.Call(func() {
_, _, oldW, oldH := intBounds(w.bounds)
newW, newH := w.window.GetSize()
w.bounds = w.bounds.ResizedMin(w.bounds.Size().Add(pixel.V(
float64(newW-oldW),
float64(newH-oldH),
)))
})
w.canvas.SetBounds(w.bounds)
mainthread.Call(func() {
w.begin()
framebufferWidth, framebufferHeight := w.window.GetFramebufferSize()
glhf.Bounds(0, 0, framebufferWidth, framebufferHeight)
glhf.Clear(0, 0, 0, 0)
w.canvas.gf.Frame().Begin()
w.canvas.gf.Frame().Blit(
nil,
0, 0, w.canvas.Texture().Width(), w.canvas.Texture().Height(),
0, 0, framebufferWidth, framebufferHeight,
)
w.canvas.gf.Frame().End()
if w.vsync {
glfw.SwapInterval(1)
} else {
glfw.SwapInterval(0)
}
w.window.SwapBuffers()
w.end()
})
w.UpdateInput()
}
// SetClosed sets the closed flag of the Window.
//
// This is useful when overriding the user's attempt to close the Window, or just to close the
// Window from within the program.
func (w *Window) SetClosed(closed bool) {
mainthread.Call(func() {
w.window.SetShouldClose(closed)
})
}
// Closed returns the closed flag of the Window, which reports whether the Window should be closed.
//
// The closed flag is automatically set when a user attempts to close the Window.
func (w *Window) Closed() bool {
var closed bool
mainthread.Call(func() {
closed = w.window.ShouldClose()
})
return closed
}
// SetTitle changes the title of the Window.
func (w *Window) SetTitle(title string) {
mainthread.Call(func() {
w.window.SetTitle(title)
})
}
// SetBounds sets the bounds of the Window in pixels. Bounds can be fractional, but the actual size
// of the window will be rounded to integers.
func (w *Window) SetBounds(bounds pixel.Rect) {
w.bounds = bounds
mainthread.Call(func() {
_, _, width, height := intBounds(bounds)
w.window.SetSize(width, height)
})
}
// SetPos sets the position, in screen coordinates, of the upper-left corner
// of the client area of the window. Position can be fractional, but the actual position
// of the window will be rounded to integers.
//
// If it is a full screen window, this function does nothing.
func (w *Window) SetPos(pos pixel.Vec) {
mainthread.Call(func() {
left, top := int(pos.X), int(pos.Y)
w.window.SetPos(left, top)
})
}
// GetPos gets the position, in screen coordinates, of the upper-left corner
// of the client area of the window. The position is rounded to integers.
func (w *Window) GetPos() pixel.Vec {
var v pixel.Vec
mainthread.Call(func() {
x, y := w.window.GetPos()
v = pixel.V(float64(x), float64(y))
})
return v
}
// Bounds returns the current bounds of the Window.
func (w *Window) Bounds() pixel.Rect {
return w.bounds
}
func (w *Window) setFullscreen(monitor *Monitor) {
mainthread.Call(func() {
w.restore.xpos, w.restore.ypos = w.window.GetPos()
w.restore.width, w.restore.height = w.window.GetSize()
mode := monitor.monitor.GetVideoMode()
w.window.SetMonitor(
monitor.monitor,
0,
0,
mode.Width,
mode.Height,
mode.RefreshRate,
)
})
}
func (w *Window) setWindowed() {
mainthread.Call(func() {
w.window.SetMonitor(
nil,
w.restore.xpos,
w.restore.ypos,
w.restore.width,
w.restore.height,
0,
)
})
}
// SetMonitor sets the Window fullscreen on the given Monitor. If the Monitor is nil, the Window
// will be restored to windowed state instead.
//
// The Window will be automatically set to the Monitor's resolution. If you want a different
// resolution, you will need to set it manually with SetBounds method.
func (w *Window) SetMonitor(monitor *Monitor) {
if w.Monitor() != monitor {
if monitor != nil {
w.setFullscreen(monitor)
} else {
w.setWindowed()
}
}
}
// Monitor returns a monitor the Window is fullscreen on. If the Window is not fullscreen, this
// function returns nil.
func (w *Window) Monitor() *Monitor {
var monitor *glfw.Monitor
mainthread.Call(func() {
monitor = w.window.GetMonitor()
})
if monitor == nil {
return nil
}
return &Monitor{
monitor: monitor,
}
}
// Focused returns true if the Window has input focus.
func (w *Window) Focused() bool {
var focused bool
mainthread.Call(func() {
focused = w.window.GetAttrib(glfw.Focused) == glfw.True
})
return focused
}
// SetVSync sets whether the Window's Update 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
}
// SetCursorVisible sets the visibility of the mouse cursor inside the Window client area.
func (w *Window) SetCursorVisible(visible bool) {
w.cursorVisible = visible
mainthread.Call(func() {
if visible {
w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
} else {
w.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
}
})
}
// CursorVisible returns the visibility status of the mouse cursor.
func (w *Window) CursorVisible() bool {
return w.cursorVisible
}
// Note: must be called inside the main thread.
func (w *Window) begin() {
if currWin != w {
w.window.MakeContextCurrent()
glhf.Init()
currWin = w
}
}
// Note: must be called inside the main thread.
func (w *Window) end() {
// nothing, really
}
// MakeTriangles generates a specialized copy of the supplied Triangles that will draw onto this
// Window.
//
// Window supports TrianglesPosition, TrianglesColor and TrianglesPicture.
func (w *Window) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
return w.canvas.MakeTriangles(t)
}
// MakePicture generates a specialized copy of the supplied Picture that will draw onto this Window.
//
// Window supports PictureColor.
func (w *Window) MakePicture(p pixel.Picture) pixel.TargetPicture {
return w.canvas.MakePicture(p)
}
// SetMatrix sets a Matrix that every point will be projected by.
func (w *Window) SetMatrix(m pixel.Matrix) {
w.canvas.SetMatrix(m)
}
// SetColorMask sets a global color mask for the Window.
func (w *Window) SetColorMask(c color.Color) {
w.canvas.SetColorMask(c)
}
// SetComposeMethod sets a Porter-Duff composition method to be used in the following draws onto
// this Window.
func (w *Window) SetComposeMethod(cmp pixel.ComposeMethod) {
w.canvas.SetComposeMethod(cmp)
}
// SetSmooth sets whether the stretched Pictures drawn onto this Window should be drawn smooth or
// pixely.
func (w *Window) SetSmooth(smooth bool) {
w.canvas.SetSmooth(smooth)
}
// Smooth returns whether the stretched Pictures drawn onto this Window are set to be drawn smooth
// or pixely.
func (w *Window) Smooth() bool {
return w.canvas.Smooth()
}
// Clear clears the Window with a single color.
func (w *Window) Clear(c color.Color) {
w.canvas.Clear(c)
}
// Color returns the color of the pixel over the given position inside the Window.
func (w *Window) Color(at pixel.Vec) pixel.RGBA {
return w.canvas.Color(at)
}

117
vendor/github.com/faiface/pixel/sprite.go generated vendored Normal file
View File

@@ -0,0 +1,117 @@
package pixel
import "image/color"
// Sprite is a drawable frame of a Picture. It's anchored by the center of it's Picture's frame.
//
// Frame specifies a rectangular portion of the Picture that will be drawn. For example, this
// creates a Sprite that draws the whole Picture:
//
// sprite := pixel.NewSprite(pic, pic.Bounds())
//
// Note, that Sprite caches the results of MakePicture from Targets it's drawn to for each Picture
// it's set to. What it means is that using a Sprite with an unbounded number of Pictures leads to a
// memory leak, since Sprite caches them and never forgets. In such a situation, create a new Sprite
// for each Picture.
type Sprite struct {
tri *TrianglesData
frame Rect
d Drawer
matrix Matrix
mask RGBA
}
// NewSprite creates a Sprite from the supplied frame of a Picture.
func NewSprite(pic Picture, frame Rect) *Sprite {
tri := MakeTrianglesData(6)
s := &Sprite{
tri: tri,
d: Drawer{Triangles: tri},
}
s.matrix = IM
s.mask = Alpha(1)
s.Set(pic, frame)
return s
}
// Set sets a new frame of a Picture for this Sprite.
func (s *Sprite) Set(pic Picture, frame Rect) {
s.d.Picture = pic
if frame != s.frame {
s.frame = frame
s.calcData()
}
}
// Picture returns the current Sprite's Picture.
func (s *Sprite) Picture() Picture {
return s.d.Picture
}
// Frame returns the current Sprite's frame.
func (s *Sprite) Frame() Rect {
return s.frame
}
// Draw draws the Sprite onto the provided Target. The Sprite will be transformed by the given Matrix.
//
// This method is equivalent to calling DrawColorMask with nil color mask.
func (s *Sprite) Draw(t Target, matrix Matrix) {
s.DrawColorMask(t, matrix, nil)
}
// DrawColorMask draws the Sprite onto the provided Target. The Sprite will be transformed by the
// given Matrix and all of it's color will be multiplied by the given mask.
//
// If the mask is nil, a fully opaque white mask will be used, which causes no effect.
func (s *Sprite) DrawColorMask(t Target, matrix Matrix, mask color.Color) {
dirty := false
if matrix != s.matrix {
s.matrix = matrix
dirty = true
}
if mask == nil {
mask = Alpha(1)
}
rgba := ToRGBA(mask)
if rgba != s.mask {
s.mask = rgba
dirty = true
}
if dirty {
s.calcData()
}
s.d.Draw(t)
}
func (s *Sprite) calcData() {
var (
center = s.frame.Center()
horizontal = V(s.frame.W()/2, 0)
vertical = V(0, s.frame.H()/2)
)
(*s.tri)[0].Position = Vec{}.Sub(horizontal).Sub(vertical)
(*s.tri)[1].Position = Vec{}.Add(horizontal).Sub(vertical)
(*s.tri)[2].Position = Vec{}.Add(horizontal).Add(vertical)
(*s.tri)[3].Position = Vec{}.Sub(horizontal).Sub(vertical)
(*s.tri)[4].Position = Vec{}.Add(horizontal).Add(vertical)
(*s.tri)[5].Position = Vec{}.Sub(horizontal).Add(vertical)
for i := range *s.tri {
(*s.tri)[i].Color = s.mask
(*s.tri)[i].Picture = center.Add((*s.tri)[i].Position)
(*s.tri)[i].Intensity = 1
}
// matrix and mask
for i := range *s.tri {
(*s.tri)[i].Position = s.matrix.Project((*s.tri)[i].Position)
(*s.tri)[i].Color = s.mask
}
s.d.Dirty()
}

21
vendor/github.com/go-gl/gl/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Eric Woroshow
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

109
vendor/github.com/go-gl/gl/v3.3-core/gl/conversions.go generated vendored Normal file
View File

@@ -0,0 +1,109 @@
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
package gl
import (
"fmt"
"reflect"
"strings"
"unsafe"
)
// #include <stdlib.h>
import "C"
// Ptr takes a slice or pointer (to a singular scalar value or the first
// element of an array or slice) and returns its GL-compatible address.
//
// For example:
//
// var data []uint8
// ...
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
func Ptr(data interface{}) unsafe.Pointer {
if data == nil {
return unsafe.Pointer(nil)
}
var addr unsafe.Pointer
v := reflect.ValueOf(data)
switch v.Type().Kind() {
case reflect.Ptr:
e := v.Elem()
switch e.Kind() {
case
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
addr = unsafe.Pointer(e.UnsafeAddr())
default:
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
}
case reflect.Uintptr:
addr = unsafe.Pointer(v.Pointer())
case reflect.Slice:
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
default:
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
}
return addr
}
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
// Useful for functions such as glVertexAttribPointer that take pointer
// parameters indicating an offset rather than an absolute memory address.
func PtrOffset(offset int) unsafe.Pointer {
return unsafe.Pointer(uintptr(offset))
}
// Str takes a null-terminated Go string and returns its GL-compatible address.
// This function reaches into Go string storage in an unsafe way so the caller
// must ensure the string is not garbage collected.
func Str(str string) *uint8 {
if !strings.HasSuffix(str, "\x00") {
panic("str argument missing null terminator: " + str)
}
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
return (*uint8)(unsafe.Pointer(header.Data))
}
// GoStr takes a null-terminated string returned by OpenGL and constructs a
// corresponding Go string.
func GoStr(cstr *uint8) string {
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
}
// Strs takes a list of Go strings (with or without null-termination) and
// returns their C counterpart.
//
// The returned free function must be called once you are done using the strings
// in order to free the memory.
//
// If no strings are provided as a parameter this function will panic.
func Strs(strs ...string) (cstrs **uint8, free func()) {
if len(strs) == 0 {
panic("Strs: expected at least 1 string")
}
// Allocate a contiguous array large enough to hold all the strings' contents.
n := 0
for i := range strs {
n += len(strs[i])
}
data := C.malloc(C.size_t(n))
// Copy all the strings into data.
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(data),
Len: n,
Cap: n,
}))
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
offset := 0
for i := range strs {
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
offset += len(strs[i])
}
return (**uint8)(&css[0]), func() { C.free(data) }
}

31
vendor/github.com/go-gl/gl/v3.3-core/gl/debug.go generated vendored Normal file
View File

@@ -0,0 +1,31 @@
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
package gl
import "C"
import "unsafe"
type DebugProc func(
source uint32,
gltype uint32,
id uint32,
severity uint32,
length int32,
message string,
userParam unsafe.Pointer)
var userDebugCallback DebugProc
//export glowDebugCallback_glcore33
func glowDebugCallback_glcore33(
source uint32,
gltype uint32,
id uint32,
severity uint32,
length int32,
message *uint8,
userParam unsafe.Pointer) {
if userDebugCallback != nil {
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
}
}

9882
vendor/github.com/go-gl/gl/v3.3-core/gl/package.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

65
vendor/github.com/go-gl/gl/v3.3-core/gl/procaddr.go generated vendored Normal file
View File

@@ -0,0 +1,65 @@
// This file implements GlowGetProcAddress for every supported platform. The
// correct version is chosen automatically based on build tags:
// windows: WGL
// darwin: CGL
// linux freebsd: GLX
// Use of EGL instead of the platform's default (listed above) is made possible
// via the "egl" build tag.
// It is also possible to install your own function outside this package for
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
package gl
/*
#cgo windows CFLAGS: -DTAG_WINDOWS
#cgo windows LDFLAGS: -lopengl32
#cgo darwin CFLAGS: -DTAG_DARWIN
#cgo darwin LDFLAGS: -framework OpenGL
#cgo linux freebsd CFLAGS: -DTAG_POSIX
#cgo linux freebsd LDFLAGS: -lGL
#cgo egl CFLAGS: -DTAG_EGL
#cgo egl LDFLAGS: -lEGL
// Check the EGL tag first as it takes priority over the platform's default
// configuration of WGL/GLX/CGL.
#if defined(TAG_EGL)
#include <stdlib.h>
#include <EGL/egl.h>
void* GlowGetProcAddress_glcore33(const char* name) {
return eglGetProcAddress(name);
}
#elif defined(TAG_WINDOWS)
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <stdlib.h>
static HMODULE ogl32dll = NULL;
void* GlowGetProcAddress_glcore33(const char* name) {
void* pf = wglGetProcAddress((LPCSTR) name);
if (pf) {
return pf;
}
if (ogl32dll == NULL) {
ogl32dll = LoadLibraryA("opengl32.dll");
}
return GetProcAddress(ogl32dll, (LPCSTR) name);
}
#elif defined(TAG_DARWIN)
#include <stdlib.h>
#include <dlfcn.h>
void* GlowGetProcAddress_glcore33(const char* name) {
return dlsym(RTLD_DEFAULT, name);
}
#elif defined(TAG_POSIX)
#include <stdlib.h>
#include <GL/glx.h>
void* GlowGetProcAddress_glcore33(const char* name) {
return glXGetProcAddress(name);
}
#endif
*/
import "C"
import "unsafe"
func getProcAddress(namea string) unsafe.Pointer {
cname := C.CString(namea)
defer C.free(unsafe.Pointer(cname))
return C.GlowGetProcAddress_glcore33(cname)
}

10
vendor/github.com/go-gl/glfw/AUTHORS generated vendored Normal file
View File

@@ -0,0 +1,10 @@
# This is the official list of glfw3-go authors for copyright purposes.
# Please keep the list sorted.
Coşku Baş
Dmitri Shuralyov
James Gray
Peter Waller <p@pwaller.net> (github:pwaller)
Robin Eklind
Stephen Gutekanst

27
vendor/github.com/go-gl/glfw/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,27 @@
Copyright (c) 2012 The glfw3-go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1,22 @@
Copyright (c) 2002-2006 Marcus Geelnard
Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would
be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.

View File

@@ -0,0 +1 @@
999f3556fdd80983b10051746264489f2cb1ef16

43
vendor/github.com/go-gl/glfw/v3.2/glfw/build.go generated vendored Normal file
View File

@@ -0,0 +1,43 @@
package glfw
/*
// Windows Build Tags
// ----------------
// GLFW Options:
#cgo windows CFLAGS: -D_GLFW_WIN32 -Iglfw/deps/mingw
// Linker Options:
#cgo windows LDFLAGS: -lopengl32 -lgdi32
// Darwin Build Tags
// ----------------
// GLFW Options:
#cgo darwin CFLAGS: -D_GLFW_COCOA -D_GLFW_USE_CHDIR -D_GLFW_USE_MENUBAR -D_GLFW_USE_RETINA -Wno-deprecated-declarations
// Linker Options:
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
// Linux Build Tags
// ----------------
// GLFW Options:
#cgo linux,!wayland CFLAGS: -D_GLFW_X11
#cgo linux,wayland CFLAGS: -D_GLFW_WAYLAND
// Linker Options:
#cgo linux,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl -lrt
#cgo linux,wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl -lrt
// FreeBSD Build Tags
// ----------------
// GLFW Options:
#cgo freebsd,!wayland CFLAGS: -D_GLFW_X11 -D_GLFW_HAS_GLXGETPROCADDRESSARB -D_GLFW_HAS_DLOPEN
#cgo freebsd,wayland CFLAGS: -D_GLFW_WAYLAND -D_GLFW_HAS_DLOPEN
// Linker Options:
#cgo freebsd,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
#cgo freebsd,wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
*/
import "C"

11
vendor/github.com/go-gl/glfw/v3.2/glfw/c_glfw.go generated vendored Normal file
View File

@@ -0,0 +1,11 @@
package glfw
/*
#include "glfw/src/context.c"
#include "glfw/src/init.c"
#include "glfw/src/input.c"
#include "glfw/src/monitor.c"
#include "glfw/src/vulkan.c"
#include "glfw/src/window.c"
*/
import "C"

View File

@@ -0,0 +1,13 @@
package glfw
/*
#cgo CFLAGS: -x objective-c
#include "glfw/src/cocoa_init.m"
#include "glfw/src/cocoa_joystick.m"
#include "glfw/src/cocoa_monitor.m"
#include "glfw/src/cocoa_window.m"
#include "glfw/src/cocoa_time.c"
#include "glfw/src/posix_tls.c"
#include "glfw/src/nsgl_context.m"
*/
import "C"

View File

@@ -0,0 +1,28 @@
// +build linux freebsd
package glfw
/*
#ifdef _GLFW_MIR
#include "glfw/src/mir_init.c"
#include "glfw/src/mir_monitor.c"
#include "glfw/src/mir_window.c"
#endif
#ifdef _GLFW_WAYLAND
#include "glfw/src/wl_init.c"
#include "glfw/src/wl_monitor.c"
#include "glfw/src/wl_window.c"
#endif
#ifdef _GLFW_X11
#include "glfw/src/x11_init.c"
#include "glfw/src/x11_monitor.c"
#include "glfw/src/x11_window.c"
#include "glfw/src/glx_context.c"
#endif
#include "glfw/src/linux_joystick.c"
#include "glfw/src/posix_time.c"
#include "glfw/src/posix_tls.c"
#include "glfw/src/xkb_unicode.c"
#include "glfw/src/egl_context.c"
*/
import "C"

View File

@@ -0,0 +1,13 @@
package glfw
/*
#include "glfw/src/win32_init.c"
#include "glfw/src/win32_joystick.c"
#include "glfw/src/win32_monitor.c"
#include "glfw/src/win32_time.c"
#include "glfw/src/win32_tls.c"
#include "glfw/src/win32_window.c"
#include "glfw/src/wgl_context.c"
#include "glfw/src/egl_context.c"
*/
import "C"

77
vendor/github.com/go-gl/glfw/v3.2/glfw/context.go generated vendored Normal file
View File

@@ -0,0 +1,77 @@
package glfw
//#include <stdlib.h>
//#include "glfw/include/GLFW/glfw3.h"
import "C"
import (
"unsafe"
)
// MakeContextCurrent makes the context of the window current.
// Originally GLFW 3 passes a null pointer to detach the context.
// But since we're using receievers, DetachCurrentContext should
// be used instead.
func (w *Window) MakeContextCurrent() {
C.glfwMakeContextCurrent(w.data)
panicError()
}
// DetachCurrentContext detaches the current context.
func DetachCurrentContext() {
C.glfwMakeContextCurrent(nil)
panicError()
}
// GetCurrentContext returns the window whose context is current.
func GetCurrentContext() *Window {
w := C.glfwGetCurrentContext()
panicError()
if w == nil {
return nil
}
return windows.get(w)
}
// SwapBuffers swaps the front and back buffers of the window. If the
// swap interval is greater than zero, the GPU driver waits the specified number
// of screen updates before swapping the buffers.
func (w *Window) SwapBuffers() {
C.glfwSwapBuffers(w.data)
panicError()
}
// SwapInterval sets the swap interval for the current context, i.e. the number
// of screen updates to wait before swapping the buffers of a window and
// returning from SwapBuffers. This is sometimes called
// 'vertical synchronization', 'vertical retrace synchronization' or 'vsync'.
//
// Contexts that support either of the WGL_EXT_swap_control_tear and
// GLX_EXT_swap_control_tear extensions also accept negative swap intervals,
// which allow the driver to swap even if a frame arrives a little bit late.
// You can check for the presence of these extensions using
// ExtensionSupported. For more information about swap tearing,
// see the extension specifications.
//
// Some GPU drivers do not honor the requested swap interval, either because of
// user settings that override the request or due to bugs in the driver.
func SwapInterval(interval int) {
C.glfwSwapInterval(C.int(interval))
panicError()
}
// ExtensionSupported reports whether the specified OpenGL or context creation
// API extension is supported by the current context. For example, on Windows
// both the OpenGL and WGL extension strings are checked.
//
// As this functions searches one or more extension strings on each call, it is
// recommended that you cache its results if it's going to be used frequently.
// The extension strings will not change during the lifetime of a context, so
// there is no danger in doing this.
func ExtensionSupported(extension string) bool {
e := C.CString(extension)
defer C.free(unsafe.Pointer(e))
ret := glfwbool(C.glfwExtensionSupported(e))
panicError()
return ret
}

9
vendor/github.com/go-gl/glfw/v3.2/glfw/error.c generated vendored Normal file
View File

@@ -0,0 +1,9 @@
#include "_cgo_export.h"
void glfwErrorCB(int code, const char *desc) {
goErrorCB(code, (char*)desc);
}
void glfwSetErrorCallbackCB() {
glfwSetErrorCallback(glfwErrorCB);
}

199
vendor/github.com/go-gl/glfw/v3.2/glfw/error.go generated vendored Normal file
View File

@@ -0,0 +1,199 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
//void glfwSetErrorCallbackCB();
import "C"
import (
"fmt"
"log"
)
// ErrorCode corresponds to an error code.
type ErrorCode int
// Error codes that are translated to panics and the programmer should not
// expect to handle.
const (
notInitialized ErrorCode = C.GLFW_NOT_INITIALIZED // GLFW has not been initialized.
noCurrentContext ErrorCode = C.GLFW_NO_CURRENT_CONTEXT // No context is current.
invalidEnum ErrorCode = C.GLFW_INVALID_ENUM // One of the enum parameters for the function was given an invalid enum.
invalidValue ErrorCode = C.GLFW_INVALID_VALUE // One of the parameters for the function was given an invalid value.
outOfMemory ErrorCode = C.GLFW_OUT_OF_MEMORY // A memory allocation failed.
platformError ErrorCode = C.GLFW_PLATFORM_ERROR // A platform-specific error occurred that does not match any of the more specific categories.
)
const (
// APIUnavailable is the error code used when GLFW could not find support
// for the requested client API on the system.
//
// The installed graphics driver does not support the requested client API,
// or does not support it via the chosen context creation backend. Below
// are a few examples.
//
// Some pre-installed Windows graphics drivers do not support OpenGL. AMD
// only supports OpenGL ES via EGL, while Nvidia and Intel only supports it
// via a WGL or GLX extension. OS X does not provide OpenGL ES at all. The
// Mesa EGL, OpenGL and OpenGL ES libraries do not interface with the
// Nvidia binary driver.
APIUnavailable ErrorCode = C.GLFW_API_UNAVAILABLE
// VersionUnavailable is the error code used when the requested OpenGL or
// OpenGL ES (including any requested profile or context option) is not
// available on this machine.
//
// The machine does not support your requirements. If your application is
// sufficiently flexible, downgrade your requirements and try again.
// Otherwise, inform the user that their machine does not match your
// requirements.
//
// Future invalid OpenGL and OpenGL ES versions, for example OpenGL 4.8 if
// 5.0 comes out before the 4.x series gets that far, also fail with this
// error and not GLFW_INVALID_VALUE, because GLFW cannot know what future
// versions will exist.
VersionUnavailable ErrorCode = C.GLFW_VERSION_UNAVAILABLE
// FormatUnavailable is the error code used for both window creation and
// clipboard querying format errors.
//
// If emitted during window creation, the requested pixel format is not
// supported. This means one or more hard constraints did not match any of
// the available pixel formats. If your application is sufficiently
// flexible, downgrade your requirements and try again. Otherwise, inform
// the user that their machine does not match your requirements.
//
// If emitted when querying the clipboard, the contents of the clipboard
// could not be converted to the requested format. You should ignore the
// error or report it to the user, as appropriate.
FormatUnavailable ErrorCode = C.GLFW_FORMAT_UNAVAILABLE
)
func (e ErrorCode) String() string {
switch e {
case notInitialized:
return "NotInitialized"
case noCurrentContext:
return "NoCurrentContext"
case invalidEnum:
return "InvalidEnum"
case invalidValue:
return "InvalidValue"
case outOfMemory:
return "OutOfMemory"
case platformError:
return "PlatformError"
case APIUnavailable:
return "APIUnavailable"
case VersionUnavailable:
return "VersionUnavailable"
case FormatUnavailable:
return "FormatUnavailable"
default:
return fmt.Sprintf("ErrorCode(%d)", e)
}
}
// Error holds error code and description.
type Error struct {
Code ErrorCode
Desc string
}
// Error prints the error code and description in a readable format.
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.Code.String(), e.Desc)
}
// Note: There are many cryptic caveats to proper error handling here.
// See: https://github.com/go-gl/glfw3/pull/86
// Holds the value of the last error.
var lastError = make(chan *Error, 1)
//export goErrorCB
func goErrorCB(code C.int, desc *C.char) {
flushErrors()
err := &Error{ErrorCode(code), C.GoString(desc)}
select {
case lastError <- err:
default:
fmt.Println("GLFW: An uncaught error has occurred:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
}
}
// Set the glfw callback internally
func init() {
C.glfwSetErrorCallbackCB()
}
// flushErrors is called by Terminate before it actually calls C.glfwTerminate,
// this ensures that any uncaught errors buffered in lastError are printed
// before the program exits.
func flushErrors() {
err := fetchError()
if err != nil {
fmt.Println("GLFW: An uncaught error has occurred:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
}
}
// acceptError fetches the next error from the error channel, it accepts only
// errors with one of the given error codes. If any other error is encountered,
// a panic will occur.
//
// Platform errors are always printed, for information why please see:
//
// https://github.com/go-gl/glfw/issues/127
//
func acceptError(codes ...ErrorCode) error {
// Grab the next error, if there is one.
err := fetchError()
if err == nil {
return nil
}
// Only if the error has the specific error code accepted by the caller, do
// we return the error.
for _, code := range codes {
if err.Code == code {
return err
}
}
// The error isn't accepted by the caller. If the error code is not a code
// defined in the GLFW C documentation as a programmer error, then the
// caller should have accepted it. This is effectively a bug in this
// package.
switch err.Code {
case platformError:
log.Println(err)
return nil
case notInitialized, noCurrentContext, invalidEnum, invalidValue, outOfMemory:
panic(err)
default:
fmt.Println("GLFW: An invalid error was not accepted by the caller:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
panic(err)
}
}
// panicError is a helper used by functions which expect no errors (except
// programmer errors) to occur. It will panic if it finds any such error.
func panicError() {
err := acceptError()
if err != nil {
panic(err)
}
}
// fetchError fetches the next error from the error channel, it does not block
// and returns nil if there is no error present.
func fetchError() *Error {
select {
case err := <-lastError:
return err
default:
return nil
}
}

76
vendor/github.com/go-gl/glfw/v3.2/glfw/glfw.go generated vendored Normal file
View File

@@ -0,0 +1,76 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
import "C"
// Version constants.
const (
VersionMajor = C.GLFW_VERSION_MAJOR // This is incremented when the API is changed in non-compatible ways.
VersionMinor = C.GLFW_VERSION_MINOR // This is incremented when features are added to the API but it remains backward-compatible.
VersionRevision = C.GLFW_VERSION_REVISION // This is incremented when a bug fix release is made that does not contain any API changes.
)
// Init initializes the GLFW library. Before most GLFW functions can be used,
// GLFW must be initialized, and before a program terminates GLFW should be
// terminated in order to free any resources allocated during or after
// initialization.
//
// If this function fails, it calls Terminate before returning. If it succeeds,
// you should call Terminate before the program exits.
//
// Additional calls to this function after successful initialization but before
// termination will succeed but will do nothing.
//
// This function may take several seconds to complete on some systems, while on
// other systems it may take only a fraction of a second to complete.
//
// On Mac OS X, this function will change the current directory of the
// application to the Contents/Resources subdirectory of the application's
// bundle, if present.
//
// This function may only be called from the main thread.
func Init() error {
C.glfwInit()
return acceptError(APIUnavailable)
}
// Terminate destroys all remaining windows, frees any allocated resources and
// sets the library to an uninitialized state. Once this is called, you must
// again call Init successfully before you will be able to use most GLFW
// functions.
//
// If GLFW has been successfully initialized, this function should be called
// before the program exits. If initialization fails, there is no need to call
// this function, as it is called by Init before it returns failure.
//
// This function may only be called from the main thread.
func Terminate() {
flushErrors()
C.glfwTerminate()
}
// GetVersion retrieves the major, minor and revision numbers of the GLFW
// library. It is intended for when you are using GLFW as a shared library and
// want to ensure that you are using the minimum required version.
//
// This function may be called before Init.
func GetVersion() (major, minor, revision int) {
var (
maj C.int
min C.int
rev C.int
)
C.glfwGetVersion(&maj, &min, &rev)
return int(maj), int(min), int(rev)
}
// GetVersionString returns a static string generated at compile-time according
// to which configuration macros were defined. This is intended for use when
// submitting bug reports, to allow developers to see which code paths are
// enabled in a binary.
//
// This function may be called before Init.
func GetVersionString() string {
return C.GoString(C.glfwGetVersionString())
}

View File

@@ -0,0 +1,22 @@
Copyright (c) 2002-2006 Marcus Geelnard
Copyright (c) 2006-2016 Camilla Berglund <elmindreda@glfw.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would
be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.

81
vendor/github.com/go-gl/glfw/v3.2/glfw/input.c generated vendored Normal file
View File

@@ -0,0 +1,81 @@
#include "_cgo_export.h"
void glfwJoystickCB(int joy, int event) {
goJoystickCB(joy, event);
}
void glfwMouseButtonCB(GLFWwindow* window, int button, int action, int mods) {
goMouseButtonCB(window, button, action, mods);
}
void glfwCursorPosCB(GLFWwindow* window, double xpos, double ypos) {
goCursorPosCB(window, xpos, ypos);
}
void glfwCursorEnterCB(GLFWwindow* window, int entered) {
goCursorEnterCB(window, entered);
}
void glfwScrollCB(GLFWwindow* window, double xoff, double yoff) {
goScrollCB(window, xoff, yoff);
}
void glfwKeyCB(GLFWwindow* window, int key, int scancode, int action, int mods) {
goKeyCB(window, key, scancode, action, mods);
}
void glfwCharCB(GLFWwindow* window, unsigned int character) {
goCharCB(window, character);
}
void glfwCharModsCB(GLFWwindow* window, unsigned int character, int mods) {
goCharModsCB(window, character, mods);
}
void glfwDropCB(GLFWwindow* window, int count, const char **names) {
goDropCB(window, count, (char**)names);
}
void glfwSetJoystickCallbackCB() {
glfwSetJoystickCallback(glfwJoystickCB);
}
void glfwSetKeyCallbackCB(GLFWwindow *window) {
glfwSetKeyCallback(window, glfwKeyCB);
}
void glfwSetCharCallbackCB(GLFWwindow *window) {
glfwSetCharCallback(window, glfwCharCB);
}
void glfwSetCharModsCallbackCB(GLFWwindow *window) {
glfwSetCharModsCallback(window, glfwCharModsCB);
}
void glfwSetMouseButtonCallbackCB(GLFWwindow *window) {
glfwSetMouseButtonCallback(window, glfwMouseButtonCB);
}
void glfwSetCursorPosCallbackCB(GLFWwindow *window) {
glfwSetCursorPosCallback(window, glfwCursorPosCB);
}
void glfwSetCursorEnterCallbackCB(GLFWwindow *window) {
glfwSetCursorEnterCallback(window, glfwCursorEnterCB);
}
void glfwSetScrollCallbackCB(GLFWwindow *window) {
glfwSetScrollCallback(window, glfwScrollCB);
}
void glfwSetDropCallbackCB(GLFWwindow *window) {
glfwSetDropCallback(window, glfwDropCB);
}
float GetAxisAtIndex(float *axis, int i) {
return axis[i];
}
unsigned char GetButtonsAtIndex(unsigned char *buttons, int i) {
return buttons[i];
}

696
vendor/github.com/go-gl/glfw/v3.2/glfw/input.go generated vendored Normal file
View File

@@ -0,0 +1,696 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
//void glfwSetJoystickCallbackCB();
//void glfwSetKeyCallbackCB(GLFWwindow *window);
//void glfwSetCharCallbackCB(GLFWwindow *window);
//void glfwSetCharModsCallbackCB(GLFWwindow *window);
//void glfwSetMouseButtonCallbackCB(GLFWwindow *window);
//void glfwSetCursorPosCallbackCB(GLFWwindow *window);
//void glfwSetCursorEnterCallbackCB(GLFWwindow *window);
//void glfwSetScrollCallbackCB(GLFWwindow *window);
//void glfwSetDropCallbackCB(GLFWwindow *window);
//float GetAxisAtIndex(float *axis, int i);
//unsigned char GetButtonsAtIndex(unsigned char *buttons, int i);
import "C"
import (
"image"
"image/draw"
"unsafe"
)
var fJoystickHolder func(joy, event int)
// Joystick corresponds to a joystick.
type Joystick int
// Joystick IDs.
const (
Joystick1 Joystick = C.GLFW_JOYSTICK_1
Joystick2 Joystick = C.GLFW_JOYSTICK_2
Joystick3 Joystick = C.GLFW_JOYSTICK_3
Joystick4 Joystick = C.GLFW_JOYSTICK_4
Joystick5 Joystick = C.GLFW_JOYSTICK_5
Joystick6 Joystick = C.GLFW_JOYSTICK_6
Joystick7 Joystick = C.GLFW_JOYSTICK_7
Joystick8 Joystick = C.GLFW_JOYSTICK_8
Joystick9 Joystick = C.GLFW_JOYSTICK_9
Joystick10 Joystick = C.GLFW_JOYSTICK_10
Joystick11 Joystick = C.GLFW_JOYSTICK_11
Joystick12 Joystick = C.GLFW_JOYSTICK_12
Joystick13 Joystick = C.GLFW_JOYSTICK_13
Joystick14 Joystick = C.GLFW_JOYSTICK_14
Joystick15 Joystick = C.GLFW_JOYSTICK_15
Joystick16 Joystick = C.GLFW_JOYSTICK_16
JoystickLast Joystick = C.GLFW_JOYSTICK_LAST
)
// Key corresponds to a keyboard key.
type Key int
// These key codes are inspired by the USB HID Usage Tables v1.12 (p. 53-60),
// but re-arranged to map to 7-bit ASCII for printable keys (function keys are
// put in the 256+ range).
const (
KeyUnknown Key = C.GLFW_KEY_UNKNOWN
KeySpace Key = C.GLFW_KEY_SPACE
KeyApostrophe Key = C.GLFW_KEY_APOSTROPHE
KeyComma Key = C.GLFW_KEY_COMMA
KeyMinus Key = C.GLFW_KEY_MINUS
KeyPeriod Key = C.GLFW_KEY_PERIOD
KeySlash Key = C.GLFW_KEY_SLASH
Key0 Key = C.GLFW_KEY_0
Key1 Key = C.GLFW_KEY_1
Key2 Key = C.GLFW_KEY_2
Key3 Key = C.GLFW_KEY_3
Key4 Key = C.GLFW_KEY_4
Key5 Key = C.GLFW_KEY_5
Key6 Key = C.GLFW_KEY_6
Key7 Key = C.GLFW_KEY_7
Key8 Key = C.GLFW_KEY_8
Key9 Key = C.GLFW_KEY_9
KeySemicolon Key = C.GLFW_KEY_SEMICOLON
KeyEqual Key = C.GLFW_KEY_EQUAL
KeyA Key = C.GLFW_KEY_A
KeyB Key = C.GLFW_KEY_B
KeyC Key = C.GLFW_KEY_C
KeyD Key = C.GLFW_KEY_D
KeyE Key = C.GLFW_KEY_E
KeyF Key = C.GLFW_KEY_F
KeyG Key = C.GLFW_KEY_G
KeyH Key = C.GLFW_KEY_H
KeyI Key = C.GLFW_KEY_I
KeyJ Key = C.GLFW_KEY_J
KeyK Key = C.GLFW_KEY_K
KeyL Key = C.GLFW_KEY_L
KeyM Key = C.GLFW_KEY_M
KeyN Key = C.GLFW_KEY_N
KeyO Key = C.GLFW_KEY_O
KeyP Key = C.GLFW_KEY_P
KeyQ Key = C.GLFW_KEY_Q
KeyR Key = C.GLFW_KEY_R
KeyS Key = C.GLFW_KEY_S
KeyT Key = C.GLFW_KEY_T
KeyU Key = C.GLFW_KEY_U
KeyV Key = C.GLFW_KEY_V
KeyW Key = C.GLFW_KEY_W
KeyX Key = C.GLFW_KEY_X
KeyY Key = C.GLFW_KEY_Y
KeyZ Key = C.GLFW_KEY_Z
KeyLeftBracket Key = C.GLFW_KEY_LEFT_BRACKET
KeyBackslash Key = C.GLFW_KEY_BACKSLASH
KeyRightBracket Key = C.GLFW_KEY_RIGHT_BRACKET
KeyGraveAccent Key = C.GLFW_KEY_GRAVE_ACCENT
KeyWorld1 Key = C.GLFW_KEY_WORLD_1
KeyWorld2 Key = C.GLFW_KEY_WORLD_2
KeyEscape Key = C.GLFW_KEY_ESCAPE
KeyEnter Key = C.GLFW_KEY_ENTER
KeyTab Key = C.GLFW_KEY_TAB
KeyBackspace Key = C.GLFW_KEY_BACKSPACE
KeyInsert Key = C.GLFW_KEY_INSERT
KeyDelete Key = C.GLFW_KEY_DELETE
KeyRight Key = C.GLFW_KEY_RIGHT
KeyLeft Key = C.GLFW_KEY_LEFT
KeyDown Key = C.GLFW_KEY_DOWN
KeyUp Key = C.GLFW_KEY_UP
KeyPageUp Key = C.GLFW_KEY_PAGE_UP
KeyPageDown Key = C.GLFW_KEY_PAGE_DOWN
KeyHome Key = C.GLFW_KEY_HOME
KeyEnd Key = C.GLFW_KEY_END
KeyCapsLock Key = C.GLFW_KEY_CAPS_LOCK
KeyScrollLock Key = C.GLFW_KEY_SCROLL_LOCK
KeyNumLock Key = C.GLFW_KEY_NUM_LOCK
KeyPrintScreen Key = C.GLFW_KEY_PRINT_SCREEN
KeyPause Key = C.GLFW_KEY_PAUSE
KeyF1 Key = C.GLFW_KEY_F1
KeyF2 Key = C.GLFW_KEY_F2
KeyF3 Key = C.GLFW_KEY_F3
KeyF4 Key = C.GLFW_KEY_F4
KeyF5 Key = C.GLFW_KEY_F5
KeyF6 Key = C.GLFW_KEY_F6
KeyF7 Key = C.GLFW_KEY_F7
KeyF8 Key = C.GLFW_KEY_F8
KeyF9 Key = C.GLFW_KEY_F9
KeyF10 Key = C.GLFW_KEY_F10
KeyF11 Key = C.GLFW_KEY_F11
KeyF12 Key = C.GLFW_KEY_F12
KeyF13 Key = C.GLFW_KEY_F13
KeyF14 Key = C.GLFW_KEY_F14
KeyF15 Key = C.GLFW_KEY_F15
KeyF16 Key = C.GLFW_KEY_F16
KeyF17 Key = C.GLFW_KEY_F17
KeyF18 Key = C.GLFW_KEY_F18
KeyF19 Key = C.GLFW_KEY_F19
KeyF20 Key = C.GLFW_KEY_F20
KeyF21 Key = C.GLFW_KEY_F21
KeyF22 Key = C.GLFW_KEY_F22
KeyF23 Key = C.GLFW_KEY_F23
KeyF24 Key = C.GLFW_KEY_F24
KeyF25 Key = C.GLFW_KEY_F25
KeyKP0 Key = C.GLFW_KEY_KP_0
KeyKP1 Key = C.GLFW_KEY_KP_1
KeyKP2 Key = C.GLFW_KEY_KP_2
KeyKP3 Key = C.GLFW_KEY_KP_3
KeyKP4 Key = C.GLFW_KEY_KP_4
KeyKP5 Key = C.GLFW_KEY_KP_5
KeyKP6 Key = C.GLFW_KEY_KP_6
KeyKP7 Key = C.GLFW_KEY_KP_7
KeyKP8 Key = C.GLFW_KEY_KP_8
KeyKP9 Key = C.GLFW_KEY_KP_9
KeyKPDecimal Key = C.GLFW_KEY_KP_DECIMAL
KeyKPDivide Key = C.GLFW_KEY_KP_DIVIDE
KeyKPMultiply Key = C.GLFW_KEY_KP_MULTIPLY
KeyKPSubtract Key = C.GLFW_KEY_KP_SUBTRACT
KeyKPAdd Key = C.GLFW_KEY_KP_ADD
KeyKPEnter Key = C.GLFW_KEY_KP_ENTER
KeyKPEqual Key = C.GLFW_KEY_KP_EQUAL
KeyLeftShift Key = C.GLFW_KEY_LEFT_SHIFT
KeyLeftControl Key = C.GLFW_KEY_LEFT_CONTROL
KeyLeftAlt Key = C.GLFW_KEY_LEFT_ALT
KeyLeftSuper Key = C.GLFW_KEY_LEFT_SUPER
KeyRightShift Key = C.GLFW_KEY_RIGHT_SHIFT
KeyRightControl Key = C.GLFW_KEY_RIGHT_CONTROL
KeyRightAlt Key = C.GLFW_KEY_RIGHT_ALT
KeyRightSuper Key = C.GLFW_KEY_RIGHT_SUPER
KeyMenu Key = C.GLFW_KEY_MENU
KeyLast Key = C.GLFW_KEY_LAST
)
// ModifierKey corresponds to a modifier key.
type ModifierKey int
// Modifier keys.
const (
ModShift ModifierKey = C.GLFW_MOD_SHIFT
ModControl ModifierKey = C.GLFW_MOD_CONTROL
ModAlt ModifierKey = C.GLFW_MOD_ALT
ModSuper ModifierKey = C.GLFW_MOD_SUPER
)
// MouseButton corresponds to a mouse button.
type MouseButton int
// Mouse buttons.
const (
MouseButton1 MouseButton = C.GLFW_MOUSE_BUTTON_1
MouseButton2 MouseButton = C.GLFW_MOUSE_BUTTON_2
MouseButton3 MouseButton = C.GLFW_MOUSE_BUTTON_3
MouseButton4 MouseButton = C.GLFW_MOUSE_BUTTON_4
MouseButton5 MouseButton = C.GLFW_MOUSE_BUTTON_5
MouseButton6 MouseButton = C.GLFW_MOUSE_BUTTON_6
MouseButton7 MouseButton = C.GLFW_MOUSE_BUTTON_7
MouseButton8 MouseButton = C.GLFW_MOUSE_BUTTON_8
MouseButtonLast MouseButton = C.GLFW_MOUSE_BUTTON_LAST
MouseButtonLeft MouseButton = C.GLFW_MOUSE_BUTTON_LEFT
MouseButtonRight MouseButton = C.GLFW_MOUSE_BUTTON_RIGHT
MouseButtonMiddle MouseButton = C.GLFW_MOUSE_BUTTON_MIDDLE
)
// StandardCursor corresponds to a standard cursor icon.
type StandardCursor int
// Standard cursors
const (
ArrowCursor StandardCursor = C.GLFW_ARROW_CURSOR
IBeamCursor StandardCursor = C.GLFW_IBEAM_CURSOR
CrosshairCursor StandardCursor = C.GLFW_CROSSHAIR_CURSOR
HandCursor StandardCursor = C.GLFW_HAND_CURSOR
HResizeCursor StandardCursor = C.GLFW_HRESIZE_CURSOR
VResizeCursor StandardCursor = C.GLFW_VRESIZE_CURSOR
)
// Action corresponds to a key or button action.
type Action int
// Action types.
const (
Release Action = C.GLFW_RELEASE // The key or button was released.
Press Action = C.GLFW_PRESS // The key or button was pressed.
Repeat Action = C.GLFW_REPEAT // The key was held down until it repeated.
)
// InputMode corresponds to an input mode.
type InputMode int
// Input modes.
const (
CursorMode InputMode = C.GLFW_CURSOR // See Cursor mode values
StickyKeysMode InputMode = C.GLFW_STICKY_KEYS // Value can be either 1 or 0
StickyMouseButtonsMode InputMode = C.GLFW_STICKY_MOUSE_BUTTONS // Value can be either 1 or 0
)
// Cursor mode values.
const (
CursorNormal int = C.GLFW_CURSOR_NORMAL
CursorHidden int = C.GLFW_CURSOR_HIDDEN
CursorDisabled int = C.GLFW_CURSOR_DISABLED
)
// Cursor represents a cursor.
type Cursor struct {
data *C.GLFWcursor
}
//export goJoystickCB
func goJoystickCB(joy, event C.int) {
fJoystickHolder(int(joy), int(event))
}
//export goMouseButtonCB
func goMouseButtonCB(window unsafe.Pointer, button, action, mods C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fMouseButtonHolder(w, MouseButton(button), Action(action), ModifierKey(mods))
}
//export goCursorPosCB
func goCursorPosCB(window unsafe.Pointer, xpos, ypos C.double) {
w := windows.get((*C.GLFWwindow)(window))
w.fCursorPosHolder(w, float64(xpos), float64(ypos))
}
//export goCursorEnterCB
func goCursorEnterCB(window unsafe.Pointer, entered C.int) {
w := windows.get((*C.GLFWwindow)(window))
hasEntered := glfwbool(entered)
w.fCursorEnterHolder(w, hasEntered)
}
//export goScrollCB
func goScrollCB(window unsafe.Pointer, xoff, yoff C.double) {
w := windows.get((*C.GLFWwindow)(window))
w.fScrollHolder(w, float64(xoff), float64(yoff))
}
//export goKeyCB
func goKeyCB(window unsafe.Pointer, key, scancode, action, mods C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fKeyHolder(w, Key(key), int(scancode), Action(action), ModifierKey(mods))
}
//export goCharCB
func goCharCB(window unsafe.Pointer, character C.uint) {
w := windows.get((*C.GLFWwindow)(window))
w.fCharHolder(w, rune(character))
}
//export goCharModsCB
func goCharModsCB(window unsafe.Pointer, character C.uint, mods C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fCharModsHolder(w, rune(character), ModifierKey(mods))
}
//export goDropCB
func goDropCB(window unsafe.Pointer, count C.int, names **C.char) { // TODO: The types of name can be `**C.char` or `unsafe.Pointer`, use whichever is better.
w := windows.get((*C.GLFWwindow)(window))
namesSlice := make([]string, int(count)) // TODO: Make this better. This part is unfinished, hacky, probably not correct, and not idiomatic.
for i := 0; i < int(count); i++ { // TODO: Make this better. It should be cleaned up and vetted.
var x *C.char // TODO: Make this better.
p := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(names)) + uintptr(i)*unsafe.Sizeof(x))) // TODO: Make this better.
namesSlice[i] = C.GoString(*p) // TODO: Make this better.
}
w.fDropHolder(w, namesSlice)
}
// GetInputMode returns the value of an input option of the window.
func (w *Window) GetInputMode(mode InputMode) int {
ret := int(C.glfwGetInputMode(w.data, C.int(mode)))
panicError()
return ret
}
// SetInputMode sets an input option for the window.
func (w *Window) SetInputMode(mode InputMode, value int) {
C.glfwSetInputMode(w.data, C.int(mode), C.int(value))
panicError()
}
// GetKey returns the last reported state of a keyboard key. The returned state
// is one of Press or Release. The higher-level state Repeat is only reported to
// the key callback.
//
// If the StickyKeys input mode is enabled, this function returns Press the first
// time you call this function after a key has been pressed, even if the key has
// already been released.
//
// The key functions deal with physical keys, with key tokens named after their
// use on the standard US keyboard layout. If you want to input text, use the
// Unicode character callback instead.
func (w *Window) GetKey(key Key) Action {
ret := Action(C.glfwGetKey(w.data, C.int(key)))
panicError()
return ret
}
// GetKeyName returns the localized name of the specified printable key.
//
// If the key is glfw.KeyUnknown, the scancode is used, otherwise the scancode is ignored.
func GetKeyName(key Key, scancode int) string {
ret := C.glfwGetKeyName(C.int(key), C.int(scancode))
panicError()
return C.GoString(ret)
}
// GetMouseButton returns the last state reported for the specified mouse button.
//
// If the StickyMouseButtons input mode is enabled, this function returns Press
// the first time you call this function after a mouse button has been pressed,
// even if the mouse button has already been released.
func (w *Window) GetMouseButton(button MouseButton) Action {
ret := Action(C.glfwGetMouseButton(w.data, C.int(button)))
panicError()
return ret
}
// GetCursorPos returns the last reported position of the cursor.
//
// If the cursor is disabled (with CursorDisabled) then the cursor position is
// unbounded and limited only by the minimum and maximum values of a double.
//
// The coordinate can be converted to their integer equivalents with the floor
// function. Casting directly to an integer type works for positive coordinates,
// but fails for negative ones.
func (w *Window) GetCursorPos() (x, y float64) {
var xpos, ypos C.double
C.glfwGetCursorPos(w.data, &xpos, &ypos)
panicError()
return float64(xpos), float64(ypos)
}
// SetCursorPos sets the position of the cursor. The specified window must
// be focused. If the window does not have focus when this function is called,
// it fails silently.
//
// If the cursor is disabled (with CursorDisabled) then the cursor position is
// unbounded and limited only by the minimum and maximum values of a double.
func (w *Window) SetCursorPos(xpos, ypos float64) {
C.glfwSetCursorPos(w.data, C.double(xpos), C.double(ypos))
panicError()
}
// CreateCursor creates a new custom cursor image that can be set for a window with SetCursor.
// The cursor can be destroyed with Destroy. Any remaining cursors are destroyed by Terminate.
//
// The image is ideally provided in the form of *image.NRGBA.
// The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight
// bits per channel with the red channel first. They are arranged canonically
// as packed sequential rows, starting from the top-left corner. If the image
// type is not *image.NRGBA, it will be converted to it.
//
// The cursor hotspot is specified in pixels, relative to the upper-left corner of the cursor image.
// Like all other coordinate systems in GLFW, the X-axis points to the right and the Y-axis points down.
func CreateCursor(img image.Image, xhot, yhot int) *Cursor {
var imgC C.GLFWimage
var pixels []uint8
b := img.Bounds()
switch img := img.(type) {
case *image.NRGBA:
pixels = img.Pix
default:
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
pixels = m.Pix
}
pix, free := bytes(pixels)
imgC.width = C.int(b.Dx())
imgC.height = C.int(b.Dy())
imgC.pixels = (*C.uchar)(pix)
c := C.glfwCreateCursor(&imgC, C.int(xhot), C.int(yhot))
free()
panicError()
return &Cursor{c}
}
// CreateStandardCursor returns a cursor with a standard shape,
// that can be set for a window with SetCursor.
func CreateStandardCursor(shape StandardCursor) *Cursor {
c := C.glfwCreateStandardCursor(C.int(shape))
panicError()
return &Cursor{c}
}
// Destroy destroys a cursor previously created with CreateCursor.
// Any remaining cursors will be destroyed by Terminate.
func (c *Cursor) Destroy() {
C.glfwDestroyCursor(c.data)
panicError()
}
// SetCursor sets the cursor image to be used when the cursor is over the client area
// of the specified window. The set cursor will only be visible when the cursor mode of the
// window is CursorNormal.
//
// On some platforms, the set cursor may not be visible unless the window also has input focus.
func (w *Window) SetCursor(c *Cursor) {
if c == nil {
C.glfwSetCursor(w.data, nil)
} else {
C.glfwSetCursor(w.data, c.data)
}
panicError()
}
// JoystickCallback is the joystick configuration callback.
type JoystickCallback func(joy, event int)
// SetJoystickCallback sets the joystick configuration callback, or removes the
// currently set callback. This is called when a joystick is connected to or
// disconnected from the system.
func SetJoystickCallback(cbfun JoystickCallback) (previous JoystickCallback) {
previous = fJoystickHolder
fJoystickHolder = cbfun
if cbfun == nil {
C.glfwSetJoystickCallback(nil)
} else {
C.glfwSetJoystickCallbackCB()
}
panicError()
return previous
}
// KeyCallback is the key callback.
type KeyCallback func(w *Window, key Key, scancode int, action Action, mods ModifierKey)
// SetKeyCallback sets the key callback which is called when a key is pressed,
// repeated or released.
//
// The key functions deal with physical keys, with layout independent key tokens
// named after their values in the standard US keyboard layout. If you want to
// input text, use the SetCharCallback instead.
//
// When a window loses focus, it will generate synthetic key release events for
// all pressed keys. You can tell these events from user-generated events by the
// fact that the synthetic ones are generated after the window has lost focus,
// i.e. Focused will be false and the focus callback will have already been
// called.
func (w *Window) SetKeyCallback(cbfun KeyCallback) (previous KeyCallback) {
previous = w.fKeyHolder
w.fKeyHolder = cbfun
if cbfun == nil {
C.glfwSetKeyCallback(w.data, nil)
} else {
C.glfwSetKeyCallbackCB(w.data)
}
panicError()
return previous
}
// CharCallback is the character callback.
type CharCallback func(w *Window, char rune)
// SetCharCallback sets the character callback which is called when a
// Unicode character is input.
//
// The character callback is intended for Unicode text input. As it deals with
// characters, it is keyboard layout dependent, whereas the
// key callback is not. Characters do not map 1:1
// to physical keys, as a key may produce zero, one or more characters. If you
// want to know whether a specific physical key was pressed or released, see
// the key callback instead.
//
// The character callback behaves as system text input normally does and will
// not be called if modifier keys are held down that would prevent normal text
// input on that platform, for example a Super (Command) key on OS X or Alt key
// on Windows. There is a character with modifiers callback that receives these events.
func (w *Window) SetCharCallback(cbfun CharCallback) (previous CharCallback) {
previous = w.fCharHolder
w.fCharHolder = cbfun
if cbfun == nil {
C.glfwSetCharCallback(w.data, nil)
} else {
C.glfwSetCharCallbackCB(w.data)
}
panicError()
return previous
}
// CharModsCallback is the character with modifiers callback.
type CharModsCallback func(w *Window, char rune, mods ModifierKey)
// SetCharModsCallback sets the character with modifiers callback which is called when a
// Unicode character is input regardless of what modifier keys are used.
//
// The character with modifiers callback is intended for implementing custom
// Unicode character input. For regular Unicode text input, see the
// character callback. Like the character callback, the character with modifiers callback
// deals with characters and is keyboard layout dependent. Characters do not
// map 1:1 to physical keys, as a key may produce zero, one or more characters.
// If you want to know whether a specific physical key was pressed or released,
// see the key callback instead.
func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) {
previous = w.fCharModsHolder
w.fCharModsHolder = cbfun
if cbfun == nil {
C.glfwSetCharModsCallback(w.data, nil)
} else {
C.glfwSetCharModsCallbackCB(w.data)
}
panicError()
return previous
}
// MouseButtonCallback is the mouse button callback.
type MouseButtonCallback func(w *Window, button MouseButton, action Action, mod ModifierKey)
// SetMouseButtonCallback sets the mouse button callback which is called when a
// mouse button is pressed or released.
//
// When a window loses focus, it will generate synthetic mouse button release
// events for all pressed mouse buttons. You can tell these events from
// user-generated events by the fact that the synthetic ones are generated after
// the window has lost focus, i.e. Focused will be false and the focus
// callback will have already been called.
func (w *Window) SetMouseButtonCallback(cbfun MouseButtonCallback) (previous MouseButtonCallback) {
previous = w.fMouseButtonHolder
w.fMouseButtonHolder = cbfun
if cbfun == nil {
C.glfwSetMouseButtonCallback(w.data, nil)
} else {
C.glfwSetMouseButtonCallbackCB(w.data)
}
panicError()
return previous
}
// CursorPosCallback the cursor position callback.
type CursorPosCallback func(w *Window, xpos float64, ypos float64)
// SetCursorPosCallback sets the cursor position callback which is called
// when the cursor is moved. The callback is provided with the position relative
// to the upper-left corner of the client area of the window.
func (w *Window) SetCursorPosCallback(cbfun CursorPosCallback) (previous CursorPosCallback) {
previous = w.fCursorPosHolder
w.fCursorPosHolder = cbfun
if cbfun == nil {
C.glfwSetCursorPosCallback(w.data, nil)
} else {
C.glfwSetCursorPosCallbackCB(w.data)
}
panicError()
return previous
}
// CursorEnterCallback is the cursor boundary crossing callback.
type CursorEnterCallback func(w *Window, entered bool)
// SetCursorEnterCallback the cursor boundary crossing callback which is called
// when the cursor enters or leaves the client area of the window.
func (w *Window) SetCursorEnterCallback(cbfun CursorEnterCallback) (previous CursorEnterCallback) {
previous = w.fCursorEnterHolder
w.fCursorEnterHolder = cbfun
if cbfun == nil {
C.glfwSetCursorEnterCallback(w.data, nil)
} else {
C.glfwSetCursorEnterCallbackCB(w.data)
}
panicError()
return previous
}
// ScrollCallback is the scroll callback.
type ScrollCallback func(w *Window, xoff float64, yoff float64)
// SetScrollCallback sets the scroll callback which is called when a scrolling
// device is used, such as a mouse wheel or scrolling area of a touchpad.
func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback) {
previous = w.fScrollHolder
w.fScrollHolder = cbfun
if cbfun == nil {
C.glfwSetScrollCallback(w.data, nil)
} else {
C.glfwSetScrollCallbackCB(w.data)
}
panicError()
return previous
}
// DropCallback is the drop callback.
type DropCallback func(w *Window, names []string)
// SetDropCallback sets the drop callback which is called when an object
// is dropped over the window.
func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {
previous = w.fDropHolder
w.fDropHolder = cbfun
if cbfun == nil {
C.glfwSetDropCallback(w.data, nil)
} else {
C.glfwSetDropCallbackCB(w.data)
}
panicError()
return previous
}
// JoystickPresent reports whether the specified joystick is present.
func JoystickPresent(joy Joystick) bool {
ret := glfwbool(C.glfwJoystickPresent(C.int(joy)))
panicError()
return ret
}
// GetJoystickAxes returns a slice of axis values.
func GetJoystickAxes(joy Joystick) []float32 {
var length int
axis := C.glfwGetJoystickAxes(C.int(joy), (*C.int)(unsafe.Pointer(&length)))
panicError()
if axis == nil {
return nil
}
a := make([]float32, length)
for i := 0; i < length; i++ {
a[i] = float32(C.GetAxisAtIndex(axis, C.int(i)))
}
return a
}
// GetJoystickButtons returns a slice of button values.
func GetJoystickButtons(joy Joystick) []byte {
var length int
buttons := C.glfwGetJoystickButtons(C.int(joy), (*C.int)(unsafe.Pointer(&length)))
panicError()
if buttons == nil {
return nil
}
b := make([]byte, length)
for i := 0; i < length; i++ {
b[i] = byte(C.GetButtonsAtIndex(buttons, C.int(i)))
}
return b
}
// GetJoystickName returns the name, encoded as UTF-8, of the specified joystick.
func GetJoystickName(joy Joystick) string {
jn := C.glfwGetJoystickName(C.int(joy))
panicError()
return C.GoString(jn)
}

25
vendor/github.com/go-gl/glfw/v3.2/glfw/monitor.c generated vendored Normal file
View File

@@ -0,0 +1,25 @@
#include "_cgo_export.h"
GLFWmonitor *GetMonitorAtIndex(GLFWmonitor **monitors, int index) {
return monitors[index];
}
GLFWvidmode GetVidmodeAtIndex(GLFWvidmode *vidmodes, int index) {
return vidmodes[index];
}
void glfwMonitorCB(GLFWmonitor* monitor, int event) {
goMonitorCB(monitor, event);
}
void glfwSetMonitorCallbackCB() {
glfwSetMonitorCallback(glfwMonitorCB);
}
unsigned int GetGammaAtIndex(unsigned short *color, int i) {
return color[i];
}
void SetGammaAtIndex(unsigned short *color, int i, unsigned short value) {
color[i] = value;
}

208
vendor/github.com/go-gl/glfw/v3.2/glfw/monitor.go generated vendored Normal file
View File

@@ -0,0 +1,208 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
//GLFWmonitor* GetMonitorAtIndex(GLFWmonitor **monitors, int index);
//GLFWvidmode GetVidmodeAtIndex(GLFWvidmode *vidmodes, int index);
//void glfwSetMonitorCallbackCB();
//unsigned int GetGammaAtIndex(unsigned short *color, int i);
//void SetGammaAtIndex(unsigned short *color, int i, unsigned short value);
import "C"
import (
"unsafe"
)
// Monitor represents a monitor.
type Monitor struct {
data *C.GLFWmonitor
}
// MonitorEvent corresponds to a monitor configuration event.
type MonitorEvent int
// GammaRamp describes the gamma ramp for a monitor.
type GammaRamp struct {
Red []uint16 // A slice of value describing the response of the red channel.
Green []uint16 // A slice of value describing the response of the green channel.
Blue []uint16 // A slice of value describing the response of the blue channel.
}
// Monitor events.
const (
Connected MonitorEvent = C.GLFW_CONNECTED
Disconnected MonitorEvent = C.GLFW_DISCONNECTED
)
// VidMode describes a single video mode.
type VidMode struct {
Width int // The width, in pixels, of the video mode.
Height int // The height, in pixels, of the video mode.
RedBits int // The bit depth of the red channel of the video mode.
GreenBits int // The bit depth of the green channel of the video mode.
BlueBits int // The bit depth of the blue channel of the video mode.
RefreshRate int // The refresh rate, in Hz, of the video mode.
}
var fMonitorHolder func(monitor *Monitor, event MonitorEvent)
//export goMonitorCB
func goMonitorCB(monitor unsafe.Pointer, event C.int) {
fMonitorHolder(&Monitor{(*C.GLFWmonitor)(monitor)}, MonitorEvent(event))
}
// GetMonitors returns a slice of handles for all currently connected monitors.
func GetMonitors() []*Monitor {
var length int
mC := C.glfwGetMonitors((*C.int)(unsafe.Pointer(&length)))
panicError()
if mC == nil {
return nil
}
m := make([]*Monitor, length)
for i := 0; i < length; i++ {
m[i] = &Monitor{C.GetMonitorAtIndex(mC, C.int(i))}
}
return m
}
// GetPrimaryMonitor returns the primary monitor. This is usually the monitor
// where elements like the Windows task bar or the OS X menu bar is located.
func GetPrimaryMonitor() *Monitor {
m := C.glfwGetPrimaryMonitor()
panicError()
if m == nil {
return nil
}
return &Monitor{m}
}
// GetPos returns the position, in screen coordinates, of the upper-left
// corner of the monitor.
func (m *Monitor) GetPos() (x, y int) {
var xpos, ypos C.int
C.glfwGetMonitorPos(m.data, &xpos, &ypos)
panicError()
return int(xpos), int(ypos)
}
// GetPhysicalSize returns the size, in millimetres, of the display area of the
// monitor.
//
// Note: Some operating systems do not provide accurate information, either
// because the monitor's EDID data is incorrect, or because the driver does not
// report it accurately.
func (m *Monitor) GetPhysicalSize() (width, height int) {
var wi, h C.int
C.glfwGetMonitorPhysicalSize(m.data, &wi, &h)
panicError()
return int(wi), int(h)
}
// GetName returns a human-readable name of the monitor, encoded as UTF-8.
func (m *Monitor) GetName() string {
mn := C.glfwGetMonitorName(m.data)
panicError()
if mn == nil {
return ""
}
return C.GoString(mn)
}
// SetMonitorCallback sets the monitor configuration callback, or removes the
// currently set callback. This is called when a monitor is connected to or
// disconnected from the system.
func SetMonitorCallback(cbfun func(monitor *Monitor, event MonitorEvent)) {
if cbfun == nil {
C.glfwSetMonitorCallback(nil)
} else {
fMonitorHolder = cbfun
C.glfwSetMonitorCallbackCB()
}
panicError()
}
// GetVideoModes returns an array of all video modes supported by the monitor.
// The returned array is sorted in ascending order, first by color bit depth
// (the sum of all channel depths) and then by resolution area (the product of
// width and height).
func (m *Monitor) GetVideoModes() []*VidMode {
var length int
vC := C.glfwGetVideoModes(m.data, (*C.int)(unsafe.Pointer(&length)))
panicError()
if vC == nil {
return nil
}
v := make([]*VidMode, length)
for i := 0; i < length; i++ {
t := C.GetVidmodeAtIndex(vC, C.int(i))
v[i] = &VidMode{int(t.width), int(t.height), int(t.redBits), int(t.greenBits), int(t.blueBits), int(t.refreshRate)}
}
return v
}
// GetVideoMode returns the current video mode of the monitor. If you
// are using a full screen window, the return value will therefore depend on
// whether it is focused.
func (m *Monitor) GetVideoMode() *VidMode {
t := C.glfwGetVideoMode(m.data)
if t == nil {
return nil
}
panicError()
return &VidMode{int(t.width), int(t.height), int(t.redBits), int(t.greenBits), int(t.blueBits), int(t.refreshRate)}
}
// SetGamma generates a 256-element gamma ramp from the specified exponent and then calls
// SetGamma with it.
func (m *Monitor) SetGamma(gamma float32) {
C.glfwSetGamma(m.data, C.float(gamma))
panicError()
}
// GetGammaRamp retrieves the current gamma ramp of the monitor.
func (m *Monitor) GetGammaRamp() *GammaRamp {
var ramp GammaRamp
rampC := C.glfwGetGammaRamp(m.data)
panicError()
if rampC == nil {
return nil
}
length := int(rampC.size)
ramp.Red = make([]uint16, length)
ramp.Green = make([]uint16, length)
ramp.Blue = make([]uint16, length)
for i := 0; i < length; i++ {
ramp.Red[i] = uint16(C.GetGammaAtIndex(rampC.red, C.int(i)))
ramp.Green[i] = uint16(C.GetGammaAtIndex(rampC.green, C.int(i)))
ramp.Blue[i] = uint16(C.GetGammaAtIndex(rampC.blue, C.int(i)))
}
return &ramp
}
// SetGammaRamp sets the current gamma ramp for the monitor.
func (m *Monitor) SetGammaRamp(ramp *GammaRamp) {
var rampC C.GLFWgammaramp
length := len(ramp.Red)
for i := 0; i < length; i++ {
C.SetGammaAtIndex(rampC.red, C.int(i), C.ushort(ramp.Red[i]))
C.SetGammaAtIndex(rampC.green, C.int(i), C.ushort(ramp.Green[i]))
C.SetGammaAtIndex(rampC.blue, C.int(i), C.ushort(ramp.Blue[i]))
}
C.glfwSetGammaRamp(m.data, &rampC)
panicError()
}

View File

@@ -0,0 +1,39 @@
package glfw
/*
#define GLFW_EXPOSE_NATIVE_COCOA
#define GLFW_EXPOSE_NATIVE_NSGL
#include "glfw/include/GLFW/glfw3.h"
#include "glfw/include/GLFW/glfw3native.h"
// workaround wrappers needed due to a cgo and/or LLVM bug.
// See: https://github.com/go-gl/glfw/issues/136
void *workaround_glfwGetCocoaWindow(GLFWwindow *w) {
return (void *)glfwGetCocoaWindow(w);
}
void *workaround_glfwGetNSGLContext(GLFWwindow *w) {
return (void *)glfwGetNSGLContext(w);
}
*/
import "C"
// GetCocoaMonitor returns the CGDirectDisplayID of the monitor.
func (m *Monitor) GetCocoaMonitor() uintptr {
ret := uintptr(C.glfwGetCocoaMonitor(m.data))
panicError()
return ret
}
// GetCocoaWindow returns the NSWindow of the window.
func (w *Window) GetCocoaWindow() uintptr {
ret := uintptr(C.workaround_glfwGetCocoaWindow(w.data))
panicError()
return ret
}
// GetNSGLContext returns the NSOpenGLContext of the window.
func (w *Window) GetNSGLContext() uintptr {
ret := uintptr(C.workaround_glfwGetNSGLContext(w.data))
panicError()
return ret
}

View File

@@ -0,0 +1,50 @@
// +build linux freebsd
package glfw
//#define GLFW_EXPOSE_NATIVE_X11
//#define GLFW_EXPOSE_NATIVE_GLX
//#include "glfw/include/GLFW/glfw3.h"
//#include "glfw/include/GLFW/glfw3native.h"
import "C"
func GetX11Display() *C.Display {
ret := C.glfwGetX11Display()
panicError()
return ret
}
// GetX11Adapter returns the RRCrtc of the monitor.
func (m *Monitor) GetX11Adapter() C.RRCrtc {
ret := C.glfwGetX11Adapter(m.data)
panicError()
return ret
}
// GetX11Monitor returns the RROutput of the monitor.
func (m *Monitor) GetX11Monitor() C.RROutput {
ret := C.glfwGetX11Monitor(m.data)
panicError()
return ret
}
// GetX11Window returns the Window of the window.
func (w *Window) GetX11Window() C.Window {
ret := C.glfwGetX11Window(w.data)
panicError()
return ret
}
// GetGLXContext returns the GLXContext of the window.
func (w *Window) GetGLXContext() C.GLXContext {
ret := C.glfwGetGLXContext(w.data)
panicError()
return ret
}
// GetGLXWindow returns the GLXWindow of the window.
func (w *Window) GetGLXWindow() C.GLXWindow {
ret := C.glfwGetGLXWindow(w.data)
panicError()
return ret
}

View File

@@ -0,0 +1,35 @@
package glfw
//#define GLFW_EXPOSE_NATIVE_WIN32
//#define GLFW_EXPOSE_NATIVE_WGL
//#include "glfw/include/GLFW/glfw3.h"
//#include "glfw/include/GLFW/glfw3native.h"
import "C"
// GetWin32Adapter returns the adapter device name of the monitor.
func (m *Monitor) GetWin32Adapter() string {
ret := C.glfwGetWin32Adapter(m.data)
panicError()
return C.GoString(ret)
}
// GetWin32Monitor returns the display device name of the monitor.
func (m *Monitor) GetWin32Monitor() string {
ret := C.glfwGetWin32Monitor(m.data)
panicError()
return C.GoString(ret)
}
// GetWin32Window returns the HWND of the window.
func (w *Window) GetWin32Window() C.HWND {
ret := C.glfwGetWin32Window(w.data)
panicError()
return ret
}
// GetWGLContext returns the HGLRC of the window.
func (w *Window) GetWGLContext() C.HGLRC {
ret := C.glfwGetWGLContext(w.data)
panicError()
return ret
}

41
vendor/github.com/go-gl/glfw/v3.2/glfw/time.go generated vendored Normal file
View File

@@ -0,0 +1,41 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
import "C"
// GetTime returns the value of the GLFW timer. Unless the timer has been set
// using SetTime, the timer measures time elapsed since GLFW was initialized.
//
// The resolution of the timer is system dependent, but is usually on the order
// of a few micro- or nanoseconds. It uses the highest-resolution monotonic time
// source on each supported platform.
func GetTime() float64 {
ret := float64(C.glfwGetTime())
panicError()
return ret
}
// SetTime sets the value of the GLFW timer. It then continues to count up from
// that value.
//
// The resolution of the timer is system dependent, but is usually on the order
// of a few micro- or nanoseconds. It uses the highest-resolution monotonic time
// source on each supported platform.
func SetTime(time float64) {
C.glfwSetTime(C.double(time))
panicError()
}
// GetTimerFrequency returns frequency of the timer, in Hz, or zero if an error occurred.
func GetTimerFrequency() uint64 {
ret := uint64(C.glfwGetTimerFrequency())
panicError()
return ret
}
// GetTimerValue returns the current value of the raw timer, measured in 1 / frequency seconds.
func GetTimerValue() uint64 {
ret := uint64(C.glfwGetTimerValue())
panicError()
return ret
}

37
vendor/github.com/go-gl/glfw/v3.2/glfw/util.go generated vendored Normal file
View File

@@ -0,0 +1,37 @@
package glfw
//#include <stdlib.h>
//#include "glfw/include/GLFW/glfw3.h"
import "C"
import (
"reflect"
"unsafe"
)
func glfwbool(b C.int) bool {
if b == C.GL_TRUE {
return true
}
return false
}
func bytes(origin []byte) (pointer *uint8, free func()) {
n := len(origin)
if n == 0 {
return nil, func() {}
}
data := C.malloc(C.size_t(n))
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(data),
Len: n,
Cap: n,
}))
copy(dataSlice, origin)
return &dataSlice[0], func() { C.free(data) }
}

14
vendor/github.com/go-gl/glfw/v3.2/glfw/vulkan.go generated vendored Normal file
View File

@@ -0,0 +1,14 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
import "C"
// VulkanSupported reports whether the Vulkan loader has been found. This check is performed by Init.
//
// The availability of a Vulkan loader does not by itself guarantee that window surface creation or
// even device creation is possible. Call GetRequiredInstanceExtensions to check whether the
// extensions necessary for Vulkan surface creation are available and GetPhysicalDevicePresentationSupport
// to check whether a queue family of a physical device supports image presentation.
func VulkanSupported() bool {
return glfwbool(C.glfwVulkanSupported())
}

57
vendor/github.com/go-gl/glfw/v3.2/glfw/window.c generated vendored Normal file
View File

@@ -0,0 +1,57 @@
#include "_cgo_export.h"
void glfwWindowPosCB(GLFWwindow* window, int xpos, int ypos) {
goWindowPosCB(window, xpos, ypos);
}
void glfwWindowSizeCB(GLFWwindow* window, int width, int height) {
goWindowSizeCB(window, width, height);
}
void glfwFramebufferSizeCB(GLFWwindow* window, int width, int height) {
goFramebufferSizeCB(window, width, height);
}
void glfwWindowCloseCB(GLFWwindow* window) {
goWindowCloseCB(window);
}
void glfwWindowRefreshCB(GLFWwindow* window) {
goWindowRefreshCB(window);
}
void glfwWindowFocusCB(GLFWwindow* window, int focused) {
goWindowFocusCB(window, focused);
}
void glfwWindowIconifyCB(GLFWwindow* window, int iconified) {
goWindowIconifyCB(window, iconified);
}
void glfwSetWindowPosCallbackCB(GLFWwindow* window) {
glfwSetWindowPosCallback(window, glfwWindowPosCB);
}
void glfwSetWindowSizeCallbackCB(GLFWwindow* window) {
glfwSetWindowSizeCallback(window, glfwWindowSizeCB);
}
void glfwSetFramebufferSizeCallbackCB(GLFWwindow* window) {
glfwSetFramebufferSizeCallback(window, glfwFramebufferSizeCB);
}
void glfwSetWindowCloseCallbackCB(GLFWwindow* window) {
glfwSetWindowCloseCallback(window, glfwWindowCloseCB);
}
void glfwSetWindowRefreshCallbackCB(GLFWwindow* window) {
glfwSetWindowRefreshCallback(window, glfwWindowRefreshCB);
}
void glfwSetWindowFocusCallbackCB(GLFWwindow* window) {
glfwSetWindowFocusCallback(window, glfwWindowFocusCB);
}
void glfwSetWindowIconifyCallbackCB(GLFWwindow* window) {
glfwSetWindowIconifyCallback(window, glfwWindowIconifyCB);
}

830
vendor/github.com/go-gl/glfw/v3.2/glfw/window.go generated vendored Normal file
View File

@@ -0,0 +1,830 @@
package glfw
//#include <stdlib.h>
//#include "glfw/include/GLFW/glfw3.h"
//void glfwSetWindowPosCallbackCB(GLFWwindow *window);
//void glfwSetWindowSizeCallbackCB(GLFWwindow *window);
//void glfwSetFramebufferSizeCallbackCB(GLFWwindow *window);
//void glfwSetWindowCloseCallbackCB(GLFWwindow *window);
//void glfwSetWindowRefreshCallbackCB(GLFWwindow *window);
//void glfwSetWindowFocusCallbackCB(GLFWwindow *window);
//void glfwSetWindowIconifyCallbackCB(GLFWwindow *window);
import "C"
import (
"image"
"image/draw"
"sync"
"unsafe"
)
// Internal window list stuff
type windowList struct {
l sync.Mutex
m map[*C.GLFWwindow]*Window
}
var windows = windowList{m: map[*C.GLFWwindow]*Window{}}
func (w *windowList) put(wnd *Window) {
w.l.Lock()
defer w.l.Unlock()
w.m[wnd.data] = wnd
}
func (w *windowList) remove(wnd *C.GLFWwindow) {
w.l.Lock()
defer w.l.Unlock()
delete(w.m, wnd)
}
func (w *windowList) get(wnd *C.GLFWwindow) *Window {
w.l.Lock()
defer w.l.Unlock()
return w.m[wnd]
}
// Hint corresponds to hints that can be set before creating a window.
//
// Hint also corresponds to the attributes of the window that can be get after
// its creation.
type Hint int
// Window related hints.
const (
Focused Hint = C.GLFW_FOCUSED // Specifies whether the window will be given input focus when created. This hint is ignored for full screen and initially hidden windows.
Iconified Hint = C.GLFW_ICONIFIED // Specifies whether the window will be minimized.
Maximized Hint = C.GLFW_MAXIMIZED // Specifies whether the window is maximized.
Visible Hint = C.GLFW_VISIBLE // Specifies whether the window will be initially visible.
Resizable Hint = C.GLFW_RESIZABLE // Specifies whether the window will be resizable by the user.
Decorated Hint = C.GLFW_DECORATED // Specifies whether the window will have window decorations such as a border, a close widget, etc.
Floating Hint = C.GLFW_FLOATING // Specifies whether the window will be always-on-top.
AutoIconify Hint = C.GLFW_AUTO_ICONIFY // Specifies whether fullscreen windows automatically iconify (and restore the previous video mode) on focus loss.
)
// Context related hints.
const (
ClientAPI Hint = C.GLFW_CLIENT_API // Specifies which client API to create the context for. Hard constraint.
ContextVersionMajor Hint = C.GLFW_CONTEXT_VERSION_MAJOR // Specifies the client API version that the created context must be compatible with.
ContextVersionMinor Hint = C.GLFW_CONTEXT_VERSION_MINOR // Specifies the client API version that the created context must be compatible with.
ContextRobustness Hint = C.GLFW_CONTEXT_ROBUSTNESS // Specifies the robustness strategy to be used by the context.
ContextReleaseBehavior Hint = C.GLFW_CONTEXT_RELEASE_BEHAVIOR // Specifies the release behavior to be used by the context.
OpenGLForwardCompatible Hint = C.GLFW_OPENGL_FORWARD_COMPAT // Specifies whether the OpenGL context should be forward-compatible. Hard constraint.
OpenGLDebugContext Hint = C.GLFW_OPENGL_DEBUG_CONTEXT // Specifies whether to create a debug OpenGL context, which may have additional error and performance issue reporting functionality. If OpenGL ES is requested, this hint is ignored.
OpenGLProfile Hint = C.GLFW_OPENGL_PROFILE // Specifies which OpenGL profile to create the context for. Hard constraint.
ContextCreationAPI Hint = C.GLFW_CONTEXT_CREATION_API // Specifies which context creation API to use to create the context.
)
// Framebuffer related hints.
const (
ContextRevision Hint = C.GLFW_CONTEXT_REVISION
RedBits Hint = C.GLFW_RED_BITS // Specifies the desired bit depth of the default framebuffer.
GreenBits Hint = C.GLFW_GREEN_BITS // Specifies the desired bit depth of the default framebuffer.
BlueBits Hint = C.GLFW_BLUE_BITS // Specifies the desired bit depth of the default framebuffer.
AlphaBits Hint = C.GLFW_ALPHA_BITS // Specifies the desired bit depth of the default framebuffer.
DepthBits Hint = C.GLFW_DEPTH_BITS // Specifies the desired bit depth of the default framebuffer.
StencilBits Hint = C.GLFW_STENCIL_BITS // Specifies the desired bit depth of the default framebuffer.
AccumRedBits Hint = C.GLFW_ACCUM_RED_BITS // Specifies the desired bit depth of the accumulation buffer.
AccumGreenBits Hint = C.GLFW_ACCUM_GREEN_BITS // Specifies the desired bit depth of the accumulation buffer.
AccumBlueBits Hint = C.GLFW_ACCUM_BLUE_BITS // Specifies the desired bit depth of the accumulation buffer.
AccumAlphaBits Hint = C.GLFW_ACCUM_ALPHA_BITS // Specifies the desired bit depth of the accumulation buffer.
AuxBuffers Hint = C.GLFW_AUX_BUFFERS // Specifies the desired number of auxiliary buffers.
Stereo Hint = C.GLFW_STEREO // Specifies whether to use stereoscopic rendering. Hard constraint.
Samples Hint = C.GLFW_SAMPLES // Specifies the desired number of samples to use for multisampling. Zero disables multisampling.
SRGBCapable Hint = C.GLFW_SRGB_CAPABLE // Specifies whether the framebuffer should be sRGB capable.
RefreshRate Hint = C.GLFW_REFRESH_RATE // Specifies the desired refresh rate for full screen windows. If set to zero, the highest available refresh rate will be used. This hint is ignored for windowed mode windows.
DoubleBuffer Hint = C.GLFW_DOUBLEBUFFER // Specifies whether the framebuffer should be double buffered. You nearly always want to use double buffering. This is a hard constraint.
)
// Values for the ClientAPI hint.
const (
OpenGLAPI int = C.GLFW_OPENGL_API
OpenGLESAPI int = C.GLFW_OPENGL_ES_API
NoAPI int = C.GLFW_NO_API
)
// Values for ContextCreationAPI hint.
const (
NativeContextAPI int = C.GLFW_NATIVE_CONTEXT_API
EGLContextAPI int = C.GLFW_EGL_CONTEXT_API
)
// Values for the ContextRobustness hint.
const (
NoRobustness int = C.GLFW_NO_ROBUSTNESS
NoResetNotification int = C.GLFW_NO_RESET_NOTIFICATION
LoseContextOnReset int = C.GLFW_LOSE_CONTEXT_ON_RESET
)
// Values for ContextReleaseBehavior hint.
const (
AnyReleaseBehavior int = C.GLFW_ANY_RELEASE_BEHAVIOR
ReleaseBehaviorFlush int = C.GLFW_RELEASE_BEHAVIOR_FLUSH
ReleaseBehaviorNone int = C.GLFW_RELEASE_BEHAVIOR_NONE
)
// Values for the OpenGLProfile hint.
const (
OpenGLAnyProfile int = C.GLFW_OPENGL_ANY_PROFILE
OpenGLCoreProfile int = C.GLFW_OPENGL_CORE_PROFILE
OpenGLCompatProfile int = C.GLFW_OPENGL_COMPAT_PROFILE
)
// Other values.
const (
True int = C.GL_TRUE
False int = C.GL_FALSE
DontCare int = C.GLFW_DONT_CARE
)
// Window represents a window.
type Window struct {
data *C.GLFWwindow
// Window.
fPosHolder func(w *Window, xpos int, ypos int)
fSizeHolder func(w *Window, width int, height int)
fFramebufferSizeHolder func(w *Window, width int, height int)
fCloseHolder func(w *Window)
fRefreshHolder func(w *Window)
fFocusHolder func(w *Window, focused bool)
fIconifyHolder func(w *Window, iconified bool)
// Input.
fMouseButtonHolder func(w *Window, button MouseButton, action Action, mod ModifierKey)
fCursorPosHolder func(w *Window, xpos float64, ypos float64)
fCursorEnterHolder func(w *Window, entered bool)
fScrollHolder func(w *Window, xoff float64, yoff float64)
fKeyHolder func(w *Window, key Key, scancode int, action Action, mods ModifierKey)
fCharHolder func(w *Window, char rune)
fCharModsHolder func(w *Window, char rune, mods ModifierKey)
fDropHolder func(w *Window, names []string)
}
// GLFWWindow returns a *C.GLFWwindow reference (i.e. the GLFW window itself). This can be used for
// passing the GLFW window handle to external C libraries.
func (w *Window) GLFWWindow() uintptr {
return uintptr(unsafe.Pointer(w.data))
}
//export goWindowPosCB
func goWindowPosCB(window unsafe.Pointer, xpos, ypos C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fPosHolder(w, int(xpos), int(ypos))
}
//export goWindowSizeCB
func goWindowSizeCB(window unsafe.Pointer, width, height C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fSizeHolder(w, int(width), int(height))
}
//export goFramebufferSizeCB
func goFramebufferSizeCB(window unsafe.Pointer, width, height C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fFramebufferSizeHolder(w, int(width), int(height))
}
//export goWindowCloseCB
func goWindowCloseCB(window unsafe.Pointer) {
w := windows.get((*C.GLFWwindow)(window))
w.fCloseHolder(w)
}
//export goWindowRefreshCB
func goWindowRefreshCB(window unsafe.Pointer) {
w := windows.get((*C.GLFWwindow)(window))
w.fRefreshHolder(w)
}
//export goWindowFocusCB
func goWindowFocusCB(window unsafe.Pointer, focused C.int) {
w := windows.get((*C.GLFWwindow)(window))
isFocused := glfwbool(focused)
w.fFocusHolder(w, isFocused)
}
//export goWindowIconifyCB
func goWindowIconifyCB(window unsafe.Pointer, iconified C.int) {
isIconified := glfwbool(iconified)
w := windows.get((*C.GLFWwindow)(window))
w.fIconifyHolder(w, isIconified)
}
// DefaultWindowHints resets all window hints to their default values.
//
// This function may only be called from the main thread.
func DefaultWindowHints() {
C.glfwDefaultWindowHints()
panicError()
}
// WindowHint sets hints for the next call to CreateWindow. The hints,
// once set, retain their values until changed by a call to WindowHint or
// DefaultWindowHints, or until the library is terminated with Terminate.
//
// This function may only be called from the main thread.
func WindowHint(target Hint, hint int) {
C.glfwWindowHint(C.int(target), C.int(hint))
panicError()
}
// CreateWindow creates a window and its associated context. Most of the options
// controlling how the window and its context should be created are specified
// through Hint.
//
// Successful creation does not change which context is current. Before you can
// use the newly created context, you need to make it current using
// MakeContextCurrent.
//
// Note that the created window and context may differ from what you requested,
// as not all parameters and hints are hard constraints. This includes the size
// of the window, especially for full screen windows. To retrieve the actual
// attributes of the created window and context, use queries like
// GetWindowAttrib and GetWindowSize.
//
// To create the window at a specific position, make it initially invisible using
// the Visible window hint, set its position and then show it.
//
// If a fullscreen window is active, the screensaver is prohibited from starting.
//
// Windows: If the executable has an icon resource named GLFW_ICON, it will be
// set as the icon for the window. If no such icon is present, the IDI_WINLOGO
// icon will be used instead.
//
// Mac OS X: The GLFW window has no icon, as it is not a document window, but the
// dock icon will be the same as the application bundle's icon. Also, the first
// time a window is opened the menu bar is populated with common commands like
// Hide, Quit and About. The (minimal) about dialog uses information from the
// application's bundle. For more information on bundles, see the Bundle
// Programming Guide provided by Apple.
//
// This function may only be called from the main thread.
func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) {
var (
m *C.GLFWmonitor
s *C.GLFWwindow
)
t := C.CString(title)
defer C.free(unsafe.Pointer(t))
if monitor != nil {
m = monitor.data
}
if share != nil {
s = share.data
}
w := C.glfwCreateWindow(C.int(width), C.int(height), t, m, s)
if w == nil {
return nil, acceptError(APIUnavailable, VersionUnavailable)
}
wnd := &Window{data: w}
windows.put(wnd)
return wnd, nil
}
// Destroy destroys the specified window and its context. On calling this
// function, no further callbacks will be called for that window.
//
// This function may only be called from the main thread.
func (w *Window) Destroy() {
windows.remove(w.data)
C.glfwDestroyWindow(w.data)
panicError()
}
// ShouldClose reports the value of the close flag of the specified window.
func (w *Window) ShouldClose() bool {
ret := glfwbool(C.glfwWindowShouldClose(w.data))
panicError()
return ret
}
// SetShouldClose sets the value of the close flag of the window. This can be
// used to override the user's attempt to close the window, or to signal that it
// should be closed.
func (w *Window) SetShouldClose(value bool) {
if !value {
C.glfwSetWindowShouldClose(w.data, C.GL_FALSE)
} else {
C.glfwSetWindowShouldClose(w.data, C.GL_TRUE)
}
panicError()
}
// SetTitle sets the window title, encoded as UTF-8, of the window.
//
// This function may only be called from the main thread.
func (w *Window) SetTitle(title string) {
t := C.CString(title)
defer C.free(unsafe.Pointer(t))
C.glfwSetWindowTitle(w.data, t)
panicError()
}
// SetIcon sets the icon of the specified window. If passed an array of candidate images,
// those of or closest to the sizes desired by the system are selected. If no images are
// specified, the window reverts to its default icon.
//
// The image is ideally provided in the form of *image.NRGBA.
// The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight
// bits per channel with the red channel first. They are arranged canonically
// as packed sequential rows, starting from the top-left corner. If the image
// type is not *image.NRGBA, it will be converted to it.
//
// The desired image sizes varies depending on platform and system settings. The selected
// images will be rescaled as needed. Good sizes include 16x16, 32x32 and 48x48.
func (w *Window) SetIcon(images []image.Image) {
count := len(images)
cimages := make([]C.GLFWimage, count)
freePixels := make([]func(), count)
for i, img := range images {
var pixels []uint8
b := img.Bounds()
switch img := img.(type) {
case *image.NRGBA:
pixels = img.Pix
default:
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
pixels = m.Pix
}
pix, free := bytes(pixels)
freePixels[i] = free
cimages[i].width = C.int(b.Dx())
cimages[i].height = C.int(b.Dy())
cimages[i].pixels = (*C.uchar)(pix)
}
var p *C.GLFWimage
if count > 0 {
p = &cimages[0]
}
C.glfwSetWindowIcon(w.data, C.int(count), p)
for _, v := range freePixels {
v()
}
panicError()
}
// GetPos returns the position, in screen coordinates, of the upper-left
// corner of the client area of the window.
func (w *Window) GetPos() (x, y int) {
var xpos, ypos C.int
C.glfwGetWindowPos(w.data, &xpos, &ypos)
panicError()
return int(xpos), int(ypos)
}
// SetPos sets the position, in screen coordinates, of the upper-left corner
// of the client area of the window.
//
// If it is a full screen window, this function does nothing.
//
// If you wish to set an initial window position you should create a hidden
// window (using Hint and Visible), set its position and then show it.
//
// It is very rarely a good idea to move an already visible window, as it will
// confuse and annoy the user.
//
// The window manager may put limits on what positions are allowed.
//
// This function may only be called from the main thread.
func (w *Window) SetPos(xpos, ypos int) {
C.glfwSetWindowPos(w.data, C.int(xpos), C.int(ypos))
panicError()
}
// GetSize returns the size, in screen coordinates, of the client area of the
// specified window.
func (w *Window) GetSize() (width, height int) {
var wi, h C.int
C.glfwGetWindowSize(w.data, &wi, &h)
panicError()
return int(wi), int(h)
}
// SetSize sets the size, in screen coordinates, of the client area of the
// window.
//
// For full screen windows, this function selects and switches to the resolution
// closest to the specified size, without affecting the window's context. As the
// context is unaffected, the bit depths of the framebuffer remain unchanged.
//
// The window manager may put limits on what window sizes are allowed.
//
// This function may only be called from the main thread.
func (w *Window) SetSize(width, height int) {
C.glfwSetWindowSize(w.data, C.int(width), C.int(height))
panicError()
}
// SetSizeLimits sets the size limits of the client area of the specified window.
// If the window is full screen or not resizable, this function does nothing.
//
// The size limits are applied immediately and may cause the window to be resized.
func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
C.glfwSetWindowSizeLimits(w.data, C.int(minw), C.int(minh), C.int(maxw), C.int(maxh))
panicError()
}
// SetAspectRatio sets the required aspect ratio of the client area of the specified window.
// If the window is full screen or not resizable, this function does nothing.
//
// The aspect ratio is specified as a numerator and a denominator and both values must be greater
// than zero. For example, the common 16:9 aspect ratio is specified as 16 and 9, respectively.
//
// If the numerator and denominator is set to glfw.DontCare then the aspect ratio limit is disabled.
//
// The aspect ratio is applied immediately and may cause the window to be resized.
func (w *Window) SetAspectRatio(numer, denom int) {
C.glfwSetWindowAspectRatio(w.data, C.int(numer), C.int(denom))
panicError()
}
// GetFramebufferSize retrieves the size, in pixels, of the framebuffer of the
// specified window.
func (w *Window) GetFramebufferSize() (width, height int) {
var wi, h C.int
C.glfwGetFramebufferSize(w.data, &wi, &h)
panicError()
return int(wi), int(h)
}
// GetFrameSize retrieves the size, in screen coordinates, of each edge of the frame
// of the specified window. This size includes the title bar, if the window has one.
// The size of the frame may vary depending on the window-related hints used to create it.
//
// Because this function retrieves the size of each window frame edge and not the offset
// along a particular coordinate axis, the retrieved values will always be zero or positive.
func (w *Window) GetFrameSize() (left, top, right, bottom int) {
var l, t, r, b C.int
C.glfwGetWindowFrameSize(w.data, &l, &t, &r, &b)
panicError()
return int(l), int(t), int(r), int(b)
}
// Focus brings the specified window to front and sets input focus.
// The window should already be visible and not iconified.
//
// By default, both windowed and full screen mode windows are focused when initially created.
// Set the glfw.Focused to disable this behavior.
//
// Do not use this function to steal focus from other applications unless you are certain that
// is what the user wants. Focus stealing can be extremely disruptive.
func (w *Window) Focus() error {
C.glfwFocusWindow(w.data)
return acceptError(APIUnavailable)
}
// Iconify iconifies/minimizes the window, if it was previously restored. If it
// is a full screen window, the original monitor resolution is restored until the
// window is restored. If the window is already iconified, this function does
// nothing.
//
// This function may only be called from the main thread.
func (w *Window) Iconify() error {
C.glfwIconifyWindow(w.data)
return acceptError(APIUnavailable)
}
// Maximize maximizes the specified window if it was previously not maximized.
// If the window is already maximized, this function does nothing.
//
// If the specified window is a full screen window, this function does nothing.
func (w *Window) Maximize() error {
C.glfwMaximizeWindow(w.data)
return acceptError(APIUnavailable)
}
// Restore restores the window, if it was previously iconified/minimized. If it
// is a full screen window, the resolution chosen for the window is restored on
// the selected monitor. If the window is already restored, this function does
// nothing.
//
// This function may only be called from the main thread.
func (w *Window) Restore() error {
C.glfwRestoreWindow(w.data)
return acceptError(APIUnavailable)
}
// Show makes the window visible, if it was previously hidden. If the window is
// already visible or is in full screen mode, this function does nothing.
//
// This function may only be called from the main thread.
func (w *Window) Show() {
C.glfwShowWindow(w.data)
panicError()
}
// Hide hides the window, if it was previously visible. If the window is already
// hidden or is in full screen mode, this function does nothing.
//
// This function may only be called from the main thread.
func (w *Window) Hide() {
C.glfwHideWindow(w.data)
panicError()
}
// GetMonitor returns the handle of the monitor that the window is in
// fullscreen on.
//
// Returns nil if the window is in windowed mode.
func (w *Window) GetMonitor() *Monitor {
m := C.glfwGetWindowMonitor(w.data)
panicError()
if m == nil {
return nil
}
return &Monitor{m}
}
// SetMonitor sets the monitor that the window uses for full screen mode or,
// if the monitor is NULL, makes it windowed mode.
//
// When setting a monitor, this function updates the width, height and refresh
// rate of the desired video mode and switches to the video mode closest to it.
// The window position is ignored when setting a monitor.
//
// When the monitor is NULL, the position, width and height are used to place
// the window client area. The refresh rate is ignored when no monitor is specified.
// If you only wish to update the resolution of a full screen window or the size of
// a windowed mode window, see window.SetSize.
//
// When a window transitions from full screen to windowed mode, this function
// restores any previous window settings such as whether it is decorated, floating,
// resizable, has size or aspect ratio limits, etc..
func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
var m *C.GLFWmonitor
if monitor == nil {
m = nil
} else {
m = monitor.data
}
C.glfwSetWindowMonitor(w.data, m, C.int(xpos), C.int(ypos), C.int(width), C.int(height), C.int(refreshRate))
panicError()
}
// GetAttrib returns an attribute of the window. There are many attributes,
// some related to the window and others to its context.
func (w *Window) GetAttrib(attrib Hint) int {
ret := int(C.glfwGetWindowAttrib(w.data, C.int(attrib)))
panicError()
return ret
}
// SetUserPointer sets the user-defined pointer of the window. The current value
// is retained until the window is destroyed. The initial value is nil.
func (w *Window) SetUserPointer(pointer unsafe.Pointer) {
C.glfwSetWindowUserPointer(w.data, pointer)
panicError()
}
// GetUserPointer returns the current value of the user-defined pointer of the
// window. The initial value is nil.
func (w *Window) GetUserPointer() unsafe.Pointer {
ret := C.glfwGetWindowUserPointer(w.data)
panicError()
return ret
}
// PosCallback is the window position callback.
type PosCallback func(w *Window, xpos int, ypos int)
// SetPosCallback sets the position callback of the window, which is called
// when the window is moved. The callback is provided with the screen position
// of the upper-left corner of the client area of the window.
func (w *Window) SetPosCallback(cbfun PosCallback) (previous PosCallback) {
previous = w.fPosHolder
w.fPosHolder = cbfun
if cbfun == nil {
C.glfwSetWindowPosCallback(w.data, nil)
} else {
C.glfwSetWindowPosCallbackCB(w.data)
}
panicError()
return previous
}
// SizeCallback is the window size callback.
type SizeCallback func(w *Window, width int, height int)
// SetSizeCallback sets the size callback of the window, which is called when
// the window is resized. The callback is provided with the size, in screen
// coordinates, of the client area of the window.
func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
previous = w.fSizeHolder
w.fSizeHolder = cbfun
if cbfun == nil {
C.glfwSetWindowSizeCallback(w.data, nil)
} else {
C.glfwSetWindowSizeCallbackCB(w.data)
}
panicError()
return previous
}
// FramebufferSizeCallback is the framebuffer size callback.
type FramebufferSizeCallback func(w *Window, width int, height int)
// SetFramebufferSizeCallback sets the framebuffer resize callback of the specified
// window, which is called when the framebuffer of the specified window is resized.
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
previous = w.fFramebufferSizeHolder
w.fFramebufferSizeHolder = cbfun
if cbfun == nil {
C.glfwSetFramebufferSizeCallback(w.data, nil)
} else {
C.glfwSetFramebufferSizeCallbackCB(w.data)
}
panicError()
return previous
}
// CloseCallback is the window close callback.
type CloseCallback func(w *Window)
// SetCloseCallback sets the close callback of the window, which is called when
// the user attempts to close the window, for example by clicking the close
// widget in the title bar.
//
// The close flag is set before this callback is called, but you can modify it at
// any time with SetShouldClose.
//
// Mac OS X: Selecting Quit from the application menu will trigger the close
// callback for all windows.
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
previous = w.fCloseHolder
w.fCloseHolder = cbfun
if cbfun == nil {
C.glfwSetWindowCloseCallback(w.data, nil)
} else {
C.glfwSetWindowCloseCallbackCB(w.data)
}
panicError()
return previous
}
// RefreshCallback is the window refresh callback.
type RefreshCallback func(w *Window)
// SetRefreshCallback sets the refresh callback of the window, which
// is called when the client area of the window needs to be redrawn, for example
// if the window has been exposed after having been covered by another window.
//
// On compositing window systems such as Aero, Compiz or Aqua, where the window
// contents are saved off-screen, this callback may be called only very
// infrequently or never at all.
func (w *Window) SetRefreshCallback(cbfun RefreshCallback) (previous RefreshCallback) {
previous = w.fRefreshHolder
w.fRefreshHolder = cbfun
if cbfun == nil {
C.glfwSetWindowRefreshCallback(w.data, nil)
} else {
C.glfwSetWindowRefreshCallbackCB(w.data)
}
panicError()
return previous
}
// FocusCallback is the window focus callback.
type FocusCallback func(w *Window, focused bool)
// SetFocusCallback sets the focus callback of the window, which is called when
// the window gains or loses focus.
//
// After the focus callback is called for a window that lost focus, synthetic key
// and mouse button release events will be generated for all such that had been
// pressed. For more information, see SetKeyCallback and SetMouseButtonCallback.
func (w *Window) SetFocusCallback(cbfun FocusCallback) (previous FocusCallback) {
previous = w.fFocusHolder
w.fFocusHolder = cbfun
if cbfun == nil {
C.glfwSetWindowFocusCallback(w.data, nil)
} else {
C.glfwSetWindowFocusCallbackCB(w.data)
}
panicError()
return previous
}
// IconifyCallback is the window iconification callback.
type IconifyCallback func(w *Window, iconified bool)
// SetIconifyCallback sets the iconification callback of the window, which is
// called when the window is iconified or restored.
func (w *Window) SetIconifyCallback(cbfun IconifyCallback) (previous IconifyCallback) {
previous = w.fIconifyHolder
w.fIconifyHolder = cbfun
if cbfun == nil {
C.glfwSetWindowIconifyCallback(w.data, nil)
} else {
C.glfwSetWindowIconifyCallbackCB(w.data)
}
panicError()
return previous
}
// SetClipboardString sets the system clipboard to the specified UTF-8 encoded
// string.
//
// This function may only be called from the main thread.
func (w *Window) SetClipboardString(str string) {
cp := C.CString(str)
defer C.free(unsafe.Pointer(cp))
C.glfwSetClipboardString(w.data, cp)
panicError()
}
// GetClipboardString returns the contents of the system clipboard, if it
// contains or is convertible to a UTF-8 encoded string.
//
// This function may only be called from the main thread.
func (w *Window) GetClipboardString() (string, error) {
cs := C.glfwGetClipboardString(w.data)
if cs == nil {
return "", acceptError(FormatUnavailable)
}
return C.GoString(cs), nil
}
// PollEvents processes only those events that have already been received and
// then returns immediately. Processing events will cause the window and input
// callbacks associated with those events to be called.
//
// This function is not required for joystick input to work.
//
// This function may not be called from a callback.
//
// This function may only be called from the main thread.
func PollEvents() {
C.glfwPollEvents()
panicError()
}
// WaitEvents puts the calling thread to sleep until at least one event has been
// received. Once one or more events have been recevied, it behaves as if
// PollEvents was called, i.e. the events are processed and the function then
// returns immediately. Processing events will cause the window and input
// callbacks associated with those events to be called.
//
// Since not all events are associated with callbacks, this function may return
// without a callback having been called even if you are monitoring all
// callbacks.
//
// This function may not be called from a callback.
//
// This function may only be called from the main thread.
func WaitEvents() {
C.glfwWaitEvents()
panicError()
}
// WaitEventsTimeout puts the calling thread to sleep until at least one event is available in the
// event queue, or until the specified timeout is reached. If one or more events are available,
// it behaves exactly like PollEvents, i.e. the events in the queue are processed and the function
// then returns immediately. Processing events will cause the window and input callbacks associated
// with those events to be called.
//
// The timeout value must be a positive finite number.
//
// Since not all events are associated with callbacks, this function may return without a callback
// having been called even if you are monitoring all callbacks.
//
// On some platforms, a window move, resize or menu operation will cause event processing to block.
// This is due to how event processing is designed on those platforms. You can use the window
// refresh callback to redraw the contents of your window when necessary during such operations.
//
// On some platforms, certain callbacks may be called outside of a call to one of the event
// processing functions.
//
// If no windows exist, this function returns immediately. For synchronization of threads in
// applications that do not create windows, use native Go primitives.
//
// Event processing is not required for joystick input to work.
func WaitEventsTimeout(timeout float64) {
C.glfwWaitEventsTimeout(C.double(timeout))
panicError()
}
// PostEmptyEvent posts an empty event from the current thread to the main
// thread event queue, causing WaitEvents to return.
//
// If no windows exist, this function returns immediately. For synchronization of threads in
// applications that do not create windows, use native Go primitives.
//
// This function may be called from secondary threads.
func PostEmptyEvent() {
C.glfwPostEmptyEvent()
panicError()
}

14
vendor/github.com/go-gl/mathgl/AUTHORS generated vendored Normal file
View File

@@ -0,0 +1,14 @@
# This is the official list of go-gl/mathgl authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS files.
# See the latter for an explanation.
# Names should be added to this file as
# Name or Organization <email address>
# The email address is not required for organizations.
# Please keep the list sorted.
Alex Vasi <eee@someuser.com>
Dmitri Shuralyov <shurcooL@gmail.com>
GlenKelley <???>
Zoe Juozapaitis <jragonmiris@gmail.com>

21
vendor/github.com/go-gl/mathgl/CONTRIBUTORS generated vendored Normal file
View File

@@ -0,0 +1,21 @@
# This is the official list of people who can contribute
# (and typically have contributed) code to the mathgl
# repository.
#
# The AUTHORS file lists the copyright holders; this file
# lists people. For example, Google employees would be listed here
# but not in AUTHORS, because Google would hold the copyright.
#
# When adding J Random Contributor's name to this file,
# either J's name or J's organization's name should be
# added to the AUTHORS file.
#
# Names should be added to this file like so:
# Name <email address>
#
# Please keep the list sorted.
Alex Vasi <eee@someuser.com>
Dmitri Shuralyov <shurcooL@gmail.com>
GlenKelley <???>
Zoe Juozapaitis <jragonmiris@gmail.com>

23
vendor/github.com/go-gl/mathgl/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,23 @@
Copyright ©2013 The go-gl Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the gonum project nor the names of its authors and
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

276
vendor/github.com/go-gl/mathgl/mgl32/codegen.go generated vendored Normal file
View File

@@ -0,0 +1,276 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
// codegen generates go code from templates. Intended to be
// used with go generate; Also makes mgl64 from mgl32.
// See the invocation in mgl32/util.go for details.
// To use it, just run "go generate github.com/go-gl/mathgl/mgl32"
// (or "go generate" in mgl32 directory).
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
type Context struct {
Comment string
TemplateName string
}
type MatrixIter struct {
M int // row
N int // column
index int
}
var mgl64RewriteRules = []string{
"mgl32 -> mgl64",
"float32 -> float64",
"f32 -> f64",
"a.Float32 -> a.Float64",
"math.MaxFloat32 -> math.MaxFloat64",
"math.SmallestNonzeroFloat32 -> math.SmallestNonzeroFloat64",
}
func main() {
flag.Usage = func() {
fmt.Println("Usage: codegen -template file.tmpl -output file.go")
fmt.Println("Usage: codegen -mgl64 [-dir ../mgl64]")
flag.PrintDefaults()
}
tmplPath := flag.String("template", "file.tmpl", "template path")
oPath := flag.String("output", "file.go", "output path")
mgl64 := flag.Bool("mgl64", false, "make mgl64")
mgl64Path := flag.String("dir", "../mgl64", "path to mgl64 location")
flag.Parse()
if flag.NArg() > 0 || flag.NFlag() == 0 {
flag.Usage()
os.Exit(2)
}
if *mgl64 {
genMgl64(*mgl64Path)
return
}
tmpl := template.New("").Delims("<<", ">>").Funcs(template.FuncMap{
"typename": typenameHelper,
"elementname": elementNameHelper,
"iter": iterHelper,
"matiter": matrixIterHelper,
"enum": enumHelper,
"sep": separatorHelper,
"repeat": repeatHelper,
"add": addHelper,
"mul": mulHelper,
})
tmpl = template.Must(tmpl.ParseFiles(*tmplPath))
tmplName := filepath.Base(*tmplPath)
oFile, err := os.Create(*oPath)
if err != nil {
panic(err)
}
context := Context{
Comment: "This file is generated by codegen.go; DO NOT EDIT",
TemplateName: tmplName,
}
if err = tmpl.ExecuteTemplate(oFile, tmplName, context); err != nil {
panic(err)
}
oFile.Close()
if err = rungofmt(*oPath, false, nil); err != nil {
panic(err)
}
}
func genMgl64(destPath string) {
HandleFile := func(source string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dest := filepath.Join(destPath, source)
if info.IsDir() {
return os.MkdirAll(dest, info.Mode())
}
if !strings.HasSuffix(source, ".go") || info.Name() == "codegen.go" {
return nil
}
if !info.Mode().IsRegular() {
fmt.Println("Ignored, not a regular file:", source)
return nil
}
in, err := ioutil.ReadFile(source)
if err != nil {
return err
}
out, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC,
info.Mode())
if err != nil {
return err
}
defer out.Close()
comment := fmt.Sprintf(
"// This file is generated from mgl32/%s; DO NOT EDIT\n\n",
source)
if _, err = out.WriteString(comment); err != nil {
return err
}
r := strings.NewReplacer("//go:generate ", "//#go:generate ") // We don't want go generate directives in mgl64 package.
if _, err = r.WriteString(out, string(in)); err != nil {
return err
}
return rungofmt(dest, true, mgl64RewriteRules)
}
if err := filepath.Walk(".", HandleFile); err != nil {
panic(err)
}
}
func rungofmt(path string, fiximports bool, rewriteRules []string) error {
args := []string{"-w", path}
output, err := exec.Command("gofmt", args...).CombinedOutput()
for i := 0; err == nil && i < len(rewriteRules); i++ {
args = []string{"-w", "-r", rewriteRules[i], path}
output, err = exec.Command("gofmt", args...).CombinedOutput()
}
if fiximports && err == nil {
args = []string{"-w", path}
output, err = exec.Command("goimports", args...).CombinedOutput()
}
if err != nil {
fmt.Println("Error executing gofmt", strings.Join(args, " "))
os.Stdout.Write(output)
}
return err
}
func typenameHelper(m, n int) string {
if m == 1 {
return fmt.Sprintf("Vec%d", n)
}
if n == 1 {
return fmt.Sprintf("Vec%d", m)
}
if m == n {
return fmt.Sprintf("Mat%d", m)
}
return fmt.Sprintf("Mat%dx%d", m, n)
}
func elementNameHelper(m int) string {
switch m {
case 0:
return "X"
case 1:
return "Y"
case 2:
return "Z"
case 3:
return "W"
default:
panic("Can't generate element name")
}
}
func iterHelper(start, end int) []int {
iter := make([]int, end-start)
for i := start; i < end; i++ {
iter[i] = i
}
return iter
}
func matrixIterHelper(rows, cols int) []MatrixIter {
res := make([]MatrixIter, 0, rows*cols)
for n := 0; n < cols; n++ {
for m := 0; m < rows; m++ {
res = append(res, MatrixIter{
M: m,
N: n,
index: n*rows + m,
})
}
}
return res
}
// Template function that returns slice from its arguments. Indended to be used
// in range loops.
func enumHelper(args ...int) []int {
return args
}
// Template function to insert commas and '+' in range loops.
func separatorHelper(sep string, iterCond int) string {
if iterCond > 0 {
return sep
}
return ""
}
// Template function to repeat string 'count' times. Inserting 'sep' between
// repetitions. Also changes all occurrences of '%d' to repetition number.
// For example, repeatHelper(3, "col%d", ",") will output "col0, col1, col2"
func repeatHelper(count int, text string, sep string) string {
var res bytes.Buffer
for i := 0; i < count; i++ {
if i > 0 {
res.WriteString(sep)
}
res.WriteString(strings.Replace(text, "%d", fmt.Sprintf("%d", i), -1))
}
return res.String()
}
func addHelper(args ...int) int {
res := 0
for _, a := range args {
res += a
}
return res
}
func mulHelper(args ...int) int {
res := 1
for _, a := range args {
res *= a
}
return res
}
func (i MatrixIter) String() string {
return fmt.Sprintf("%d", i.index)
}

94
vendor/github.com/go-gl/mathgl/mgl32/conv.go generated vendored Normal file
View File

@@ -0,0 +1,94 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// Converts 3-dimensional cartesian coordinates (x,y,z) to spherical
// coordinates with radius r, inclination theta, and azimuth phi.
//
// All angles are in radians.
func CartesianToSpherical(coord Vec3) (r, theta, phi float32) {
r = coord.Len()
theta = float32(math.Acos(float64(coord[2] / r)))
phi = float32(math.Atan2(float64(coord[1]), float64(coord[0])))
return
}
// Converts 3-dimensional cartesian coordinates (x,y,z) to cylindrical
// coordinates with radial distance r, azimuth phi, and height z.
//
// All angles are in radians.
func CartesianToCylindical(coord Vec3) (rho, phi, z float32) {
rho = float32(math.Hypot(float64(coord[0]), float64(coord[1])))
phi = float32(math.Atan2(float64(coord[1]), float64(coord[0])))
z = coord[2]
return
}
// Converts spherical coordinates with radius r, inclination theta,
// and azimuth phi to cartesian coordinates (x,y,z).
//
// Angles are in radians.
func SphericalToCartesian(r, theta, phi float32) Vec3 {
st, ct := math.Sincos(float64(theta))
sp, cp := math.Sincos(float64(phi))
return Vec3{r * float32(st*cp), r * float32(st*sp), r * float32(ct)}
}
// Converts spherical coordinates with radius r, inclination theta,
// and azimuth phi to cylindrical coordinates with radial distance r,
// azimuth phi, and height z.
//
// Angles are in radians
func SphericalToCylindrical(r, theta, phi float32) (rho, phi2, z float32) {
s, c := math.Sincos(float64(theta))
rho = r * float32(s)
z = r * float32(c)
phi2 = phi
return
}
// Converts cylindrical coordinates with radial distance r,
// azimuth phi, and height z to spherical coordinates with radius r,
// inclination theta, and azimuth phi.
//
// Angles are in radians
func CylindircalToSpherical(rho, phi, z float32) (r, theta, phi2 float32) {
r = float32(math.Hypot(float64(rho), float64(z)))
phi2 = phi
theta = float32(math.Atan2(float64(rho), float64(z)))
return
}
// Converts cylindrical coordinates with radial distance r,
// azimuth phi, and height z to cartesian coordinates (x,y,z)
//
// Angles are in radians.
func CylindricalToCartesian(rho, phi, z float32) Vec3 {
s, c := math.Sincos(float64(phi))
return Vec3{rho * float32(c), rho * float32(s), z}
}
// Converts degrees to radians
func DegToRad(angle float32) float32 {
return angle * float32(math.Pi) / 180
}
// Converts radians to degrees
func RadToDeg(angle float32) float32 {
return angle * 180 / float32(math.Pi)
}

22
vendor/github.com/go-gl/mathgl/mgl32/doc.go generated vendored Normal file
View File

@@ -0,0 +1,22 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package mgl[32|64] (an abbreviation of mathgl since the packages were split between 32 and 64-bit versions)
is a pure Go math package specialized for 3D math, with inspiration from GLM. It provides statically-sized vectors and matrices with
compile-time generated calculations for most basic math operations. It also provides several basic graphics utilities such as bezier curves and surfaces,
generation of basic primitives like circles, easy creation of common matrices such as perspective or rotation, and common operations like converting
to/from screen/OpenGL coordinates or Projecting/Unprojecting from an MVP matrix. Quaternions are also supported.
The basic vectors and matrices are written with code generation, so looking directly at the source will probably be a bit confusing. I recommend looking at the Godoc
instead, as all basic functions are documented.
This package is written in Column Major Order to make it easier with OpenGL. This means for uniform blocks you can use the default ordering, and when you call
pass-in functions you can leave the "transpose" argument as false.
The package now contains variable sized vectors and matrices. Using these is discouraged. They exist for corner cases where you need "small" matrices that are still
bigger than 4x4. An example may be a Jacobean used for inverse kinematics. Things like computer vision or general linear algebra are best left to packages
more directly suited for that task -- OpenCV, BLAS, LAPACK, numpy, gonum (if you want to stay in Go), and so on.
*/
package mgl32

494
vendor/github.com/go-gl/mathgl/mgl32/matmn.go generated vendored Normal file
View File

@@ -0,0 +1,494 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// An arbitrary mxn matrix backed by a slice of floats.
//
// This is emphatically not recommended for hardcore n-dimensional
// linear algebra. For that purpose I recommend github.com/gonum/matrix or
// well-tested C libraries such as BLAS or LAPACK.
//
// This is meant to complement future algorithms that may require matrices larger than
// 4x4, but still relatively small (e.g. Jacobeans for inverse kinematics).
//
// It makes use of the same memory sync.Pool set that VecN does, with the same sizing rules.
//
// MatMN will always check if the receiver is nil on any method. Meaning MathMN(nil).Add(dst,m2)
// should always work. Except for the Reshape function, the semantics of this is to "propogate" nils
// forward, so if an invalid operation occurs in a long chain of matrix operations, the overall result will be nil.
type MatMxN struct {
m, n int
dat []float32
}
// Creates a matrix backed by a new slice of size m*n
func NewMatrix(m, n int) (mat *MatMxN) {
if shouldPool {
return &MatMxN{m: m, n: n, dat: grabFromPool(m * n)}
} else {
return &MatMxN{m: m, n: n, dat: make([]float32, m*n)}
}
}
// Returns a matrix with data specified by the data in src
//
// For instance, to create a 3x3 MatMN from a Mat3
//
// m1 := mgl32.Rotate3DX(3.14159)
// mat := mgl32.NewBackedMatrix(m1[:],3,3)
//
// will create an MN matrix matching the data in the original
// rotation matrix. This matrix is NOT backed by the initial slice;
// it's a copy of the data
//
// If m*n > cap(src), this function will panic.
func NewMatrixFromData(src []float32, m, n int) *MatMxN {
var internal []float32
if shouldPool {
internal = grabFromPool(m * n)
} else {
internal = make([]float32, m*n)
}
copy(internal, src[:m*n])
return &MatMxN{m: m, n: n, dat: internal}
}
// Copies src into dst. This Reshapes dst
// to the same size as src.
//
// If dst or src is nil, this is a no-op
func CopyMatMN(dst, src *MatMxN) {
if dst == nil || src == nil {
return
}
dst.Reshape(src.m, src.n)
copy(dst.dat, src.dat)
}
// Stores the NxN identity matrix in dst, reallocating as necessary.
func IdentN(dst *MatMxN, n int) *MatMxN {
dst = dst.Reshape(n, n)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i == j {
dst.Set(i, j, 1)
} else {
dst.Set(i, j, 0)
}
}
}
return dst
}
// Creates an NxN diagonal matrix seeded by the diagonal vector
// diag. Meaning: for all entries, where i==j, dst.At(i,j) = diag[i]. Otherwise
// dst.At(i,j) = 0
//
// This reshapes dst to the correct size, returning/grabbing from the memory pool as necessary.
func DiagN(dst *MatMxN, diag *VecN) *MatMxN {
dst = dst.Reshape(len(diag.vec), len(diag.vec))
n := len(diag.vec)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i == j {
dst.Set(i, j, diag.vec[i])
} else {
dst.Set(i, j, 0)
}
}
}
return dst
}
// Reshapes the matrix to m by n and zeroes out all
// elements.
func (mat *MatMxN) Zero(m, n int) {
if mat == nil {
return
}
mat.Reshape(m, n)
for i := range mat.dat {
mat.dat[i] = 0
}
}
// Returns the underlying matrix slice to the memory pool
func (mat *MatMxN) destroy() {
if mat == nil {
return
}
if shouldPool && mat.dat != nil {
returnToPool(mat.dat)
}
mat.m, mat.n = 0, 0
mat.dat = nil
}
// Reshapes the matrix to the desired dimensions.
// If the overall size of the new matrix (m*n) is bigger
// than the current size, the underlying slice will
// be grown, sending the current slice to the memory pool
// and grabbing a bigger one if necessary
//
// If the caller is a nil pointer, the return value will be a new
// matrix, as if NewMatrix(m,n) had been called. Otherwise it's
// simply the caller.
func (mat *MatMxN) Reshape(m, n int) *MatMxN {
if mat == nil {
return NewMatrix(m, n)
}
if m*n <= cap(mat.dat) {
if mat.dat != nil {
mat.dat = mat.dat[:m*n]
} else {
mat.dat = []float32{}
}
mat.m, mat.n = m, n
return mat
}
if shouldPool && mat.dat != nil {
returnToPool(mat.dat)
}
(*mat) = (*NewMatrix(m, n))
return mat
}
// Infers an MxN matrix from a constant matrix from this package. For instance,
// a Mat2x3 inferred with this function will work just like NewMatrixFromData(m[:],2,3)
// where m is the Mat2x3. This uses a type switch.
//
// I personally recommend using NewMatrixFromData, because it avoids a potentially costly type switch.
// However, this is also more robust and less error prone if you change the size of your matrix somewhere.
//
// If the value passed in is not recognized, it returns an InferMatrixError.
func (mat *MatMxN) InferMatrix(m interface{}) (*MatMxN, error) {
switch raw := m.(type) {
case Mat2:
return NewMatrixFromData(raw[:], 2, 2), nil
case Mat2x3:
return NewMatrixFromData(raw[:], 2, 3), nil
case Mat2x4:
return NewMatrixFromData(raw[:], 2, 4), nil
case Mat3:
return NewMatrixFromData(raw[:], 3, 3), nil
case Mat3x2:
return NewMatrixFromData(raw[:], 3, 2), nil
case Mat3x4:
return NewMatrixFromData(raw[:], 3, 4), nil
case Mat4:
return NewMatrixFromData(raw[:], 4, 4), nil
case Mat4x2:
return NewMatrixFromData(raw[:], 4, 2), nil
case Mat4x3:
return NewMatrixFromData(raw[:], 4, 3), nil
default:
return nil, InferMatrixError{}
}
}
// Returns the trace of a square matrix (sum of all diagonal elements). If the matrix
// is nil, or not square, the result will be NaN.
func (mat *MatMxN) Trace() float32 {
if mat == nil || mat.m != mat.n {
return float32(math.NaN())
}
var out float32
for i := 0; i < mat.m; i++ {
out += mat.At(i, i)
}
return out
}
// Takes the transpose of mat and puts it in dst.
//
// If dst is not of the correct dimensions, it will be Reshaped,
// if dst and mat are the same, a temporary matrix of the correct size will
// be allocated; these resources will be released via the memory pool.
//
// This should be improved in the future.
func (mat *MatMxN) Transpose(dst *MatMxN) (t *MatMxN) {
if mat == nil {
return nil
}
if dst == mat {
dst = NewMatrix(mat.n, mat.m)
// Copy data to correct matrix,
// delete temporary buffer,
// and set the return value to the
// correct one
defer func() {
copy(mat.dat, dst.dat)
mat.m, mat.n = mat.n, mat.m
dst.destroy()
t = mat
}()
return mat
} else {
dst = dst.Reshape(mat.n, mat.m)
}
for r := 0; r < mat.m; r++ {
for c := 0; c < mat.n; c++ {
dst.dat[r*dst.m+c] = mat.dat[c*mat.m+r]
}
}
return dst
}
// Returns the raw slice backing this matrix
func (mat *MatMxN) Raw() []float32 {
if mat == nil {
return nil
}
return mat.dat
}
// Returns the number of rows in this matrix
func (mat *MatMxN) NumRows() int {
return mat.m
}
// Returns the number of columns in this matrix
func (mat *MatMxN) NumCols() int {
return mat.n
}
// Returns the number of rows and columns in this matrix
// as a single operation
func (mat *MatMxN) NumRowCols() (rows, cols int) {
return mat.m, mat.n
}
// Returns the element at the given row and column.
// This is garbage in/garbage out and does no bounds
// checking. If the computation happens to lead to an invalid
// element, it will be returned; or it may panic.
func (mat *MatMxN) At(row, col int) float32 {
return mat.dat[col*mat.m+row]
}
// Sets the element at the given row and column.
// This is garbage in/garbage out and does no bounds
// checking. If the computation happens to lead to an invalid
// element, it will be set; or it may panic.
func (mat *MatMxN) Set(row, col int, val float32) {
mat.dat[col*mat.m+row] = val
}
func (mat *MatMxN) Add(dst *MatMxN, addend *MatMxN) *MatMxN {
if mat == nil || addend == nil || mat.m != addend.m || mat.n != addend.n {
return nil
}
dst = dst.Reshape(mat.m, mat.n)
// No need to care about rows and columns
// since it's element-wise anyway
for i, el := range mat.dat {
dst.dat[i] = el + addend.dat[i]
}
return dst
}
func (mat *MatMxN) Sub(dst *MatMxN, subtrahend *MatMxN) *MatMxN {
if mat == nil || subtrahend == nil || mat.m != subtrahend.m || mat.n != subtrahend.n {
return nil
}
dst = dst.Reshape(mat.m, mat.n)
// No need to care about rows and columns
// since it's element-wise anyway
for i, el := range mat.dat {
dst.dat[i] = el - subtrahend.dat[i]
}
return dst
}
// Performs matrix multiplication on MxN matrix mat and NxO matrix mul, storing the result in dst.
// This returns dst, or nil if the operation is not able to be performed.
//
// If mat == dst, or mul == dst a temporary matrix will be used.
//
// This uses the naive algorithm (though on smaller matrices,
// this can actually be faster; about len(mat)+len(mul) < ~100)
func (mat *MatMxN) MulMxN(dst *MatMxN, mul *MatMxN) *MatMxN {
if mat == nil || mul == nil || mat.n != mul.m {
return nil
}
if dst == mul {
mul = NewMatrix(mul.m, mul.n)
copy(mul.dat, dst.dat)
// If mat==dst==mul, we need to change
// mat too or we have a bug
if mat == dst {
mat = mul
}
defer mul.destroy()
} else if dst == mat {
mat = NewMatrix(mat.m, mat.n)
copy(mat.dat, dst.dat)
defer mat.destroy()
}
dst = dst.Reshape(mat.m, mul.n)
for r1 := 0; r1 < mat.m; r1++ {
for c2 := 0; c2 < mul.n; c2++ {
dst.dat[c2*mat.m+r1] = 0
for i := 0; i < mat.n; i++ {
dst.dat[c2*mat.m+r1] += mat.dat[i*mat.m+r1] * mul.dat[c2*mul.m+i]
}
}
}
return dst
}
// Performs a scalar multiplication between mat and some constant c,
// storing the result in dst. Mat and dst can be equal. If dst is not the
// correct size, a Reshape will occur.
func (mat *MatMxN) Mul(dst *MatMxN, c float32) *MatMxN {
if mat == nil {
return nil
}
dst = dst.Reshape(mat.m, mat.n)
for i, el := range mat.dat {
dst.dat[i] = el * c
}
return dst
}
// Multiplies the matrix by a vector of size n. If mat or v is
// nil, this returns nil. If the number of columns in mat does not match
// the Size of v, this also returns nil.
//
// Dst will be resized if it's not big enough. If dst == v; a temporary
// vector will be allocated and returned via the realloc callback when complete.
func (mat *MatMxN) MulNx1(dst, v *VecN) *VecN {
if mat == nil || v == nil || mat.n != len(v.vec) {
return nil
}
if dst == v {
v = NewVecN(len(v.vec))
copy(v.vec, dst.vec)
defer v.destroy()
}
dst = dst.Resize(mat.m)
for r := 0; r < mat.m; r++ {
dst.vec[r] = 0
for c := 0; c < mat.n; c++ {
dst.vec[r] += mat.At(r, c) * v.vec[c]
}
}
return dst
}
func (mat *MatMxN) ApproxEqual(m2 *MatMxN) bool {
if mat == m2 {
return true
}
if mat.m != m2.m || mat.n != m2.n {
return false
}
for i, el := range mat.dat {
if !FloatEqual(el, m2.dat[i]) {
return false
}
}
return true
}
func (mat *MatMxN) ApproxEqualThreshold(m2 *MatMxN, epsilon float32) bool {
if mat == m2 {
return true
}
if mat.m != m2.m || mat.n != m2.n {
return false
}
for i, el := range mat.dat {
if !FloatEqualThreshold(el, m2.dat[i], epsilon) {
return false
}
}
return true
}
func (mat *MatMxN) ApproxEqualFunc(m2 *MatMxN, comp func(float32, float32) bool) bool {
if mat == m2 {
return true
}
if mat.m != m2.m || mat.n != m2.n {
return false
}
for i, el := range mat.dat {
if !comp(el, m2.dat[i]) {
return false
}
}
return true
}
type InferMatrixError struct{}
func (me InferMatrixError) Error() string {
return "could not infer matrix. Make sure you're using a constant matrix such as Mat3 from within the same package (meaning: mgl32.MatMxN can't handle a mgl64.Mat2x3)."
}
type RectangularMatrixError struct{}
func (mse RectangularMatrixError) Error() string {
return "the matrix was the wrong shape, needed a square matrix."
}
type NilMatrixError struct{}
func (me NilMatrixError) Error() string {
return "the matrix is nil"
}

2341
vendor/github.com/go-gl/mathgl/mgl32/matrix.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

370
vendor/github.com/go-gl/mathgl/mgl32/matrix.tmpl generated vendored Normal file
View File

@@ -0,0 +1,370 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// <<.Comment>>
// Edit <<.TemplateName>> and run "go generate" to make changes.
package mgl32
import (
"bytes"
"fmt"
"golang.org/x/image/math/f32"
"text/tabwriter"
)
type Mat2 [4]float32
type Mat2x3 [6]float32
type Mat2x4 [8]float32
type Mat3x2 [6]float32
type Mat3 f32.Mat3
type Mat3x4 [12]float32
type Mat4x2 [8]float32
type Mat4x3 [12]float32
type Mat4 f32.Mat4
func (m Mat2) Mat3() Mat3 {
col0, col1 := m.Cols()
return Mat3FromCols(
col0.Vec3(0),
col1.Vec3(0),
Vec3{0, 0, 1},
)
}
func (m Mat2) Mat4() Mat4 {
col0, col1 := m.Cols()
return Mat4FromCols(
col0.Vec4(0, 0),
col1.Vec4(0, 0),
Vec4{0, 0, 1, 0},
Vec4{0, 0, 0, 1},
)
}
func (m Mat3) Mat2() Mat2 {
col0, col1, _ := m.Cols()
return Mat2FromCols(
col0.Vec2(),
col1.Vec2(),
)
}
func (m Mat3) Mat4() Mat4 {
col0, col1, col2 := m.Cols()
return Mat4FromCols(
col0.Vec4(0),
col1.Vec4(0),
col2.Vec4(0),
Vec4{0, 0, 0, 1},
)
}
func (m Mat4) Mat2() Mat2 {
col0, col1, _, _ := m.Cols()
return Mat2FromCols(
col0.Vec2(),
col1.Vec2(),
)
}
func (m Mat4) Mat3() Mat3 {
col0, col1, col2, _ := m.Cols()
return Mat3FromCols(
col0.Vec3(),
col1.Vec3(),
col2.Vec3(),
)
}
<</* Common functions for all matrices */>>
<<range $m := enum 2 3 4>><<range $n := enum 2 3 4>>
<<$type := typename $m $n>>
// Sets a Column within the Matrix, so it mutates the calling matrix.
func (m *<<$type>>) SetCol(col int, v <<typename $m 1>>) {
<<range $i := iter 0 $m>><<sep "," $i>>m[col*<<$m>>+<<$i>>]<<end>> = <<repeat $m "v[%d]" ",">>
}
// Sets a Row within the Matrix, so it mutates the calling matrix.
func (m *<<$type>>) SetRow(row int, v <<typename $n 1>>) {
<<range $i := iter 0 $n>><<sep "," $i>>m[row+<<mul $m $i>>]<<end>> = <<repeat $n "v[%d]" ",">>
}
<<if eq $m $n>>
// Diag is a basic operation on a square matrix that simply
// returns main diagonal (meaning all elements such that row==col).
func (m <<$type>>) Diag() <<typename $m 1>> {
return <<typename $m 1>>{<<range $i := iter 0 $m>>m[<<mul $i $m | add $i>>],<<end>>}
}
<<end>>
<<if eq $m $n>>
// Ident<<$m>> returns the <<$m>>x<<$n>> identity matrix.
// The identity matrix is a square matrix with the value 1 on its
// diagonals. The characteristic property of the identity matrix is that
// any matrix multiplied by it is itself. (MI = M; IN = N)
func Ident<<$m>>() <<$type>> {
return <<$type>>{<<range $i := matiter $m $n>><<if eq $i.M $i.N>>1<<else>>0<<end>>,<<end>>}
}
<<end>>
<<if eq $m $n>>
// Diag<<$m>> creates a diagonal matrix from the entries of the input vector.
// That is, for each pointer for row==col, vector[row] is the entry. Otherwise it's 0.
//
// Another way to think about it is that the identity is this function where the every vector element is 1.
func Diag<<$m>>(v <<typename $m 1>>) <<$type>> {
var m <<$type>>
<<range $i := iter 0 $m>><<sep "," $i>>m[<<mul $i $m | add $i>>]<<end>> = <<repeat $m "v[%d]" ",">>
return m
}
<<end>>
// <<$type>>FromRows builds a new matrix from row vectors.
// The resulting matrix will still be in column major order, but this can be
// good for hand-building matrices.
func <<$type>>FromRows(<<range $i := iter 0 $m>><<sep "," $i>>row<<$i>><<end>> <<typename $n 1>>) <<$type>> {
return <<$type>>{<<range $i := matiter $m $n>>row<<$i.M>>[<<$i.N>>],<<end>>}
}
// <<$type>>FromCols builds a new matrix from column vectors.
func <<$type>>FromCols(<<repeat $n "col%d" ",">> <<typename $m 1>>) <<$type>> {
return <<$type>>{<<range $i := matiter $m $n>>col<<$i.N>>[<<$i.M>>], <<end>>}
}
// Add performs an element-wise addition of two matrices, this is
// equivalent to iterating over every element of m1 and adding the corresponding value of m2.
func (m1 <<$type>>) Add(m2 <<$type>>) <<$type>> {
return <<$type>>{<< range $i := matiter $m $n>>m1[<<$i>>] + m2[<<$i>>], <<end>>}
}
// Sub performs an element-wise subtraction of two matrices, this is
// equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.
func (m1 <<$type>>) Sub(m2 <<$type>>) <<$type>> {
return <<$type>>{<< range $i := matiter $m $n>>m1[<<$i>>] - m2[<<$i>>], <<end>>}
}
// Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating
// over every element of the matrix and multiply it by c.
func (m1 <<$type>>) Mul(c float32) <<$type>> {
return <<$type>>{<< range $i := matiter $m $n>>m1[<<$i>>] * c, <<end>>}
}
<<range $o := enum 1 2 3 4>>
// Mul<<$n>><<if ne $n $o>>x<<$o>><<end>> performs a "matrix product" between this matrix
// and another of the given dimension. For any two matrices of dimensionality
// MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using
// Mul4x2 will result in a Mat4x2.
func (m1 <<$type>>) Mul<<$n>><<if ne $n $o>>x<<$o>><<end>>(m2 <<typename $n $o>>) <<typename $m $o>> {
return <<typename $m $o>>{<<range $i := matiter $m $o>>
<<range $k := iter 0 $n>><<sep "+" $k>>m1[<<mul $k $m | add $i.M>>]*m2[<<mul $i.N $n| add $k>>]<<end>>,<<end>>
}
}
<<end>>
// Transpose produces the transpose of this matrix. For any MxN matrix
// the transpose is an NxM matrix with the rows swapped with the columns. For instance
// the transpose of the Mat3x2 is a Mat2x3 like so:
//
// [[a b]] [[a c e]]
// [[c d]] = [[b d f]]
// [[e f]]
func (m1 <<$type>>) Transpose() <<typename $n $m>> {
return <<typename $n $m>>{<<range $i := matiter $n $m>>m1[<<mul $m $i.M | add $i.N>>], <<end>>}
}
<<if eq $m $n>>
// The determinant of a matrix is a measure of a square matrix's
// singularity and invertability, among other things. In this library, the
// determinant is hard coded based on pre-computed cofactor expansion, and uses
// no loops. Of course, the addition and multiplication must still be done.
func (m <<$type>>) Det() float32 {
<<if eq $m 2 ->>
return m[0]*m[3] - m[1]*m[2]
<<else if eq $m 3 ->>
return m[0]*m[4]*m[8] + m[3]*m[7]*m[2] + m[6]*m[1]*m[5] - m[6]*m[4]*m[2] - m[3]*m[1]*m[8] - m[0]*m[7]*m[5]
<<else if eq $m 4 ->>
return m[0]*m[5]*m[10]*m[15] - m[0]*m[5]*m[11]*m[14] - m[0]*m[6]*m[9]*m[15] + m[0]*m[6]*m[11]*m[13] + m[0]*m[7]*m[9]*m[14] - m[0]*m[7]*m[10]*m[13] - m[1]*m[4]*m[10]*m[15] + m[1]*m[4]*m[11]*m[14] + m[1]*m[6]*m[8]*m[15] - m[1]*m[6]*m[11]*m[12] - m[1]*m[7]*m[8]*m[14] + m[1]*m[7]*m[10]*m[12] + m[2]*m[4]*m[9]*m[15] - m[2]*m[4]*m[11]*m[13] - m[2]*m[5]*m[8]*m[15] + m[2]*m[5]*m[11]*m[12] + m[2]*m[7]*m[8]*m[13] - m[2]*m[7]*m[9]*m[12] - m[3]*m[4]*m[9]*m[14] + m[3]*m[4]*m[10]*m[13] + m[3]*m[5]*m[8]*m[14] - m[3]*m[5]*m[10]*m[12] - m[3]*m[6]*m[8]*m[13] + m[3]*m[6]*m[9]*m[12]
<<end ->>
}
<<end>>
<<if eq $m $n>>
// Inv computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the
// original, yields the identity.
//
// M_inv * M = M * M_inv = I
//
// In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling
// are still done. This can still be (relatively) expensive for a 4x4.
//
// This function checks the determinant to see if the matrix is invertible.
// If the determinant is 0.0, this function returns the zero matrix. However, due to floating point errors, it is
// entirely plausible to get a false positive or negative.
// In the future, an alternate function may be written which takes in a pre-computed determinant.
func (m <<$type>>) Inv() <<$type>> {
det := m.Det()
if FloatEqual(det, float32(0.0)) {
return <<$type>>{}
}
<<if eq $m 2>>
retMat := Mat2{m[3], -m[1], -m[2], m[0]}
<<else if eq $m 3>>
retMat := Mat3{
m[4]*m[8] - m[5]*m[7],
m[2]*m[7] - m[1]*m[8],
m[1]*m[5] - m[2]*m[4],
m[5]*m[6] - m[3]*m[8],
m[0]*m[8] - m[2]*m[6],
m[2]*m[3] - m[0]*m[5],
m[3]*m[7] - m[4]*m[6],
m[1]*m[6] - m[0]*m[7],
m[0]*m[4] - m[1]*m[3],
}
<<else if eq $m 4>>
retMat := Mat4{
-m[7]*m[10]*m[13] + m[6]*m[11]*m[13] + m[7]*m[9]*m[14] - m[5]*m[11]*m[14] - m[6]*m[9]*m[15] + m[5]*m[10]*m[15],
m[3]*m[10]*m[13] - m[2]*m[11]*m[13] - m[3]*m[9]*m[14] + m[1]*m[11]*m[14] + m[2]*m[9]*m[15] - m[1]*m[10]*m[15],
-m[3]*m[6]*m[13] + m[2]*m[7]*m[13] + m[3]*m[5]*m[14] - m[1]*m[7]*m[14] - m[2]*m[5]*m[15] + m[1]*m[6]*m[15],
m[3]*m[6]*m[9] - m[2]*m[7]*m[9] - m[3]*m[5]*m[10] + m[1]*m[7]*m[10] + m[2]*m[5]*m[11] - m[1]*m[6]*m[11],
m[7]*m[10]*m[12] - m[6]*m[11]*m[12] - m[7]*m[8]*m[14] + m[4]*m[11]*m[14] + m[6]*m[8]*m[15] - m[4]*m[10]*m[15],
-m[3]*m[10]*m[12] + m[2]*m[11]*m[12] + m[3]*m[8]*m[14] - m[0]*m[11]*m[14] - m[2]*m[8]*m[15] + m[0]*m[10]*m[15],
m[3]*m[6]*m[12] - m[2]*m[7]*m[12] - m[3]*m[4]*m[14] + m[0]*m[7]*m[14] + m[2]*m[4]*m[15] - m[0]*m[6]*m[15],
-m[3]*m[6]*m[8] + m[2]*m[7]*m[8] + m[3]*m[4]*m[10] - m[0]*m[7]*m[10] - m[2]*m[4]*m[11] + m[0]*m[6]*m[11],
-m[7]*m[9]*m[12] + m[5]*m[11]*m[12] + m[7]*m[8]*m[13] - m[4]*m[11]*m[13] - m[5]*m[8]*m[15] + m[4]*m[9]*m[15],
m[3]*m[9]*m[12] - m[1]*m[11]*m[12] - m[3]*m[8]*m[13] + m[0]*m[11]*m[13] + m[1]*m[8]*m[15] - m[0]*m[9]*m[15],
-m[3]*m[5]*m[12] + m[1]*m[7]*m[12] + m[3]*m[4]*m[13] - m[0]*m[7]*m[13] - m[1]*m[4]*m[15] + m[0]*m[5]*m[15],
m[3]*m[5]*m[8] - m[1]*m[7]*m[8] - m[3]*m[4]*m[9] + m[0]*m[7]*m[9] + m[1]*m[4]*m[11] - m[0]*m[5]*m[11],
m[6]*m[9]*m[12] - m[5]*m[10]*m[12] - m[6]*m[8]*m[13] + m[4]*m[10]*m[13] + m[5]*m[8]*m[14] - m[4]*m[9]*m[14],
-m[2]*m[9]*m[12] + m[1]*m[10]*m[12] + m[2]*m[8]*m[13] - m[0]*m[10]*m[13] - m[1]*m[8]*m[14] + m[0]*m[9]*m[14],
m[2]*m[5]*m[12] - m[1]*m[6]*m[12] - m[2]*m[4]*m[13] + m[0]*m[6]*m[13] + m[1]*m[4]*m[14] - m[0]*m[5]*m[14],
-m[2]*m[5]*m[8] + m[1]*m[6]*m[8] + m[2]*m[4]*m[9] - m[0]*m[6]*m[9] - m[1]*m[4]*m[10] + m[0]*m[5]*m[10],
}
<<end>>
return retMat.Mul(1 / det)
}
<<end>>
// ApproxEqual performs an element-wise approximate equality test between two matrices,
// as if FloatEqual had been used.
func (m1 <<$type>>) ApproxEqual(m2 <<$type>>) bool {
for i := range m1 {
if !FloatEqual(m1[i], m2[i]) {
return false
}
}
return true
}
// ApproxEqualThreshold performs an element-wise approximate equality test between two matrices
// with a given epsilon threshold, as if FloatEqualThreshold had been used.
func (m1 <<$type>>) ApproxEqualThreshold(m2 <<$type>>, threshold float32) bool {
for i := range m1 {
if !FloatEqualThreshold(m1[i], m2[i], threshold) {
return false
}
}
return true
}
// ApproxEqualFunc performs an element-wise approximate equality test between two matrices
// with a given equality functions, intended to be used with FloatEqualFunc; although and comparison
// function may be used in practice.
func (m1 <<$type>>) ApproxFuncEqual(m2 <<$type>>, eq func(float32, float32) bool) bool {
for i := range m1 {
if !eq(m1[i], m2[i]) {
return false
}
}
return true
}
// At returns the matrix element at the given row and column.
// This is equivalent to mat[col * numRow + row] where numRow is constant
// (E.G. for a Mat3x2 it's equal to 3)
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.
func (m <<$type>>) At(row, col int) float32 {
return m[col*<<$m>>+row]
}
// Set sets the corresponding matrix element at the given row and column.
// This has a pointer receiver because it mutates the matrix.
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.
func (m *<<$type>>) Set(row, col int, value float32) {
m[col*<<$m>>+row] = value
}
// Index returns the index of the given row and column, to be used with direct
// access. E.G. Index(0,0) = 0.
//
// This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of
// (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause
// a panic if you try to access the array with it if it's truly out of bounds.
func (m <<$type>>) Index(row, col int) int {
return col*<<$m>> + row
}
// Row returns a vector representing the corresponding row (starting at row 0).
// This package makes no distinction between row and column vectors, so it
// will be a normal VecM for a MxN matrix.
func (m <<$type>>) Row(row int) <<typename $n 1>> {
return <<typename $n 1>>{<<range $i := iter 0 $n>>m[row+<<mul $m $i>>],<<end>>}
}
// Rows decomposes a matrix into its corresponding row vectors.
// This is equivalent to calling mat.Row for each row.
func (m <<$type>>) Rows() (<<repeat $m "row%d" ",">> <<typename $n 1>>) {
return <<repeat $m "m.Row(%d)" ",">>
}
// Col returns a vector representing the corresponding column (starting at col 0).
// This package makes no distinction between row and column vectors, so it
// will be a normal VecN for a MxN matrix.
func (m <<$type>>) Col(col int) <<typename $m 1>> {
return <<typename $m 1>>{<<range $i := iter 0 $m>>m[col*<<$m>>+<<$i>>],<<end>>}
}
// Cols decomposes a matrix into its corresponding column vectors.
// This is equivalent to calling mat.Col for each column.
func (m <<$type>>) Cols() (<<repeat $n "col%d" ",">> <<typename $m 1>>) {
return <<repeat $n "m.Col(%d)" ",">>
}
<<if eq $m $n>>
// Trace is a basic operation on a square matrix that simply
// sums up all elements on the main diagonal (meaning all elements such that row==col).
func (m <<$type>>) Trace() float32 {
return <<range $i := iter 0 $m>><<sep "+" $i>> m[<<mul $i $m | add $i>>]<<end>>
}
<<end>>
// Abs returns the element-wise absolute value of this matrix
func (m <<$type>>) Abs() <<$type>> {
return <<$type>>{<<repeat (mul $m $n) "Abs(m[%d])" ",">>}
}
// Pretty prints the matrix
func (m <<$type>>) String() string {
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 4, 4, 1, ' ', tabwriter.AlignRight)
for i := 0; i < <<$m>>; i++ {
for _, col := range m.Row(i) {
fmt.Fprintf(w, "%f\t", col)
}
fmt.Fprintln(w, "")
}
w.Flush()
return buf.String()
}
<<end>><<end>> <</* range $m range $n */>>

121
vendor/github.com/go-gl/mathgl/mgl32/mempool.go generated vendored Normal file
View File

@@ -0,0 +1,121 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"sync"
)
var (
slicePools []*sync.Pool
listLock sync.RWMutex
)
var shouldPool = true
func DisableMemoryPooling() {
shouldPool = false
}
// Returns the given memory pool. If the pool doesn't exist, it will
// create all pools up to element i. The number "i" corresponds to "p"
// in most other comments. That is, it's Ceil(log_2(whatever)). So i=0
// means you'll get the pool for slices of size 1, i=1 for size 2, i=2 for size 4,
// and so on.
//
// This is concurrency safe and uses an RWMutex to protect the list expansion.
func getPool(i int) *sync.Pool {
listLock.RLock()
if i >= len(slicePools) {
// Promote to a write lock because we now
// need to mutate the pool
listLock.RUnlock()
listLock.Lock()
defer listLock.Unlock()
for n := i - len(slicePools); n >= 0; n-- {
newFunc := genPoolNew(1 << uint(len(slicePools)))
slicePools = append(slicePools, &sync.Pool{New: newFunc})
}
} else {
defer listLock.RUnlock()
}
return slicePools[i]
}
func genPoolNew(i int) func() interface{} {
return func() interface{} {
return make([]float32, 0, i)
}
}
// Grabs a slice from the memory pool, such that its cap
// is 2^p where p is Ceil(log_2(size)). It will be downsliced
// such that the len is size.
func grabFromPool(size int) []float32 {
pool, exact := binLog(size)
// Tried to grab something of size
// zero or less
if pool == -1 {
return nil
}
// If the log is not exact, we
// need to "overallocate" so we have
// log+1
if !exact {
pool++
}
slice := getPool(pool).Get().([]float32)
slice = slice[:size]
return slice
}
// Returns a slice to the appropriate pool. If the slice does not have a cap that's precisely
// a power of 2, this will panic.
func returnToPool(slice []float32) {
if cap(slice) == 0 {
return
}
pool, exact := binLog(cap(slice))
if !exact {
panic("attempt to pool slice with non-exact cap. If you're a user, please file an issue with github.com/go-gl/mathgl about this bug. This should never happen.")
}
getPool(pool).Put(slice)
}
// This returns the integer base 2 log of the value
// and whether the log is exact or rounded down.
//
// This is only for positive integers.
//
// There are faster ways to do this, I'm open to suggestions. Most rely on knowing system endianness
// which Go makes hard to do. I'm hesistant to use float conversions and the math package because of off-by-one errors.
func binLog(val int) (int, bool) {
if val <= 0 {
return -1, false
}
exact := true
l := 0
for ; val > 1; val = val >> 1 {
// If the current lsb is 1 and the number
// is not equal to 1, this is not an exact
// log, but rather a rounding of it
if val&1 != 0 {
exact = false
}
l++
}
return l, exact
}

98
vendor/github.com/go-gl/mathgl/mgl32/project.go generated vendored Normal file
View File

@@ -0,0 +1,98 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"errors"
"math"
)
func Ortho(left, right, bottom, top, near, far float32) Mat4 {
rml, tmb, fmn := (right - left), (top - bottom), (far - near)
return Mat4{float32(2. / rml), 0, 0, 0, 0, float32(2. / tmb), 0, 0, 0, 0, float32(-2. / fmn), 0, float32(-(right + left) / rml), float32(-(top + bottom) / tmb), float32(-(far + near) / fmn), 1}
}
// Equivalent to Ortho with the near and far planes being -1 and 1, respectively
func Ortho2D(left, right, bottom, top float32) Mat4 {
return Ortho(left, right, bottom, top, -1, 1)
}
func Perspective(fovy, aspect, near, far float32) Mat4 {
// fovy = (fovy * math.Pi) / 180.0 // convert from degrees to radians
nmf, f := near-far, float32(1./math.Tan(float64(fovy)/2.0))
return Mat4{float32(f / aspect), 0, 0, 0, 0, float32(f), 0, 0, 0, 0, float32((near + far) / nmf), -1, 0, 0, float32((2. * far * near) / nmf), 0}
}
func Frustum(left, right, bottom, top, near, far float32) Mat4 {
rml, tmb, fmn := (right - left), (top - bottom), (far - near)
A, B, C, D := (right+left)/rml, (top+bottom)/tmb, -(far+near)/fmn, -(2*far*near)/fmn
return Mat4{float32((2. * near) / rml), 0, 0, 0, 0, float32((2. * near) / tmb), 0, 0, float32(A), float32(B), float32(C), -1, 0, 0, float32(D), 0}
}
func LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ float32) Mat4 {
return LookAtV(Vec3{eyeX, eyeY, eyeZ}, Vec3{centerX, centerY, centerZ}, Vec3{upX, upY, upZ})
}
// LookAtV generates a transform matrix from world space into the specific eye space
func LookAtV(eye, center, up Vec3) Mat4 {
f := center.Sub(eye).Normalize()
s := f.Cross(up.Normalize()).Normalize()
u := s.Cross(f)
M := Mat4{
s[0], u[0], -f[0], 0,
s[1], u[1], -f[1], 0,
s[2], u[2], -f[2], 0,
0, 0, 0, 1,
}
return M.Mul4(Translate3D(float32(-eye[0]), float32(-eye[1]), float32(-eye[2])))
}
// Transform a set of coordinates from object space (in obj) to window coordinates (with depth)
//
// Window coordinates are continuous, not discrete (well, as continuous as an IEEE Floating Point can be), so you won't get exact pixel locations
// without rounding or similar
func Project(obj Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (win Vec3) {
obj4 := obj.Vec4(1)
vpp := projection.Mul4(modelview).Mul4x1(obj4)
vpp = vpp.Mul(1 / vpp.W())
win[0] = float32(initialX) + (float32(width)*(vpp[0]+1))/2
win[1] = float32(initialY) + (float32(height)*(vpp[1]+1))/2
win[2] = (vpp[2] + 1) / 2
return win
}
// Transform a set of window coordinates to object space. If your MVP (projection.Mul(modelview) matrix is not invertible, this will return an error
//
// Note that the projection may not be perfect if you use strict pixel locations rather than the exact values given by Projectf.
// (It's still unlikely to be perfect due to precision errors, but it will be closer)
func UnProject(win Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (obj Vec3, err error) {
inv := projection.Mul4(modelview).Inv()
var blank Mat4
if inv == blank {
return Vec3{}, errors.New("Could not find matrix inverse (projection times modelview is probably non-singular)")
}
obj4 := inv.Mul4x1(Vec4{
(2 * (win[0] - float32(initialX)) / float32(width)) - 1,
(2 * (win[1] - float32(initialY)) / float32(height)) - 1,
2*win[2] - 1,
1.0,
})
obj = obj4.Vec3()
//if obj4[3] > MinValue {}
obj[0] /= obj4[3]
obj[1] /= obj4[3]
obj[2] /= obj4[3]
return obj, nil
}

458
vendor/github.com/go-gl/mathgl/mgl32/quat.go generated vendored Normal file
View File

@@ -0,0 +1,458 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// A rotation order is the order in which
// rotations will be transformed for the purposes of AnglesToQuat
type RotationOrder int
const (
XYX RotationOrder = iota
XYZ
XZX
XZY
YXY
YXZ
YZY
YZX
ZYZ
ZYX
ZXZ
ZXY
)
// A Quaternion is an extension of the imaginary numbers; there's all sorts of
// interesting theory behind it. In 3D graphics we mostly use it as a cheap way of
// representing rotation since quaternions are cheaper to multiply by, and easier to
// interpolate than matrices.
//
// A Quaternion has two parts: W, the so-called scalar component,
// and "V", the vector component. The vector component is considered to
// be the part in 3D space, while W (loosely interpreted) is its 4D coordinate.
type Quat struct {
W float32
V Vec3
}
// The quaternion identity: W=1; V=(0,0,0).
//
// As with all identities, multiplying any quaternion by this will yield the same
// quaternion you started with.
func QuatIdent() Quat {
return Quat{1., Vec3{0, 0, 0}}
}
// Creates an angle from an axis and an angle relative to that axis.
//
// This is cheaper than HomogRotate3D.
func QuatRotate(angle float32, axis Vec3) Quat {
// angle = (float32(math.Pi) * angle) / 180.0
c, s := float32(math.Cos(float64(angle/2))), float32(math.Sin(float64(angle/2)))
return Quat{c, axis.Mul(s)}
}
// A convenient alias for q.V[0]
func (q Quat) X() float32 {
return q.V[0]
}
// A convenient alias for q.V[1]
func (q Quat) Y() float32 {
return q.V[1]
}
// A convenient alias for q.V[2]
func (q Quat) Z() float32 {
return q.V[2]
}
// Adds two quaternions. It's no more complicated than
// adding their W and V components.
func (q1 Quat) Add(q2 Quat) Quat {
return Quat{q1.W + q2.W, q1.V.Add(q2.V)}
}
// Subtracts two quaternions. It's no more complicated than
// subtracting their W and V components.
func (q1 Quat) Sub(q2 Quat) Quat {
return Quat{q1.W - q2.W, q1.V.Sub(q2.V)}
}
// Multiplies two quaternions. This can be seen as a rotation. Note that
// Multiplication is NOT commutative, meaning q1.Mul(q2) does not necessarily
// equal q2.Mul(q1).
func (q1 Quat) Mul(q2 Quat) Quat {
return Quat{q1.W*q2.W - q1.V.Dot(q2.V), q1.V.Cross(q2.V).Add(q2.V.Mul(q1.W)).Add(q1.V.Mul(q2.W))}
}
// Scales every element of the quaternion by some constant factor.
func (q1 Quat) Scale(c float32) Quat {
return Quat{q1.W * c, Vec3{q1.V[0] * c, q1.V[1] * c, q1.V[2] * c}}
}
// Returns the conjugate of a quaternion. Equivalent to
// Quat{q1.W, q1.V.Mul(-1)}
func (q1 Quat) Conjugate() Quat {
return Quat{q1.W, q1.V.Mul(-1)}
}
// Returns the Length of the quaternion, also known as its Norm. This is the same thing as
// the Len of a Vec4
func (q1 Quat) Len() float32 {
return float32(math.Sqrt(float64(q1.W*q1.W + q1.V[0]*q1.V[0] + q1.V[1]*q1.V[1] + q1.V[2]*q1.V[2])))
}
// Norm() is an alias for Len() since both are very common terms.
func (q1 Quat) Norm() float32 {
return q1.Len()
}
// Normalizes the quaternion, returning its versor (unit quaternion).
//
// This is the same as normalizing it as a Vec4.
func (q1 Quat) Normalize() Quat {
length := q1.Len()
if FloatEqual(1, length) {
return q1
}
if length == 0 {
return QuatIdent()
}
if length == InfPos {
length = MaxValue
}
return Quat{q1.W * 1 / length, q1.V.Mul(1 / length)}
}
// The inverse of a quaternion. The inverse is equivalent
// to the conjugate divided by the square of the length.
//
// This method computes the square norm by directly adding the sum
// of the squares of all terms instead of actually squaring q1.Len(),
// both for performance and precision.
func (q1 Quat) Inverse() Quat {
return q1.Conjugate().Scale(1 / q1.Dot(q1))
}
// Rotates a vector by the rotation this quaternion represents.
// This will result in a 3D vector. Strictly speaking, this is
// equivalent to q1.v.q* where the "."" is quaternion multiplication and v is interpreted
// as a quaternion with W 0 and V v. In code:
// q1.Mul(Quat{0,v}).Mul(q1.Conjugate()), and
// then retrieving the imaginary (vector) part.
//
// In practice, we hand-compute this in the general case and simplify
// to save a few operations.
func (q1 Quat) Rotate(v Vec3) Vec3 {
cross := q1.V.Cross(v)
// v + 2q_w * (q_v x v) + 2q_v x (q_v x v)
return v.Add(cross.Mul(2 * q1.W)).Add(q1.V.Mul(2).Cross(cross))
}
// Returns the homogeneous 3D rotation matrix corresponding to the quaternion.
func (q1 Quat) Mat4() Mat4 {
w, x, y, z := q1.W, q1.V[0], q1.V[1], q1.V[2]
return Mat4{
1 - 2*y*y - 2*z*z, 2*x*y + 2*w*z, 2*x*z - 2*w*y, 0,
2*x*y - 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z + 2*w*x, 0,
2*x*z + 2*w*y, 2*y*z - 2*w*x, 1 - 2*x*x - 2*y*y, 0,
0, 0, 0, 1,
}
}
// The dot product between two quaternions, equivalent to if this was a Vec4
func (q1 Quat) Dot(q2 Quat) float32 {
return q1.W*q2.W + q1.V[0]*q2.V[0] + q1.V[1]*q2.V[1] + q1.V[2]*q2.V[2]
}
// Returns whether the quaternions are approximately equal, as if
// FloatEqual was called on each matching element
func (q1 Quat) ApproxEqual(q2 Quat) bool {
return FloatEqual(q1.W, q2.W) && q1.V.ApproxEqual(q2.V)
}
// Returns whether the quaternions are approximately equal with a given tolerence, as if
// FloatEqualThreshold was called on each matching element with the given epsilon
func (q1 Quat) ApproxEqualThreshold(q2 Quat, epsilon float32) bool {
return FloatEqualThreshold(q1.W, q2.W, epsilon) && q1.V.ApproxEqualThreshold(q2.V, epsilon)
}
// Returns whether the quaternions are approximately equal using the given comparison function, as if
// the function had been called on each individual element
func (q1 Quat) ApproxEqualFunc(q2 Quat, f func(float32, float32) bool) bool {
return f(q1.W, q2.W) && q1.V.ApproxFuncEqual(q2.V, f)
}
// Returns whether the quaternions represents the same orientation
//
// Different values can represent the same orientation (q == -q) because quaternions avoid singularities
// and discontinuities involved with rotation in 3 dimensions by adding extra dimensions
func (q1 Quat) OrientationEqual(q2 Quat) bool {
return q1.OrientationEqualThreshold(q2, Epsilon)
}
// Returns whether the quaternions represents the same orientation with a given tolerence
func (q1 Quat) OrientationEqualThreshold(q2 Quat, epsilon float32) bool {
return Abs(q1.Normalize().Dot(q2.Normalize())) > 1-epsilon
}
// Slerp is *S*pherical *L*inear Int*erp*olation, a method of interpolating
// between two quaternions. This always takes the straightest path on the sphere between
// the two quaternions, and maintains constant velocity.
//
// However, it's expensive and QuatSlerp(q1,q2) is not the same as QuatSlerp(q2,q1)
func QuatSlerp(q1, q2 Quat, amount float32) Quat {
q1, q2 = q1.Normalize(), q2.Normalize()
dot := q1.Dot(q2)
// If the inputs are too close for comfort, linearly interpolate and normalize the result.
if dot > 0.9995 {
return QuatNlerp(q1, q2, amount)
}
// This is here for precision errors, I'm perfectly aware that *technically* the dot is bound [-1,1], but since Acos will freak out if it's not (even if it's just a liiiiitle bit over due to normal error) we need to clamp it
dot = Clamp(dot, -1, 1)
theta := float32(math.Acos(float64(dot))) * amount
c, s := float32(math.Cos(float64(theta))), float32(math.Sin(float64(theta)))
rel := q2.Sub(q1.Scale(dot)).Normalize()
return q1.Scale(c).Add(rel.Scale(s))
}
// *L*inear Int*erp*olation between two Quaternions, cheap and simple.
//
// Not excessively useful, but uses can be found.
func QuatLerp(q1, q2 Quat, amount float32) Quat {
return q1.Add(q2.Sub(q1).Scale(amount))
}
// *Normalized* *L*inear Int*erp*olation between two Quaternions. Cheaper than Slerp
// and usually just as good. This is literally Lerp with Normalize() called on it.
//
// Unlike Slerp, constant velocity isn't maintained, but it's much faster and
// Nlerp(q1,q2) and Nlerp(q2,q1) return the same path. You should probably
// use this more often unless you're suffering from choppiness due to the
// non-constant velocity problem.
func QuatNlerp(q1, q2 Quat, amount float32) Quat {
return QuatLerp(q1, q2, amount).Normalize()
}
// Performs a rotation in the specified order. If the order is not
// a valid RotationOrder, this function will panic
//
// The rotation "order" is more of an axis descriptor. For instance XZX would
// tell the function to interpret angle1 as a rotation about the X axis, angle2 about
// the Z axis, and angle3 about the X axis again.
//
// Based off the code for the Matlab function "angle2quat", though this implementation
// only supports 3 single angles as opposed to multiple angles.
func AnglesToQuat(angle1, angle2, angle3 float32, order RotationOrder) Quat {
var s [3]float64
var c [3]float64
s[0], c[0] = math.Sincos(float64(angle1 / 2))
s[1], c[1] = math.Sincos(float64(angle2 / 2))
s[2], c[2] = math.Sincos(float64(angle3 / 2))
ret := Quat{}
switch order {
case ZYX:
ret.W = float32(c[0]*c[1]*c[2] + s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*c[1]*s[2]),
float32(s[0]*c[1]*c[2] - c[0]*s[1]*s[2]),
}
case ZYZ:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*c[1]*c[2] + c[0]*c[1]*s[2]),
}
case ZXY:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] - s[0]*c[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*s[1]*c[2]),
float32(c[0]*s[1]*s[2] + s[0]*c[1]*c[2]),
}
case ZXZ:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*s[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
}
case YXZ:
ret.W = float32(c[0]*c[1]*c[2] + s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] + s[0]*c[1]*s[2]),
float32(s[0]*c[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] - s[0]*s[1]*c[2]),
}
case YXY:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*c[1]*c[2] + c[0]*c[1]*s[2]),
float32(c[0]*s[1]*s[2] - s[0]*s[1]*c[2]),
}
case YZX:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] + s[0]*s[1]*c[2]),
float32(c[0]*s[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] - s[0]*c[1]*s[2]),
}
case YZY:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(s[0]*s[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
}
case XYZ:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] - s[0]*c[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*s[1]*c[2]),
}
case XYX:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*s[1]*c[2] - c[0]*s[1]*s[2]),
}
case XZY:
ret.W = float32(c[0]*c[1]*c[2] + s[0]*s[1]*s[2])
ret.V = Vec3{float32(s[0]*c[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*c[1]*s[2]),
}
case XZX:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
}
default:
panic("Unsupported rotation order")
}
return ret
}
// Mat4ToQuat converts a pure rotation matrix into a quaternion
func Mat4ToQuat(m Mat4) Quat {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
if tr := m[0] + m[5] + m[10]; tr > 0 {
s := float32(0.5 / math.Sqrt(float64(tr+1.0)))
return Quat{
0.25 / s,
Vec3{
(m[6] - m[9]) * s,
(m[8] - m[2]) * s,
(m[1] - m[4]) * s,
},
}
}
if (m[0] > m[5]) && (m[0] > m[10]) {
s := float32(2.0 * math.Sqrt(float64(1.0+m[0]-m[5]-m[10])))
return Quat{
(m[6] - m[9]) / s,
Vec3{
0.25 * s,
(m[4] + m[1]) / s,
(m[8] + m[2]) / s,
},
}
}
if m[5] > m[10] {
s := float32(2.0 * math.Sqrt(float64(1.0+m[5]-m[0]-m[10])))
return Quat{
(m[8] - m[2]) / s,
Vec3{
(m[4] + m[1]) / s,
0.25 * s,
(m[9] + m[6]) / s,
},
}
}
s := float32(2.0 * math.Sqrt(float64(1.0+m[10]-m[0]-m[5])))
return Quat{
(m[1] - m[4]) / s,
Vec3{
(m[8] + m[2]) / s,
(m[9] + m[6]) / s,
0.25 * s,
},
}
}
// QuatLookAtV creates a rotation from an eye vector to a center vector
//
// It assumes the front of the rotated object at Z- and up at Y+
func QuatLookAtV(eye, center, up Vec3) Quat {
// http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/#I_need_an_equivalent_of_gluLookAt__How_do_I_orient_an_object_towards_a_point__
// https://bitbucket.org/sinbad/ogre/src/d2ef494c4a2f5d6e2f0f17d3bfb9fd936d5423bb/OgreMain/src/OgreCamera.cpp?at=default#cl-161
direction := center.Sub(eye).Normalize()
// Find the rotation between the front of the object (that we assume towards Z-,
// but this depends on your model) and the desired direction
rotDir := QuatBetweenVectors(Vec3{0, 0, -1}, direction)
// Recompute up so that it's perpendicular to the direction
// You can skip that part if you really want to force up
//right := direction.Cross(up)
//up = right.Cross(direction)
// Because of the 1rst rotation, the up is probably completely screwed up.
// Find the rotation between the "up" of the rotated object, and the desired up
upCur := rotDir.Rotate(Vec3{0, 1, 0})
rotUp := QuatBetweenVectors(upCur, up)
rotTarget := rotUp.Mul(rotDir) // remember, in reverse order.
return rotTarget.Inverse() // camera rotation should be inversed!
}
// QuatBetweenVectors calculates the rotation between two vectors
func QuatBetweenVectors(start, dest Vec3) Quat {
// http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/#I_need_an_equivalent_of_gluLookAt__How_do_I_orient_an_object_towards_a_point__
// https://github.com/g-truc/glm/blob/0.9.5/glm/gtx/quaternion.inl#L225
// https://bitbucket.org/sinbad/ogre/src/d2ef494c4a2f5d6e2f0f17d3bfb9fd936d5423bb/OgreMain/include/OgreVector3.h?at=default#cl-654
start = start.Normalize()
dest = dest.Normalize()
epsilon := float32(0.001)
cosTheta := start.Dot(dest)
if cosTheta < -1.0+epsilon {
// special case when vectors in opposite directions:
// there is no "ideal" rotation axis
// So guess one; any will do as long as it's perpendicular to start
axis := Vec3{1, 0, 0}.Cross(start)
if axis.Dot(axis) < epsilon {
// bad luck, they were parallel, try again!
axis = Vec3{0, 1, 0}.Cross(start)
}
return QuatRotate(math.Pi, axis.Normalize())
}
axis := start.Cross(dest)
s := float32(math.Sqrt(float64(1.0+cosTheta) * 2.0))
return Quat{
s * 0.5,
axis.Mul(1.0 / s),
}
}

306
vendor/github.com/go-gl/mathgl/mgl32/shapes.go generated vendored Normal file
View File

@@ -0,0 +1,306 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"fmt"
"math"
)
// Generates a circle centered at (0,0) with a given radius.
// The radii are assumed to be in GL's coordinate sizing.
//
// Technically this draws an ellipse with two axes that match with the X and Y axes, the reason it has a radiusX and radiusY is because GL's coordinate system
// is proportional to screen width and screen height. So if you have a non-square viewport, a single radius will appear
// to "squash" the circle in one direction (usually the Y direction), so the X and Y radius allow for a circle to be made.
// A good way to get the correct radii is with mathgl.ScreenToGLCoords(radius, radius, screenWidth, screenHeight) which will get you the correct
// proportional GL coords.
//
// The numSlices argument specifies how many triangles you want your circle divided into, setting this
// number to too low a value may cause problem (and too high will cause it to take a lot of memory and time to compute
// without much gain in resolution).
//
// This uses discrete triangles, not a triangle fan
func Circle(radiusX, radiusY float32, numSlices int) []Vec2 {
twoPi := float32(2.0 * math.Pi)
circlePoints := make([]Vec2, 0, numSlices*3)
center := Vec2{0.0, 0.0}
previous := Vec2{radiusX, 0.0}
for theta := twoPi / float32(numSlices); !FloatEqual(theta, twoPi); theta = Clamp(theta+twoPi/float32(numSlices), 0.0, twoPi) {
sin, cos := math.Sincos(float64(theta))
curr := Vec2{float32(cos) * radiusX, float32(sin) * radiusY}
circlePoints = append(circlePoints, center, previous, curr)
previous = curr
}
// Now add the final point at theta=2pi
circlePoints = append(circlePoints, center, previous, Vec2{radiusX, 0.0})
return circlePoints
}
// Generates a 2-triangle rectangle for use with GL_TRIANGLES. The width and height should use GL's proportions (that is, where a width of 1.0
// is equivalent to half of the width of the render target); however, the y-coordinates grow downwards, not upwards. That is, it
// assumes you want the origin of the rectangle with the top-left corner at (0.0,0.0).
//
// Keep in mind that GL's coordinate system is proportional, so width=height will not result in a square unless your viewport is square.
// If you want to maintain proportionality regardless of screen size, use the results of w,h := ScreenToGLCoordsf(absoluteWidth, absoluteHeight, screenWidth, screenHeight);
// w,h=w+1,h-1 in the call to this function. (The w+1,h-1 step maps the coordinates to start at 0.0 rather than -1.0)
func Rect(width, height float32) []Vec2 {
return []Vec2{
{0.0, 0.0},
{0.0, -height},
{width, -height},
{0.0, 0.0},
{width, -height},
{width, 0.0},
}
}
func QuadraticBezierCurve2D(t float32, cPoint1, cPoint2, cPoint3 Vec2) Vec2 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1.0 - t) * (1.0 - t)).Add(cPoint2.Mul(2 * (1 - t) * t)).Add(cPoint3.Mul(t * t))
}
func QuadraticBezierCurve3D(t float32, cPoint1, cPoint2, cPoint3 Vec3) Vec3 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1.0 - t) * (1.0 - t)).Add(cPoint2.Mul(2 * (1 - t) * t)).Add(cPoint3.Mul(t * t))
}
func CubicBezierCurve2D(t float32, cPoint1, cPoint2, cPoint3, cPoint4 Vec2) Vec2 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1 - t) * (1 - t) * (1 - t)).Add(cPoint2.Mul(3 * (1 - t) * (1 - t) * t)).Add(cPoint3.Mul(3 * (1 - t) * t * t)).Add(cPoint4.Mul(t * t * t))
}
func CubicBezierCurve3D(t float32, cPoint1, cPoint2, cPoint3, cPoint4 Vec3) Vec3 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1 - t) * (1 - t) * (1 - t)).Add(cPoint2.Mul(3 * (1 - t) * (1 - t) * t)).Add(cPoint3.Mul(3 * (1 - t) * t * t)).Add(cPoint4.Mul(t * t * t))
}
// Returns the point at point t along an n-control point Bezier curve
//
// t must be in the range 0.0 and 1.0 or this function will panic. Consider [0.0,1.0] to be similar to a percentage,
// 0.0 is first control point, and the point at 1.0 is the last control point. Any point in between is how far along the path you are between 0 and 1.
//
// This function is not sensitive to the coordinate system of the control points. It will correctly interpolate regardless of whether they're in screen coords,
// gl coords, or something else entirely
func BezierCurve2D(t float32, cPoints []Vec2) Vec2 {
if t < 0.0 || t > 1.0 {
panic("Input to bezier has t not in range [0,1]. If you think this is a precision error, use mathgl.Clamp[f|d] before calling this function")
}
n := len(cPoints) - 1
point := cPoints[0].Mul(float32(math.Pow(float64(1.0-t), float64(n))))
for i := 1; i <= n; i++ {
point = point.Add(cPoints[i].Mul(float32(float64(choose(n, i)) * math.Pow(float64(1-t), float64(n-i)) * math.Pow(float64(t), float64(i))))) // P += P_i * nCi * (1-t)^(n-i) * t^i
}
return point
}
// Same as the 2D version, except the line is in 3D space
func BezierCurve3D(t float32, cPoints []Vec3) Vec3 {
if t < 0.0 || t > 1.0 {
panic("Input to bezier has t not in range [0,1]. If you think this is a precision error, use mathgl.Clamp[f|d] before calling this function")
}
n := len(cPoints) - 1
point := cPoints[0].Mul(float32(math.Pow(float64(1.0-t), float64(n))))
for i := 1; i <= n; i++ {
point = point.Add(cPoints[i].Mul(float32(float64(choose(n, i)) * math.Pow(float64(1-t), float64(n-i)) * math.Pow(float64(t), float64(i))))) // P += P_i * nCi * (1-t)^(n-i) * t^i
}
return point
}
// Generates a bezier curve with controlPoints cPoints. The numPoints argument
// determines how many "samples" it makes along the line. For instance, a
// call to this with numPoints 2 will have exactly two points: the start and end points
// For any points above 2 it will divide it into numPoints-1 chunks (which means it will generate numPoints-2 vertices other than the beginning and end).
// So for 3 points it will divide it in half, 4 points into thirds, and so on.
//
// This is likely to get rather expensive for anything over perhaps a cubic curve.
func MakeBezierCurve2D(numPoints int, cPoints []Vec2) (line []Vec2) {
line = make([]Vec2, numPoints)
if numPoints == 0 {
return
} else if numPoints == 1 {
line[0] = cPoints[0]
return
} else if numPoints == 2 {
line[0] = cPoints[0]
line[1] = cPoints[len(cPoints)-1]
return
}
line[0] = cPoints[0]
for i := 1; i < numPoints-1; i++ {
line[i] = BezierCurve2D(Clamp(float32(i)/float32(numPoints-1), 0.0, 1.0), cPoints)
}
line[numPoints-1] = cPoints[len(cPoints)-1]
return
}
// Same as the 2D version, except with the line in 3D space
func MakeBezierCurve3D(numPoints int, cPoints []Vec3) (line []Vec3) {
line = make([]Vec3, numPoints)
if numPoints == 0 {
return
} else if numPoints == 1 {
line[0] = cPoints[0]
return
} else if numPoints == 2 {
line[0] = cPoints[0]
line[1] = cPoints[len(cPoints)-1]
return
}
line[0] = cPoints[0]
for i := 1; i < numPoints-1; i++ {
line[i] = BezierCurve3D(Clamp(float32(i)/float32(numPoints-1), 0.0, 1.0), cPoints)
}
line[numPoints-1] = cPoints[len(cPoints)-1]
return
}
// Creates a 2-dimensional Bezier surface of arbitrary degree in 3D Space
// Like the curve functions, if u or v are not in the range [0.0,1.0] the function will panic, use Clamp[f|d]
// to ensure it is correct.
//
// The control point matrix must not be jagged, or this will end up panicking from an index out of bounds exception
func BezierSurface(u, v float32, cPoints [][]Vec3) Vec3 {
if u < 0.0 || u > 1.0 || v < 1.0 || v > 1.0 {
panic("u or v not in range [0.0,1.0] in BezierSurface")
}
n := len(cPoints) - 1
m := len(cPoints[0]) - 1
point := cPoints[0][0].Mul(float32(math.Pow(float64(1.0-u), float64(n)) * math.Pow(float64(1.0-v), float64(m))))
for i := 0; i <= n; i++ {
for j := 0; j <= m; j++ {
if i == 0 && j == 0 {
continue
}
point = point.Add(cPoints[i][j].Mul(float32(float64(choose(n, i)) * math.Pow(float64(u), float64(i)) * math.Pow(float64(1.0-u), float64(n-i)) * float64(choose(m, j)) * math.Pow(float64(v), float64(j)) * math.Pow(float64(1.0-v), float64(m-j)))))
}
}
return point
}
// Does interpolation over a spline of several bezier curves. Each bezier curve must have a finite range,
// though the spline may be disjoint. The bezier curves are not required to be in any particular order.
//
// If t is out of the range of all given curves, this function will panic
func BezierSplineInterpolate2D(t float32, ranges [][2]float32, cPoints [][]Vec2) Vec2 {
if len(ranges) != len(cPoints) {
panic("Each bezier curve needs a range")
}
for i, curveRange := range ranges {
if t >= curveRange[0] && t <= curveRange[1] {
return BezierCurve2D((t-curveRange[0])/(curveRange[1]-curveRange[0]), cPoints[i])
}
}
panic("t is out of the range of all bezier curves in this spline")
}
// Does interpolation over a spline of several bezier curves. Each bezier curve must have a finite range,
// though the spline may be disjoint. The bezier curves are not required to be in any particular order.
//
// If t is out of the range of all given curves, this function will panic
func BezierSplineInterpolate3D(t float32, ranges [][2]float32, cPoints [][]Vec3) Vec3 {
if len(ranges) != len(cPoints) {
panic("Each bezier curve needs a range")
}
for i, curveRange := range ranges {
if t >= curveRange[0] && t <= curveRange[1] {
return BezierCurve3D((t-curveRange[0])/(curveRange[1]-curveRange[0]), cPoints[i])
}
}
panic("t is out of the range of all bezier curves in this spline")
}
// Reticulates ALL the Splines
//
// For the overly serious: the function is just for fun. It does nothing except prints a Maxis reference. Technically you could "reticulate splines"
// by joining a bunch of splines together, but that ruins the joke.
func ReticulateSplines(ranges [][][2]float32, cPoints [][][]Vec2, withLlamas bool) {
if !withLlamas {
fmt.Println("You can't reticulate splines without llamas, silly.")
} else {
fmt.Println("Actually, you can't even reticulate splines WITH llamas")
}
}
// Transform from pixel coordinates to GL coordinates.
//
// This assumes that your pixel coordinate system considers its origin to be in the top left corner (GL's is in the bottom left).
// The coordinates x and y may be out of the range [0,screenWidth-1] and [0,screeneHeight-1].
//
// GL's coordinate system maps [screenWidth-1,0] to [1.0,1.0] and [0,screenHeight-1] to [-1.0,-1.0]. If x and y are out of the range, they'll still
// be mapped correctly, just off the screen. (e.g. if y = 2*(screenHeight-1) you'll get -3.0 for yOut)
//
// This is similar to Unproject, except for 2D cases and much simpler (especially since an inverse may always be found)
func ScreenToGLCoords(x, y int, screenWidth, screenHeight int) (xOut, yOut float32) {
xOut = 2.0*float32(x)/float32(screenWidth-1) - 1.0
yOut = -2.0*float32(y)/float32(screenHeight-1) + 1.0
return
}
// Transform from GL's proportional system to pixel coordinates.
//
// Assumes the pixel coordinate system has its origin in the top left corner. (GL's is in the bottom left)
//
// GL's coordinate system maps [screenWidth-1,0] to [1.0,1.0] and [0,screenHeight-1] to [-1.0,-1.0]. If x and y are out of the range, they'll still
// be mapped correctly, just off the screen. (e.g. if y=-3.0, you'll get 2*(screenHeight-1) for yOut)
//
// This is similar to Project, except for 2D cases and much simpler
func GLToScreenCoords(x, y float32, screenWidth, screenHeight int) (xOut, yOut int) {
xOut = int((x + 1.0) * float32(screenWidth-1) / 2.0)
yOut = int((1.0 - y) * float32(screenHeight-1) / 2.0)
return
}
func choose(n, k int) (result int) {
if k == 0 {
return 1
} else if n == 0 {
return 0
}
result = (n - (k - 1))
for i := 2; i <= k; i++ {
result *= (n - (k - i)) / i
}
return result
}

223
vendor/github.com/go-gl/mathgl/mgl32/transform.go generated vendored Normal file
View File

@@ -0,0 +1,223 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import "math"
// Rotate2D returns a rotation Matrix about a angle in 2-D space. Specifically about the origin.
// It is a 2x2 matrix, if you need a 3x3 for Homogeneous math (e.g. composition with a Translation matrix)
// see HomogRotate2D
func Rotate2D(angle float32) Mat2 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat2{cos, sin, -sin, cos}
}
// Rotate3DX returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the X-axis
//
// Where c is cos(angle) and s is sin(angle)
// [1 0 0]
// [0 c -s]
// [0 s c ]
func Rotate3DX(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{1, 0, 0, 0, cos, sin, 0, -sin, cos}
}
// Rotate3DY returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the Y-axis
//
// Where c is cos(angle) and s is sin(angle)
// [c 0 s]
// [0 1 0]
// [s 0 c ]
func Rotate3DY(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{cos, 0, -sin, 0, 1, 0, sin, 0, cos}
}
// Rotate3DZ returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the Z-axis
//
// Where c is cos(angle) and s is sin(angle)
// [c -s 0]
// [s c 0]
// [0 0 1 ]
func Rotate3DZ(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}
// Translate2D returns a homogeneous (3x3 for 2D-space) Translation matrix that moves a point by Tx units in the x-direction and Ty units in the y-direction
//
// [[1, 0, Tx]]
// [[0, 1, Ty]]
// [[0, 0, 1 ]]
func Translate2D(Tx, Ty float32) Mat3 {
return Mat3{1, 0, 0, 0, 1, 0, float32(Tx), float32(Ty), 1}
}
// Translate3D returns a homogeneous (4x4 for 3D-space) Translation matrix that moves a point by Tx units in the x-direction, Ty units in the y-direction,
// and Tz units in the z-direction
//
// [[1, 0, 0, Tx]]
// [[0, 1, 0, Ty]]
// [[0, 0, 1, Tz]]
// [[0, 0, 0, 1 ]]
func Translate3D(Tx, Ty, Tz float32) Mat4 {
return Mat4{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, float32(Tx), float32(Ty), float32(Tz), 1}
}
// Same as Rotate2D, except homogeneous (3x3 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate2D(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}
// Same as Rotate3DX, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DX(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat4{1, 0, 0, 0, 0, cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1}
}
// Same as Rotate3DY, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DY(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat4{cos, 0, -sin, 0, 0, 1, 0, 0, sin, 0, cos, 0, 0, 0, 0, 1}
}
// Same as Rotate3DZ, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DZ(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat4{cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
}
// Scale3D creates a homogeneous 3D scaling matrix
// [[ scaleX, 0 , 0 , 0 ]]
// [[ 0 , scaleY, 0 , 0 ]]
// [[ 0 , 0 , scaleZ, 0 ]]
// [[ 0 , 0 , 0 , 1 ]]
func Scale3D(scaleX, scaleY, scaleZ float32) Mat4 {
return Mat4{float32(scaleX), 0, 0, 0, 0, float32(scaleY), 0, 0, 0, 0, float32(scaleZ), 0, 0, 0, 0, 1}
}
// Scale2D creates a homogeneous 2D scaling matrix
// [[ scaleX, 0 , 0 ]]
// [[ 0 , scaleY, 0 ]]
// [[ 0 , 0 , 1 ]]
func Scale2D(scaleX, scaleY float32) Mat3 {
return Mat3{float32(scaleX), 0, 0, 0, float32(scaleY), 0, 0, 0, 1}
}
// ShearX2D creates a homogeneous 2D shear matrix along the X-axis
func ShearX2D(shear float32) Mat3 {
return Mat3{1, 0, 0, float32(shear), 1, 0, 0, 0, 1}
}
// ShearY2D creates a homogeneous 2D shear matrix along the Y-axis
func ShearY2D(shear float32) Mat3 {
return Mat3{1, float32(shear), 0, 0, 1, 0, 0, 0, 1}
}
// ShearX3D creates a homogeneous 3D shear matrix along the X-axis
func ShearX3D(shearY, shearZ float32) Mat4 {
return Mat4{1, float32(shearY), float32(shearZ), 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
}
// ShearY3D creates a homogeneous 3D shear matrix along the Y-axis
func ShearY3D(shearX, shearZ float32) Mat4 {
return Mat4{1, 0, 0, 0, float32(shearX), 1, float32(shearZ), 0, 0, 0, 1, 0, 0, 0, 0, 1}
}
// ShearZ3D creates a homogeneous 3D shear matrix along the Z-axis
func ShearZ3D(shearX, shearY float32) Mat4 {
return Mat4{1, 0, 0, 0, 0, 1, 0, 0, float32(shearX), float32(shearY), 1, 0, 0, 0, 0, 1}
}
// HomogRotate3D creates a 3D rotation Matrix that rotates by (radian) angle about some arbitrary axis given by a Vector.
// It produces a homogeneous matrix (4x4)
//
// Where c is cos(angle) and s is sin(angle), and x, y, and z are the first, second, and third elements of the axis vector (respectively):
//
// [[ x^2(1-c)+c, xy(1-c)-zs, xz(1-c)+ys, 0 ]]
// [[ xy(1-c)+zs, y^2(1-c)+c, yz(1-c)-xs, 0 ]]
// [[ xz(1-c)-ys, yz(1-c)+xs, z^2(1-c)+c, 0 ]]
// [[ 0 , 0 , 0 , 1 ]]
func HomogRotate3D(angle float32, axis Vec3) Mat4 {
x, y, z := axis[0], axis[1], axis[2]
s, c := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
k := 1 - c
return Mat4{x*x*k + c, x*y*k + z*s, x*z*k - y*s, 0, x*y*k - z*s, y*y*k + c, y*z*k + x*s, 0, x*z*k + y*s, y*z*k - x*s, z*z*k + c, 0, 0, 0, 0, 1}
}
// Extracts the 3d scaling from a homogeneous matrix
func Extract3DScale(m Mat4) (x, y, z float32) {
return float32(math.Sqrt(float64(m[0]*m[0] + m[1]*m[1] + m[2]*m[2]))),
float32(math.Sqrt(float64(m[4]*m[4] + m[5]*m[5] + m[6]*m[6]))),
float32(math.Sqrt(float64(m[8]*m[8] + m[9]*m[9] + m[10]*m[10])))
}
// Extracts the maximum scaling from a homogeneous matrix
func ExtractMaxScale(m Mat4) float32 {
scaleX := float64(m[0]*m[0] + m[1]*m[1] + m[2]*m[2])
scaleY := float64(m[4]*m[4] + m[5]*m[5] + m[6]*m[6])
scaleZ := float64(m[8]*m[8] + m[9]*m[9] + m[10]*m[10])
return float32(math.Sqrt(math.Max(scaleX, math.Max(scaleY, scaleZ))))
}
// Calculates the Normal of the Matrix (aka the inverse transpose)
func Mat4Normal(m Mat4) Mat3 {
n := m.Inv().Transpose()
return Mat3{n[0], n[1], n[2], n[4], n[5], n[6], n[8], n[9], n[10]}
}
// Multiplies a 3D vector by a transformation given by
// the homogeneous 4D matrix m, applying any translation.
// If this transformation is non-affine, it will project this
// vector onto the plane w=1 before returning the result.
//
// This is similar to saying you're transforming and projecting a point.
//
// This is effectively equivalent to the GLSL code
// vec4 r = (m * vec4(v,1.));
// r = r/r.w;
// vec3 newV = r.xyz;
func TransformCoordinate(v Vec3, m Mat4) Vec3 {
t := v.Vec4(1)
t = m.Mul4x1(t)
t = t.Mul(1 / t[3])
return t.Vec3()
}
// Multiplies a 3D vector by a transformation given by
// the homogeneous 4D matrix m, NOT applying any translations.
//
// This is similar to saying you're applying a transformation
// to a direction or normal. Rotation still applies (as does scaling),
// but translating a direction or normal is meaningless.
//
// This is effectively equivalent to the GLSL code
// vec4 r = (m * vec4(v,0.));
// vec3 newV = r.xyz
func TransformNormal(v Vec3, m Mat4) Vec3 {
t := v.Vec4(0)
t = m.Mul4x1(t)
return t.Vec3()
}

142
vendor/github.com/go-gl/mathgl/mgl32/util.go generated vendored Normal file
View File

@@ -0,0 +1,142 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run codegen.go -template vector.tmpl -output vector.go
//go:generate go run codegen.go -template matrix.tmpl -output matrix.go
//go:generate go run codegen.go -mgl64
package mgl32
import (
"math"
)
// Epsilon is some tiny value that determines how precisely equal we want our floats to be
// This is exported and left as a variable in case you want to change the default threshold for the
// purposes of certain methods (e.g. Unproject uses the default epsilon when determining
// if the determinant is "close enough" to zero to mean there's no inverse).
//
// This is, obviously, not mutex protected so be **absolutely sure** that no functions using Epsilon
// are being executed when you change this.
var Epsilon float32 = 1e-10
// A direct copy of the math package's Abs. This is here for the mgl32
// package, to prevent rampant type conversions during equality tests.
func Abs(a float32) float32 {
if a < 0 {
return -a
} else if a == 0 {
return 0
}
return a
}
// FloatEqual is a safe utility function to compare floats.
// It's Taken from http://floating-point-gui.de/errors/comparison/
//
// It is slightly altered to not call Abs when not needed.
func FloatEqual(a, b float32) bool {
return FloatEqualThreshold(a, b, Epsilon)
}
// FloatEqualFunc is a utility closure that will generate a function that
// always approximately compares floats like FloatEqualThreshold with a different
// threshold.
func FloatEqualFunc(epsilon float32) func(float32, float32) bool {
return func(a, b float32) bool {
return FloatEqualThreshold(a, b, epsilon)
}
}
var (
MinNormal = float32(1.1754943508222875e-38) // 1 / 2**(127 - 1)
MinValue = float32(math.SmallestNonzeroFloat32)
MaxValue = float32(math.MaxFloat32)
InfPos = float32(math.Inf(1))
InfNeg = float32(math.Inf(-1))
NaN = float32(math.NaN())
)
// FloatEqualThreshold is a utility function to compare floats.
// It's Taken from http://floating-point-gui.de/errors/comparison/
//
// It is slightly altered to not call Abs when not needed.
//
// This differs from FloatEqual in that it lets you pass in your comparison threshold, so that you can adjust the comparison value to your specific needs
func FloatEqualThreshold(a, b, epsilon float32) bool {
if a == b { // Handles the case of inf or shortcuts the loop when no significant error has accumulated
return true
}
diff := Abs(a - b)
if a*b == 0 || diff < MinNormal { // If a or b are 0 or both are extremely close to it
return diff < epsilon*epsilon
}
// Else compare difference
return diff/(Abs(a)+Abs(b)) < epsilon
}
// Clamp takes in a value and two thresholds. If the value is smaller than the low
// threshold, it returns the low threshold. If it's bigger than the high threshold
// it returns the high threshold. Otherwise it returns the value.
//
// Useful to prevent some functions from freaking out because a value was
// teeeeechnically out of range.
func Clamp(a, low, high float32) float32 {
if a < low {
return low
} else if a > high {
return high
}
return a
}
// ClampFunc generates a closure that returns its parameter
// clamped to the range [low,high].
func ClampFunc(low, high float32) func(float32) float32 {
return func(a float32) float32 {
return Clamp(a, low, high)
}
}
/* The IsClamped functions use strict equality (meaning: not the FloatEqual function)
there shouldn't be any major issues with this since clamp is often used to fix minor errors*/
// Checks if a is clamped between low and high as if
// Clamp(a, low, high) had been called.
//
// In most cases it's probably better to just call Clamp
// without checking this since it's relatively cheap.
func IsClamped(a, low, high float32) bool {
return a >= low && a <= high
}
// If a > b, then a will be set to the value of b.
func SetMin(a, b *float32) {
if *b < *a {
*a = *b
}
}
// If a < b, then a will be set to the value of b.
func SetMax(a, b *float32) {
if *a < *b {
*a = *b
}
}
// Round shortens a float32 value to a specified precision (number of digits after the decimal point)
// with "round half up" tie-braking rule. Half-way values (23.5) are always rounded up (24).
func Round(v float32, precision int) float32 {
p := float64(precision)
t := float64(v) * math.Pow(10, p)
if t > 0 {
return float32(math.Floor(t+0.5) / math.Pow(10, p))
}
return float32(math.Ceil(t-0.5) / math.Pow(10, p))
}

350
vendor/github.com/go-gl/mathgl/mgl32/vecn.go generated vendored Normal file
View File

@@ -0,0 +1,350 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// A vector of N elements backed by a slice
//
// As with MatMxN, this is not for hardcore linear algebra with large dimensions. Use github.com/gonum/matrix
// or something like BLAS/LAPACK for that. This is for corner cases in 3D math where you require
// something a little bigger that 4D, but still relatively small.
//
// This VecN uses several sync.Pool objects as a memory pool. The rule is that for any sized vector, the backing slice
// has CAPACITY (not length) of 2^p where p is Ceil(log_2(N)) -- or in other words, rounding up the base-2
// log of the size of the vector. E.G. a VecN of size 17 will have a backing slice of Cap 32.
type VecN struct {
vec []float32
}
// Creates a new vector with a backing slice filled with the contents
// of initial. It is NOT backed by initial, but rather a slice with cap
// 2^p where p is Ceil(log_2(len(initial))), with the data from initial copied into
// it.
func NewVecNFromData(initial []float32) *VecN {
if initial == nil {
return &VecN{}
}
var internal []float32
if shouldPool {
internal = grabFromPool(len(initial))
} else {
internal = make([]float32, len(initial))
}
copy(internal, initial)
return &VecN{vec: internal}
}
// Creates a new vector with a backing slice of
// 2^p where p = Ceil(log_2(n))
func NewVecN(n int) *VecN {
if shouldPool {
return &VecN{vec: grabFromPool(n)}
} else {
return &VecN{vec: make([]float32, n)}
}
}
// Returns the raw slice backing the VecN
//
// This may be sent back to the memory pool at any time
// and you aren't advised to rely on this value
func (vn VecN) Raw() []float32 {
return vn.vec
}
// Gets the element at index i from the vector.
// This does not bounds check, and will panic if i is
// out of range.
func (vn VecN) Get(i int) float32 {
return vn.vec[i]
}
func (vn *VecN) Set(i int, val float32) {
vn.vec[i] = val
}
// Sends the allocated memory through the callback if it exists
func (vn *VecN) destroy() {
if vn == nil || vn.vec == nil {
return
}
if shouldPool {
returnToPool(vn.vec)
}
vn.vec = nil
}
// Resizes the underlying slice to the desired amount, reallocating or retrieving from the pool
// if necessary. The values after a Resize cannot be expected to be related to the values before a Resize.
//
// If the caller is a nil pointer, this returns a value as if NewVecN(n) had been called,
// otherwise it simply returns the caller.
func (vn *VecN) Resize(n int) *VecN {
if vn == nil {
return NewVecN(n)
}
if n <= cap(vn.vec) {
if vn.vec != nil {
vn.vec = vn.vec[:n]
} else {
vn.vec = []float32{}
}
return vn
}
if shouldPool && vn.vec != nil {
returnToPool(vn.vec)
}
*vn = (*NewVecN(n))
return vn
}
// Sets the vector's backing slice to the given
// new one.
func (vn *VecN) SetBackingSlice(newSlice []float32) {
vn.vec = newSlice
}
// Return the len of the vector's underlying slice.
// This is not titled Len because it conflicts the package's
// convention of calling the Norm the Len.
func (vn *VecN) Size() int {
return len(vn.vec)
}
// Returns the cap of the vector's underlying slice.
func (vn *VecN) Cap() int {
return cap(vn.vec)
}
// Sets the vector's size to n and zeroes out the vector.
// If n is bigger than the vector's size, it will realloc.
func (vn *VecN) Zero(n int) {
vn.Resize(n)
for i := range vn.vec {
vn.vec[i] = 0
}
}
// Adds vn and addend, storing the result in dst.
// If dst does not have sufficient size it will be resized
// Dst may be one of the other arguments. If dst is nil, it will be allocated.
// The value returned is dst, for easier method chaining
//
// If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size())
// elements.
func (vn *VecN) Add(dst *VecN, subtrahend *VecN) *VecN {
if vn == nil || subtrahend == nil {
return nil
}
size := intMin(len(vn.vec), len(subtrahend.vec))
dst = dst.Resize(size)
for i := 0; i < size; i++ {
dst.vec[i] = vn.vec[i] + subtrahend.vec[i]
}
return dst
}
// Subtracts addend from vn, storing the result in dst.
// If dst does not have sufficient size it will be resized
// Dst may be one of the other arguments. If dst is nil, it will be allocated.
// The value returned is dst, for easier method chaining
//
// If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size())
// elements.
func (vn *VecN) Sub(dst *VecN, addend *VecN) *VecN {
if vn == nil || addend == nil {
return nil
}
size := intMin(len(vn.vec), len(addend.vec))
dst = dst.Resize(size)
for i := 0; i < size; i++ {
dst.vec[i] = vn.vec[i] - addend.vec[i]
}
return dst
}
// Takes the binary cross product of vn and other, and stores it in dst.
// If either vn or other are not of size 3 this function will panic
//
// If dst is not of sufficient size, or is nil, a new slice is allocated.
// Dst is permitted to be one of the other arguments
func (vn *VecN) Cross(dst *VecN, other *VecN) *VecN {
if vn == nil || other == nil {
return nil
}
if len(vn.vec) != 3 || len(other.vec) != 3 {
panic("Cannot take binary cross product of non-3D elements (7D cross product not implemented)")
}
dst = dst.Resize(3)
dst.vec[0], dst.vec[1], dst.vec[2] = vn.vec[1]*other.vec[2]-vn.vec[2]*other.vec[1], vn.vec[2]*other.vec[0]-vn.vec[0]*other.vec[2], vn.vec[0]*other.vec[1]-vn.vec[1]*other.vec[0]
return dst
}
func intMin(a, b int) int {
if a < b {
return a
}
return b
}
// Computes the dot product of two VecNs, if
// the two vectors are not of the same length -- this
// will return NaN.
func (vn *VecN) Dot(other *VecN) float32 {
if vn == nil || other == nil || len(vn.vec) != len(other.vec) {
return float32(math.NaN())
}
var result float32 = 0.0
for i, el := range vn.vec {
result += el * other.vec[i]
}
return result
}
// Computes the vector length (also called the Norm) of the
// vector. Equivalent to math.Sqrt(vn.Dot(vn)) with the appropriate
// type conversions.
//
// If vn is nil, this returns NaN
func (vn *VecN) Len() float32 {
if vn == nil {
return float32(math.NaN())
}
if len(vn.vec) == 0 {
return 0
}
return float32(math.Sqrt(float64(vn.Dot(vn))))
}
// Normalizes the vector and stores the result in dst, which
// will be returned. Dst will be appropraitely resized to the
// size of vn.
//
// The destination can be vn itself and nothing will go wrong.
//
// This is equivalent to vn.Mul(dst, 1/vn.Len())
func (vn *VecN) Normalize(dst *VecN) *VecN {
if vn == nil {
return nil
}
return vn.Mul(dst, 1/vn.Len())
}
// Multiplied the vector by some scalar value and stores the result in dst, which
// will be returned. Dst will be appropraitely resized to the
// size of vn.
//
// The destination can be vn itself and nothing will go wrong.
func (vn *VecN) Mul(dst *VecN, c float32) *VecN {
if vn == nil {
return nil
}
dst = dst.Resize(len(vn.vec))
for i, el := range vn.vec {
dst.vec[i] = el * c
}
return dst
}
// Performs the vector outer product between vn and v2.
// The outer product is like a "reverse" dot product. Where the dot product
// aligns both vectors with the "sized" part facing "inward" (Vec3*Vec3=Mat1x3*Mat3x1=Mat1x1=Scalar).
// The outer product multiplied them with it facing "outward"
// (Vec3*Vec3=Mat3x1*Mat1x3=Mat3x3).
//
// The matrix dst will be Reshaped to the correct size, if vn or v2 are nil,
// this returns nil.
func (vn *VecN) OuterProd(dst *MatMxN, v2 *VecN) *MatMxN {
if vn == nil || v2 == nil {
return nil
}
dst = dst.Reshape(len(vn.vec), len(v2.vec))
for c, el1 := range v2.vec {
for r, el2 := range vn.vec {
dst.Set(r, c, el1*el2)
}
}
return dst
}
func (vn *VecN) ApproxEqual(vn2 *VecN) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !FloatEqual(el, vn2.vec[i]) {
return false
}
}
return true
}
func (vn *VecN) ApproxEqualThreshold(vn2 *VecN, epsilon float32) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !FloatEqualThreshold(el, vn2.vec[i], epsilon) {
return false
}
}
return true
}
func (vn *VecN) ApproxEqualFunc(vn2 *VecN, comp func(float32, float32) bool) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !comp(el, vn2.vec[i]) {
return false
}
}
return true
}
func (vn *VecN) Vec2() Vec2 {
raw := vn.Raw()
return Vec2{raw[0], raw[1]}
}
func (vn *VecN) Vec3() Vec3 {
raw := vn.Raw()
return Vec3{raw[0], raw[1], raw[2]}
}
func (vn *VecN) Vec4() Vec4 {
raw := vn.Raw()
return Vec4{raw[0], raw[1], raw[2], raw[3]}
}

562
vendor/github.com/go-gl/mathgl/mgl32/vector.go generated vendored Normal file
View File

@@ -0,0 +1,562 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file is generated by codegen.go; DO NOT EDIT
// Edit vector.tmpl and run "go generate" to make changes.
package mgl32
import (
"golang.org/x/image/math/f32"
"math"
)
type Vec2 f32.Vec2
type Vec3 f32.Vec3
type Vec4 f32.Vec4
func (v Vec2) Vec3(z float32) Vec3 {
return Vec3{v[0], v[1], z}
}
func (v Vec2) Vec4(z, w float32) Vec4 {
return Vec4{v[0], v[1], z, w}
}
func (v Vec3) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec3) Vec4(w float32) Vec4 {
return Vec4{v[0], v[1], v[2], w}
}
func (v Vec4) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec4) Vec3() Vec3 {
return Vec3{v[0], v[1], v[2]}
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec2) Elem() (x, y float32) {
return v[0], v[1]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec3) Elem() (x, y, z float32) {
return v[0], v[1], v[2]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec4) Elem() (x, y, z, w float32) {
return v[0], v[1], v[2], v[3]
}
// The vector cross product is an operation only defined on 3D vectors. It is equivalent to
// Vec3{v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}.
// Another interpretation is that it's the vector whose magnitude is |v1||v2|sin(theta)
// where theta is the angle between v1 and v2.
//
// The cross product is most often used for finding surface normals. The cross product of vectors
// will generate a vector that is perpendicular to the plane they form.
//
// Technically, a generalized cross product exists as an "(N-1)ary" operation
// (that is, the 4D cross product requires 3 4D vectors). But the binary
// 3D (and 7D) cross product is the most important. It can be considered
// the area of a parallelogram with sides v1 and v2.
//
// Like the dot product, the cross product is roughly a measure of directionality.
// Two normalized perpendicular vectors will return a vector with a magnitude of
// 1.0 or -1.0 and two parallel vectors will return a vector with magnitude 0.0.
// The cross product is "anticommutative" meaning v1.Cross(v2) = -v2.Cross(v1),
// this property can be useful to know when finding normals,
// as taking the wrong cross product can lead to the opposite normal of the one you want.
func (v1 Vec3) Cross(v2 Vec3) Vec3 {
return Vec3{v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}
}
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 Vec2) Add(v2 Vec2) Vec2 {
return Vec2{v1[0] + v2[0], v1[1] + v2[1]}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 Vec2) Sub(v2 Vec2) Vec2 {
return Vec2{v1[0] - v2[0], v1[1] - v2[1]}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 Vec2) Mul(c float32) Vec2 {
return Vec2{v1[0] * c, v1[1] * c}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 Vec2) Dot(v2 Vec2) float32 {
return v1[0]*v2[0] + v1[1]*v2[1]
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 Vec2) Len() float32 {
return float32(math.Hypot(float64(v1[0]), float64(v1[1])))
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 Vec2) Normalize() Vec2 {
l := 1.0 / v1.Len()
return Vec2{v1[0] * l, v1[1] * l}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 Vec2) ApproxEqual(v2 Vec2) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 Vec2) ApproxEqualThreshold(v2 Vec2, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 Vec2) ApproxFuncEqual(v2 Vec2, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec2) X() float32 {
return v[0]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec2) Y() float32 {
return v[1]
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 2x2 matrix. E.G. a Vec2 * Vec2 = Mat2.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec2) OuterProd2(v2 Vec2) Mat2 {
return Mat2{v1[0] * v2[0], v1[1] * v2[0], v1[0] * v2[1], v1[1] * v2[1]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 2x3 matrix. E.G. a Vec2 * Vec3 = Mat2x3.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec2) OuterProd3(v2 Vec3) Mat2x3 {
return Mat2x3{v1[0] * v2[0], v1[1] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[0] * v2[2], v1[1] * v2[2]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 2x4 matrix. E.G. a Vec2 * Vec4 = Mat2x4.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec2) OuterProd4(v2 Vec4) Mat2x4 {
return Mat2x4{v1[0] * v2[0], v1[1] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[0] * v2[3], v1[1] * v2[3]}
}
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 Vec3) Add(v2 Vec3) Vec3 {
return Vec3{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 Vec3) Sub(v2 Vec3) Vec3 {
return Vec3{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 Vec3) Mul(c float32) Vec3 {
return Vec3{v1[0] * c, v1[1] * c, v1[2] * c}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 Vec3) Dot(v2 Vec3) float32 {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 Vec3) Len() float32 {
return float32(math.Sqrt(float64(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2])))
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 Vec3) Normalize() Vec3 {
l := 1.0 / v1.Len()
return Vec3{v1[0] * l, v1[1] * l, v1[2] * l}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 Vec3) ApproxEqual(v2 Vec3) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 Vec3) ApproxEqualThreshold(v2 Vec3, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 Vec3) ApproxFuncEqual(v2 Vec3, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec3) X() float32 {
return v[0]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec3) Y() float32 {
return v[1]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec3) Z() float32 {
return v[2]
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 3x2 matrix. E.G. a Vec3 * Vec2 = Mat3x2.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec3) OuterProd2(v2 Vec2) Mat3x2 {
return Mat3x2{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 3x3 matrix. E.G. a Vec3 * Vec3 = Mat3.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec3) OuterProd3(v2 Vec3) Mat3 {
return Mat3{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 3x4 matrix. E.G. a Vec3 * Vec4 = Mat3x4.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec3) OuterProd4(v2 Vec4) Mat3x4 {
return Mat3x4{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2], v1[0] * v2[3], v1[1] * v2[3], v1[2] * v2[3]}
}
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 Vec4) Add(v2 Vec4) Vec4 {
return Vec4{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2], v1[3] + v2[3]}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 Vec4) Sub(v2 Vec4) Vec4 {
return Vec4{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2], v1[3] - v2[3]}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 Vec4) Mul(c float32) Vec4 {
return Vec4{v1[0] * c, v1[1] * c, v1[2] * c, v1[3] * c}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 Vec4) Dot(v2 Vec4) float32 {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 Vec4) Len() float32 {
return float32(math.Sqrt(float64(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2] + v1[3]*v1[3])))
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 Vec4) Normalize() Vec4 {
l := 1.0 / v1.Len()
return Vec4{v1[0] * l, v1[1] * l, v1[2] * l, v1[3] * l}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 Vec4) ApproxEqual(v2 Vec4) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 Vec4) ApproxEqualThreshold(v2 Vec4, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 Vec4) ApproxFuncEqual(v2 Vec4, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) X() float32 {
return v[0]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) Y() float32 {
return v[1]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) Z() float32 {
return v[2]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) W() float32 {
return v[3]
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 4x2 matrix. E.G. a Vec4 * Vec2 = Mat4x2.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec4) OuterProd2(v2 Vec2) Mat4x2 {
return Mat4x2{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[3] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[3] * v2[1]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 4x3 matrix. E.G. a Vec4 * Vec3 = Mat4x3.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec4) OuterProd3(v2 Vec3) Mat4x3 {
return Mat4x3{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[3] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[3] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2], v1[3] * v2[2]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 4x4 matrix. E.G. a Vec4 * Vec4 = Mat4.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec4) OuterProd4(v2 Vec4) Mat4 {
return Mat4{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[3] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[3] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2], v1[3] * v2[2], v1[0] * v2[3], v1[1] * v2[3], v1[2] * v2[3], v1[3] * v2[3]}
}

207
vendor/github.com/go-gl/mathgl/mgl32/vector.tmpl generated vendored Normal file
View File

@@ -0,0 +1,207 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// <<.Comment>>
// Edit <<.TemplateName>> and run "go generate" to make changes.
package mgl32
import (
"golang.org/x/image/math/f32"
"math"
)
type Vec2 f32.Vec2
type Vec3 f32.Vec3
type Vec4 f32.Vec4
func (v Vec2) Vec3(z float32) Vec3 {
return Vec3{v[0], v[1], z}
}
func (v Vec2) Vec4(z, w float32) Vec4 {
return Vec4{v[0], v[1], z, w}
}
func (v Vec3) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec3) Vec4(w float32) Vec4 {
return Vec4{v[0], v[1], v[2], w}
}
func (v Vec4) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec4) Vec3() Vec3 {
return Vec3{v[0], v[1], v[2]}
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec2) Elem() (x, y float32) {
return v[0], v[1]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec3) Elem() (x, y, z float32) {
return v[0], v[1], v[2]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec4) Elem() (x, y, z, w float32) {
return v[0], v[1], v[2], v[3]
}
// The vector cross product is an operation only defined on 3D vectors. It is equivalent to
// Vec3{v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}.
// Another interpretation is that it's the vector whose magnitude is |v1||v2|sin(theta)
// where theta is the angle between v1 and v2.
//
// The cross product is most often used for finding surface normals. The cross product of vectors
// will generate a vector that is perpendicular to the plane they form.
//
// Technically, a generalized cross product exists as an "(N-1)ary" operation
// (that is, the 4D cross product requires 3 4D vectors). But the binary
// 3D (and 7D) cross product is the most important. It can be considered
// the area of a parallelogram with sides v1 and v2.
//
// Like the dot product, the cross product is roughly a measure of directionality.
// Two normalized perpendicular vectors will return a vector with a magnitude of
// 1.0 or -1.0 and two parallel vectors will return a vector with magnitude 0.0.
// The cross product is "anticommutative" meaning v1.Cross(v2) = -v2.Cross(v1),
// this property can be useful to know when finding normals,
// as taking the wrong cross product can lead to the opposite normal of the one you want.
func (v1 Vec3) Cross(v2 Vec3) Vec3 {
return Vec3{v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}
}
<</* Common functions for all vectors */>>
<<range $m := enum 2 3 4>>
<<$type := typename $m 1>>
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 <<$type>>) Add(v2 <<$type>>) <<$type>> {
return <<$type>>{<<range $i := iter 0 $m>> v1[<<$i>>] + v2[<<$i>>], <<end>>}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 <<$type>>) Sub(v2 <<$type>>) <<$type>> {
return <<$type>>{<<range $i := iter 0 $m>> v1[<<$i>>] - v2[<<$i>>], <<end>>}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 <<$type>>) Mul(c float32) <<$type>> {
return <<$type>>{<<range $i := iter 0 $m>> v1[<<$i>>] * c, <<end>>}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 <<$type>>) Dot(v2 <<$type>>) float32 {
return <<range $i := iter 0 $m>><<sep "+" $i>> v1[<<$i>>]*v2[<<$i>>] <<end>>
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 <<$type>>) Len() float32 {
<<if eq $m 2 >>
return float32(math.Hypot(float64(v1[0]), float64(v1[1])))
<<else>>
return float32(math.Sqrt(float64(<<repeat $m "v1[%d]*v1[%d]" "+">>)))
<<end>>
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 <<$type>>) Normalize() <<$type>> {
l := 1.0 / v1.Len()
return <<$type>>{<<range $i := iter 0 $m>>v1[<<$i>>] * l,<<end>>}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 <<$type>>) ApproxEqual(v2 <<$type>>) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 <<$type>>) ApproxEqualThreshold(v2 <<$type>>, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 <<$type>>) ApproxFuncEqual(v2 <<$type>>, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
<<range $i := iter 0 $m>>
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v <<$type>>) <<elementname $i>>() float32 {
return v[<<$i>>]
}
<<end>>
<<range $n := enum 2 3 4>>
// Does the vector outer product
// of two vectors. The outer product produces an
// <<$m>>x<<$n>> matrix. E.G. a Vec<<$m>> * Vec<<$n>> = <<typename $m $n>>.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 <<$type>>) OuterProd<<$n>>(v2 <<typename $n 1>>) <<typename $m $n>> {
return <<typename $m $n>>{<<range $i := matiter $m $n>>v1[<<$i.M>>] * v2[<<$i.N>>], <<end>>}
}
<<end>>
<<end>> <</* range $m */>>

24
vendor/github.com/pkg/errors/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

11
vendor/github.com/pkg/errors/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,11 @@
language: go
go_import_path: github.com/pkg/errors
go:
- 1.4.3
- 1.5.4
- 1.6.2
- 1.7.1
- tip
script:
- go test -v ./...

23
vendor/github.com/pkg/errors/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,23 @@
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

52
vendor/github.com/pkg/errors/README.md generated vendored Normal file
View File

@@ -0,0 +1,52 @@
# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors)
Package errors provides simple error handling primitives.
`go get github.com/pkg/errors`
The traditional error handling idiom in Go is roughly akin to
```go
if err != nil {
return err
}
```
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
## Adding context to an error
The errors.Wrap function returns a new error that adds context to the original error. For example
```go
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}
```
## Retrieving the cause of an error
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
```go
type causer interface {
Cause() error
}
```
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
```go
switch err := errors.Cause(err).(type) {
case *MyError:
// handle specifically
default:
// unknown error
}
```
[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
## Contributing
We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.
Before proposing a change, please discuss your change by raising an issue.
## Licence
BSD-2-Clause

32
vendor/github.com/pkg/errors/appveyor.yml generated vendored Normal file
View File

@@ -0,0 +1,32 @@
version: build-{build}.{branch}
clone_folder: C:\gopath\src\github.com\pkg\errors
shallow_clone: true # for startup speed
environment:
GOPATH: C:\gopath
platform:
- x64
# http://www.appveyor.com/docs/installed-software
install:
# some helpful output for debugging builds
- go version
- go env
# pre-installed MinGW at C:\MinGW is 32bit only
# but MSYS2 at C:\msys64 has mingw64
- set PATH=C:\msys64\mingw64\bin;%PATH%
- gcc --version
- g++ --version
build_script:
- go install -v ./...
test_script:
- set PATH=C:\gopath\bin;%PATH%
- go test -v ./...
#artifacts:
# - path: '%GOPATH%\bin\*.exe'
deploy: off

269
vendor/github.com/pkg/errors/errors.go generated vendored Normal file
View File

@@ -0,0 +1,269 @@
// Package errors provides simple error handling primitives.
//
// The traditional error handling idiom in Go is roughly akin to
//
// if err != nil {
// return err
// }
//
// which applied recursively up the call stack results in error reports
// without context or debugging information. The errors package allows
// programmers to add context to the failure path in their code in a way
// that does not destroy the original value of the error.
//
// Adding context to an error
//
// The errors.Wrap function returns a new error that adds context to the
// original error by recording a stack trace at the point Wrap is called,
// and the supplied message. For example
//
// _, err := ioutil.ReadAll(r)
// if err != nil {
// return errors.Wrap(err, "read failed")
// }
//
// If additional control is required the errors.WithStack and errors.WithMessage
// functions destructure errors.Wrap into its component operations of annotating
// an error with a stack trace and an a message, respectively.
//
// Retrieving the cause of an error
//
// Using errors.Wrap constructs a stack of errors, adding context to the
// preceding error. Depending on the nature of the error it may be necessary
// to reverse the operation of errors.Wrap to retrieve the original error
// for inspection. Any error value which implements this interface
//
// type causer interface {
// Cause() error
// }
//
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
// the topmost error which does not implement causer, which is assumed to be
// the original cause. For example:
//
// switch err := errors.Cause(err).(type) {
// case *MyError:
// // handle specifically
// default:
// // unknown error
// }
//
// causer interface is not exported by this package, but is considered a part
// of stable public API.
//
// Formatted printing of errors
//
// All error values returned from this package implement fmt.Formatter and can
// be formatted by the fmt package. The following verbs are supported
//
// %s print the error. If the error has a Cause it will be
// printed recursively
// %v see %s
// %+v extended format. Each Frame of the error's StackTrace will
// be printed in detail.
//
// Retrieving the stack trace of an error or wrapper
//
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
// invoked. This information can be retrieved with the following interface.
//
// type stackTracer interface {
// StackTrace() errors.StackTrace
// }
//
// Where errors.StackTrace is defined as
//
// type StackTrace []Frame
//
// The Frame type represents a call site in the stack trace. Frame supports
// the fmt.Formatter interface that can be used for printing information about
// the stack trace of this error. For example:
//
// if err, ok := err.(stackTracer); ok {
// for _, f := range err.StackTrace() {
// fmt.Printf("%+s:%d", f)
// }
// }
//
// stackTracer interface is not exported by this package, but is considered a part
// of stable public API.
//
// See the documentation for Frame.Format for more details.
package errors
import (
"fmt"
"io"
)
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) error {
return &fundamental{
msg: message,
stack: callers(),
}
}
// Errorf formats according to a format specifier and returns the string
// as a value that satisfies error.
// Errorf also records the stack trace at the point it was called.
func Errorf(format string, args ...interface{}) error {
return &fundamental{
msg: fmt.Sprintf(format, args...),
stack: callers(),
}
}
// fundamental is an error that has a message and a stack, but no caller.
type fundamental struct {
msg string
*stack
}
func (f *fundamental) Error() string { return f.msg }
func (f *fundamental) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, f.msg)
f.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, f.msg)
case 'q':
fmt.Fprintf(s, "%q", f.msg)
}
}
// WithStack annotates err with a stack trace at the point WithStack was called.
// If err is nil, WithStack returns nil.
func WithStack(err error) error {
if err == nil {
return nil
}
return &withStack{
err,
callers(),
}
}
type withStack struct {
error
*stack
}
func (w *withStack) Cause() error { return w.error }
func (w *withStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", w.Cause())
w.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, w.Error())
case 'q':
fmt.Fprintf(s, "%q", w.Error())
}
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: message,
}
return &withStack{
err,
callers(),
}
}
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is call, and the format specifier.
// If err is nil, Wrapf returns nil.
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
return &withStack{
err,
callers(),
}
}
// WithMessage annotates err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessage(err error, message string) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: message,
}
}
type withMessage struct {
cause error
msg string
}
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
func (w *withMessage) Cause() error { return w.cause }
func (w *withMessage) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", w.Cause())
io.WriteString(s, w.msg)
return
}
fallthrough
case 's', 'q':
io.WriteString(s, w.Error())
}
}
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
func Cause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}

178
vendor/github.com/pkg/errors/stack.go generated vendored Normal file
View File

@@ -0,0 +1,178 @@
package errors
import (
"fmt"
"io"
"path"
"runtime"
"strings"
)
// Frame represents a program counter inside a stack frame.
type Frame uintptr
// pc returns the program counter for this frame;
// multiple frames may have the same PC value.
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
// file returns the full path to the file that contains the
// function for this Frame's pc.
func (f Frame) file() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
file, _ := fn.FileLine(f.pc())
return file
}
// line returns the line number of source code of the
// function for this Frame's pc.
func (f Frame) line() int {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return 0
}
_, line := fn.FileLine(f.pc())
return line
}
// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s path of source file relative to the compile time GOPATH
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
switch verb {
case 's':
switch {
case s.Flag('+'):
pc := f.pc()
fn := runtime.FuncForPC(pc)
if fn == nil {
io.WriteString(s, "unknown")
} else {
file, _ := fn.FileLine(pc)
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
}
default:
io.WriteString(s, path.Base(f.file()))
}
case 'd':
fmt.Fprintf(s, "%d", f.line())
case 'n':
name := runtime.FuncForPC(f.pc()).Name()
io.WriteString(s, funcname(name))
case 'v':
f.Format(s, 's')
io.WriteString(s, ":")
f.Format(s, 'd')
}
}
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame
func (st StackTrace) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case s.Flag('+'):
for _, f := range st {
fmt.Fprintf(s, "\n%+v", f)
}
case s.Flag('#'):
fmt.Fprintf(s, "%#v", []Frame(st))
default:
fmt.Fprintf(s, "%v", []Frame(st))
}
case 's':
fmt.Fprintf(s, "%s", []Frame(st))
}
}
// stack represents a stack of program counters.
type stack []uintptr
func (s *stack) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case st.Flag('+'):
for _, pc := range *s {
f := Frame(pc)
fmt.Fprintf(st, "\n%+v", f)
}
}
}
}
func (s *stack) StackTrace() StackTrace {
f := make([]Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = Frame((*s)[i])
}
return f
}
func callers() *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var st stack = pcs[0:n]
return &st
}
// funcname removes the path prefix component of a function's name reported by func.Name().
func funcname(name string) string {
i := strings.LastIndex(name, "/")
name = name[i+1:]
i = strings.Index(name, ".")
return name[i+1:]
}
func trimGOPATH(name, file string) string {
// Here we want to get the source file path relative to the compile time
// GOPATH. As of Go 1.6.x there is no direct way to know the compiled
// GOPATH at runtime, but we can infer the number of path segments in the
// GOPATH. We note that fn.Name() returns the function name qualified by
// the import path, which does not include the GOPATH. Thus we can trim
// segments from the beginning of the file path until the number of path
// separators remaining is one more than the number of path separators in
// the function name. For example, given:
//
// GOPATH /home/user
// file /home/user/src/pkg/sub/file.go
// fn.Name() pkg/sub.Type.Method
//
// We want to produce:
//
// pkg/sub/file.go
//
// From this we can easily see that fn.Name() has one less path separator
// than our desired output. We count separators from the end of the file
// path until it finds two more than in the function name and then move
// one character forward to preserve the initial path segment without a
// leading separator.
const sep = "/"
goal := strings.Count(name, sep) + 2
i := len(file)
for n := 0; n < goal; n++ {
i = strings.LastIndex(file[:i], sep)
if i == -1 {
// not enough separators found, set i so that the slice expression
// below leaves file unmodified
i = -len(sep)
break
}
}
// get back to 0 or trim the leading separator
file = file[i+len(sep):]
return file
}

3
vendor/golang.org/x/image/AUTHORS generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# This source code refers to The Go Authors for copyright purposes.
# The master list of authors is in the main Go distribution,
# visible at http://tip.golang.org/AUTHORS.

3
vendor/golang.org/x/image/CONTRIBUTORS generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# This source code was written by the Go contributors.
# The master list of contributors is in the main Go distribution,
# visible at http://tip.golang.org/CONTRIBUTORS.

Some files were not shown because too many files have changed in this diff Show More