gl_utils

package
v0.0.0-...-7b1f4f2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// VertexShaderBase is the simplest vertex shader you can have. It uses only the model and the projection matrix
	VertexShaderBase = `
        #version 410 core

        uniform mat4 model;
        uniform mat4 projection;

        layout(location=0) in vec2 vertex;
        layout(location=1) in vec2 uv;

        out vec2 uv_out;

        void main() {
            vec4 vertex_world = model * vec4(vertex, 0, 1);
            gl_Position = projection * vertex_world;
            uv_out = uv;
        }
        ` + "\x00"

	// FragmentShaderSolidColor used to have a solid color shape/primitive
	FragmentShaderSolidColor = `
        #version 410 core

        in vec2 uv_out;
        out vec4 out_color;
        uniform vec4 color;

        uniform sampler2D tex;

        void main() {
            out_color = color;
        }
        ` + "\x00"

	// FragmentShaderTexture implements a basic texture mapping
	FragmentShaderTexture = `
        #version 410 core

        in vec2 uv_out;
        out vec4 color;

        uniform sampler2D tex;

        void main() {
            color = texture(tex, uv_out);
        }
        ` + "\x00"
)
View Source
const (
	// Float32Size is the size (in bytes) of a float32
	Float32Size = 4
)

Variables

This section is empty.

Functions

func CircleToPolygon

func CircleToPolygon(center mgl32.Vec2, radius float32, numSegments int, startAngle float32) ([]mgl32.Vec2, error)

CircleToPolygon approximate a circle shape with a regular polygon

func GetBoundingBox

func GetBoundingBox(points []mgl32.Vec2) (mgl32.Vec2, mgl32.Vec2)

GetBoundingBox returns the top left and the bottom right points of the 2D box bounding all the points passed.

func Mat4From64to32Bits

func Mat4From64to32Bits(mat mgl64.Mat4) mgl32.Mat4

Types

type Camera2D

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

Camera2D a Camera based on an orthogonal projection

func NewCamera2D

func NewCamera2D(width int, height int, zoom float32) *Camera2D

NewCamera2D sets up an orthogonal projection camera

func (*Camera2D) Height

func (c *Camera2D) Height() float32

func (*Camera2D) MaxZoom

func (c *Camera2D) MaxZoom() float32

MaxZoom returns the maximum zoom level allowed

func (*Camera2D) MinZoom

func (c *Camera2D) MinZoom() float32

MinZoom returns the minimum zoom level allowed

func (*Camera2D) ProjectionMatrix

func (c *Camera2D) ProjectionMatrix() *mgl32.Mat4

ProjectionMatrix returns the projection matrix of the camera

func (*Camera2D) ScreenToWorld

func (c *Camera2D) ScreenToWorld(vec mgl32.Vec2) mgl32.Vec3

func (*Camera2D) SetCentered

func (c *Camera2D) SetCentered(centered bool)

SetCentered sets the center of the camera to the center of the screen

func (*Camera2D) SetFlipVertical

func (c *Camera2D) SetFlipVertical(flip bool)

SetFlipVertical sets the orientation of the vertical axis. Pass true to have a cartesian coordinate system

func (*Camera2D) SetPosition

func (c *Camera2D) SetPosition(x float32, y float32)

SetPosition sets the current position of the camera. If the camera is centered, the center will be moving

func (*Camera2D) SetVisibleArea

func (c *Camera2D) SetVisibleArea(x1 float32, y1 float32, x2 float32, y2 float32)

SetVisibleArea configures the camera to make the specified area completely visible, position and zoom are changed accordingly

func (*Camera2D) SetZoom

func (c *Camera2D) SetZoom(zoom float32)

SetZoom sets the zoom factor

func (*Camera2D) SetZoomRange

func (c *Camera2D) SetZoomRange(minZoom float32, maxZoom float32)

SetZoomRange sets the minimum and maximum zoom factors allowed

func (*Camera2D) Translate

func (c *Camera2D) Translate(x float32, y float32)

Translate move the camera position by the specified amount

func (*Camera2D) Width

func (c *Camera2D) Width() float32

func (*Camera2D) WorldToScreen

func (c *Camera2D) WorldToScreen(vec mgl32.Vec3) mgl32.Vec2

func (*Camera2D) Zoom

