/// <summary> /// Main access point for the XML reader. Reads /// the Scene description as given in Path /// </summary> public void Parse(bool silent) { var state = new XmlReadState { BasePath = System.IO.Path.GetDirectoryName(Path), Scene = Client.Scene, Shader = Client.Scene.DefaultSurface, DicingRate = 0.1f, Smooth = false, Transform = ccl.Transform.Identity(), Silent = silent }; ReadInclude(ref state, System.IO.Path.GetFileName(Path)); }
bool MoveNextRow() { bool moved = this.enumerator.MoveNext(); if (moved) { this.record = this.enumerator.Current; this.xmlState = XmlReadState.Row; if (this.fieldCount == 0) { this.fieldCount = this.record.FieldCount; } } return(moved); }
public override bool Read() { if (this.EOF) { return(false); } if (this.readState == ReadState.Initial) { this.readState = ReadState.Interactive; this.xmlState = XmlReadState.Table; if (!String.IsNullOrEmpty(this.tableName.Namespace)) { AddAttribute("", "xmlns", this.tableName.Namespace); } if (this.xsiNil || this.xsiType) { AddAttribute("xmlns", xsiPrefix, xsiNamespace); } if (this.xsiType) { AddAttribute("xmlns", xsPrefix, "http://www.w3.org/2001/XMLSchema"); } return(true); } this.attribs.Clear(); if (this.multipartClose > 0) { this.multipartClose--; this.multipartNameIndex--; this.depth--; if (this.multipartClose == 0) { this.xmlState = XmlReadState.Field; if (this.multipartNameIndex == this.name.Length - 1) { ReadValue(); } } return(true); } switch (this.xmlState) { case XmlReadState.Table: if (MoveNextRow()) { this.depth++; } else { this.xmlState = XmlReadState.EndTable; } break; case XmlReadState.Row: if (this.fieldCount == 0) { this.xmlState = XmlReadState.EndRow; Debug.Assert(this.depth == 2); } else { if (this.xsiNil) { this.ordinal = 0; } else { for (int i = 0; i < this.fieldCount; i++) { if (!this.record.IsDBNull(i)) { this.ordinal = i; break; } } } if (this.ordinal == -1) { this.xmlState = XmlReadState.EndRow; Debug.Assert(this.depth == 2); } else { this.xmlState = XmlReadState.Field; this.depth++; ReadField(); if (this.multipartNameIndex == this.name.Length - 1) { ReadValue(); } } } break; case XmlReadState.Field: if (this.IsEmptyElement) { this.xmlState = XmlReadState.EndField; goto case XmlReadState.EndField; } else { this.depth++; if (this.multipartNameIndex < this.name.Length - 1) { this.multipartNameIndex++; if (this.multipartNameIndex == this.name.Length - 1) { ReadValue(); } break; } this.xmlState = XmlReadState.Value; } break; case XmlReadState.Value: this.depth--; this.xmlState = XmlReadState.EndField; break; case XmlReadState.EndField: int nextOrdinal = this.ordinal + 1; this.ordinal = -1; if (nextOrdinal > 0) { if (this.xsiNil) { this.ordinal = nextOrdinal; } else { for (int i = nextOrdinal; i < this.fieldCount; i++) { if (!this.record.IsDBNull(i)) { this.ordinal = i; break; } } } } if (this.ordinal == -1 || this.ordinal + 1 > this.fieldCount) { this.depth--; if (this.multipartNameIndex > 0) { this.multipartNameIndex--; break; } this.ordinal = -1; this.xmlState = XmlReadState.EndRow; } else { ReadField(); if (this.multipartNameIndex > 0) { // e.g. // prevName: A$B$C // name: A$D$E$F // close C, B -> open D, E, F int i = 0; for (; i < this.name.Length - 1; i++) { if (i > this.prevName.Length - 1 || this.name[i] != this.prevName[i]) { break; } } this.multipartClose = this.prevName.Length - i - 1; if (this.multipartClose > 0) { break; } } this.xmlState = XmlReadState.Field; if (this.multipartNameIndex == this.name.Length - 1) { ReadValue(); } } break; case XmlReadState.EndRow: if (!MoveNextRow()) { this.xmlState = XmlReadState.EndTable; this.depth--; } break; case XmlReadState.EndTable: this.eof = true; Debug.Assert(this.depth == 0); return(false); } return(true); }
bool MoveNextRow() { bool moved = this.enumerator.MoveNext(); if (moved) { this.record = this.enumerator.Current; this.xmlState = XmlReadState.Row; if (this.fieldCount == 0) this.fieldCount = this.record.FieldCount; } return moved; }
public override bool Read() { if (this.EOF) return false; if (this.readState == ReadState.Initial) { this.readState = ReadState.Interactive; this.xmlState = XmlReadState.Table; if (!String.IsNullOrEmpty(this.tableName.Namespace)) AddAttribute("", "xmlns", this.tableName.Namespace); if (this.xsiNil || this.xsiType) AddAttribute("xmlns", xsiPrefix, xsiNamespace); if (this.xsiType) AddAttribute("xmlns", xsPrefix, "http://www.w3.org/2001/XMLSchema"); return true; } this.attribs.Clear(); if (this.multipartClose > 0) { this.multipartClose--; this.multipartNameIndex--; this.depth--; if (this.multipartClose == 0) { this.xmlState = XmlReadState.Field; if (this.multipartNameIndex == this.name.Length - 1) ReadValue(); } return true; } switch (this.xmlState) { case XmlReadState.Table: if (MoveNextRow()) { this.depth++; } else { this.xmlState = XmlReadState.EndTable; } break; case XmlReadState.Row: if (this.fieldCount == 0) { this.xmlState = XmlReadState.EndRow; Debug.Assert(this.depth == 2); } else { if (this.xsiNil) { this.ordinal = 0; } else { for (int i = 0; i < this.fieldCount; i++) { if (!this.record.IsDBNull(i)) { this.ordinal = i; break; } } } if (this.ordinal == -1) { this.xmlState = XmlReadState.EndRow; Debug.Assert(this.depth == 2); } else { this.xmlState = XmlReadState.Field; this.depth++; ReadField(); if (this.multipartNameIndex == this.name.Length - 1) ReadValue(); } } break; case XmlReadState.Field: if (this.IsEmptyElement) { this.xmlState = XmlReadState.EndField; goto case XmlReadState.EndField; } else { this.depth++; if (this.multipartNameIndex < this.name.Length - 1) { this.multipartNameIndex++; if (this.multipartNameIndex == this.name.Length - 1) ReadValue(); break; } this.xmlState = XmlReadState.Value; } break; case XmlReadState.Value: this.depth--; this.xmlState = XmlReadState.EndField; break; case XmlReadState.EndField: int nextOrdinal = this.ordinal + 1; this.ordinal = -1; if (nextOrdinal > 0) { if (this.xsiNil) { this.ordinal = nextOrdinal; } else { for (int i = nextOrdinal; i < this.fieldCount; i++) { if (!this.record.IsDBNull(i)) { this.ordinal = i; break; } } } } if (this.ordinal == -1 || this.ordinal + 1 > this.fieldCount) { this.depth--; if (this.multipartNameIndex > 0) { this.multipartNameIndex--; break; } this.ordinal = -1; this.xmlState = XmlReadState.EndRow; } else { ReadField(); if (this.multipartNameIndex > 0) { // e.g. // prevName: A$B$C // name: A$D$E$F // close C, B -> open D, E, F int i = 0; for (; i < this.name.Length - 1; i++) { if (i > this.prevName.Length - 1 || this.name[i] != this.prevName[i]) break; } this.multipartClose = this.prevName.Length - i - 1; if (this.multipartClose > 0) break; } this.xmlState = XmlReadState.Field; if (this.multipartNameIndex == this.name.Length - 1) ReadValue(); } break; case XmlReadState.EndRow: if (!MoveNextRow()) { this.xmlState = XmlReadState.EndTable; this.depth--; } break; case XmlReadState.EndTable: this.eof = true; Debug.Assert(this.depth == 0); return false; } return true; }
public XmlReadState(XmlReadState old) : this(old.Scene, new Transform(old.Transform), old.Smooth, old.Shader, old.BasePath, old.DicingRate, old.Silent) { }
/// <summary> /// Read the scene description from the file in src /// </summary> /// <param name="state"></param> /// <param name="src"></param> public void ReadInclude(ref XmlReadState state, string src) { var path = System.IO.Path.Combine(state.BasePath, src); var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment, IgnoreComments = true, IgnoreProcessingInstructions = true, IgnoreWhitespace = true }; var reader = System.Xml.XmlReader.Create(path, settings); var substate = new XmlReadState(state) { BasePath = System.IO.Path.GetDirectoryName(path) }; ReadScene(ref substate, reader); reader.Close(); }
private void ReadScene(ref XmlReadState state, System.Xml.XmlReader node) { while (node.Read()) { if (!node.IsStartElement()) continue; if(!state.Silent) Console.WriteLine("XML node: {0}", node.Name); switch (node.Name) { case "camera": ReadCamera(ref state, node.ReadSubtree()); break; case "background": ReadBackground(ref state, node.ReadSubtree()); break; case "transform": var transform_substate = new XmlReadState(state); var t = transform_substate.Transform; ReadTransform(node, ref t); transform_substate.Transform = t; node.Read(); /* advance forward one, otherwise we'll end up in internal loop */ ReadScene(ref transform_substate, node.ReadSubtree()); break; case "state": var state_substate = new XmlReadState(state); ReadState(ref state_substate, node.ReadSubtree()); node.Read(); /* advance one forward */ ReadScene(ref state_substate, node.ReadSubtree()); break; case "integrator": ReadIntegrator(ref state, node.ReadSubtree()); break; case "shader": var shader_substate = new XmlReadState(state); ReadShader(ref shader_substate, node.ReadSubtree()); break; case "mesh": ReadMesh(ref state, node.ReadSubtree()); break; case "include": var src = node.GetAttribute("src"); if (!string.IsNullOrEmpty(src)) { ReadInclude(ref state, src); } break; default: Console.WriteLine("Uknown node {0}", node.Name); break; } } }
private void ReadState(ref XmlReadState state, System.Xml.XmlReader node) { node.Read(); var shader = node.GetAttribute("shader"); var dicing_rate = node.GetAttribute("dicing_rate"); var interpolation = node.GetAttribute("interpolation"); var displacement_method = node.GetAttribute("displacement_method"); if (!string.IsNullOrEmpty(shader)) { state.Shader = state.Scene.ShaderWithName(shader); } if (!string.IsNullOrEmpty(dicing_rate)) state.DicingRate = float.Parse(dicing_rate, NumberFormatInfo); if (!string.IsNullOrEmpty(interpolation)) state.Smooth = interpolation.Equals("smooth", StringComparison.OrdinalIgnoreCase); if (!string.IsNullOrEmpty(displacement_method)) { /* \todo wrap displacement method stuff */ } }
private void ReadMesh(ref XmlReadState state, System.Xml.XmlReader node) { node.Read(); var P = node.GetAttribute("P"); var UV = node.GetAttribute("UV"); var nverts = node.GetAttribute("nverts"); var verts = node.GetAttribute("verts"); if(!state.Silent) Console.WriteLine("{0}", node); var has_uv = !string.IsNullOrEmpty(UV); float[] uvfloats = null; if (has_uv) { uvfloats = Utilities.Instance.parse_floats(UV); } var pfloats = Utilities.Instance.parse_floats(P); var nvertsints = Utilities.Instance.parse_ints(nverts); var vertsints = Utilities.Instance.parse_ints(verts); var ob = new ccl.Object(Client) { Transform = state.Transform }; var me = new Mesh(Client, state.Shader); ob.Mesh = me; me.SetVerts(ref pfloats); var index_offset = 0; /* count triangles */ var fc = nvertsints.Aggregate(0, (total, next) => next == 4 ? total + 2 : total + 1); float[] uvs = null; if(has_uv) uvs = new float[fc*3*2]; var uvoffs = 0; foreach (var t in nvertsints) { for (var j = 0; j < t - 2; j++) { var v0 = vertsints[index_offset]; var v1 = vertsints[index_offset + j + 1]; var v2 = vertsints[index_offset + j + 2]; if (has_uv) { uvs[uvoffs] = uvfloats[index_offset*2]; uvs[uvoffs + 1] = uvfloats[index_offset*2 + 1]; uvs[uvoffs + 2] = uvfloats[(index_offset + j + 1)*2]; uvs[uvoffs + 3] = uvfloats[(index_offset + j + 1)*2 + 1]; uvs[uvoffs + 4] = uvfloats[(index_offset + j + 2)*2]; uvs[uvoffs + 5] = uvfloats[(index_offset + j + 2)*2 + 1]; uvoffs += 6; } me.AddTri((uint)v0, (uint)v1, (uint)v2, state.Shader, state.Smooth); } index_offset += t; } if (has_uv) { me.SetUvs(ref uvs); } }
private void ReadIntegrator(ref XmlReadState state, System.Xml.XmlReader node) { node.Read(); /* \todo AA sample stuff */ var boolvar = false; var intvar = 0; var floatvar = 0.0f; var stringvar = ""; Utilities.Instance.get_bool(ref boolvar, node.GetAttribute("branched")); state.Scene.Integrator.IntegratorMethod = boolvar ? IntegratorMethod.BranchedPath : IntegratorMethod.Path; if (Utilities.Instance.get_bool(ref boolvar, node.GetAttribute("sample_all_lights_direct"))) state.Scene.Integrator.SampleAllLightsDirect = boolvar; if (Utilities.Instance.get_bool(ref boolvar, node.GetAttribute("sample_all_lights_indirect"))) state.Scene.Integrator.SampleAllLightsIndirect = boolvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("diffuse_samples"))) state.Scene.Integrator.DiffuseSamples = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("glossy_samples"))) state.Scene.Integrator.GlossySamples = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("transmission_samples"))) state.Scene.Integrator.TransmissionSamples = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("ao_samples"))) state.Scene.Integrator.AoSamples = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("mesh_light_samples"))) state.Scene.Integrator.MeshLightSamples = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("subsurface_samples"))) state.Scene.Integrator.SubsurfaceSamples = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("volume_samples"))) state.Scene.Integrator.VolumeSamples = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("min_bounce"))) state.Scene.Integrator.MinBounce = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("max_bounce"))) state.Scene.Integrator.MaxBounce = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("max_diffuse_bounce"))) state.Scene.Integrator.MaxDiffuseBounce = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("max_glossy_bounce"))) state.Scene.Integrator.MaxGlossyBounce = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("max_transmission_bounce"))) state.Scene.Integrator.MaxTransmissionBounce = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("max_volume_bounce"))) state.Scene.Integrator.MaxVolumeBounce = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("transparent_min_bounce"))) state.Scene.Integrator.TransparentMinBounce = intvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("transparent_max_bounce"))) state.Scene.Integrator.TransparentMaxBounce = intvar; if (Utilities.Instance.get_bool(ref boolvar, node.GetAttribute("transparent_shadows"))) state.Scene.Integrator.TransparentShadows = boolvar; if (Utilities.Instance.get_float(ref floatvar, node.GetAttribute("volume_step_size"))) state.Scene.Integrator.VolumeStepSize = floatvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("volume_max_steps"))) state.Scene.Integrator.VolumeMaxSteps = intvar; /* \todo wrap caustics form separation * if (Utilities.Instance.get_bool(ref boolvar, node.GetAttribute("caustics_reflective"))) state.Scene.Integrator.DoCausticsReflective = boolvar; if (Utilities.Instance.get_bool(ref boolvar, node.GetAttribute("caustics_refractive"))) state.Scene.Integrator.DoCausticsRefractive = boolvar; */ if (Utilities.Instance.get_bool(ref boolvar, node.GetAttribute("no_caustics"))) state.Scene.Integrator.NoCaustics = boolvar; if (Utilities.Instance.get_float(ref floatvar, node.GetAttribute("filter_glossy"))) state.Scene.Integrator.FilterGlossy = floatvar; if (Utilities.Instance.get_int(ref intvar, node.GetAttribute("seed"))) state.Scene.Integrator.Seed = intvar; if (Utilities.Instance.get_float(ref floatvar, node.GetAttribute("sample_clamp_direct"))) state.Scene.Integrator.SampleClampDirect = floatvar; if (Utilities.Instance.get_float(ref floatvar, node.GetAttribute("sample_clamp_indirect"))) state.Scene.Integrator.SampleClampIndirect = floatvar; if (Utilities.Instance.read_string(ref stringvar, node.GetAttribute("sampling_pattern"))) state.Scene.Integrator.SamplingPattern = stringvar.Equals("sobol") ? SamplingPattern.Sobol : SamplingPattern.CMJ; state.Scene.Integrator.TagForUpdate(); }
private void ReadCamera(ref XmlReadState state, System.Xml.XmlReader node) { node.Read(); var width = node.GetAttribute("width"); var height = node.GetAttribute("height"); var type = node.GetAttribute("type"); var fov = node.GetAttribute("fov"); var nearclip = node.GetAttribute("nearclip"); var farclip = node.GetAttribute("farclip"); var aperturesize = node.GetAttribute("aperturesize"); var focaldistance = node.GetAttribute("focaldistance"); var shuttertime = node.GetAttribute("shuttertime"); var panorama_type = node.GetAttribute("panorama_type"); var fisheye_fov = node.GetAttribute("fisheye_fov"); var fisheye_lens = node.GetAttribute("fisheye_lens"); var sensorwidth = node.GetAttribute("sensorwidth"); var sensorheight = node.GetAttribute("sensorheight"); if (!string.IsNullOrEmpty(width) && !string.IsNullOrEmpty(height)) Client.Scene.Camera.Size = new Size(int.Parse(width), int.Parse(height)); if (!string.IsNullOrEmpty(type)) { CameraType camera_type; if (Enum.TryParse(type, true, out camera_type)) { Client.Scene.Camera.Type = camera_type; } } if (!string.IsNullOrEmpty(panorama_type)) { PanoramaType panoramatype; if (Enum.TryParse(panorama_type, true, out panoramatype)) { Client.Scene.Camera.PanoramaType = panoramatype; } } if (!string.IsNullOrEmpty(fov)) Client.Scene.Camera.Fov = float.Parse(fov, NumberFormatInfo); if (!string.IsNullOrEmpty(nearclip)) Client.Scene.Camera.NearClip = float.Parse(nearclip, NumberFormatInfo); if (!string.IsNullOrEmpty(farclip)) Client.Scene.Camera.FarClip = float.Parse(farclip, NumberFormatInfo); if (!string.IsNullOrEmpty(aperturesize)) Client.Scene.Camera.ApertureSize = float.Parse(aperturesize, NumberFormatInfo); if (!string.IsNullOrEmpty(focaldistance)) Client.Scene.Camera.FocalDistance = float.Parse(focaldistance, NumberFormatInfo); if (!string.IsNullOrEmpty(shuttertime)) Client.Scene.Camera.ShutterTime = float.Parse(shuttertime, NumberFormatInfo); if (!string.IsNullOrEmpty(fisheye_fov)) Client.Scene.Camera.FishEyeFov = float.Parse(fisheye_fov, NumberFormatInfo); if (!string.IsNullOrEmpty(fisheye_lens)) Client.Scene.Camera.FishEyeLens = float.Parse(fisheye_lens, NumberFormatInfo); if (!string.IsNullOrEmpty(sensorwidth)) Client.Scene.Camera.SensorWidth = float.Parse(sensorwidth, NumberFormatInfo); if (!string.IsNullOrEmpty(sensorheight)) Client.Scene.Camera.SensorHeight = float.Parse(sensorheight, NumberFormatInfo); Client.Scene.Camera.Matrix = state.Transform; Client.Scene.Camera.ComputeAutoViewPlane(); Client.Scene.Camera.Update(); }
private void ReadBackground(ref XmlReadState state, System.Xml.XmlReader node) { node.Read(); if(!state.Silent) Console.WriteLine("Background shader"); var shader = new Shader(Client, Shader.ShaderType.World) {Name = Guid.NewGuid().ToString()}; Utilities.Instance.ReadNodeGraph(ref shader, node.ReadSubtree()); state.Scene.AddShader(shader); state.Scene.Background.Shader = shader; }
public void ReadShader(ref XmlReadState state, System.Xml.XmlReader node) { node.Read(); var name = node.GetAttribute("name"); if(!state.Silent) Console.WriteLine("Shader: {0}", node.GetAttribute("name")); if (string.IsNullOrEmpty(name)) return; var shader = new Shader(Client, Shader.ShaderType.Material) {Name = name}; Utilities.Instance.ReadNodeGraph(ref shader, node.ReadSubtree()); state.Scene.AddShader(shader); }