graphics

package
v0.0.0-...-c243d71 Latest Latest
Warning

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

Go to latest
Published: May 3, 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
)
View Source
const MaxZoom float64 = 20
View Source
const MinZoom float64 = 0.01

Variables

This section is empty.

Functions

This section is empty.

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 float64) *Camera2D

NewCamera2D sets up an orthogonal projection camera

func (*Camera2D) ProjectionMatrix

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

ProjectionMatrix returns the projection matrix of the camera

func (*Camera2D) ProjectionMatrix32

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

ProjectionMatrix32 returns the projection matrix of the camera as mgl32.Mat4

func (*Camera2D) ScreenToWorld

func (c *Camera2D) ScreenToWorld(vec mgl64.Vec2) mgl64.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 float64, y float64)

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 float64)

SetZoom sets the zoom factor

func (*Camera2D) Translate

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

Translate move the camera position by the specified amount

func (*Camera2D) WorldToScreen

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

func (*Camera2D) Zoom

func (c *Camera2D) Zoom() float64

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 Context

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

Context ...

func (*Context) BindShader

func (c *Context) BindShader(shader *ShaderProgram)

BindShader sets shader to be current shader if it isn't already

func (*Context) BindTexture

func (c *Context) BindTexture(texture *Texture)

BindTexture sets texture to be current texture if it isn't already

type Drawable

type Drawable interface {
	Texture() *Texture
	Shader() *ShaderProgram
	Draw(context *Context)
	DrawInBatch(context *Context)
}

Drawable ...

type ModelMatrix

type ModelMatrix struct {
	mgl64.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(context *Context)

func (*Primitive) DrawInBatch

func (p *Primitive) DrawInBatch(context *Context)

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 NewPolylinePrimitive

func NewPolylinePrimitive(center mgl64.Vec3, points []mgl64.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 mgl64.Vec3, size mgl64.Vec2) *Primitive2D

NewQuadPrimitive creates a rectangular primitive

func NewRegularPolygonPrimitive

func NewRegularPolygonPrimitive(center mgl64.Vec3, radius float64, numSegments int, filled bool) *Primitive2D

NewRegularPolygonPrimitive creates a primitive from a regular polygon

func NewTriangles

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

NewTriangles creates a primitive as a collection of triangles

func (*Primitive2D) Angle

func (p *Primitive2D) Angle() float64

Angle in radians

func (*Primitive2D) Draw

func (p *Primitive2D) Draw(context *Context)

Draw draws the primitive

func (*Primitive2D) DrawInBatch

func (p *Primitive2D) DrawInBatch(context *Context)

DrawInBatch draws the primitive assuming that the correct texture and shader are already bound

func (*Primitive2D) ModelMatrix

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

ModelMatrix returns the current model matrix

func (*Primitive2D) ModelMatrix32

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

ModelMatrix returns the current model matrix as mgl32.Mat4

func (*Primitive2D) Position

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

Position gets X,Y,Z of the primitive.

func (*Primitive2D) SetAnchor

func (p *Primitive2D) SetAnchor(anchor mgl64.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 float64)

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 mgl64.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 mgl64.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 mgl64.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() mgl64.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) (*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) 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) Width

func (t *Texture) Width() int32

Width returns the texture width in pixels

type TextureStorage

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

TextureStorage handles multiple textures, each one with its own id

func MakeTextureStorage

func MakeTextureStorage(path string) *TextureStorage

MakeTextureStorage creates a new initialized instance

func (*TextureStorage) LoadTexture

func (ts *TextureStorage) LoadTexture(fileName string, textureId string)

LoadTexture loads a texture from file and assigns an id to it

func (*TextureStorage) TextureForId

func (ts *TextureStorage) TextureForId(textureId string) *Texture

TextureForId returns the texture associated with the id

Jump to

Keyboard shortcuts

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