func (c *Camera2D) Zoom() float32

Zoom returns the current zoom level

type Color

type Color mgl32.Vec4

Color is a Vec4

func NewColor

func NewColor(r, g, b, a float32) *Color

NewColor creates a new color from the RBGA components

func (*Color) A

func (c *Color) A() float32

A returns the alpha component of the color

func (*Color) B

func (c *Color) B() float32

B returns the blue component of the color

func (*Color) G

func (c *Color) G() float32

G returns the green component of the color

func (*Color) R

func (c *Color) R() float32

R returns the red component of the color

func (*Color) Set

func (c *Color) Set(r, g, b, a float32)

Set the color using new RBGA components

type ModelMatrix

type ModelMatrix struct {
	mgl32.Mat4
	// contains filtered or unexported fields
}

ModelMatrix matrix representing the primitive transformation

type Primitive

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

func (*Primitive) Draw

func (p *Primitive) Draw(projectionMatrix *mgl32.Mat4)

func (*Primitive) SetShader

func (p *Primitive) SetShader(shader *ShaderProgram)

func (*Primitive) SetTexture

func (p *Primitive) SetTexture(texture *Texture)

func (*Primitive) Shader

func (p *Primitive) Shader() *ShaderProgram

func (*Primitive) Texture

func (p *Primitive) Texture() *Texture

type Primitive2D

type Primitive2D struct {
	Primitive
	// contains filtered or unexported fields
}

Primitive2D a drawing primitive on the XY plane

func NewGridPrimitive

func NewGridPrimitive(center mgl32.Vec3, width int, height int, gridSize int) *Primitive2D

Creates a grid of lines with a distance of gridSize and filling the area 0,0 -> width,height

func NewPolylinePrimitive

func NewPolylinePrimitive(center mgl32.Vec3, points []mgl32.Vec2, closed bool) *Primitive2D

NewPolylinePrimitive creates a primitive from a sequence of points. The points coordinates are relative to the passed center

func NewQuadPrimitive

func NewQuadPrimitive(position mgl32.Vec3, size mgl32.Vec2) *Primitive2D

NewQuadPrimitive creates a rectangular primitive filled with a texture

func NewQuadPrimitiveExt

func NewQuadPrimitiveExt(position mgl32.Vec3, size mgl32.Vec2, shader *ShaderProgram, vertices []float32, uvCoords []float32) *Primitive2D

NewQuadPrimitiveExt creates a rectangular primitive filled with a texture. It accepts custom shader and coordinates

func NewRectPrimitive

func NewRectPrimitive(position mgl32.Vec3, size mgl32.Vec2, filled bool) *Primitive2D

NewRectPrimitive creates a rectangular primitive

func NewRegularPolygonPrimitive

func NewRegularPolygonPrimitive(center mgl32.Vec3, radius float32, numSegments int, filled bool) *Primitive2D

NewRegularPolygonPrimitive creates a primitive from a regular polygon

func NewTriangles

func NewTriangles(
	vertices []float32,
	uvCoords []float32,
	texture *Texture,
	position mgl32.Vec3,
	size mgl32.Vec2,
	shaderProgram *ShaderProgram,
) *Primitive2D

NewTriangles creates a primitive as a collection of triangles

func (*Primitive2D) Angle

func (p *Primitive2D) Angle() float32

Angle in radians

func (*Primitive2D) Color

func (p *Primitive2D) Color() Color

Color return the color passed to the shader

func (*Primitive2D) Draw

func (p *Primitive2D) Draw(projectionMatrix *mgl32.Mat4)

Draw draws the primitive

func (*Primitive2D) ModelMatrix

func (p *Primitive2D) ModelMatrix() *mgl32.Mat4

ModelMatrix returns the current model matrix

func (*Primitive2D) Position

func (p *Primitive2D) Position() mgl32.Vec3

Position gets X,Y,Z of the primitive.

func (*Primitive2D) SetAnchor

func (p *Primitive2D) SetAnchor(anchor mgl32.Vec2)

SetAnchor sets the anchor point of the primitive, this will be the point placed at Position

func (*Primitive2D) SetAnchorToCenter

func (p *Primitive2D) SetAnchorToCenter()

SetAnchorToCenter sets the anchor at the center of the primitive

