private void ProcessLine(string[] tokens) { string command = tokens[0]; TargetedCamera camera = (TargetedCamera) _scene.Camera; switch (command) { case "size": { camera.Width = tokens.ToInt(1); camera.Height = tokens.ToInt(2); break; } case "maxdepth ": { _scene.MaxDepth = tokens.ToInt(1); break; } case "output": { _scene.OutputFileName = tokens[1]; break; } case "camera": { camera.Translation = Matrix.CreateTranslation(tokens.ToVec3(1)); camera.Target = tokens.ToVec3(4); camera.Up = tokens.ToVec3(7); float yFov = tokens.ToFloat(10); camera.Fov = yFov; camera.UseXFov = false; break; } case "sphere": { Sphere sphere = new Sphere(tokens.ToVec3(1), tokens.ToFloat(4)) { Material = (Material) _material.Clone(), Transform = _transforms.Peek() }; _scene.Geomertries.Add(sphere); break; } case "maxverts": { _vertices = new List<Vector3>(tokens.ToInt(1)); break; } case "maxvertnorms": { _vertices = new List<Vector3>(tokens.ToInt(1)); _verticesNormals = new List<Vector3>(tokens.ToInt(1)); break; } case "vertex": { _vertices.Add(tokens.ToVec3(1)); break; } case "vertexnormal": { _vertices.Add(tokens.ToVec3(1)); _verticesNormals.Add(tokens.ToVec3(4)); break; } case "tri": { // Triangle triangle = new Triangle(_vertices[tokens.ToInt(1)], _vertices[tokens.ToInt(2)], _vertices[tokens.ToInt(3)]) // { // Material = (Material) _material.Clone(), // Transform = _transforms.Peek() // }; CalculatedTriangle triangle = new CalculatedTriangle(_vertices[tokens.ToInt(1)], _vertices[tokens.ToInt(2)], _vertices[tokens.ToInt(3)],_transforms.Peek()) { Material = (Material) _material.Clone() }; _scene.Geomertries.Add(triangle); break; } case "trinormal": { Triangle triangle = new Triangle(_vertices[tokens.ToInt(1)], _vertices[tokens.ToInt(2)], _vertices[tokens.ToInt(3)]) { Na = _verticesNormals[tokens.ToInt(1)], Nb = _verticesNormals[tokens.ToInt(2)], Nc = _verticesNormals[tokens.ToInt(3)], Material = (Material) _material.Clone(), Transform = _transforms.Peek() }; _scene.Geomertries.Add(triangle); break; } case "translate": { Matrix matrix = Matrix.CreateTranslation(tokens.ToVec3(1)); RightMultiply(matrix); break; } case "rotate": { Matrix matrix = Transform.Rotate(tokens.ToFloat(4), tokens.ToVec3(1)); RightMultiply(matrix); break; } case "scale": { Matrix matrix = Matrix.CreateScale(tokens.ToVec3(1)); RightMultiply(matrix); break; } case "pushTransform": { Matrix matrix = _transforms.Peek(); _transforms.Push(matrix); break; } case "popTransform": { _transforms.Pop(); break; } case "directional": { DirectionalLight light = new DirectionalLight { Color = tokens.ToVec3(4) }; Matrix rotation = Matrix.Identity; rotation.Backward = tokens.ToVec3(1); light.Rotation = rotation; _scene.Lights.Add(light); break; } case "point": { PointLight light = new PointLight { Color = tokens.ToVec3(4), Translation = Matrix.CreateTranslation(tokens.ToVec3(1)), AttenuationConst = _attenuationConst, AttenuationLinear = _attenuationLinear, AttenuationQuadratic = _attenuationQuadratic }; _scene.Lights.Add(light); break; } case "attenuation": { _attenuationConst = tokens.ToFloat(1); _attenuationLinear = tokens.ToFloat(2); _attenuationQuadratic = tokens.ToFloat(3); break; } case "ambient": { _material.AmbientColor = tokens.ToVec3(1); break; } case "diffuse": { _material.DiffuseColor = new PlaneColorSampler(tokens.ToVec3(1)); break; } case "specular": { _material.SpecularColor = tokens.ToVec3(1); _material.ReflectiveColor = tokens.ToVec3(1); _material.Reflectivity = 1.0f; break; } case "shininess": { _material.Shininess = tokens.ToFloat(1); break; } case "emission": { _material.EmissionColor = tokens.ToVec3(1); break; } } }
private Light ReadLight(string url) { XElement instanceElement = _document.XPathSelectElement(string.Format("//c:library_lights/c:light[@id='{0}']", url), _nsMgr); XElement lightElement = instanceElement.XPathSelectElement("c:technique_common/node()", _nsMgr); switch (lightElement.Name.LocalName) { case "point": { PointLight light = new PointLight(); light.Color = lightElement.Element(Ns + "color").Value.ToVec3(); light.AttenuationConst = lightElement.Element(Ns + "constant_attenuation").Value.ToFloat(); light.AttenuationLinear = lightElement.Element(Ns + "linear_attenuation").Value.ToFloat(); light.AttenuationQuadratic = lightElement.Element(Ns + "quadratic_attenuation").Value.ToFloat(); return light; } case "directional": { DirectionalLight light = new DirectionalLight(); light.Color = lightElement.Element(Ns + "color").Value.ToVec3(); return light; } default: { throw new Exception("Unknown light type"); } } }