func LoadObjFile(file string) (*Object, error) {
	r, err := loader.Load(file)
	if err != nil {
		return nil, err
	}
	result := Object{}
	scanner := bufio.NewScanner(r)

	var verts []mgl32.Vec3
	var normals []mgl32.Vec3
	var texCoords []mgl32.Vec2
	mtllib := make(map[string]material)
	var g group

	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, commentPrefix) {
			continue
		}
		if strings.HasPrefix(line, mtllibPrefix) {
			mtllibFilename := ""
			if _, err := fmt.Sscanf(line, mtllibFormat, &mtllibFilename); err != nil {
				return nil, fmt.Errorf("Got error while parsing mtllib line: %s, %v", line, err)
			}
			if len(mtllibFilename) == 0 {
				return nil, fmt.Errorf("Got empty filename for mtllib in: %s", file)
			}
			newmtllib, err := loadMtlFile(mtllibFilename)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing mtl file %s: %v", mtllibFilename, err)
			}
			// Merge the new map with any previously seen maps.
			for k, v := range newmtllib {
				mtllib[k] = v
			}
		}
		if strings.HasPrefix(line, gPrefix) {
			if g.end > g.start {
				result.groups = append(result.groups, g)
				g = group{
					start: int32(len(result.vertices)) + 1,
					end:   int32(len(result.vertices)) + 1,
				}
			}
		}
		if strings.HasPrefix(line, usemtlPrefix) {
			usemtlValue := ""
			if _, err := fmt.Sscanf(line, usemtlFormat, &usemtlValue); err != nil {
				return nil, fmt.Errorf("Got error while parsing usemtl line: %s, %v", line, err)
			}
			if len(usemtlValue) == 0 {
				return nil, fmt.Errorf("Got empty material name for usemtl in: %s", file)
			}
			mat, ok := mtllib[usemtlValue]
			if !ok {
				return nil, fmt.Errorf("Attempted to use material that has not been loaded: %s", usemtlValue)
			}
			g.mat = mat
		}
		if strings.HasPrefix(line, vPrefix) {
			vert, err := parseVec3Line(vFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing vertex line: %s, %v", line, err)
			}
			verts = append(verts, vert)
		}
		if strings.HasPrefix(line, vnPrefix) {
			normal, err := parseVec3Line(vnFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing normal line: %s, %v", line, err)
			}
			normals = append(normals, normal)
		}
		if strings.HasPrefix(line, vtPrefix) {
			texCoord, err := parseVec2Line(vtFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing texcoord line: %s, %v", line, err)
			}
			texCoords = append(texCoords, texCoord)
		}
		if strings.HasPrefix(line, fPrefix) {
			v1, t1, n1, v2, t2, n2, v3, t3, n3, err := parseFaceLine(fFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing face line: %s, %v", line, err)
			}
			vert1 := Vertex{
				Vert: verts[v1-1],
				Norm: normals[n1-1],
				UV:   texCoords[t1-1],
			}
			vert2 := Vertex{
				Vert: verts[v2-1],
				Norm: normals[n2-1],
				UV:   texCoords[t2-1],
			}
			vert3 := Vertex{
				Vert: verts[v3-1],
				Norm: normals[n3-1],
				UV:   texCoords[t3-1],
			}
			g.end += 3
			result.vertices = append(result.vertices, vert1, vert2, vert3)
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	if g.end > g.start {
		result.groups = append(result.groups, g)
		g = group{
			start: int32(len(result.vertices)),
		}
	}
	return &result, nil
}
// LoadObjFile loads the provided .obj file.
LoadObjFile loads the provided .obj file.
// LoadObjFile loads the provided . obj file .
func LoadObjFile(file string) (*Object, error) {
	r, err := loader.Load(file)
	if err != nil {
		return nil, err
	}
	result := Object{}
	scanner := bufio.NewScanner(r)
	var verts []mgl32.Vec3
	var normals []mgl32.Vec3
	var texCoords []mgl32.Vec2
	mtllib := make(map[string]material)
	var g group
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, commentPrefix) {
			continue
		}
		if strings.HasPrefix(line, mtllibPrefix) {
			mtllibFilename := ""
			if _, err := fmt.Sscanf(line, mtllibFormat, &mtllibFilename); err != nil {
				return nil, fmt.Errorf("Got error while parsing mtllib line: %s, %v", line, err)
			}
			if len(mtllibFilename) == 0 {
				return nil, fmt.Errorf("Got empty filename for mtllib in: %s", file)
			}
			newmtllib, err := loadMtlFile(mtllibFilename)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing mtl file %s: %v", mtllibFilename, err)
			}
			for k, v := range newmtllib {
				mtllib[k] = v
			}
		}
		if strings.HasPrefix(line, gPrefix) {
			if g.end > g.start {
				result.groups = append(result.groups, g)
				g = group{
					start: int32(len(result.vertices)) + 1,
					end:   int32(len(result.vertices)) + 1,
				}
			}
		}
		if strings.HasPrefix(line, usemtlPrefix) {
			usemtlValue := ""
			if _, err := fmt.Sscanf(line, usemtlFormat, &usemtlValue); err != nil {
				return nil, fmt.Errorf("Got error while parsing usemtl line: %s, %v", line, err)
			}
			if len(usemtlValue) == 0 {
				return nil, fmt.Errorf("Got empty material name for usemtl in: %s", file)
			}
			mat, ok := mtllib[usemtlValue]
			if !ok {
				return nil, fmt.Errorf("Attempted to use material that has not been loaded: %s", usemtlValue)
			}
			g.mat = mat
		}
		if strings.HasPrefix(line, vPrefix) {
			vert, err := parseVec3Line(vFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing vertex line: %s, %v", line, err)
			}
			verts = append(verts, vert)
		}
		if strings.HasPrefix(line, vnPrefix) {
			normal, err := parseVec3Line(vnFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing normal line: %s, %v", line, err)
			}
			normals = append(normals, normal)
		}
		if strings.HasPrefix(line, vtPrefix) {
			texCoord, err := parseVec2Line(vtFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing texcoord line: %s, %v", line, err)
			}
			texCoords = append(texCoords, texCoord)
		}
		if strings.HasPrefix(line, fPrefix) {
			v1, t1, n1, v2, t2, n2, v3, t3, n3, err := parseFaceLine(fFormat, line)
			if err != nil {
				return nil, fmt.Errorf("Got error while parsing face line: %s, %v", line, err)
			}
			vert1 := Vertex{
				Vert: verts[v1-1],
				Norm: normals[n1-1],
				UV:   texCoords[t1-1],
			}
			vert2 := Vertex{
				Vert: verts[v2-1],
				Norm: normals[n2-1],
				UV:   texCoords[t2-1],
			}
			vert3 := Vertex{
				Vert: verts[v3-1],
				Norm: normals[n3-1],
				UV:   texCoords[t3-1],
			}
			g.end += 3
			result.vertices = append(result.vertices, vert1, vert2, vert3)
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	if g.end > g.start {
		result.groups = append(result.groups, g)
		g = group{
			start: int32(len(result.vertices)),
		}
	}
	return &result, nil
}
func LoadObjFile ( file string ) ( * Object , error ) { r , err := loader . Load ( file ) if err != nil { return nil , err } result := Object { } scanner := bufio . NewScanner ( r ) var verts [ ] mgl32 . Vec3 var normals [ ] mgl32 . Vec3 var texCoords [ ] mgl32 . Vec2 mtllib := make ( map [ string ] material ) var g group for scanner . Scan ( ) { line := scanner . Text ( ) if strings . HasPrefix ( line , commentPrefix ) { continue } if strings . HasPrefix ( line , mtllibPrefix ) { mtllibFilename := " " if _ , err := fmt . Sscanf ( line , mtllibFormat , & mtllibFilename ) ; err != nil { return nil , fmt . Errorf ( " " , line , err ) } if len ( mtllibFilename ) == 0 { return nil , fmt . Errorf ( " " , file ) } newmtllib , err := loadMtlFile ( mtllibFilename ) if err != nil { return nil , fmt . Errorf ( " " , mtllibFilename , err ) } for k , v := range newmtllib { mtllib [ k ] = v } } if strings . HasPrefix ( line , gPrefix ) { if g . end > g . start { result . groups = append ( result . groups , g ) g = group { start : int32 ( len ( result . vertices ) ) + 1 , end : int32 ( len ( result . vertices ) ) + 1 , } } } if strings . HasPrefix ( line , usemtlPrefix ) { usemtlValue := " " if _ , err := fmt . Sscanf ( line , usemtlFormat , & usemtlValue ) ; err != nil { return nil , fmt . Errorf ( " " , line , err ) } if len ( usemtlValue ) == 0 { return nil , fmt . Errorf ( " " , file ) } mat , ok := mtllib [ usemtlValue ] if ! ok { return nil , fmt . Errorf ( " " , usemtlValue ) } g . mat = mat } if strings . HasPrefix ( line , vPrefix ) { vert , err := parseVec3Line ( vFormat , line ) if err != nil { return nil , fmt . Errorf ( " " , line , err ) } verts = append ( verts , vert ) } if strings . HasPrefix ( line , vnPrefix ) { normal , err := parseVec3Line ( vnFormat , line ) if err != nil { return nil , fmt . Errorf ( " " , line , err ) } normals = append ( normals , normal ) } if strings . HasPrefix ( line , vtPrefix ) { texCoord , err := parseVec2Line ( vtFormat , line ) if err != nil { return nil , fmt . Errorf ( " " , line , err ) } texCoords = append ( texCoords , texCoord ) } if strings . HasPrefix ( line , fPrefix ) { v1 , t1 , n1 , v2 , t2 , n2 , v3 , t3 , n3 , err := parseFaceLine ( fFormat , line ) if err != nil { return nil , fmt . Errorf ( " " , line , err ) } vert1 := Vertex { Vert : verts [ v1 - 1 ] , Norm : normals [ n1 - 1 ] , UV : texCoords [ t1 - 1 ] , } vert2 := Vertex { Vert : verts [ v2 - 1 ] , Norm : normals [ n2 - 1 ] , UV : texCoords [ t2 - 1 ] , } vert3 := Vertex { Vert : verts [ v3 - 1 ] , Norm : normals [ n3 - 1 ] , UV : texCoords [ t3 - 1 ] , } g . end += 3 result . vertices = append ( result . vertices , vert1 , vert2 , vert3 ) } } if err := scanner . Err ( ) ; err != nil { return nil , err } if g . end > g . start { result . groups = append ( result . groups , g ) g = group { start : int32 ( len ( result . vertices ) ) , } } return & result , nil }
LoadObjFile loads the provided .obj file.
LoadObjFile loads the provided . obj file .
// Merge the new map with any previously seen maps.