func parseMaterial(world *scene.Scene, jsondata map[string]interface{}, material_id string) (material *objects.Material) { log.Println("Parsing Json Material") materialdata := jsondata["materials"].(map[string]interface{})[material_id].(map[string]interface{}) material = new(objects.Material) material.ID = material_id material.Type = materialdata["type"].(string) if materialdata["parameters"].(map[string]interface{})["materials"] == nil { material.Color = util.Uint32toVec3ui8(uint32(materialdata["parameters"].(map[string]interface{})["color"].(float64))) material.Ambient = util.Uint32toVec3ui8(uint32(materialdata["parameters"].(map[string]interface{})["ambient"].(float64))) material.Emissive = util.Uint32toVec3ui8(uint32(materialdata["parameters"].(map[string]interface{})["emissive"].(float64))) material.Transparent = materialdata["parameters"].(map[string]interface{})["transparent"].(bool) material.Reflectivity = float32(materialdata["parameters"].(map[string]interface{})["reflectivity"].(float64)) material.Opacity = float32(materialdata["parameters"].(map[string]interface{})["opacity"].(float64)) material.Wireframe = materialdata["parameters"].(map[string]interface{})["wireframe"].(bool) material.WireframeLinewidth = int32(materialdata["parameters"].(map[string]interface{})["wireframeLinewidth"].(float64)) } else { materials := materialdata["parameters"].(map[string]interface{})["materials"].([]interface{}) for _, v := range materials { id := v.(string) if world.HasMaterial(id) { material.Materials = append(material.Materials, world.GetMaterial(id)) } else { material.Materials = append(material.Materials, parseMaterial(world, jsondata, id)) } } } return }
func parseObjects(world *scene.Scene, jsondata map[string]interface{}) { items := jsondata["objects"].(map[string]interface{}) log.Println("Parsing Json Objects") for k, v := range items { if strings.Contains(k, "camera") { val := v.(map[string]interface{}) world.Camera.Type = val["type"].(string) world.Camera.FOV = float32(val["fov"].(float64) * (2 * math.Pi / 360)) world.Camera.Near = float32(val["near"].(float64)) world.Camera.Far = float32(val["far"].(float64)) // world.Camera.Position.X = float32(val["position"].([]interface{})[0].(float64)) // world.Camera.Position.Y = float32(val["position"].([]interface{})[1].(float64)) // world.Camera.Position.Z = float32(val["position"].([]interface{})[2].(float64)) // Let's override the camera position and put it in a sensible position world.Camera.Position.X = 0 world.Camera.Position.Y = 0 world.Camera.Position.Z = float32((-1) * (800 / math.Tan((float64(world.Camera.FOV) / 2)))) world.Camera.Position.W = 1 world.Camera.Rotation = quaternion.Quaternion{X: 0, Y: 0, Z: 0, W: 1} delete(items, k) } else if strings.Contains(k, "light") { var light objects.Light val := v.(map[string]interface{}) light.Type = val["type"].(string) light.Colour = util.Uint32toVec3ui8(uint32(val["color"].(float64))) light.Intensity = float32(val["intensity"].(float64)) light.Direction.X = float32(val["direction"].([]interface{})[0].(float64)) light.Direction.Y = float32(val["direction"].([]interface{})[1].(float64)) light.Direction.Z = float32(val["direction"].([]interface{})[2].(float64)) light.Target = val["target"].(string) world.Lights = append(world.Lights, light) delete(items, k) } } world.Objects = append(world.Objects, parsePhysicalObjects(world, jsondata, jsondata["objects"].(map[string]interface{}))...) }