Documentation ¶
Overview ¶
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.
Index ¶
- Constants
- func BlendFunc(src, dst BlendFactor)
- func Bounds(x, y, w, h int)
- func Clear(r, g, b, a float32)
- func Init()
- type Attr
- type AttrFormat
- type AttrType
- type BeginEnder
- type BlendFactor
- type Frame
- type Shader
- type Texture
- func (t *Texture) Begin()
- func (t *Texture) End()
- func (t *Texture) Height() int
- func (t *Texture) ID() uint32
- func (t *Texture) Pixels(x, y, w, h int) []uint8
- func (t *Texture) SetPixels(x, y, w, h int, pixels []uint8)
- func (t *Texture) SetSmooth(smooth bool)
- func (t *Texture) Smooth() bool
- func (t *Texture) Width() int
- type VertexSlice
- func (vs *VertexSlice) Begin()
- func (vs *VertexSlice) Cap() int
- func (vs *VertexSlice) Draw()
- func (vs *VertexSlice) End()
- func (vs *VertexSlice) Len() int
- func (vs *VertexSlice) SetLen(len int)
- func (vs *VertexSlice) SetVertexData(data []float32)
- func (vs *VertexSlice) Slice(i, j int) *VertexSlice
- func (vs *VertexSlice) Stride() int
- func (vs *VertexSlice) VertexData() []float32
- func (vs *VertexSlice) VertexFormat() AttrFormat
Constants ¶
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) )
Here's the list of all blend factors.
Variables ¶
This section is empty.
Functions ¶
func BlendFunc ¶
func BlendFunc(src, dst BlendFactor)
BlendFunc sets the source and destination blend factor.
func Bounds ¶
func Bounds(x, y, w, h int)
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 Clear ¶
func Clear(r, g, b, a float32)
Clear clears the current framebuffer or window with the given color.
func Init ¶
func Init()
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.
Types ¶
type Attr ¶
Attr represents an arbitrary OpenGL attribute, such as a vertex attribute or a shader uniform attribute.
type AttrFormat ¶
type AttrFormat []Attr
AttrFormat defines names and types of OpenGL attributes (vertex format, uniform format, etc.).
Example:
AttrFormat{{"position", Vec2}, {"color", Vec4}, {"texCoord": Vec2}}
func (AttrFormat) Size ¶
func (af AttrFormat) Size() int
Size returns the total size of all attributes of the AttrFormat.
type AttrType ¶
type AttrType int
AttrType represents the type of an OpenGL attribute.
type BeginEnder ¶
type BeginEnder interface { Begin() End() }
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 BlendFactor ¶
type BlendFactor int
BlendFactor represents a source or destination blend factor.
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame is a fixed resolution texture that you can draw on.
func (*Frame) Begin ¶
func (f *Frame) Begin()
Begin binds the Frame. All draw operations will target this Frame until End is called.
func (*Frame) Blit ¶
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.
type Shader ¶
type Shader struct {
// contains filtered or unexported fields
}
Shader is an OpenGL shader program.
func NewShader ¶
func NewShader(vertexFmt, uniformFmt AttrFormat, vertexShader, fragmentShader string) (*Shader, error)
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 (*Shader) Begin ¶
func (s *Shader) Begin()
Begin binds the Shader program. This is necessary before using the Shader.
func (*Shader) End ¶
func (s *Shader) End()
End unbinds the Shader program and restores the previous one.
func (*Shader) SetUniformAttr ¶
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 (*Shader) UniformFormat ¶
func (s *Shader) UniformFormat() AttrFormat
UniformFormat returns the uniform attribute format of this Shader. Do not change it.
func (*Shader) UniformIndex ¶
UniformIndex returns the index position of the named uniform. This is the index within the uniformFmt/Attr array as well as the index within the uniformLoc array.
func (*Shader) VertexFormat ¶
func (s *Shader) VertexFormat() AttrFormat
VertexFormat returns the vertex attribute format of this Shader. Do not change it.
type Texture ¶
type Texture struct {
// contains filtered or unexported fields
}
Texture is an OpenGL texture.
func NewTexture ¶
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 (*Texture) Begin ¶
func (t *Texture) Begin()
Begin binds the Texture. This is necessary before using the Texture.
func (*Texture) End ¶
func (t *Texture) End()
End unbinds the Texture and restores the previous one.
func (*Texture) Pixels ¶
Pixels returns the content of a sub-region of the Texture as an RGBA byte sequence.
func (*Texture) SetPixels ¶
SetPixels sets the content of a sub-region of the Texture. Pixels must be an RGBA byte sequence.
func (*Texture) SetSmooth ¶
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.
type VertexSlice ¶
type VertexSlice struct {
// contains filtered or unexported fields
}
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.
func MakeVertexSlice ¶
func MakeVertexSlice(shader *Shader, len, cap int) *VertexSlice
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 (*VertexSlice) Begin ¶
func (vs *VertexSlice) Begin()
Begin binds the underlying vertex array. Calling this method is necessary before using the VertexSlice.
func (*VertexSlice) Cap ¶
func (vs *VertexSlice) Cap() int
Cap returns the capacity of an underlying vertex array.
func (*VertexSlice) End ¶
func (vs *VertexSlice) End()
End unbinds the underlying vertex array. Call this method when you're done with VertexSlice.
func (*VertexSlice) Len ¶
func (vs *VertexSlice) Len() int
Len returns the length of the VertexSlice (number of vertices).
func (*VertexSlice) SetLen ¶
func (vs *VertexSlice) SetLen(len int)
SetLen resizes the VertexSlice to length len.
func (*VertexSlice) SetVertexData ¶
func (vs *VertexSlice) SetVertexData(data []float32)
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 (*VertexSlice) Slice ¶
func (vs *VertexSlice) Slice(i, j int) *VertexSlice
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 (*VertexSlice) Stride ¶
func (vs *VertexSlice) Stride() int
Stride returns the number of float32 elements occupied by one vertex.
func (*VertexSlice) VertexData ¶
func (vs *VertexSlice) VertexData() []float32
VertexData returns the contents of the VertexSlice.
The data is in the same format as with SetVertexData.
func (*VertexSlice) VertexFormat ¶
func (vs *VertexSlice) VertexFormat() AttrFormat
VertexFormat returns the format of vertex attributes inside the underlying vertex array of this VertexSlice.