shapes

package module
v0.0.0-...-ff796a9 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2014 License: MIT Imports: 6 Imported by: 0

README

Shapes GoDoc

shapes is a small package for drawing simple two-dimensional shapes on an OpenGL ES 2 context. Used in conjuction with Mandala it provides a quick way to represent geometric 2D objects on Android games.

Example API

// Create a 100x100 pixels² box
box := NewBox(100, 100)

// Place the box at a given position
box.Position(10, 10)

// Assign a color to it
box.Color(color.White)

// Attach the box to a world object (see World interface)
box.AttachToWorld(world)

// Render the box on the surface
box.Draw()

// swap the buffers

Supported shapes

  • Box
  • Segment

Test

See test for a black-box testing approach.

LICENSE

See LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultBoxVS is a default vertex shader for boxes.
	DefaultBoxVS = (shaders.VertexShader)(
		`
                 precision mediump float;
                 attribute vec4 pos;
                 attribute vec4 color;
                 attribute vec2 texIn;
                 varying vec2 texOut;
                 varying vec4 vColor;
                 uniform mat4 model;
                 uniform mat4 projection;
                 uniform mat4 view;
                 void main() {
                     gl_Position = projection*model*view*pos;
                     vColor = color;
                     texOut = texIn;
                 }`)

	// DefaultBoxVS is a default fragment shader for boxes.
	DefaultBoxFS = (shaders.FragmentShader)(
		`
                 precision mediump float;
                 varying vec4 vColor;
	         varying vec2 texOut;
                 uniform sampler2D texture;
                 uniform float texRatio;
                 void main() {
                     vec2 flippedTexCoords = vec2(texOut.x, 1.0 - texOut.y);
                     vec4 texColor = texture2D(texture, flippedTexCoords) * texRatio;
                     vec4 vertColor = vColor * (1.0 - texRatio);
                     gl_FragColor = texColor + vertColor;
                 }`)
)
View Source
var (
	// DefaultSegmentVS is a default vertex shader for the segment.
	DefaultSegmentVS = (shaders.VertexShader)(
		`precision mediump float;
                 attribute vec4 pos;
                 attribute vec4 color;
                 varying vec4 vColor;
                 uniform mat4 model;
                 uniform mat4 projection;
                 uniform mat4 view;
                 void main() {
                     gl_Position = projection*model*view*pos;
                     vColor = color;
                 }`)

	// DefaultSegmentVS is a default fragment shader for the
	// segment.
	DefaultSegmentFS = (shaders.FragmentShader)(
		`precision mediump float;
                 varying vec4 vColor;
                 void main() {
                     gl_FragColor = vColor;
                 }`)
)
View Source
var (
	// The default color for shapes is blue.
	DefaultColor = color.RGBA{0, 0, 0xff, 0xff}
)

Functions

This section is empty.

Types

type Base

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

Base represent a basic structure for shapes.

func (*Base) Angle

func (b *Base) Angle() float32

Angle returns the current angle of the shape in degrees.

func (*Base) AttachToWorld

func (b *Base) AttachToWorld(world World)

AttachToWorld fills projection and view matrices with world's matrices.

func (*Base) Bounds

func (b *Base) Bounds() image.Rectangle

Bounds returns the bounds of the shape as a Rectangle.

func (*Base) Center

func (b *Base) Center() (float32, float32)

Center returns the coordinates of the transformed center of the shape.

func (*Base) Color

func (b *Base) Color() color.Color

Color returns the color of the shape.

func (*Base) Move

func (b *Base) Move(dx, dy float32)

Move moves the shape by dx, dy.

func (*Base) MoveTo

func (b *Base) MoveTo(x, y float32)

MoveTo moves the shape in x, y position.

func (*Base) NColor

func (b *Base) NColor() [4]float32

NColor returns the color of the shape as a normalized float32 array.

func (*Base) Rotate

func (b *Base) Rotate(angle float32)

Rotates a shape by the given angle in degrees.

func (*Base) RotateAround

func (b *Base) RotateAround(x, y, angle float32)

RotateAround rotates the shape around the given point, by the given angle in degrees.

func (*Base) Scale

func (b *Base) Scale(sx, sy float32)

Scale scales the shape relative to its center, by the given factors.

func (*Base) SetCenter

func (b *Base) SetCenter(x, y float32)

SetCenter sets the coordinates of the center of the shape.

func (*Base) SetColor

func (s *Base) SetColor(c color.Color)

SetColor sets the color of the shape.

func (*Base) SetTexture

func (b *Base) SetTexture(texture uint32, texCoords []float32) error

SetTexture sets a texture for the shape. Texture argument is an uint32 value returned by the OpenGL context. It's a client-code responsibility to provide that value.

