Exemple #1
0
//imports an obj from a filePath and return a Geometry
func ImportObj(filePath string) (geometry *renderer.Geometry, material *renderer.Material, err error) {

	obj := &objData{Indicies: make([]uint32, 0, 0), Vertices: make([]float32, 0, 0)}
	vertexList := make([]float32, 0, 0)
	uvList := make([]float32, 0, 0)
	normalList := make([]float32, 0, 0)

	//split the file name from the file path
	filePathTokens := strings.Split(strings.Replace(filePath, "\\", "/", -1), "/")
	fileName := filePathTokens[len(filePathTokens)-1]
	path := strings.TrimRight(filePath, fileName)

	//open the file and read all lines
	file, err := os.Open(filePath)
	if err != nil {
		fmt.Printf("Error opening geometry file: %v\n", err)
		return
	}
	defer file.Close()

	var mtlErr error
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		tokens := strings.Fields(line)
		if len(tokens) > 0 {
			dataType := tokens[0]
			if dataType == "o" { //sub mesh
				obj.Name = tokens[1]
			} else if dataType == "v" { //xyz vertex
				vertexList = append(vertexList, stf(tokens[1]), stf(tokens[2]), stf(tokens[3]))
			} else if dataType == "vt" { //uv coord
				uvList = append(uvList, stf(tokens[1]), stf(tokens[2]))
			} else if dataType == "vn" { //xyz vertex normal
				normalList = append(normalList, stf(tokens[1]), stf(tokens[2]), stf(tokens[3]))
			} else if dataType == "f" { // v/t/n face
				obj.processFace(line, vertexList, uvList, normalList)
			} else if dataType == "mtllib" {
				obj.Mtl, mtlErr = importMTL(path, tokens[1])
			} else if dataType == "usemtl" { //mtl material
				//TODO: multiple mtls
			}
		}
	}

	geometry = renderer.CreateGeometry(obj.Indicies, obj.Vertices)
	if mtlErr == nil && obj.Mtl != nil {
		textures := []*renderer.Texture{}
		for key, img := range obj.Mtl.maps {
			textures = append(textures, renderer.NewTexture(key, img, true))
		}
		material = renderer.NewMaterial(textures...)
	}

	if err = scanner.Err(); err != nil {
		fmt.Printf("Error loading geometry: %v\n", err)
		return
	}
	return
}
Exemple #2
0
func CreateParticleSystem(settings ParticleSettings) *ParticleSystem {
	geometry := renderer.CreateGeometry(make([]uint32, 0, 0), make([]float32, 0, 0))
	node := renderer.NewNode()
	node.Add(geometry)

	ps := ParticleSystem{
		Settings:   settings,
		FaceCamera: true,
		Node:       node,
		geometry:   geometry,
		particles:  make([]*Particle, settings.MaxParticles),
	}
	ps.initParitcles()
	return &ps
}