public RailCamera(Vector3 camEye, Vector3 camFocus, Vector3 camUp) : base(camEye, camFocus, camUp) { model = QModel.FromSimpleModel( Editor.Instance.LoadModel("camera.obj", false) ); focusModel = Editor.Instance.LoadModel("focus_pin.obj", false); model.Transformation.Translation = Eye; focusModel.Transformation.Translation = Focus; focusModel.Transformation.Scaling = new Vector3(0.2f, 0.2f, 0.2f); Editor.Instance.AddModel(focusModel); Editor.Instance.AddModel(model); model.Meshes[0].WireFrame = true; Update(); }
public static QModel FromSimpleModel(SimpleModel simpleModel) { QModel model = new QModel(simpleModel.Id, simpleModel.Name); model.Meshes = simpleModel.Meshes; return model; }
public static SimpleModel Load(string inputFile, int modelId) { Positions = new List<Vector3>(); Normals = new List<Vector3>(); UVs = new List<Vector2>(); String[] text = File.ReadAllText(inputFile).Split(System.Environment.NewLine.ToCharArray()); String modelName = Path.GetFileNameWithoutExtension(inputFile); List<ObjectGroup> meshes = new List<ObjectGroup>(); Dictionary<string, string> textureBindings = new Dictionary<string, string>(); ObjectGroup currentMesh = null; var model = new GameEditor.SimpleModel(modelId, modelName); foreach (string line in text) { string[] tokens = line.Trim().Split(' '); if (tokens[0] == "") continue; switch (tokens[0]) { case "v": Positions.Add(ReadVector3(tokens)); break; case "vn": Normals.Add(ReadVector3(tokens)); break; case "vt": UVs.Add(ReadVector2(tokens)); break; case "f": if (currentMesh == null) { currentMesh = new ObjectGroup(modelName); // First mesh has just the name of the model meshes.Add(currentMesh); } currentMesh.Faces.Add( ReadFace(tokens) ); break; case "o": currentMesh = new ObjectGroup(tokens[1]); meshes.Add(currentMesh); break; case "#": if (tokens[1] == "texture") { textureBindings.Add(tokens[2], tokens[3]); } break; default: break; } } foreach (var mesh in meshes) { mesh.GenerateVerticesAndIndices(); var vertices = mesh.Vertices; var indices = mesh.Indices; var _mesh = new GameEditor.Mesh(vertices, indices); string texturesFolder = Path.GetDirectoryName(inputFile) + @"\..\textures\"; string textureName = textureBindings.ContainsKey(mesh.Name) ? textureBindings[mesh.Name] : "no_texture"; FileStream stream = File.OpenRead(texturesFolder + textureName + ".png"); _mesh.Texture = Texture2D.FromStream(Game1.Instance.GraphicsDevice, stream); stream.Close(); model.AddMesh(_mesh); } //Console.WriteLine("Meshes: " + model.Meshes.Count + " Vertices: " + model.Meshes[0].VertexBuffer.VertexCount); model.bbox = model.BoundingBoxFromMeshes(); return model; }
void OpenScene() { var dialog = new System.Windows.Forms.OpenFileDialog(); dialog.Filter = "Scene files (*.txt)|*.txt"; dialog.InitialDirectory = NDSFrameworkHelper.ScenesDirectory; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Models = new List<Model3D>(); SelectedModel = new SimpleModel(); String file = File.ReadAllText(dialog.FileName); String[] lines = file.Split('\n'); for (int i = 0; i < lines.Length; i++) { String currentLine = lines[i].Trim(); if (currentLine.Equals("GameObject")) { String gameObject = ""; i++; // Skip the open bracket int depth = 1; while (depth > 0 && i < lines.Length) { currentLine = lines[++i].Trim(); if (currentLine.Equals("{")) depth++; else if (currentLine.Equals("}")) depth--; else gameObject += currentLine + '\n'; } Model3D m = ParseGameObject(gameObject); } } } }
void HandleHotKeys() { switch (ControlState.HotKey) { case HotKeys.Add: LoadModelWithDialog(); break; case HotKeys.Save: SaveScene(); break; case HotKeys.Open: OpenScene(); break; case HotKeys.Insert: KeyFrameControl.InsertKeyFrameOnCurrentFrame(SelectedModel); break; case HotKeys.Previous: if (!KeyFrameControl.AutoPlay) KeyFrameControl.CurrentFrameIndex--; break; case HotKeys.Next: if (!KeyFrameControl.AutoPlay) KeyFrameControl.CurrentFrameIndex++; break; case HotKeys.PlayMode: if (Models.Count == 0) break; KeyFrameControl.Playing = true; KeyFrameControl.AutoPlay = true; KeyFrameControl.CalculateFrames(); break; case HotKeys.TogglePlay: KeyFrameControl.AutoPlay = !KeyFrameControl.AutoPlay; break; case HotKeys.Delete: DeleteModel(SelectedModel); SelectedModel = new SimpleModel(); break; case HotKeys.Make: ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "make"; startInfo.Arguments = "run"; startInfo.WorkingDirectory = NDSFrameworkHelper.CodeDirectory; startInfo.UseShellExecute = false; Process pr = Process.Start(startInfo); pr.WaitForExit(); break; case HotKeys.Camera0: Camera.Eye = RailCamera.Eye; Camera.Focus = RailCamera.Focus; break; default: break; } ControlState.HotKey = HotKeys.None; }