glen

package module
v0.0.0-...-9a070ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package glen provides hardware accelerated desktop windows based on OpenGL 3.2.

Index

Constants

View Source
const (
	KeyDelete = 119 // X11 Delete key

	KeyEscape = 9  // X11 Escape key
	KeyF1     = 67 // X11 F1 key
	KeyF2     = 68 // X11 F2 key
	KeyF3     = 69 // X11 F3 key
	KeyF4     = 70 // X11 F4 key
	KeyF5     = 71 // X11 F5 key
	KeyF6     = 72 // X11 F6 key
	KeyF7     = 73 // X11 F7 key
	KeyF8     = 74 // X11 F8 key
	KeyF9     = 75 // X11 F9 key
	KeyF10    = 76 // X11 F10 key
	KeyF11    = 95 // X11 F11 key
	KeyF12    = 96 // X11 F12 key

	KeyBackQuote = 49 // X11 grave accent (`)
	Key1         = 10 // X11 1 key
	Key2         = 11 // X11 2 key
	Key3         = 12 // X11 3 key
	Key4         = 13 // X11 4 key
	Key5         = 14 // X11 5 key
	Key6         = 15 // X11 6 key
	Key7         = 16 // X11 7 key
	Key8         = 17 // X11 8 key
	Key9         = 18 // X11 9 key
	Key0         = 19 // X11 0 key
	KeyMinus     = 20 // X11 minus (-) key
	KeyEqual     = 21 // X11 equal (=) key
	KeyBackspace = 22 // X11 BackSpace key

	KeyTab          = 23 // X11 Tab key
	KeyQ            = 24 // X11 Q key
	KeyW            = 25 // X11 W key
	KeyE            = 26 // X11 E key
	KeyR            = 27 // X11 R key
	KeyT            = 28 // X11 T key
	KeyY            = 29 // X11 Y key
	KeyU            = 30 // X11 U key
	KeyI            = 31 // X11 I key
	KeyO            = 32 // X11 O key
	KeyP            = 33 // X11 P key
	KeyLeftBracket  = 34 // X11 left bracket ([)
	KeyRightBracket = 35 // X11 right bracket (])
	KeyBackSlash    = 51 // X11 backslash (\)

	KeyCapsLock  = 66 // X11 Caps Lock key
	KeyA         = 38 // X11 A key
	KeyS         = 39 // X11 S key
	KeyD         = 40 // X11 D key
	KeyF         = 41 // X11 F key
	KeyG         = 42 // X11 G key
	KeyH         = 43 // X11 H key
	KeyJ         = 44 // X11 J key
	KeyK         = 45 // X11 K key
	KeyL         = 46 // X11 L key
	KeySemiColon = 47 // X11 semicolon (;)
	KeyQuote     = 48 // X11 apostrophe (')
	KeyEnter     = 36 // X11 Return key

	KeyLeftShift  = 50 // X11 left Shift key
	KeyLeftShift2 = 62 // X11 right Shift key (alternative)
	KeyZ          = 52 // X11 Z key
	KeyX          = 53 // X11 X key
	KeyC          = 54 // X11 C key
	KeyV          = 55 // X11 V key
	KeyB          = 56 // X11 B key
	KeyN          = 57 // X11 N key
	KeyM          = 58 // X11 M key
	KeyComma      = 59 // X11 comma (,)
	KeyPeriod     = 60 // X11 period (.)
	KeySlash      = 61 // X11 slash (/)
	KeyRightShift = 62 // X11 right Shift key

	KeyFn        = 151 // X11 Fn key (if available)
	KeyLeftCtrl  = 37  // X11 left Control key
	KeyLeftAlt   = 64  // X11 left Alt key
	KeyRightCtrl = 105 // X11 right Control key
	KeyLeftCmd   = 133 // X11 left Super/Windows key
	KeySpace     = 65  // X11 space key
	KeyRightCmd  = 134 // X11 right Super/Windows key
	KeyRightAlt  = 108 // X11 right Alt key
	KeyLeft      = 113 // X11 Left arrow key
	KeyRight     = 114 // X11 Right arrow key
	KeyDown      = 116 // X11 Down arrow key
	KeyUp        = 111 // X11 Up arrow key
)
View Source
const (
	TextureFormatRGB32F  = TextureFormat(0x8815)
	TextureFormatRGBA32F = TextureFormat(0x8814)
	TextureFormatRGBA    = TextureFormat(0x1908)
)
View Source
const MaxTextures = 16

