layergl

package module
v0.0.0-...-66ee525 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2019 License: MIT Imports: 16 Imported by: 0

README

LayerGL

OpenGL 3.3 abstraction layer (In other words, graphics library) for Go.

Minimal example window

Installation

 $ go get -u github.com/iostapyshyn/layergl

Usage

Minimal Example
package main

import (
	"github.com/go-gl/glfw/v3.2/glfw"
	"github.com/iostapyshyn/layergl"
	"runtime"
)

var window *glfw.Window

const (
	width  = 640
	height = 480
)

func init() {
	runtime.LockOSThread()
}

func main() {
	var err error
	if err := glfw.Init(); err != nil {
		panic(err)
	}

	defer glfw.Terminate()

	// OpenGL version 3.3 Core.
	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

	// Required for MSAA anti-aliasing.
	glfw.WindowHint(glfw.Samples, 4)

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.Visible, glfw.False)

	window, err = glfw.CreateWindow(width, height, "Example", nil, nil)
	if err != nil {
		panic(err)
	}

	defer window.Destroy()

	// Center window on the screen.
	vidmode := glfw.GetPrimaryMonitor().GetVideoMode()
	window.SetPos((vidmode.Width-width)/2, (vidmode.Height-height)/2)

	window.MakeContextCurrent()
	glfw.SwapInterval(1)

	window.Show()

	loop()
}

func loop() {
	err := layergl.Init(width, height)
	if err != nil {
		panic(err)
	}

	for !window.ShouldClose() {
		layergl.Clear()

		layergl.DrawVertexObject(layergl.Triangles([]layergl.Point{
			{X: width/2 - 100, Y: height/2 - 100},
			{X: width/2 + 100, Y: height/2 - 100},
			{X: width / 2, Y: height/2 + 100},
		}), layergl.Color{1.0, 0.0, 1.0, 1.0})

		window.SwapBuffers()
		glfw.PollEvents()
	}
}

For more features, please refer to demo program source code included in the repository.

Libraries used:

  • go-gl: Go bindings for OpenGL.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear()

func ClearColor

func ClearColor(color Color)

func Distance

func Distance(a, b Point) float64

Return distance between two points.

func DrawLines

func DrawLines(points []Point, color Color)

func DrawPoint

func DrawPoint(d Point, r float64, color Color)

func DrawRect

func DrawRect(rect Rect, color Color)

func DrawTexture

func DrawTexture(d *Texture)

func DrawVertexObject

func DrawVertexObject(d *VertexObject, color Color)

func Init

func Init(width, height int) error

Types

type Color

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

type Font

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

func LoadFont

func LoadFont(file string, scale int32) (*Font, error)

func (*Font) Printf

func (f *Font) Printf(point Point, color Color, scale float64, fs string, argv ...interface{})

type Point

type Point struct {
	X, Y float64
}

type Rect

type Rect struct {
	X1, Y1 float64
	X2, Y2 float64
}

func (Rect) Area

func (rect Rect) Area() float64

func (Rect) Contains

func (rect Rect) Contains(p Point) bool

func (Rect) Height

func (rect Rect) Height() float64

func (Rect) Intersects

func (rect Rect) Intersects(r2 Rect) bool

func (Rect) Width

func (rect Rect) Width() float64

type Texture

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

func NewTexture

func NewTexture(fileName string, width, height float64) (texture *Texture, err error)

Loads and creates new Texture object.

type VertexObject

type VertexObject struct {
	Vertices []Point
	Indices  []int
}

func FromVertices

func FromVertices(p []Point) (*VertexObject, error)

Performs triangulation creating new VertexObject.

func Rectangle

func Rectangle(rect Rect) (polygon *VertexObject)

func Triangles

func Triangles(vertices []Point) (polygon *VertexObject)

func (VertexObject) Bounds

func (v VertexObject) Bounds() Rect

Return rectangle approximating bounds of the polygon.

func (*VertexObject) CenterAt

func (p *VertexObject) CenterAt(point Point)

Centers VertexObject at exact point.

func (VertexObject) Centroid

func (v VertexObject) Centroid() (center Point)

Returns geometrical center, center of mass (centroid) of a polygon.

func (*VertexObject) Move

func (p *VertexObject) Move(x, y float64)

Translation of the VertexObject by x, y pixels in the respective directions.

func (*VertexObject) RotateDeg

func (v *VertexObject) RotateDeg(angle float64)

Rotation.

func (*VertexObject) RotateRad

func (v *VertexObject) RotateRad(angle float64)

func (*VertexObject) Scale

func (v *VertexObject) Scale(scale float64)

Scaling.

func (*VertexObject) Triangulate

func (vo *VertexObject) Triangulate() error

Performs triangulation of VertexObject, writing to the Indices field.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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