gwob

package module
Version: v0.0.0-...-619810f Latest Latest
Warning

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

Go to latest
Published: May 24, 2020 License: MIT Imports: 9 Imported by: 2

README

license GoDoc Go Report Card

gwob

gwob - Pure Go Golang parser for Wavefront .OBJ 3D geometry file format

Install

Install with Go Modules (Go 1.11 or higher)

git clone https://github.com/udhos/gwob
cd gwob
go install

Install without Go Modules (Go before 1.11)

go get github.com/udhos/gwob
cd ~/go/src/github.com/udhos/gwob
go install github.com/udhos/gwob

Usage

Import the package in your Go program:

import "github.com/udhos/gwob"

Example:

// Error handling omitted for simplicity.

import "github.com/udhos/gwob"

options := &gwob.ObjParserOptions{} // parser options

o, errObj := gwob.NewObjFromFile("gopher.obj", options) // parse/load OBJ

// Scan OBJ groups
for _, g := range o.Groups {
    // ...
}

Example

Run the example:

cd example
go run main.go

You can supply a custom input OBJ by setting the env var INPUT:

INPUT=gopher.obj go run main.go

If you specify any command line argument, the OBJ will be dumped to stdout:

go run main.go d

See directory example.

Documentation

See the GoDoc documentation.

Documentation

Overview

Package gwob is a pure Go parser for Wavefront .OBJ 3D geometry file format.

Example:

// Error handling omitted for simplicity.

import "github.com/udhos/gwob"

options := &gwob.ObjParserOptions{} // parser options

o, errObj := gwob.NewObjFromFile("gopher.obj", options) // parse

// Scan OBJ groups
for _, g := range o.Groups {
    // snip
}

See also: https://github.com/udhos/gwob

Index

Constants

View Source
const (
	ErrFatal    = true  // ErrFatal means fatal stream error
	ErrNonFatal = false // ErrNonFatal means non-fatal parsing error
)

Internal parsing error

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {
	Name       string
	Smooth     int
	Usemtl     string
	IndexBegin int
	IndexCount int
}

Group holds parser result for a group.

type Material

type Material struct {
	Name  string
	MapKd string
	MapKa string
	MapKs string
	MapD  string
	Bump  string
	MapKe string
	Kd    [3]float32
	Ka    [3]float32
	Ks    [3]float32
	Ns    float32
	Ni    float32
	Illum int
	D     float32
	Tr    float32
}

Material holds information for a material. Kd - diffuse color. Ka - ambient color. Ks - specular color. Ns - specular exponent. Ni - optical density aka. index of refraction. Illum - illumination model enum id. D / Tr - trasparency (Tr = 1 - D) MapKa - ambient map MapKd - diffuse map MapKs - specular map MapD - scalar procedural texture map Bump/map_Bump - bump texture map - modify surface normal Ke/MapKe - emissive map - clara.io extension

type MaterialLib

type MaterialLib struct {
	Lib map[string]*Material
}

MaterialLib stores materials.

func NewMaterialLib

func NewMaterialLib() MaterialLib

NewMaterialLib creates a new material lib.

func ReadMaterialLibFromBuf

func ReadMaterialLibFromBuf(buf []byte, options *ObjParserOptions) (MaterialLib, error)

ReadMaterialLibFromBuf parses material lib from a buffer.

func ReadMaterialLibFromFile

func ReadMaterialLibFromFile(filename string, options *ObjParserOptions) (MaterialLib, error)

ReadMaterialLibFromFile parses material lib from a file.

func ReadMaterialLibFromReader

func ReadMaterialLibFromReader(rd io.Reader, options *ObjParserOptions) (MaterialLib, error)

ReadMaterialLibFromReader parses material lib from a reader.

func ReadMaterialLibFromStringReader

func ReadMaterialLibFromStringReader(rd StringReader, options *ObjParserOptions) (MaterialLib, error)

ReadMaterialLibFromStringReader parses material lib from StringReader.

type Obj

type Obj struct {
	Indices []int
	Coord   []float32 // vertex data pos=(x,y,z) tex=(tx,ty) norm=(nx,ny,nz)
	Mtllib  string
	Groups  []*Group

	BigIndexFound  bool // index larger than 65535
	TextCoordFound bool // texture coord
	NormCoordFound bool // normal coord

	StrideSize           int // (px,py,pz),(tu,tv),(nx,ny,nz) = 8 x 4-byte floats = 32 bytes max
	StrideOffsetPosition int // 0
	StrideOffsetTexture  int // 3 x 4-byte floats
	StrideOffsetNormal   int // 5 x 4-byte floats
}

Obj holds parser result for .obj file.

func NewObjFromBuf

func NewObjFromBuf(objName string, buf []byte, options *ObjParserOptions) (*Obj, error)

NewObjFromBuf parses Obj from a buffer.

func NewObjFromFile

func NewObjFromFile(filename string, options *ObjParserOptions) (*Obj, error)

NewObjFromFile parses Obj from a file.

func NewObjFromReader

func NewObjFromReader(objName string, rd io.Reader, options *ObjParserOptions) (*Obj, error)

NewObjFromReader parses Obj from a reader.

func NewObjFromStringReader

func NewObjFromStringReader(objName string, rd StringReader, options *ObjParserOptions) (*Obj, error)

NewObjFromStringReader parses Obj from a StringReader.

func NewObjFromVertex

func NewObjFromVertex(objName string, coord []float32, indices []int) (*Obj, error)

NewObjFromVertex creates Obj from vertex data.

func (*Obj) Coord64

func (o *Obj) Coord64(i int) float64

Coord64 gets vertex data as float64.

func (*Obj) NumberOfElements

func (o *Obj) NumberOfElements() int

NumberOfElements gets the number of strides.

func (*Obj) ToFile

func (o *Obj) ToFile(filename string) error

ToFile saves OBJ to file.

func (*Obj) ToWriter

func (o *Obj) ToWriter(w io.Writer) error

ToWriter writes OBJ to writer stream.

func (*Obj) VertexCoordinates

func (o *Obj) VertexCoordinates(stride int) (float32, float32, float32)

VertexCoordinates gets vertex coordinates for a stride index.

type ObjParserOptions

type ObjParserOptions struct {
	LogStats      bool
	Logger        func(string)
	IgnoreNormals bool
}

ObjParserOptions sets options for the parser.

type StringReader

type StringReader interface {
	ReadString(delim byte) (string, error) // Example: bufio.Reader
}

StringReader is input for the parser.

Source Files

Directories

Path Synopsis
Package main shows how to use the 'gwob' package to parse geometry data from OBJ files.
Package main shows how to use the 'gwob' package to parse geometry data from OBJ files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL