three

package module
v0.0.0-...-926907d Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2015 License: MIT Imports: 13 Imported by: 0

README

go-three Build Status GoDoc

go-three provides a simple API to create and display animated 3D computer graphics.

Prerequisites

NOTE: Check out the scripts directory on how to install dependencies.

Installation

Once you have installed the prerequisites you can install the package via go get:

go get github.com/tobscher/go-three

Usage

package main

import (
	"log"

	three "github.com/tobscher/go-three"
	"github.com/tobscher/go-three/geometries"
)

const (
	fov    = 75.0
	width  = 640
	height = 480
	near   = 1
	far    = 10000
)

func main() {
	window, err := three.NewWindow(width, height, "Example")
	if err != nil {
		log.Fatal(err)
	}

	renderer, err := three.NewRenderer(window)
	if err != nil {
		log.Fatal(err)
	}

	scene := three.NewScene()
	camera := three.NewPerspectiveCamera(fov, width/height, near, far)
	camera.Transform.TranslateZ(1000)

	box := geometries.NewCube(200)
	red := three.NewBasicMaterial()
	red.SetColor(three.NewColor(1.0, 0.0, 0.0))

	mesh := three.NewMesh(box, red)

	scene.Add(mesh)

	for !window.ShouldClose() {
		mesh.Transform.RotateX(0.01)
		mesh.Transform.RotateY(0.02)

		renderer.Render(scene, camera)
	}

	renderer.Unload(scene)

	renderer.OpenGLSentinel()
}

Documentation

Documentation can be found on godoc.org.

Examples

Examples can be found in the examples/ subdirectory.

Contributing

If you encounter any issues please file an issue.

Pull requests are welcome.

Documentation

Overview

Package three provides a simple API to create and display animated 3D computer graphics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTime

func GetTime() float64

GetTime returns the number of seconds since the timer was started.

Please refer to http://www.glfw.org/docs/latest/input.html#time for more information.

func MakeProgram

func MakeProgram(features ProgramFeature) gl.Program

MakeProgram loads a shader program for the given features. Features will be activated via pre-processor directives. e.g. #define USE_TEXTURE

func MakeTextShader

func MakeTextShader() gl.Program

MakeTextShader creates a new shader program for text rendering purposes.

func TextureFromDDS

func TextureFromDDS(fname string) (gl.Texture, error)

TextureFromDDS creates an OpenGL texture from the given file.

Types

type Appearance

type Appearance interface {
	SetProgram(*Program)
	Program() *Program
}

Appearance is the interface which defines the appearance of 3D objects. For example: solid color, textured, etc.

SetProgram is used to store the given program and to cache it inside the material. Program returns a program which is used to define the appearance of the 3D object. A program consists of a vertex and a fragment shader.

type Attribute

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

Attribute describes an attribute which is passed to a shader program.

func NewAttribute

func NewAttribute(index int, size uint, buffer gl.Buffer) Attribute

NewAttribute creates a new attribute for a shader program.

type BasicMaterial

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

BasicMaterial describes a material with basic shading. No lights or shadows are considered.

func NewBasicMaterial

func NewBasicMaterial() *BasicMaterial

NewBasicMaterial creates a new Basic material.

func (BasicMaterial) Color

func (b BasicMaterial) Color() *Color

Color returns the set color for this material.

func (*BasicMaterial) Program

func (b *BasicMaterial) Program() *Program

Program returns the current program which is used for this material.

func (*BasicMaterial) SetColor

func (b *BasicMaterial) SetColor(color *Color) *BasicMaterial

SetColor sets a solid color for this material.

func (*BasicMaterial) SetProgram

func (b *BasicMaterial) SetProgram(p *Program)

SetProgram stores the given program for further use (cache).

func (*BasicMaterial) SetTexture

func (b *BasicMaterial) SetTexture(texture *Texture)

SetTexture sets the texture for this material.

func (*BasicMaterial) SetWireframe

func (b *BasicMaterial) SetWireframe(wireframe bool) *BasicMaterial

SetWireframe enables or disables wireframes for the material.

func (BasicMaterial) Texture

func (b BasicMaterial) Texture() *Texture

Texture returns the set texture for this material.

func (BasicMaterial) Wireframe

func (b BasicMaterial) Wireframe() bool

Wireframe returns the current value for wireframe rendering.

type CameraSettings

type CameraSettings struct {
	FOV  float32
	Near float32
	Far  float32
}

CameraSettings holds information to construct a new camera object.

type Color

type Color [3]float32

Color stores color information for the red, green and blue channel.

func NewColor

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

NewColor returns a new rgb color.

func (Color) B

func (c Color) B() float32

B returns the value for the blue channel. Value can be between 0 and 1

func (Color) Float

func (c Color) Float() []float32

Float returns the current as array of float32's

func (Color) G

func (c Color) G() float32

G returns the value for the green channel. Value can be between 0 and 1

func (Color) R

func (c Color) R() float32

R returns the value for the red channel. Value can be between 0 and 1

type Colored

type Colored interface {
	Color() *Color
}

Colored is an interface that indicates that a material can have a solid color.

type Face

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

Face stores indices to vertices and normals.

func NewFace

func NewFace(a, b, c uint16) *Face

NewFace returns a new triangle face.

func (*Face) A

func (f *Face) A() uint16

A returns the index of the first vertex in the triangle.

func (*Face) AddNormal

func (f *Face) AddNormal(x, y, z uint16)

AddNormal adds normal indices for this face.

func (*Face) At

func (f *Face) At(i int) uint16

At returns the vertex index stored at the given position.

func (*Face) B

func (f *Face) B() uint16

B returns the index of the second vertex in the triangle.

func (*Face) C

func (f *Face) C() uint16

C returns the index of the third vertex in the triangle.

func (*Face) NormalAt

func (f *Face) NormalAt(i int) uint16

NormalAt returns the normal index stored at the given position

type Font

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

Font defines a font with its texture

func NewFont

func NewFont(path string, scale int32) (*Font, error)

NewFont loads a font file from path at a given scale. The font will be rendered to a texture which is later used for drawing.

type Geometry

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

Geometry is a base struct with fields for Vertices and UVs.

func (*Geometry) ArrayCount

func (g *Geometry) ArrayCount() int

ArrayCount returns the number of elements to draw in the draw call. Faces * 3 if faces are used or number of vertices.

func (*Geometry) Faces

func (g *Geometry) Faces() []*Face

Faces returns the triangle faces for the geometry.

func (*Geometry) Normals

func (g *Geometry) Normals() []mgl32.Vec3

Normals returns the normals for the geometry.

func (*Geometry) SetFaces

func (g *Geometry) SetFaces(faces []*Face)

SetFaces stores the given faces in an internal field.

func (*Geometry) SetNormals

func (g *Geometry) SetNormals(normals []mgl32.Vec3)

SetNormals stores the given normals in an internal field.

func (*Geometry) SetUVs

func (g *Geometry) SetUVs(uvs []mgl32.Vec2)

SetUVs stores the given uv mappings in an internal field.

func (*Geometry) SetVertices

func (g *Geometry) SetVertices(vertices []mgl32.Vec3)

SetVertices stores the given vertices in an internal field.

func (*Geometry) UVs

func (g *Geometry) UVs() []mgl32.Vec2

UVs returns the uv mappings for the geometry.

func (*Geometry) Vertices

func (g *Geometry) Vertices() []mgl32.Vec3

Vertices returns the vertices for the geometry.

type Index

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

Index stores information about element indices

func NewIndex

func NewIndex(data []uint16) *Index

NewIndex creates a new element index

type Line

type Line struct {
	Object3D
	// contains filtered or unexported fields
}

Line is a representation of a line in 3D space. It consists of a geometry which defines all vertices of the lines to render and a material. Lines can be transformed.

func NewLine

func NewLine(geometry Shape, material Appearance) *Line

NewLine creates a new line for the given geometry (set of vertices) and material.

func (*Line) Geometry

func (l *Line) Geometry() Shape

Geometry returns an object which describes the shape of the line.

func (*Line) Material

func (l *Line) Material() Appearance

Material returns an objects which described the appaerance of the line.

func (*Line) Mode

func (l *Line) Mode() gl.GLenum

Mode returns an enum which is used to define the type of primitive to render.

type Mesh

type Mesh struct {
	Object3D
	// contains filtered or unexported fields
}

Mesh is a representation of a 3D object. It consists of a geometry and a material. Meshes can be transformed in 3D space.

func NewMesh

func NewMesh(geometry Shape, material Appearance) *Mesh

NewMesh creates a new mesh for the given geometry (shape) and material (appearance).

func (*Mesh) Geometry

func (m *Mesh) Geometry() Shape

Geometry returns an object which describes the shape of the mesh.

func (*Mesh) Material

func (m *Mesh) Material() Appearance

Material returns an objects which described the appaerance of the mesh.

func (*Mesh) Mode

func (m *Mesh) Mode() gl.GLenum

Mode returns an enum which is used to define the type of primitive to render.

type Object3D

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

Object3D described an object in 3D space.

func NewObject3D

func NewObject3D() Object3D

NewObject3D returns a new Object3D. It initializes the transform of the object.

func (*Object3D) Index

func (o *Object3D) Index() *Index

Index returns the FBO index.

func (*Object3D) NormalBuffer

func (o *Object3D) NormalBuffer() gl.Buffer

NormalBuffer returns the buffer object for this objects normals.

func (*Object3D) Transform

func (o *Object3D) Transform() *Transform

Transform returns the transform object (translation, rotation, scale) for this object.

func (*Object3D) UVBuffer

func (o *Object3D) UVBuffer() gl.Buffer

UVBuffer returns the buffer object for this objects uvs.

func (*Object3D) VertexBuffer

func (o *Object3D) VertexBuffer() gl.Buffer

VertexBuffer returns the buffer object for this objects vertices.

type PerspectiveCamera

type PerspectiveCamera struct {
	Transform *Transform
	// contains filtered or unexported fields
}

PerspectiveCamera has information about the transformation of the camera and it's projection matrix.

Note: The underlying matrix for the transform structure must be inverted.

func NewPerspectiveCamera

func NewPerspectiveCamera(fov, aspect, near, far float32) *PerspectiveCamera

NewPerspectiveCamera creates a new perspective camera for the given values.

fov: Field of view in degrees aspect: aspect ratio near: near clip plane far: far clip plane

The cameras transform matrix will be inverted.

type Program

type Program struct {
	Loaded bool
	// contains filtered or unexported fields
}

Program is a GLSL shader program.

func NewProgram

func NewProgram() *Program

NewProgram returns a new Program with attributes and uniforms collection initialised.

func (*Program) Load

func (p *Program) Load(program gl.Program)

Load sets the given OpenGL program.

func (Program) Unuse

func (p Program) Unuse()

Unuse disables the current program.

func (Program) Use

func (p Program) Use()

Use makes this program current.

type ProgramFeature

type ProgramFeature int

ProgramFeature type.

const (
	// COLOR feature
	COLOR ProgramFeature = 1 << iota
	// TEXTURE feature
	TEXTURE
	// SHADING_BASIC feature
	SHADING_BASIC
)

type Renderer

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

Renderer handles mesh rendering to the window.

func NewRenderer

func NewRenderer(window *Window) (*Renderer, error)

NewRenderer creates a new Renderer with the given window size and title.

func (*Renderer) OpenGLSentinel

func (r *Renderer) OpenGLSentinel()

OpenGLSentinel reports any OpenGL related errors.

func (*Renderer) Render

func (r *Renderer) Render(scene *Scene, camera *PerspectiveCamera)

Render renders the given scene with the given camera to the window.

func (*Renderer) Unload

func (r *Renderer) Unload(s *Scene)

Unload deallocates the given scene and all its shader programs.

type Scene

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

Scene represents a tree-like structure (graph) of 3D objects.

func NewScene

func NewScene() *Scene

NewScene returns a new Scene.

func (*Scene) Add

func (s *Scene) Add(object SceneObject)

Add adds the given scene object to the scene tree.

func (*Scene) AddText

func (s *Scene) AddText(text *Text)

AddText adds the given text object to the scene tree. Text is always rendered last.

type SceneObject

type SceneObject interface {
	Geometry() Shape
	Material() Appearance
	Transform() *Transform
	Mode() gl.GLenum

	Index() *Index
	VertexBuffer() gl.Buffer
	UVBuffer() gl.Buffer
	NormalBuffer() gl.Buffer
}

SceneObject is an interface that describes an object that can be added to the scene graph.

type Shape

type Shape interface {
	Vertices() []mgl32.Vec3
	UVs() []mgl32.Vec2
	Normals() []mgl32.Vec3
	Faces() []*Face
	ArrayCount() int
}

Shape is the interface which defines the shape of a 3D object.

type Text

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

Text defines a 2D text geometry and its appearance.

func NewText

func NewText(geometry *TextGeometry, material Texter) *Text

NewText creates a new 2D text object that can be added to the scene.

NOTE: This method overrides the texture for the given texture to the texture from the font.

func (*Text) Geometry

func (t *Text) Geometry() *TextGeometry

Geometry returns the geometry object for this text object.

func (*Text) Material

func (t *Text) Material() Texter

Material returns the appearance defintion for this text object.

func (*Text) SetText

func (t *Text) SetText(text string)

SetText updates the text that is drawn. This will update the underlying vertex and uv buffer. BUG(tobscher) Vertex and UV buffers are deleted and re-created; not updated.

type TextGeometry

type TextGeometry struct {
	Vertices []mgl32.Vec2
	UVs      []mgl32.Vec2

	Text     string
	Position mgl32.Vec2
	Size     float32
	Font     *Font
}

TextGeometry defines the geometry of 2D text.

func NewTextGeometry

func NewTextGeometry(text string, position mgl32.Vec2, size float32, font *Font) *TextGeometry

NewTextGeometry creates a new 2D text geometry for the given text.

type TextMaterial

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

TextMaterial describes a material for 2D text.

func NewTextMaterial

func NewTextMaterial() *TextMaterial

NewTextMaterial creates a new Basic material.

func (TextMaterial) Color

func (b TextMaterial) Color() *Color

Color returns the set color for this material.

func (*TextMaterial) Program

func (b *TextMaterial) Program() *Program

Program returns the current program which is used for this material.

func (*TextMaterial) SetColor

func (b *TextMaterial) SetColor(color *Color) *TextMaterial

SetColor sets a solid color for this material.

func (*TextMaterial) SetProgram

func (b *TextMaterial) SetProgram(p *Program)

SetProgram stores the given program for further use (cache).

func (*TextMaterial) SetTexture

func (b *TextMaterial) SetTexture(texture *Texture) *TextMaterial

SetTexture sets the texture for this material.

func (*TextMaterial) SetWireframe

func (b *TextMaterial) SetWireframe(wireframe bool) *TextMaterial

SetWireframe enables or disables wireframes for the material.

func (TextMaterial) Texture

func (b TextMaterial) Texture() *Texture

Texture returns the set texture for this material.

func (TextMaterial) Wireframe

func (b TextMaterial) Wireframe() bool

Wireframe returns the current value for wireframe rendering.

type Texter

type Texter interface {
	Appearance
	Textured
}

Texter is an interface composition of Appaerance and Textured.

type Texture

type Texture struct {
	WrapS  Wrapping
	WrapT  Wrapping
	Repeat mgl32.Vec2
	// contains filtered or unexported fields
}

Texture represents a graphic that can be rendered on any geometry.

func NewTexture

func NewTexture(path string) (*Texture, error)

NewTexture returns a new DDS texture loaded from the given path. WrapS and WrapT are defaultd to ClampToEdge. Repeat is set to 1,1 (no repeat)

func (*Texture) Bind

func (t *Texture) Bind()

Bind binds this texture.

func (*Texture) Unbind

func (t *Texture) Unbind()

Unbind unbinds this texture.

func (*Texture) Unload

func (t *Texture) Unload()

Unload deallocates the opengl texture object.

type Textured

type Textured interface {
	Texture() *Texture
	SetTexture(*Texture)
}

Textured is an interface that indicates that a material can have a texture.

type Transform

type Transform struct {
	Up      mgl32.Vec3
	Right   mgl32.Vec3
	Forward mgl32.Vec3
	// contains filtered or unexported fields
}

Transform stores information about the position, rotation and scale of an 3D object.

func NewTransform

func NewTransform() *Transform

NewTransform creates a new Transform struct with defaults. The given multiplier can be used to invert the matrix, e.g. camera matrix This value should be 1 or -1 (inverted).

Position: 0,0,0 Rotation: 0,0,0 Scale: 1,1,1

Up: 0,1,0 Right: 1,0,0 Forward: 0,0,-1

func (*Transform) LookAt

func (t *Transform) LookAt(x, y, z float32)

LookAt changes the transformation of the 3D object to face the target's position. The model matrix will be updated accordingly.

Note: This transformation makes use of the up vector.

func (*Transform) RotateX

func (t *Transform) RotateX(angle float32)

RotateX rotates the 3D object by the given angle (in radians) around the x axis. The model matrix is updated accordingly.

func (*Transform) RotateY

func (t *Transform) RotateY(angle float32)

RotateY rotates the 3D object by the given angle (in radians) around the x axis. The model matrix is updated accordingly.

func (*Transform) RotateZ

func (t *Transform) RotateZ(angle float32)

RotateZ rotates the 3D object by the given angle (in radians) around the x axis. The model matrix is updated accordingly.

func (*Transform) Scale

func (t *Transform) Scale(x, y, z float32)

Scale sets the scale factor of the 3D object to the given values and updates it's matrix accordingly.

func (*Transform) SetPosition

func (t *Transform) SetPosition(x, y, z float32)

SetPosition sets the position of the 3D object and updates it's matrix accordingly.

func (*Transform) Translate

func (t *Transform) Translate(v mgl32.Vec3)

Translate moves the object by the given vector. The model matrix is updated accordingly.

func (*Transform) TranslateX

func (t *Transform) TranslateX(x float32)

TranslateX moves the object along the x axis by the given units. The model matrix is updated accordingly.

func (*Transform) TranslateY

func (t *Transform) TranslateY(y float32)

TranslateY moves the object along the y axis by the given units. The model matrix is updated accordingly.

func (*Transform) TranslateZ

func (t *Transform) TranslateZ(z float32)

TranslateZ moves the object along the z axis by the given units. The model matrix is updated accordingly.

type Uniform

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

Uniform describes a uniform value which is passed to the shader program.

func NewUniform

func NewUniform(program *Program, identifier string) *Uniform

NewUniform creates a new uniform using the given identifier to lookup the uniform position in the shader program.

type Window

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

Window holds information about the dimensions and title of a window.

func NewWindow

func NewWindow(settings WindowSettings) (*Window, error)

NewWindow creates a new window for the given dimensions and title. The window is created via GLFW.

func (*Window) Height

func (w *Window) Height() int

Height returns the height of the window in pixels.

func (*Window) SetTitle

func (w *Window) SetTitle(title string)

SetTitle sets the window title

func (*Window) ShouldClose

func (w *Window) ShouldClose() bool

ShouldClose indicates if the OS has received a signal to close this window.

func (*Window) Swap

func (w *Window) Swap()

Swap swaps buffers and polls events.

func (*Window) Unload

func (w *Window) Unload()

Unload terminates GLFW and closes the current window.

func (*Window) Width

func (w *Window) Width() int

Width returns the width of the window in pixels.

type WindowSettings

type WindowSettings struct {
	Width      int
	Height     int
	Title      string
	Fullscreen bool
	ClearColor *Color
}

WindowSettings holds information that describe how the window should constructed.

type Wireframed

type Wireframed interface {
	Wireframe() bool
}

Wireframed is an interface that indicates that a material can be rendered as wireframes only.

type Wrapping

type Wrapping int

Wrapping describes an enum for texture wrapping modes. Possible modes are: ClampToEdge, Repeat or MirroredRepeat

const (
	// ClampToEdge extends texture to the nearest edge, texture is not repeated
	ClampToEdge Wrapping = iota
	// Repeat repeats the texture
	Repeat
	// MirroredRepeat repeats texture mirrored
	MirroredRepeat
)

Notes

Bugs

  • Vertex and UV buffers are deleted and re-created; not updated.

Directories

Path Synopsis
examples
Package logging provides basic logging features (modules, log levels).
Package logging provides basic logging features (modules, log levels).

Jump to

Keyboard shortcuts

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