func (*Primitive2D) SetAngle

func (p *Primitive2D) SetAngle(radians float32)

SetAngle sets the rotation angle around the Z axis

func (*Primitive2D) SetColor

func (p *Primitive2D) SetColor(color Color)

SetColor sets the color passed to the shader

func (*Primitive2D) SetFlipX

func (p *Primitive2D) SetFlipX(flipX bool)

SetFlipX flips the primitive around the Y axis

func (*Primitive2D) SetFlipY

func (p *Primitive2D) SetFlipY(flipY bool)

SetFlipY flips the primitive around the X axis

func (*Primitive2D) SetPosition

func (p *Primitive2D) SetPosition(position mgl32.Vec3)

SetPosition sets the X,Y,Z position of the primitive. Z is used for the drawing order

func (*Primitive2D) SetScale

func (p *Primitive2D) SetScale(scale mgl32.Vec2)

SetScale sets the scaling factor on X and Y for the primitive. The scaling respects the anchor and the rotation

func (*Primitive2D) SetSize

func (p *Primitive2D) SetSize(size mgl32.Vec2)

SetSize sets the size (in pixels) of the current primitive

func (*Primitive2D) SetSizeFromTexture

func (p *Primitive2D) SetSizeFromTexture()

SetSizeFromTexture sets the size of the current primitive to the pixel size of the texture

func (*Primitive2D) SetUVCoords

func (p *Primitive2D) SetUVCoords(uvCoords []float32)

SetUVCoords uploads new UV coordinates

func (*Primitive2D) SetUniforms

func (p *Primitive2D) SetUniforms()

SetUniforms sets the shader's uniform variables

func (*Primitive2D) SetVertices

func (p *Primitive2D) SetVertices(vertices []float32)

SetVertices uploads new set of vertices into opengl buffer

func (*Primitive2D) Size

func (p *Primitive2D) Size() mgl32.Vec2

Size in pixels

type ShaderProgram

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

ShaderProgram a representation of an OpenGL shader program

func NewDefaultShaderProgram

func NewDefaultShaderProgram() *ShaderProgram

NewDefaultShaderProgram creates a base shader that can render solid color pixels

func NewShaderProgram

func NewShaderProgram(vertSource string, geomSource string, fragSource string) *ShaderProgram

NewShaderProgram creates a new program using the shaders source code passed as plain text

func (*ShaderProgram) AttachShader

func (s *ShaderProgram) AttachShader(source string, shaderType ShaderType)

AttachShader attaches a shader to this program

func (*ShaderProgram) GetUniform

func (s *ShaderProgram) GetUniform(name string) int32

GetUniform returns a uniform from the shader. Uses a uniform's location cache to speed up the look up

func (*ShaderProgram) ID

func (s *ShaderProgram) ID() uint32

ID returns the OpenGL ID assigned to this shader program

func (s *ShaderProgram) Link()

Link links together all the shaders into a shader program

func (*ShaderProgram) Release

func (s *ShaderProgram) Release()

Release releases all the resources associated with this program

func (*ShaderProgram) SetUniform

func (s *ShaderProgram) SetUniform(name string, val interface{})

SetUniform sets the shader's uniforms based on the type of the value passed

type ShaderType

type ShaderType uint32

ShaderType Type of the shader

Types of shaders supported

type Texture

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

Texture a representation of an image file in memory

func NewEmptyTexture

func NewEmptyTexture(width int, height int, pixelFormat int32) (*Texture, error)

NewEmptyTexture creates an empty texture with a specified size

func NewTextureFromFile

func NewTextureFromFile(filePath string) *Texture

NewTextureFromFile loads the image from a file into a texture

func NewTextureFromImage

func NewTextureFromImage(imageData image.Image) *Texture

NewTextureFromImage uses the data from an Image struct to create a texture

func (*Texture) Bind

func (t *Texture) Bind()

func (*Texture) Height

func (t *Texture) Height() int32

Height returns the texture width in pixels

func (*Texture) ID

func (t *Texture) ID() uint32

ID returns the unique OpenGL ID of this texture

func (*Texture) Unbind

func (t *Texture) Unbind()

func (*Texture) Width

func (t *Texture) Width() int32

Width returns the texture width in pixels

Jump to

Keyboard shortcuts

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