/// <summary> /// Loads a model for view. /// </summary> /// <param name="id">ModelID.</param> private void LoadModel(uint?id) { // Load the model currentModel = host.ModelManager.GetModelData(id.Value); // Load texture for each submesh. for (int sm = 0; sm < currentModel.SubMeshes.Length; sm++) { // Load textures currentModel.SubMeshes[sm].TextureKey = host.TextureManager.LoadTexture( currentModel.DFMesh.SubMeshes[sm].TextureArchive, currentModel.DFMesh.SubMeshes[sm].TextureRecord, TextureManager.TextureCreateFlags.ApplyClimate | TextureManager.TextureCreateFlags.MipMaps | TextureManager.TextureCreateFlags.PowerOfTwo); } // Centre camera and reset model rotation Vector3 Min = currentModel.BoundingBox.Min; Vector3 Max = currentModel.BoundingBox.Max; cameraPosition.X = (float)(Min.X + ((Max.X - Min.X) / 2)); cameraPosition.Y = (float)(Min.Y + ((Max.Y - Min.Y) / 2)); cameraPosition.Z = 700.0f + (Max.Z - Min.Z); modelRotation = Matrix.Identity; }
private void DrawSingleModel(ref ModelManager.ModelData model) { // Set render states host.GraphicsDevice.BlendState = BlendState.Opaque; host.GraphicsDevice.DepthStencilState = DepthStencilState.Default; host.GraphicsDevice.RasterizerState = RasterizerState.CullNone; host.GraphicsDevice.SamplerStates[0] = SamplerState.AnisotropicWrap; // Set view and projection matrices effect.View = viewMatrix; effect.Projection = projectionMatrix; // Draw submeshes foreach (var submesh in model.SubMeshes) { effect.Texture = host.TextureManager.GetTexture(submesh.TextureKey); effect.CurrentTechnique.Passes[0].Apply(); host.GraphicsDevice.DrawUserIndexedPrimitives( PrimitiveType.TriangleList, model.Vertices, 0, model.Vertices.Length, model.Indices, submesh.StartIndex, submesh.PrimitiveCount); } }
/// <summary> /// Constructor. /// </summary> public ModelView(ViewHost host) : base(host) { currentModel = new ModelManager.ModelData(); // Start in normal camera mode CameraMode = CameraModes.None; renderableBounds = new RenderableBoundingBox(host.GraphicsDevice); }
/// <summary> /// Adds model data to the batch builder. /// </summary> /// <param name="key">Key to batch against.</param> /// <param name="modelData">Model data to add.</param> /// <param name="matrix">Transform to apply before adding model data.</param> /// <param name="matrix">Geometry transform to apply before adding.</param> public void AddToBuilder(ref ModelManager.ModelData modelData, Matrix matrix) { // Do nothing if sealed if (isSealed) { return; } // Iterate submeshes BatchData batchData; foreach (var sm in modelData.SubMeshes) { // Start new batch data for this submesh batchData.Vertices = new List <VertexPositionNormalTextureBump>(); batchData.Indices = new List <int>(); int counter = 0; int index = sm.StartIndex; for (int tri = 0; tri < sm.PrimitiveCount; tri++) { // Get indices int i1 = modelData.Indices[index++]; int i2 = modelData.Indices[index++]; int i3 = modelData.Indices[index++]; // Get vertices VertexPositionNormalTextureBump vert1 = modelData.Vertices[i1]; VertexPositionNormalTextureBump vert2 = modelData.Vertices[i2]; VertexPositionNormalTextureBump vert3 = modelData.Vertices[i3]; // Add vertices batchData.Vertices.Add(vert1); batchData.Vertices.Add(vert2); batchData.Vertices.Add(vert3); // Add indices batchData.Indices.Add(counter++); batchData.Indices.Add(counter++); batchData.Indices.Add(counter++); } // Add to builder AddToBuilder((uint)sm.MaterialKey, batchData, matrix); } }
/// <summary> /// Loads a new model to display with this component. /// Replaces any previously loaded model. /// </summary> /// <param name="id">Model ID to load.</param> /// <returns>True if successful.</returns> public bool LoadModel(uint id) { try { // Load model data and set bounds modelData = core.ModelManager.GetModelData(id); this.BoundingSphere = modelData.BoundingSphere; this.boundingBox = modelData.BoundingBox; this.id = id; } catch (Exception e) { Console.WriteLine(e.Message); modelData = null; return(false); } return(true); }
/// <summary> /// Loads model data. /// </summary> /// <param name="id">ID of model to load.</param> /// <param name="modelData">Model data out.</param> private void LoadModel(uint id, out ModelManager.ModelData modelData) { // Load model and textures modelData = core.ModelManager.GetModelData(id); }