MaxTextures defines the maximum number of textures that can be created and used simultaneously in the application.

View Source
const ShaderVersion = "#version 330"

Shader version to use for OpenGL shaders.

Variables

View Source
var (
	GLColorMask   func(red, green, blue, alpha bool)
	GLStencilFunc func(function StencilFunc, ref, mask uint32)
	GLStencilOp   func(fail, zfail StencilFail, zpass StencilOp)
)
View Source
var (
	Transparent = Color{0, 0, 0, 0}
	White       = Color{1, 1, 1, 1}
	Black       = Color{0, 0, 0, 1}
	Gray        = Color{0.5, 0.5, 0.5, 1}
	Red         = Color{1, 0, 0, 1}
	Green       = Color{0, 1, 0, 1}
	Blue        = Color{0, 0, 1, 1}
	Yellow      = Color{1, 1, 0, 1}
	Cyan        = Color{0, 1, 1, 1}
	Magenta     = Color{1, 0, 1, 1}
)
View Source
var (
	ErrShaderCompilation = errors.New("shader compilation failed")
	ErrShaderLinking     = errors.New("shader linking failed")
)
View Source
var ASCII = []rune(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")

ASCII defines all ascii characters.

View Source
var Logger = log.New(os.Stderr, "glen ", log.LstdFlags|log.Lshortfile)

Logger is the default logger used by glen. Disable by setting it to nil.

Functions

This section is empty.

Types

type BufferKind

type BufferKind uint8

BufferKind represents the type of buffer usage in terms of data upload frequency and intended GPU access.

const (
	// StaticBuffer represents a buffer with data that is not expected to change
	// frequently after being uploaded to the GPU.
	StaticBuffer BufferKind = iota
	// DynamicBuffer represents a buffer with data that is frequently updated
	// and suited for dynamic GPU usage.
	DynamicBuffer
	// StreamBuffer represents a buffer with data that changes frequently and
	// is suitable for temporary GPU usage.
	StreamBuffer
)

type Color

type Color struct {
	R, G, B, A float32
}

Color represents an RGBA color with float32 components ranging from 0.0 to 1.0.

func (*Color) From

func (c *Color) From(col color.Color)

func (*Color) FromRGBA

func (c *Color) FromRGBA(rgba uint32)

func (Color) String

func (c Color) String() string

func (Color) To

func (c Color) To() color.RGBA

func (Color) ToRGBA

func (c Color) ToRGBA() uint32

type CursorType

type CursorType int

CursorType represents different cursor styles.

const (
	CursorDefault CursorType = iota
	CursorIBeam
	CursorCross
	CursorHand
	CursorResizeNS
	CursorResizeWE
	CursorResizeNWSE
	CursorResizeNESW
	CursorMove
	CursorNotAllowed
	CursorHidden
)

type CustomCursor

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

CustomCursor represents a custom mouse cursor image.

func NewCustomCursor

func NewCustomCursor(img image.Image, hotspotX, hotspotY float32) *CustomCursor

NewCustomCursor creates a new custom cursor from an image. hotspotX and hotspotY define the cursor's click point (in pixels).

func (*CustomCursor) Delete

func (c *CustomCursor) Delete()

type Glyph

type Glyph struct {
	// Index in the texture atlas.
	Index int
	// Ascent is the distance from the baseline to the top of the glyph's bounding box.
	Ascent float32
	// Descent is the distance from the baseline to the bottom of the glyph's bounding box.
	Descent float32
	// Advance is the horizontal distance from the current position to the next glyph.
	Advance float32
}

Glyph represents a typographic character with its metrics and index in the font texture.

type InputState

type InputState struct {
	KeyJustPressed  []Key
	KeyJustReleased []Key
	KeyPressed      map[Key]bool
	KeyReleased     map[Key]bool
	KeyModifier     KeyModifier
	MousePos        geom.Vec2 // current mouse position in last frame
	MouseScroll     geom.Vec2 // scroll deltas from last frame
	MouseBtn        [6]bool   // pressed mouse buttons in last frame
	IsFocused       bool      // whether the window is focused
	IsMouseInWindow bool      // whether the mouse is within the window area
	// contains filtered or unexported fields
}

InputState holds the current state of keyboard and mouse inputs.

func (*InputState) Changed

func (s *InputState) Changed() bool

Changed returns true if any of the following happens:

  • dragging is active
  • scrolling is active
  • any mouse button was pressed or released
  • any key was pressed or released

func (*InputState) IsKeyJustPressed

func (s *InputState) IsKeyJustPressed(key Key) bool

IsKeyJustPressed returns whether the specified key was pressed in the current frame and not in the previous frame.

func (*InputState) IsKeyJustReleased

func (s *InputState) IsKeyJustReleased(key Key) bool

IsKeyJustReleased returns whether the specified key was released in the current frame but was pressed in the previous frame.

func (*InputState) IsMouseBtnJustPressed

func (s *InputState) IsMouseBtnJustPressed(btn MouseButton) bool

IsMouseBtnJustPressed returns whether the specified mouse button was pressed in the current frame but not in the last frame.

func (*InputState) IsMouseBtnJustReleased

func (s *InputState) IsMouseBtnJustReleased(btn MouseButton) bool

IsMouseBtnJustReleased returns whether the specified mouse button was just released in the current frame.

func (*InputState) MouseIn

func (s *InputState) MouseIn(pos, dims geom.Vec2) bool

MouseIn returns true if the current mouse position is within the bounds defined by pos (top-left) and dims (dimensions).

func (*InputState) Scroll

func (s *InputState) Scroll() geom.Vec2

Scroll returns the scroll delta since the last frame only on the most scrolled axis.

type Key

type Key uint8

Key represents a keyboard key.

func (Key) Rune

func (k Key) Rune(win *Window) rune

Rune returns the corresponding rune for the Key, translating it to its printable character representation, based on the current keyboard layout and modifier keys state.

type KeyModifier

type KeyModifier uint8
const (
	KeyModifierShift KeyModifier = 1 << (iota + 1)
	KeyModifierCtrl
	KeyModifierAlt
	KeyModifierCmd
)

type MouseButton

type MouseButton uint8

MouseButton represents a mouse button.

const (
	MouseButtonLeft MouseButton = iota
	MouseButtonRight
	MouseButtonMiddle
	MouseButton3
	MouseButton4
	MouseButton5
)

type Shader

type Shader[T any] struct {
	// contains filtered or unexported fields
}

Shader represents a programmable pipeline for rendering, including vertex and fragment shaders.

func NewShader

func NewShader[T any](vertexShader, fragmentShader string, kind VertexKind) (Shader[T], error)

NewShader creates a new Shader, compiling its vertex and fragment shaders from given sources.

T must either be a float32, a struct or array of float32s or an int32. Any other type will panic.

func (*Shader[T]) Delete

func (shader *Shader[T]) Delete()

Delete releases all resources associated with the Shader.

func (*Shader[T]) DisableStencil

func (shader *Shader[T]) DisableStencil()

func (*Shader[T]) Draw

func (shader *Shader[T]) Draw()

Draw renders the associated vertex or instance buffer.

func (*Shader[T]) EnableStencil

func (shader *Shader[T]) EnableStencil()

func (*Shader[T]) SetUniform

func (shader *Shader[T]) SetUniform(name string, value any) bool

SetUniform updates a shader uniform with a value of type bool, int, int32, float32, float64, Color, Vec2, image.Point, Texture2D, Texture2DAtlas or TextureFont.

func (*Shader[T]) SetVertices

func (shader *Shader[T]) SetVertices(vertices []T)

type ShaderInst

type ShaderInst[V geom.Vec, I any] struct {
	// contains filtered or unexported fields
}

func NewShaderInst

func NewShaderInst[V geom.Vec, I any](vertexShader, fragmentShader string, vertices []V, kind VertexKind) (*ShaderInst[V, I], error)

NewShaderInst creates a new Shader, compiling its vertex and fragment shaders from the given sources. The shader is bound to a VAO and a VBO for vertex data, and a VBO for instance data. The instance data is uploaded to the GPU using instanced rendering.

func (*ShaderInst[V, I]) Delete

func (shader *ShaderInst[V, I]) Delete()

Delete releases all resources associated with the Shader.

func (*ShaderInst[V, I]) Draw

func (shader *ShaderInst[V, I]) Draw()

Draw renders the associated vertex or instance buffer.

func (*ShaderInst[V, I]) SetInstances

func (shader *ShaderInst[V, I]) SetInstances(instances []I)

func (*ShaderInst[V, I]) SetUniform

func (shader *ShaderInst[V, I]) SetUniform(name string, value any) bool

SetUniform updates a shader uniform with a value of type bool, int, int32, float32, float64, Color, Vec2, Vec3, image.Point, Texture2D, Texture2DAtlas or TextureFont.

type ShaderInstance

type ShaderInstance interface {
	UseField(reflect.StructField) bool
}

ShaderInstance is to be implemented to select only instance fields to be mapped in a shader.

type StencilFail

type StencilFail uint32
const (
	GL_KEEP      StencilFail = 0x1E00
	GL_ZERO      StencilFail = 0x0000
	GL_REPLACE   StencilFail = 0x1E01
	GL_INCR      StencilFail = 0x1E02
	GL_INCR_WRAP StencilFail = 0x8507
	GL_DECR      StencilFail = 0x1E03
	GL_DECR_WRAP StencilFail = 0x8508
	GL_INVERT    StencilFail = 0x150A
)

type StencilFunc

type StencilFunc uint32
const (
	GL_EQUAL   StencilFunc = 0x0202
	GL_LESS    StencilFunc = 0x0201
	GL_LEQUAL  StencilFunc = 0x0203
	GL_GREATER StencilFunc = 0x0204
	GL_GEQUAL  StencilFunc = 0x0206
	GL_NEVER   StencilFunc = 0x0200
	GL_ALWAYS  StencilFunc = 0x0207
)

type StencilOp

type StencilOp uint32
const (
	STENCIL_NEVER     StencilOp = 0x0200
	STENCIL_LESS      StencilOp = 0x0201
	STENCIL_EQUAL     StencilOp = 0x0202
	STENCIL_LEQUAL    StencilOp = 0x0203
	STENCIL_GREATER   StencilOp = 0x0204
	STENCIL_NOTEQUAL  StencilOp = 0x0205
	STENCIL_GEQUAL    StencilOp = 0x0206
	STENCIL_ALWAYS    StencilOp = 0x0207
	STENCIL_KEEP      StencilOp = 0x1E00
	STENCIL_REPLACE   StencilOp = 0x1E01
	STENCIL_INCR      StencilOp = 0x1E02
	STENCIL_DECR      StencilOp = 0x1E03
	STENCIL_INCR_WRAP StencilOp = 0x8507
	STENCIL_DECR_WRAP StencilOp = 0x8508
	STENCIL_INVERT    StencilOp = 0x150A
)

type Texture2D

type Texture2D struct {
	Index int32
	Size  geom.Vec2
	// contains filtered or unexported fields
}

Texture2D represents a 2D texture used in OpenGL rendering.

func NewTexture

func NewTexture(img image.Image) Texture2D

NewTexture creates a new OpenGL 2D texture from the given image and returns a Texture2D object.

The Texture2D can then be used in shaders by referencing its Index field.

Panics if MaxTextures is exceeded.

func NewTexture2D

func NewTexture2D(size geom.Vec2, internal TextureFormat) Texture2D

NewTexture2D creates a new OpenGL 2D texture with the specified size and format. This is useful when using textures as data storage.

Panics if MaxTextures is exceeded.

func (*Texture2D) Delete

func (tex *Texture2D) Delete()

Delete removes the texture from the global texture list and deletes the OpenGL texture object.

func (*Texture2D) UpdateRegion

func (tex *Texture2D) UpdateRegion(x, y, width, height int, data []float32)

UpdateRegion updates a specific region of the texture with the provided data array. x and y specify the starting coordinates of the region within the texture. width and height define the dimensions of the region to be updated. data contains the new texture data for the specified region.

type Texture2DAtlas

type Texture2DAtlas struct {
	TileSize      geom.Vec2
	Width, Height int // in tiles
	Texture       Texture2D
}

Texture2DAtlas represents a collection of texture tiles within a single 2D texture.

func NewTexture2DAtlas

func NewTexture2DAtlas(img image.Image, tileSize geom.Vec2) Texture2DAtlas

NewTexture2DAtlas creates a Texture2DAtlas by dividing an image into tiles of the specified tile size.

Panics if the image dimensions are not divisible by the tile size.

func NewTexture2DAtlasFromIVG

func NewTexture2DAtlasFromIVG(size geom.Vec2, ivgData ...[]byte) (Texture2DAtlas, error)

NewTexture2DAtlasFromIVG creates a new texture font using IVG data, arranging icons in a grid based on the given size.

See https://godoc.org/golang.org/x/exp/shiny/iconvg for details.

func (*Texture2DAtlas) Delete

func (tex *Texture2DAtlas) Delete()

Delete removes the associated OpenGL texture object and cleans up the atlas texture's resources.

type TextureFont

type TextureFont struct {
	Font    font.Face
	Glyphs  map[rune]Glyph
	Texture Texture2DAtlas
}

func NewTextureFont

func NewTextureFont(fnt font.Face, runes ...rune) TextureFont

NewTextureFont creates a TextureFont for the provided runes using font.Face. If no runes are provided, ASCII is used.

func NewTextureFontFromTTF

func NewTextureFontFromTTF(ttf []byte, options *opentype.FaceOptions, runes ...rune) (TextureFont, error)

NewTextureFontFromTTF creates a TextureFont for the provided runes using the specified TTF font data, where `size` is the font size in points.

func (*TextureFont) Delete

func (tex *TextureFont) Delete()

Delete releases the resources associated with the texture atlas of the TextureFont instance.

func (*TextureFont) MeasureRunes

func (tex *TextureFont) MeasureRunes(text []rune) geom.Vec2

func (*TextureFont) MeasureText

func (tex *TextureFont) MeasureText(text string) geom.Vec2

type TextureFormat

type TextureFormat int32

type VertexKind

type VertexKind int

VertexKind defines the type of OpenGL primitive to be rendered, such as points, triangles, or strips.

const (
	POINTS         VertexKind = 0x0000
	LINES          VertexKind = 0x0001
	LINE_LOOP      VertexKind = 0x0002
	LINE_STRIP     VertexKind = 0x0003
	TRIANGLES      VertexKind = 0x0004
	TRIANGLE_STRIP VertexKind = 0x0005
	TRIANGLE_FAN   VertexKind = 0x0006
)

type Window

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

func NewWindow

func NewWindow(config WindowConfig) *Window

NewWindow creates a new Window with the given title and dimensions (in pixels).

func (*Window) Clear

func (*Window) Clear(color Color)

func (*Window) Close

func (w *Window) Close()

func (*Window) DPI

func (w *Window) DPI() float32

func (*Window) GetCursor

func (w *Window) GetCursor() CursorType

func (*Window) InputState

func (w *Window) InputState() *InputState

func (*Window) Open

func (w *Window) Open(onFrame func(*Window, time.Duration))

func (*Window) Resize

func (w *Window) Resize(dims geom.Vec2)

func (*Window) ScaleFactor

func (w *Window) ScaleFactor() float32

func (*Window) Screenshot

func (w *Window) Screenshot(area geom.Rect) *image.RGBA

func (*Window) ScreenshotBuffer

func (w *Window) ScreenshotBuffer(area geom.Rect, buf []byte)

func (*Window) SetCursor

func (w *Window) SetCursor(cursor CursorType)

func (*Window) SetCustomCursor

func (w *Window) SetCustomCursor(cursor *CustomCursor)

func (*Window) SetTitle

func (w *Window) SetTitle(title string)

func (*Window) Size

func (w *Window) Size() geom.Vec2

type WindowCallbackID

type WindowCallbackID uint32

WindowCallbackID is a unique identifier for managing window-related callbacks such as resize or close events. The id is always > 0.

type WindowConfig

type WindowConfig struct {
	Title      string
	Size       geom.Vec2
	VSync      bool
	Resizable  bool
	Decorated  bool
	Minimized  bool
	Maximized  bool
	Fullscreen bool
	Cursor     CursorType
}

type WindowIfc

type WindowIfc interface {

	// Open the window and call `onFrame` on every frame with the elapsed time since last frame.
	Open(onFrame func(*Window, time.Duration))

	// Size of the window.
	Size() geom.Vec2
	// Resize changes the window's dimensions to the specified size.
	Resize(dims geom.Vec2)
	// SetTitle changes the window title.
	SetTitle(title string)

	// Clear the window with the given color.
	Clear(color Color)
	// Close the window.
	Close()
	// OnResizeAdd adds the callback triggered when the window is resizing.
	OnResizeAdd(callback func(*Window)) WindowCallbackID
	// OnResizeDelete removes a previously added callback.
	OnResizeDelete(id WindowCallbackID)
	// OnMinimizeAdd adds the callback triggered when the window is minimized.
	OnMinimizeAdd(callback func(*Window)) WindowCallbackID
	// OnMinimizeDelete removes a previously added callback.
	OnMinimizeDelete(id WindowCallbackID)
	// OnDeMinimizeAdd adds the callback triggered when the window is deminimized.
	OnDeMinimizeAdd(callback func(*Window)) WindowCallbackID
	// OnDeMinimizeDelete removes a previously added callback.
	OnDeMinimizeDelete(id WindowCallbackID)
	// OnCloseAdd registers a callback triggered when the window is about to close.
	OnCloseAdd(callback func(*Window)) WindowCallbackID
	// OnCloseDelete removes a previously added callback.
	OnCloseDelete(id WindowCallbackID)

	// GetCursor returns the current cursor type.
	GetCursor() CursorType
	// SetCursor changes the cursor type.
	SetCursor(cursor CursorType)
	// SetCustomCursor sets a custom mouse cursor.
	SetCustomCursor(cursor *CustomCursor)

	// InputState returns the current state of keyboard and mouse inputs.
	InputState() *InputState

	// DPI returns the dots per inch of the window's screen.
	DPI() float32

	// ScaleFactor returns the scaling factor of the window,
	// typically determined by the DPI or system display settings.
	ScaleFactor() float32

	// Screenshot returns a screenshot of the window.
	Screenshot(area geom.Rect) *image.RGBA
	// ScreenshotBuffer saves the current window's framebuffer to a buffer.
	// The buffer must be large enough to hold the framebuffer's data,
	// or it will be truncated.
	ScreenshotBuffer(area geom.Rect, buf []byte)
	// contains filtered or unexported methods
}

WindowIfc represents an interface satisfied by Window for a graphical window with various functionalities for handling events and rendering.

Directories

Path Synopsis
cmd
demo command
demoui command
web command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL