data

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2013 License: BSD-2-Clause-Views, Zlib Imports: 15 Imported by: 0

Documentation

Overview

Package data turns file resource data into data objects. Specifically data needed by 3D applications. This package:

  • Defines a data type for each of the various data resources.
  • Provides caching and fetching of data resources.
  • Loads data directly from disk for development builds. Production builds load data from a zip file attached to the binary.
  • Loads data such that it is easily consumed by the rendering and audio components.

The resource data that can be loaded from disk are the types exposed by this package:

  Data                          File           Object
 ------                        ------         --------
bitmapped fonts              : txtfile.fnt --> Glyphs
colour and surface data      : txtfile.mtl --> Material
vertex data                  : txtfile.obj --> Mesh
vertex shader program        : txtfile.vsh -┐
fragment shader program      : txtfile.fsh --> Shader
audio                        : binfile.wav --> Sound
images                       : binfile.png --> Texture

Package data is currently intended for smaller 3D applications where data is loaded directly from files to memory, i.e. no database involved.

Package data is provided as part of the vu (virtual universe) 3D engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Glyphs

type Glyphs struct {
	Name string // Unique id for a glyph set.
	// contains filtered or unexported fields
}

Glyphs holds a single bitmapped font. It knows how to pull individual character images out of a single image that contains all the characters for a font. It has to be combined with a texture (the font bitmapped image) in order to produce displayable strings.

Glyphs can be used with any image of the same font and font size with identical bitmapped layouts, i.e. different colours of the same font and font size can use the same Glyph instance.

func (*Glyphs) Panel

func (gs *Glyphs) Panel(b *Mesh, phrase string) (panelWidth int)

Panel creates a string image for the given string using a flat mesh. The mesh has the necessary texture (uv) mapping information and is ready to be combined with a texture corresponding to the bitmapped font. Note that the mesh has not yet been bound to the graphics card.

The width in pixels for the resulting string image is returned.

type Loader

type Loader interface {

	// Load looks for the named resource on disk and returns a corresponding resource
	// data object. The loader will look for "name" with any of the supported file
	// types for the given resource.
	//
	// Load expects data to be a pointer to one of the resource data types.
	// If found the resource data is copied into the supplied data pointer,
	// otherwise the supplied data pointer is set to nil.
	Load(name string, data interface{})

	// Cache the given resource data so that any single resource only needs
	// to be loaded once. Cache expects data to be one of the valid resource data types
	// and to be uniquely named within its data type.
	Cache(data interface{})

	// Cached returns true if the named data resource has already been cached and ready
	// to use. Cached expects data to be one of the valid resource data types.
	Cached(name string, data interface{}) bool

	// Fetch retrieves a previously cached resource using the given name.
	// Fetch expects the resource data to be a pointer to one of the resource data types.
	// If found the resource data is copied into the supplied data pointer.
	// Otherwise the supplied data pointer is set to nil
	Fetch(name string, data interface{})

	// SetDir overrides the default directory location for the given data type.
	// SetDir expects data to be one of the valid resource data types.
	//
	// Note that all directories are expected to be relative to the
	// application location.
	SetDir(dir string, data interface{})

	// Dispose needs to be called to properly terminate and clean up loaded resources.
	Dispose()
}

Loader provides methods for loading, caching, and fetching data resources from disk. The current resource data types are the types exposed by this package:

*Glyphs
*Material
*Mesh
*Shader
*Sound
*Texture

Loader methods log attempts to use unsupported data types as development errors.

func NewLoader

func NewLoader() Loader

NewLoader provides the default loader implmentation.

type Material

type Material struct {
	Name string  // Unique matrial name.
	Kd   Rgb     // Diffuse colour of the material.
	Ka   Rgb     // Ambient colour of the material.
	Ks   Rgb     // Specular colour of the material.
	Tr   float32 // Transparency (alpha, dissolve) for the material.
}

Material is used to cover a mesh. It specifies the surface colour and how the surface is lit. Materials are expected to be combined with a Mesh.

type Mesh

type Mesh struct {
	Name string // Unique mesh name.

	// Vertices are points in 3D space.  Each vertex is specified with
	// (x, y, z, w) where
	//    x, y are  mandatory 2D minimum coordinates,
	//    z can be defaulted to 0 for a 2D vertex, and
	//    w can be defaulted to 1 for a constant depth.
	V []float32 // Arranged as [][4]float32

	// Normals for each vertex. Normals are specified as (x, y, z) where each
	// value is between 0.0 and 1.0. The slice length is expected to be the same
	// length as the vertex slice, and each normal is calculated to be the
	// normalized sum of the normals for each face that shares the vertex.
	N []float32 // Arranged as [][3]float32

	// Texture coordinates. Specifies how the texture data is aligned relative
	// to the fragment. There are two floats for each texture (u, v) where the
	// values are expected to be between 0.0 and 1.0. The texture data is loaded
	// separately from an image file.
	//
	// The texture corresponding to these texture coordinates is expected to be
	// available to the rendering layer at the same time as this mesh.
	T []float32 // Arranged as [][2]float32

	// Faces are used to index the vertex data into shapes. These are
	// expected to be triangles so 3 indices form one face. Each face value
	// refers to a single vertex.
	F []uint16 // Arranged as [][3]uint16

	// Vao is an vertex array object. It is a graphic card reference to the above
	// group of data.  The "*buf" variables are individual graphic card references
	// for the above data.
	Vao  uint32 // Vertex array references all buffers.
	Vbuf uint32 // Vertex buffer.
	Tbuf uint32 // Texture buffer.
	Nbuf uint32 // Normals buffer.
	Fbuf uint32 // Face index buffer.
}

Mesh holds 3D model data in a format that is easily consumed by a rendering layer. A mesh is expected to be reused and thus does not contain any instance information like location or scale.

Keep individual mesh vertices to less than 65,000 so that it works on lower capability systems like mobile devices.

type Rgb

type Rgb struct {
	R float32 // Red.
	G float32 // Green.
	B float32 // Blue.
}

Rgb holds colour information where each field is expected to contain a value from 0.0 to 1.0. A value of 0 means none of that colour while a value of 1.0 means as much as possible of that colour. For example:

black := &Rgb{0, 0, 0}
white := &Rgb{1, 1, 1}
red := &Rgb{1, 0, 0}
gray := &Rgb{0.5, 0.5, 0.5}

type Shader

type Shader struct {
	Name     string           // Unique name (file name) of the shader.
	Vsh      []string         // Vertex shader source.
	Fsh      []string         // Fragment shader source.
	Program  uint32           // Reference to the compiled vertex and fragment program.
	Uniforms map[string]int32 // Uniform data is required.
}

Shader controls rendering. A shader is a set of programs that run on the graphics card. Different shaders are created for different effects. Shaders commonly need input from the CPU side. These input variables are called uniforms.

func (*Shader) EnsureNewLines

func (s *Shader) EnsureNewLines()

EnsureNewLines properly terminates shader program lines for the shader compiler.

type Sound

type Sound struct {
	Name       string // Unique sound name.
	AudioData  []byte // The raw audio data.
	Channels   uint16 // Number of audio channels.
	SampleBits uint16 // 8 bits = 8, 16 bits = 16, etc.
	Frequency  uint32 // 8000, 44100, etc.
	DataSize   uint32 // Size of audio data (total file size minus header size).

	// Buffer is the sound card buffer reference that the sound data is loaded
	// into. Sound buffers can be shared among many sound sources.
	Buffer uint32
}

Sound is a shared audio resource. Note that the audio data has not yet been bound to a sound card.

type Texture

type Texture struct {
	Name string      // Unique name of the texture.
	Img  image.Image // Texture data.
	Tid  uint32      // Graphics card texture identifier.
}

Texture deals with 2D pictures that are mapped onto objects. Textures are copied to the graphics card and expected to be combined with a Mesh.

Jump to

Keyboard shortcuts

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