Beispiel #1
0
func parseGeometry(world *scene.Scene, jsondata map[string]interface{}, geometry_id string) (geometry *objects.Geometry) {
	log.Println("Parsing Json Geometry")
	embeds := jsondata["embeds"].(map[string]interface{})
	geometrydata := jsondata["geometries"].(map[string]interface{})[geometry_id].(map[string]interface{})

	geometry = new(objects.Geometry)
	geometry.ID = geometry_id
	if geometrydata["type"].(string) == "embedded" {
		embed := embeds[geometrydata["id"].(string)].(map[string]interface{})

		// Parse metadata
		geometry.Metadata.Vertices = int32(embed["metadata"].(map[string]interface{})["vertices"].(float64))
		geometry.Metadata.Normals = int32(embed["metadata"].(map[string]interface{})["normals"].(float64))
		geometry.Metadata.Colors = int32(embed["metadata"].(map[string]interface{})["colors"].(float64))
		geometry.Metadata.Faces = int32(embed["metadata"].(map[string]interface{})["faces"].(float64))

		// Parse bounding box
		geometry.BB.Min.X = float32(embed["boundingBox"].(map[string]interface{})["min"].([]interface{})[0].(float64))
		geometry.BB.Min.Y = float32(embed["boundingBox"].(map[string]interface{})["min"].([]interface{})[1].(float64))
		geometry.BB.Min.Z = float32(embed["boundingBox"].(map[string]interface{})["min"].([]interface{})[2].(float64))
		geometry.BB.Max.X = float32(embed["boundingBox"].(map[string]interface{})["max"].([]interface{})[0].(float64))
		geometry.BB.Max.Y = float32(embed["boundingBox"].(map[string]interface{})["max"].([]interface{})[1].(float64))
		geometry.BB.Max.Z = float32(embed["boundingBox"].(map[string]interface{})["max"].([]interface{})[2].(float64))

		geometry.Scale = float32(embed["scale"].(float64))
		vertices := embed["vertices"].([]interface{})
		for i := 0; i < len(vertices); {
			v := vertex.NewVertex(float32(vertices[i].(float64)), float32(vertices[i+1].(float64)), float32(vertices[i+2].(float64)))
			i += 3
			world.Vertices = append(world.Vertices, *v)
			geometry.Vertices = append(geometry.Vertices, &world.Vertices[len(world.Vertices)-1])
		}

		normals := embed["normals"].([]interface{})
		for i := 0; i < len(normals); {
			v := vertex.NewNormal(float32(normals[i].(float64)), float32(normals[i+1].(float64)), float32(normals[i+2].(float64)))
			i += 3
			world.Normals = append(world.Normals, *v)
			geometry.Normals = append(geometry.Normals, &world.Normals[len(world.Normals)-1])
		}

		faces := embed["faces"].([]interface{})
		for i := 0; i < len(faces); {
			mask := uint32(faces[i].(float64))
			i++

			// is not triangle?
			if isBitSet(mask, 0) {
				log.Fatalln("Quads are not supported")
			}
			tri := triangle.Triangle{Vertices: [3]*vertex.Vertex{&world.Vertices[int32(faces[i].(float64))], &world.Vertices[int32(faces[i+1].(float64))], &world.Vertices[int32(faces[i+2].(float64))]}}
			i += 3
			// has face material
			if isBitSet(mask, 1) {
				log.Println("Ignoring face material, material defined in the geometry json")
				i++
			}

			// has face uv
			if isBitSet(mask, 2) {
				log.Fatalln("Face UVs are not supported")
				i++
			}

			// has face vertex uv
			if isBitSet(mask, 3) {
				log.Fatalln("Face vertex UVs are not supported")
				i += 3
			}

			// has face normals
			if isBitSet(mask, 4) {
				log.Fatalln("Face normals are not supported")
				i++
			}

			// has face vertex normals
			if isBitSet(mask, 5) {
				tri.Normals = [3]*vertex.Normal{&world.Normals[int32(faces[i].(float64))], &world.Normals[int32(faces[i+1].(float64))], &world.Normals[int32(faces[i+2].(float64))]}
				i += 3
			}

			// has face color
			if isBitSet(mask, 6) {
				log.Fatalln("Face colors are not supported")
				i++
			}

			if isBitSet(mask, 7) {
				log.Fatalln("Face vertex colors are not supported")
				i += 3
			}

			world.Triangles = append(world.Triangles, tri)
			log.Println(tri)
		}
	} else {
		log.Panicln("I don't know what to do with non-embedded geometry types!")
	}

	return
}