glhf: github.com/faiface/glhf Index | Files

package glhf

import "github.com/faiface/glhf"

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

Package Files

attr.go doc.go frame.go interface.go orphan.go shader.go texture.go util.go vertex.go

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.

func BlendFunc Uses

func BlendFunc(src, dst BlendFactor)

BlendFunc sets the source and destination blend factor.

func Bounds Uses

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 Uses

func Clear(r, g, b, a float32)

Clear clears the current framebuffer or window with the given color.

func Init Uses

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.

type Attr Uses

type Attr struct {
    Name string
    Type AttrType
}

Attr represents an arbitrary OpenGL attribute, such as a vertex attribute or a shader uniform attribute.

type AttrFormat Uses

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 Uses

func (af AttrFormat) Size() int

Size returns the total size of all attributes of the AttrFormat.

type AttrType Uses

type AttrType int

AttrType represents the type of an OpenGL attribute.

const (
    Int AttrType = iota
    Float
    Vec2
    Vec3
    Vec4
    Mat2
    Mat23
    Mat24
    Mat3
    Mat32
    Mat34
    Mat4
    Mat42
    Mat43
)

List of all possible attribute types.

func (AttrType) Size Uses

func (at AttrType) Size() int

Size returns the size of a type in bytes.

type BeginEnder Uses

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 Uses

type BlendFactor int

BlendFactor represents a source or destination blend factor.

type Frame Uses

type Frame struct {
    // contains filtered or unexported fields
}

Frame is a fixed resolution texture that you can draw on.

func NewFrame Uses

func NewFrame(width, height int, smooth bool) *Frame

NewFrame creates a new fully transparent Frame with given dimensions in pixels.

func (*Frame) Begin Uses

func (f *Frame) Begin()

Begin binds the Frame. All draw operations will target this Frame until End is called.

func (*Frame) Blit Uses

func (f *Frame) Blit(dst *Frame, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1 int)

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 (*Frame) End Uses

func (f *Frame) End()

End unbinds the Frame. All draw operations will go to whatever was bound before this Frame.

func (*Frame) ID Uses

func (f *Frame) ID() uint32

ID returns the OpenGL framebuffer ID of this Frame.

func (*Frame) Texture Uses

func (f *Frame) Texture() *Texture

Texture returns the Frame's underlying Texture that the Frame draws on.

type Shader Uses

type Shader struct {
    // contains filtered or unexported fields
}

Shader is an OpenGL shader program.

func NewShader Uses

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 Uses

func (s *Shader) Begin()

Begin binds the Shader program. This is necessary before using the Shader.

func (*Shader) End Uses

func (s *Shader) End()

End unbinds the Shader program and restores the previous one.

func (*Shader) ID Uses

func (s *Shader) ID() uint32

ID returns the OpenGL ID of this Shader.

func (*Shader) SetUniformAttr Uses

func (s *Shader) SetUniformAttr(uniform int, value interface{}) (ok bool)

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 Uses

func (s *Shader) UniformFormat() AttrFormat

UniformFormat returns the uniform attribute format of this Shader. Do not change it.

func (*Shader) VertexFormat Uses

func (s *Shader) VertexFormat() AttrFormat

VertexFormat returns the vertex attribute format of this Shader. Do not change it.

type Texture Uses

type Texture struct {
    // contains filtered or unexported fields
}

Texture is an OpenGL texture.

func NewTexture Uses

func NewTexture(width, height int, smooth bool, pixels []uint8) *Texture

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 Uses

func (t *Texture) Begin()

Begin binds the Texture. This is necessary before using the Texture.

func (*Texture) End Uses

func (t *Texture) End()

End unbinds the Texture and restores the previous one.

func (*Texture) Height Uses

func (t *Texture) Height() int

Height returns the height of the Texture in pixels.

func (*Texture) ID Uses

func (t *Texture) ID() uint32

ID returns the OpenGL ID of this Texture.

func (*Texture) Pixels Uses

func (t *Texture) Pixels(x, y, w, h int) []uint8

Pixels returns the content of a sub-region of the Texture as an RGBA byte sequence.

func (*Texture) SetPixels Uses

func (t *Texture) SetPixels(x, y, w, h int, pixels []uint8)

SetPixels sets the content of a sub-region of the Texture. Pixels must be an RGBA byte sequence.

func (*Texture) SetSmooth Uses

func (t *Texture) SetSmooth(smooth bool)

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 (*Texture) Smooth Uses

func (t *Texture) Smooth() bool

Smooth returns whether the Texture is set to be drawn "smooth" or "pixely".

func (*Texture) Width Uses

func (t *Texture) Width() int

Width returns the width of the Texture in pixels.

type VertexSlice Uses

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 Uses

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 Uses

func (vs *VertexSlice) Begin()

Begin binds the underlying vertex array. Calling this method is necessary before using the VertexSlice.

func (*VertexSlice) Cap Uses

func (vs *VertexSlice) Cap() int

Cap returns the capacity of an underlying vertex array.

func (*VertexSlice) Draw Uses

func (vs *VertexSlice) Draw()

Draw draws the content of the VertexSlice.

func (*VertexSlice) End Uses

func (vs *VertexSlice) End()

End unbinds the underlying vertex array. Call this method when you're done with VertexSlice.

func (*VertexSlice) Len Uses

func (vs *VertexSlice) Len() int

Len returns the length of the VertexSlice (number of vertices).

func (*VertexSlice) SetLen Uses

func (vs *VertexSlice) SetLen(len int)

SetLen resizes the VertexSlice to length len.

func (*VertexSlice) SetVertexData Uses

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 Uses

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 Uses

func (vs *VertexSlice) Stride() int

Stride returns the number of float32 elements occupied by one vertex.

func (*VertexSlice) VertexData Uses

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 Uses

func (vs *VertexSlice) VertexFormat() AttrFormat

VertexFormat returns the format of vertex attributes inside the underlying vertex array of this VertexSlice.

Package glhf imports 6 packages (graph) and is imported by 7 packages. Updated 2018-10-22. Refresh now. Tools for package owners.