public static BasicModel LoadSdkMesh(Device device, TextureManager texMgr, string filename, string texturePath) { // NOTE: this assumes that the model file only contains a single mesh var sdkMesh = new SdkMesh(filename); var ret = new BasicModel(); var faceStart = 0; var vertexStart = 0; foreach (var sdkMeshSubset in sdkMesh.Subsets) { var subset = new MeshGeometry.Subset { FaceCount = (int)(sdkMeshSubset.IndexCount / 3), FaceStart = faceStart, VertexCount = (int)sdkMeshSubset.VertexCount, VertexStart = vertexStart }; // fixup any subset indices that assume that all vertices and indices are not in the same buffers faceStart = subset.FaceStart + subset.FaceCount; vertexStart = subset.VertexStart + subset.VertexCount; ret.Subsets.Add(subset); } var max = new Vector3(float.MinValue); var min = new Vector3(float.MaxValue); foreach (var vb in sdkMesh.VertexBuffers) { foreach (var vertex in vb.Vertices) { max = Vector3.Maximize(max, vertex.Pos); min = Vector3.Minimize(min, vertex.Pos); ret.Vertices.Add(vertex); } } ret.BoundingBox = new BoundingBox(min, max); foreach (var ib in sdkMesh.IndexBuffers) { ret.Indices.AddRange(ib.Indices.Select(i => i)); } foreach (var sdkMeshMaterial in sdkMesh.Materials) { var material = new Material { Ambient = sdkMeshMaterial.Ambient, Diffuse = sdkMeshMaterial.Diffuse, Reflect = Color.Black, Specular = sdkMeshMaterial.Specular }; material.Specular.Alpha = sdkMeshMaterial.Power; ret.Materials.Add(material); if (!string.IsNullOrEmpty(sdkMeshMaterial.DiffuseTexture)) { ret.DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, sdkMeshMaterial.DiffuseTexture))); } else { ret.DiffuseMapSRV.Add(texMgr["default"]); } if (!string.IsNullOrEmpty(sdkMeshMaterial.NormalTexture)) { ret.NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, sdkMeshMaterial.NormalTexture))); } else { ret.NormalMapSRV.Add(texMgr["defaultNorm"]); } } ret.ModelMesh.SetSubsetTable(ret.Subsets); ret.ModelMesh.SetVertices(device, ret.Vertices); ret.ModelMesh.SetIndices(device, ret.Indices); return ret; }
public void Init(Device device, TextureManager texMgr, float width, float depth, string texture1 = "Textures/waves0.dds", string texture2 = "textures/waves1.dds") { NormalMap0 = texMgr.CreateTexture(texture1); NormalMap1 = texMgr.CreateTexture(texture2); _gridModel = new BasicModel(); _gridModel.CreateGrid(device, width, depth, ((int)width) * 2, ((int)depth) * 2); // BasicModel.CreateGrid(device, width, depth, ((int)width) * 2, ((int)depth) * 2); Material = new Material { Ambient = new Color4(0.1f, 0.1f, 0.3f), Diffuse = new Color4(0.4f, 0.4f, 0.7f), Specular = new Color4(128f, 0.8f, 0.8f, 0.8f), Reflect = new Color4(0.4f, 0.4f, 0.4f) }; _grid = new BasicModelInstance(_gridModel); TexTransform = Matrix.Identity; World = Matrix.Translation(0, -0.2f, 0); }
public override bool Init() { if (!base.Init()) return false; Effects.InitAll(Device); InputLayouts.InitAll(Device); RenderStates.InitAll(Device); _texMgr = new TextureManager(); _texMgr.Init(Device); _gridModel = new BasicModel(); _gridModel.CreateGrid(Device, 20, 20, 40, 40); _gridModel.Materials[0] = new Material() { Diffuse = Color.SaddleBrown, Specular = new Color4(16, .9f, .9f, .9f) }; _gridModel.DiffuseMapSRV[0] = _texMgr.CreateTexture("Textures/floor.dds"); _gridModel.NormalMapSRV[0] = _texMgr.CreateTexture("textures/floor_nmap.dds"); _sphereModel = new BasicModel(); _sphereModel.CreateSphere(Device, 0.3f, 5, 4); _sphereModel.Materials[0] = new Material() { Ambient = Color.Blue, Diffuse = Color.Blue, Specular = new Color4(64.0f, 1.0f, 1.0f, 1.0f) }; _sphereModel.NormalMapSRV[0] = _texMgr.CreateTexture("Textures/stones_nmap.dds"); _cylinderModel = new BasicModel(); _cylinderModel.CreateCylinder(Device, 1, 1, 3, 20, 20); _cylinderModel.Materials[0] = new Material() { Ambient = Color.Green, Diffuse = Color.Green, Specular = new Color4(64.0f, 1.0f, 1.0f, 1.0f) }; _cylinderModel.NormalMapSRV[0] = _texMgr.CreateTexture("Textures/stones_nmap.dds"); _grid = new BasicModelInstance(_gridModel) { TexTransform = Matrix.Scaling(10, 10, 1), World = Matrix.Scaling(10, 1, 10)*Matrix.Translation(0, 0, 90) }; _sphere = new BasicModelInstance(_sphereModel); _cylinder = new BasicModelInstance(_cylinderModel) { World = Matrix.Translation(0, 1.5f, 0) }; return true; }
public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipUV = false) { var importer = new AssimpImporter(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException("Model format " + Path.GetExtension(filename) + " is not supported! Cannot load {1}", "filename"); } #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUV) { postProcessFlags |= PostProcessSteps.FlipUVs; } var model = importer.ImportFile(filename, postProcessFlags); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { var verts = new List <PosNormalTexTan>(); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = Vector3.Minimize(min, pos); max = Vector3.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.GetTextureCoords(0)[i] : new Vector3D(); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new PosNormalTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } Vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); Indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } BoundingBox = new BoundingBox(min, max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
private void BuildShapeGeometryBuffers() { _texMgr = new TextureManager(); _texMgr.Init(Device); _boxModel =new BasicModel(); _boxModel.CreateBox(Device, 1, 1, 1); _boxModel.Materials[0] = new Material { Ambient = new Color4(0.8f, 0.8f, 0.8f), Diffuse = new Color4(0.4f, 0.4f, 0.4f), Specular = new Color4(16.0f, 0.8f, 0.8f, 0.8f), Reflect = Color.Black }; _boxModel.DiffuseMapSRV[0] = _texMgr.CreateTexture("Textures/bricks.dds"); _boxModel.NormalMapSRV[0] = _texMgr.CreateTexture("Textures/bricks_nmap.dds"); _gridModel = new BasicModel(); _gridModel.CreateGrid(Device, 20, 30, 50, 40); _gridModel.Materials[0] = new Material { Ambient = new Color4(0.7f, 0.7f, 0.7f), Diffuse = new Color4(0.6f, 0.6f, 0.6f), Specular = new Color4(16.0f, 0.4f, 0.4f, 0.4f), Reflect = Color.Black }; _gridModel.DiffuseMapSRV[0] = _texMgr.CreateTexture("Textures/floor.dds"); _gridModel.NormalMapSRV[0] = _texMgr.CreateTexture("Textures/floor_nmap.dds"); _sphereModel = new BasicModel(); _sphereModel.CreateSphere(Device, 0.5f, 20, 20); _sphereModel.Materials[0] = new Material { Ambient = new Color4(0.3f, 0.4f, 0.5f), Diffuse = new Color4(0.2f, 0.3f, 0.4f), Specular = new Color4(16.0f, 0.9f, 0.9f, 0.9f), Reflect = new Color4(0.3f, 0.3f, 0.3f) }; _cylinderModel = new BasicModel(); _cylinderModel.CreateCylinder(Device, 0.5f, 0.5f, 3.0f, 15, 15); _cylinderModel.Materials[0] = new Material { Ambient = new Color4(0.8f, 0.8f, 0.8f), Diffuse = new Color4(0.4f, 0.4f, 0.4f), Specular = new Color4(32.0f, 1f, 1f, 1f), Reflect = Color.Black }; _cylinderModel.DiffuseMapSRV[0] = _texMgr.CreateTexture("Textures/bricks.dds"); _cylinderModel.NormalMapSRV[0] = _texMgr.CreateTexture("Textures/bricks_nmap.dds"); for (var i = 0; i < 5; i++) { _cylinders[i * 2] = new BasicModelInstance(_cylinderModel) { World = Matrix.Translation(-5.0f, 1.5f, -10.0f + i * 5.0f), TexTransform = Matrix.Scaling(1, 2, 1) }; _cylinders[i * 2 + 1] = new BasicModelInstance(_cylinderModel) { World = Matrix.Translation(5.0f, 1.5f, -10.0f + i * 5.0f), TexTransform = Matrix.Scaling(1, 2, 1) }; _spheres[i * 2] = new BasicModelInstance(_sphereModel) { World = Matrix.Translation(-5.0f, 3.5f, -10.0f + i * 5.0f) }; _spheres[i * 2 + 1] = new BasicModelInstance(_sphereModel) { World = Matrix.Translation(5.0f, 3.5f, -10.0f + i * 5.0f) }; } _grid = new BasicModelInstance(_gridModel) { TexTransform = Matrix.Scaling(8, 10, 1), World = Matrix.Identity }; _box = new BasicModelInstance(_boxModel) { TexTransform = Matrix.Scaling(2, 1, 1), World = Matrix.Scaling(3.0f, 1.0f, 3.0f) * Matrix.Translation(0, 0.5f, 0) }; }
public override bool Init() { if (!base.Init()) return false; Effects.InitAll(Device); InputLayouts.InitAll(Device); RenderStates.InitAll(Device); _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f); _texMgr = new TextureManager(); _texMgr.Init(Device); _gridModel = new BasicModel(); _gridModel.CreateGrid(Device, 20, 20, 40, 40); _gridModel.Materials[0] = new Material { Diffuse = Color.LightGray, Specular = new Color4(16, .9f, .9f, .9f) }; _gridModel.DiffuseMapSRV[0] = _texMgr.CreateTexture("Textures/floor.dds"); _gridModel.NormalMapSRV[0] = _texMgr.CreateTexture("textures/floor_nmap.dds"); _grid = new BasicModelInstance(_gridModel) { TexTransform = Matrix.Scaling(10, 10, 1), World = Matrix.Scaling(10, 1, 10) }; InitFireworksRules(); _cylinderModel = new BasicModel(); _cylinderModel.CreateCylinder(Device, 1, 1, 3, 20, 20); _cylinderModel.Materials[0] = new Material { Ambient = Color.Green, Diffuse = Color.Green, Specular = new Color4(64.0f, 1.0f, 1.0f, 1.0f) }; _cylinderModel.DiffuseMapSRV[0] = _texMgr.CreateTexture("Textures/stones.dds"); _cylinderModel.NormalMapSRV[0] = _texMgr.CreateTexture("Textures/stones_nmap.dds"); _cylinder = new BasicModelInstance(_cylinderModel) { World = Matrix.Translation(0, 1.5f, 0) }; return true; }
public SkinnedModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipTexY = false) { var importer = new AssimpImporter(); #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace); // Load animation data Animator = new SceneAnimator(); Animator.Init(model); // create our vertex-to-boneweights lookup var vertToBoneWeight = new Dictionary <uint, List <VertexWeight> >(); // create bounding box extents _min = new Vector3(float.MaxValue); _max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { ExtractBoneWeightsFromMesh(mesh, vertToBoneWeight); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); var verts = ExtractVertices(mesh, vertToBoneWeight, flipTexY); Vertices.AddRange(verts); // extract indices and shift them to the proper offset into the combined vertex buffer var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); Indices.AddRange(indices); // extract materials var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); // extract material textures var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { // for models created without a normal map baked, we'll check for a texture with the same // filename as the diffure texture, and _nmap suffixed // this lets us add our own normal maps easily var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; if (File.Exists(Path.Combine(texturePath, normalPath))) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } } BoundingBox = new BoundingBox(_min, _max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public SkinnedModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipTexY = false) { // initialize collections _subsets = new List<MeshGeometry.Subset>(); _vertices = new List<PosNormalTexTanSkinned>(); _indices = new List<short>(); DiffuseMapSRV = new List<ShaderResourceView>(); NormalMapSRV = new List<ShaderResourceView>(); Materials = new List<Material>(); var importer = new AssimpImporter(); #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace ); // Load animation data Animator = new SceneAnimator(); Animator.Init(model); // create our vertex-to-boneweights lookup var vertToBoneWeight = new Dictionary<uint, List<VertexWeight>>(); // create bounding box extents _min = new Vector3(float.MaxValue); _max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { ExtractBoneWeightsFromMesh(mesh, vertToBoneWeight); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = _vertices.Count, FaceStart = _indices.Count / 3, FaceCount = mesh.FaceCount }; _subsets.Add(subset); var verts = ExtractVertices(mesh, vertToBoneWeight, flipTexY); _vertices.AddRange(verts); // extract indices and shift them to the proper offset into the combined vertex buffer var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); _indices.AddRange(indices); // extract materials var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); // extract material textures var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { // for models created without a normal map baked, we'll check for a texture with the same // filename as the diffure texture, and _nmap suffixed // this lets us add our own normal maps easily var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; if (File.Exists(Path.Combine(texturePath, normalPath))) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } } BoundingBox = new BoundingBox(_min, _max); _modelMesh = new MeshGeometry(); _modelMesh.SetSubsetTable(_subsets); _modelMesh.SetVertices(device, _vertices); _modelMesh.SetIndices(device, _indices); }
public override bool Init() { if (!base.Init()) return false; Effects.InitAll(Device); InputLayouts.InitAll(Device); RenderStates.InitAll(Device); _texMgr = new TextureManager(); _texMgr.Init(Device); _drone = new SkinnedModel(Device, _texMgr, "Models/drone.x", "Textures", true); _droneInstance = new SkinnedModelInstance( "Attack", Matrix.RotationY(MathF.PI), _drone ); foreach (var clip in _droneInstance.Clips) { _droneInstance.AddClip(clip); } _droneInstance.LoopClips = true; _mage = new SkinnedModel(Device, _texMgr, "Models/magician.x", "textures", true); _mageInstance = new SkinnedModelInstance( "Attack", Matrix.RotationY(MathF.PI)*Matrix.Translation(4.0f, 0, 0), _mage ); foreach (var clip in _mageInstance.Clips) { _mageInstance.AddClip(clip); } _mageInstance.LoopClips = true; _soldier = new SkinnedModel(Device, _texMgr, "Models/soldier.x", "Textures", true); _soldierInstance = new SkinnedModelInstance ( "Attack", Matrix.RotationY(MathF.PI)*Matrix.Translation(10, 0, 0), _soldier ); foreach (var clip in _soldierInstance.Clips) { _soldierInstance.AddClip(clip); } _soldierInstance.LoopClips = true; _grid = BasicModel.CreateGrid(Device, 30, 30, 60, 60); _grid.DiffuseMapSRV[0] = (_texMgr.CreateTexture("Textures/floor.dds")); _grid.NormalMapSRV[0]= (_texMgr.CreateTexture("Textures/floor_nmap.dds")); _gridInstance = new BasicModelInstance() { Model = _grid, World = Matrix.Translation(0, -1.5f, 0) }; return true; }
public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath) { _subsets = new List<MeshGeometry.Subset>(); _vertices = new List<PosNormalTexTan>(); _indices = new List<short>(); DiffuseMapSRV = new List<ShaderResourceView>(); NormalMapSRV = new List<ShaderResourceView>(); Materials = new List<Material>(); _modelMesh = new MeshGeometry(); var importer = new AssimpImporter(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException("Model format " + Path.GetExtension(filename) + " is not supported! Cannot load {1}", "filename"); } #if DEBUG importer.AttachLogStream(new ConsoleLogStream()); importer.VerboseLoggingEnabled = true; #endif var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); var verts = new List<PosNormalTexTan>(); foreach (var mesh in model.Meshes) { var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = _vertices.Count, FaceStart = _indices.Count / 3, FaceCount = mesh.FaceCount }; _subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = Vector3.Minimize(min, pos); max = Vector3.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.GetTextureCoords(0)[i] : new Vector3D(); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new PosNormalTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } _vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => (short)(i + (uint)subset.VertexStart)).ToList(); _indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } var normalPath = mat.GetTexture(TextureType.Normals, 0).FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } BoundingBox = new BoundingBox(min, max); _modelMesh.SetSubsetTable(_subsets); _modelMesh.SetVertices(device, _vertices); _modelMesh.SetIndices(device, _indices); }