func (*Base) String

func (b *Base) String() string

String returns a string representation of the shape.

func (*Base) Vertices

func (b *Base) Vertices() []float32

Vertices returns the vertices slice.

type Box

type Box struct {
	Base
}

Box represents a box shape.

func NewBox

func NewBox(program shaders.Program, width, height float32) *Box

NewBox creates a new box of given sizes. It takes as arguments a linked program and width and height values.

func (*Box) Clone

func (box *Box) Clone() Shape

Clone makes a copy of the shape.

func (*Box) Draw

func (box *Box) Draw()

Draw actually renders the shape on the surface.

type Group

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

Group is a structure for grouping shapes. It implements Shape.

func NewGroup

func NewGroup() *Group

NewGroup instantiates a group object.

func (*Group) Angle

func (g *Group) Angle() float32

Angle returns the rotation angle of the group.

func (*Group) Append

func (g *Group) Append(s Shape)

Append appends a shape to the group.

func (*Group) AttachToWorld

func (g *Group) AttachToWorld(world World)

AttachToWorld attaches the group to a world.

func (*Group) Bounds

func (g *Group) Bounds() image.Rectangle

Bounds returns the bounding rectangle of the group.

func (*Group) Center

func (g *Group) Center() (float32, float32)

Center returns the center of the group.

func (*Group) Clone

func (g *Group) Clone() Shape

Clone returns a copy of the group.

func (*Group) Draw

func (g *Group) Draw()

Draw draws all the shapes in the group calling their Draw method.

func (*Group) GetAt

func (g *Group) GetAt(id int) Shape

GetAt returns the shape at position id in the group.

func (*Group) Move

func (g *Group) Move(dx, dy float32)

Move moves each shape of the group by dx, dy.

func (*Group) MoveTo

func (g *Group) MoveTo(x, y float32)

MoveTo moves the group in the (x,y) position.

func (*Group) Rotate

func (g *Group) Rotate(angle float32)

Rotate rotates the group aroung its center.

func (*Group) RotateAround

func (g *Group) RotateAround(x, y, angle float32)

RotateAround rotates the group around the given point, by the given angle in degrees.

func (*Group) Scale

func (g *Group) Scale(sx, sy float32)

Scale scales each shape of the group.

func (*Group) SetCenter

func (g *Group) SetCenter(x, y float32)

SetCenter sets a new center for the group.

func (*Group) SetTexture

func (g *Group) SetTexture(texture uint32, texCoords []float32) error

SetTexture sets the same texture to all shapes in the group.

func (*Group) String

func (g *Group) String() string

String returns a textual representation of the group.

func (*Group) Vertices

func (g *Group) Vertices() []float32

type Segment

type Segment struct {
	Base
	// contains filtered or unexported fields
}

Segment is a structure representing a segment.

func NewSegment

func NewSegment(program shaders.Program, x1, y1, x2, y2 float32) *Segment

NewSegment returns a new segment object. It takes a program shader and segment coordinates as arguments.

func (*Segment) Draw

func (segment *Segment) Draw()

Draw actually renders the segment on the surface.

type Shape

type Shape interface {
	// Rotate rotates the shape by angle in degree.
	Rotate(angle float32)

	// RotateAround rotates the shape around the given point, by
	// the given angle in degrees.
	RotateAround(x, y, angle float32)

	// Scale scales the shape by (sx,sy) factor.
	Scale(sx, sy float32)

	// Move moves the shape by (dx, dy).
	Move(dx, dy float32)

	// MoveTo moves the (center of the) shape in position (x,y).
	MoveTo(x, y float32)

	// Draw renders the shape on the surface.
	Draw()

	// Vertices returns the vertices slice of the shape.
	Vertices() []float32

	// Center returns the center coordinates of the shape.
	Center() (float32, float32)

	// Angle returns the rotation angle of the shape.
	Angle() float32

	// Bounds returns the bounding rectangle of the shape.
	Bounds() image.Rectangle

	// String returns a string representation of the shape.
	String() string

	// AttachToWorld attaches the shape to a world.
	AttachToWorld(world World)

	// Clone clones the current shape and returns a new shape.
	Clone() Shape

	// SetTexture sets a texture for the shape.
	SetTexture(texture uint32, texCoords []float32) error
}

Shape is an interface describing a shape.

type World

type World interface {
	// Projection returns the projection matrix used to render
	// the objects in the World.
	Projection() mathgl.Mat4f

	// View returns the view matrix used to render the World from
	// the point-of-view of a camera.
	View() mathgl.Mat4f
}

World in an interface for projection and view matrix.

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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