示例#1
0
func (e *Entity) Load(reader io.Reader) {
	decoder := json.NewDecoder(reader)
	m := map[string][4]float32{}
	decoder.Decode(m)
	e.Position = mgl.Vec4(m["Position"])
	q := m["Orientation"]
	e.Orientation.W = q[0]
	e.Orientation.V = mgl.Vec3{q[1], q[2], q[3]}
}
func LoadMesh(filename string) (mesh *renderstuff.Mesh) {
	scene := ai.ImportFile(filename, 0)
	if scene == nil {
		panic(ai.GetErrorString())
	}
	scene = scene.ApplyPostProcessing(ai.Process_Triangulate)
	meshes := scene.Meshes()
	if len(meshes) != 1 {
		fmt.Println("Cameras", len(scene.Cameras()))
		fmt.Println("Animations", len(scene.Animations()))
		panic(fmt.Sprintf("not a single mesh in %s, found %d meshes", filename, len(meshes)))
	}
	aimesh := meshes[0]

	mesh = new(renderstuff.Mesh)

	meshvertices := make([]MeshVertex, aimesh.NumVertices())
	for i, pos := range aimesh.Vertices() {
		v := pos.Values()
		meshvertices[i].Vertex_ms = mgl.Vec4([4]float32{v[0], v[1], v[2], 1})
	}
	for i, norm := range aimesh.Normals() {
		n := norm.Values()
		meshvertices[i].Normal_ms = mgl.Vec4([4]float32{n[0], n[1], n[2], 0})
	}
	mesh.Vertices = meshvertices

	meshindices := make([]MeshIndex, aimesh.NumFaces()*3)
	for i, face := range aimesh.Faces() {
		indices := face.CopyIndices()
		meshindices[i*3+0] = MeshIndex(indices[0])
		meshindices[i*3+1] = MeshIndex(indices[1])
		meshindices[i*3+2] = MeshIndex(indices[2])
	}
	mesh.Indices = meshindices

	mesh.Mode = renderstuff.Triangles
	fmt.Println("loaded mesh with", aimesh.NumVertices(), "Vertices")
	return mesh
}