private void SaveModelFile() { // Save model var vb = model.Meshes[0].MeshParts[0].VertexBuffer; // var verts = vb.GetData() var elements = vb.VertexDeclaration.GetVertexElements(); var data = new VertexPositionNormalTextureColor[vb.VertexCount]; vb.GetData(data); var ib = model.Meshes[0].MeshParts[0].IndexBuffer; var indexByteArray = new byte[2 * ib.IndexCount]; ib.GetData(indexByteArray); // 16 bit index buffer + 16 byte vertex buffer; var standardV = new StandardVertexDeclaration[vb.VertexCount]; for (int i = 0; i < vb.VertexCount; i++) { standardV[i].PositionPlusUVx = new HalfVector4(data[i].position.X, data[i].position.Y, data[i].position.Z, data[i].texcoord.X); standardV[i].NormalPlusUVy = new HalfVector4(data[i].normal.X, data[i].normal.Y, data[i].normal.Z, data[i].texcoord.Y); } var byteArray = ByteMarshal.WriteDataMarshalCopy(standardV); // vb.SetData(standardV); NewModel = new StandardModel(); NewModel.VertexBuffer = new VertexBuffer(GraphicsDevice, StandardVertexDeclaration.Declaration, standardV.Length, BufferUsage.None); NewModel.VertexBuffer.SetData(standardV); NewModel.IndexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, ib.IndexCount, BufferUsage.None); NewModel.IndexBuffer.SetData(indexByteArray); // Write new file using (var fs = new FileStream("temp4.smod", FileMode.CreateNew)) { using (var bw = new BinaryWriter(fs)) { bw.Write((Int32)42); bw.Write((Int32)indexByteArray.Length); bw.Write((Int32)byteArray.Length); bw.Write(indexByteArray); bw.Write(byteArray); } } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { GraphicsDevice.PresentationParameters.MultiSampleCount = 8; // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Standard = Content.Load <Effect>("Standard"); Half = Content.Load <Effect>("Half"); texture = Content.Load <Texture>("WallDIF"); var sw = new Stopwatch(); sw.Start(); model = Content.Load <Model>("Cave_Tunnel_Pipe"); Console.WriteLine("Time taken to load model via contentpipeline:" + sw.ElapsedMilliseconds + "ms"); sw.Stop(); sw.Reset(); sw.Start(); using (var fs = new FileStream("temp4.smod", FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { var id = br.ReadInt32(); var indexLenth = br.ReadInt32(); var vertLength = br.ReadInt32(); var indexArray = br.ReadBytes(indexLenth); var byteArray = br.ReadBytes(vertLength); NewModel = new StandardModel(); NewModel.VertexBuffer = new VertexBuffer(GraphicsDevice, StandardVertexDeclaration.Declaration, byteArray.Length, BufferUsage.None); NewModel.VertexBuffer.SetData(byteArray); NewModel.IndexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, indexArray.Length / 2, BufferUsage.None); NewModel.IndexBuffer.SetData(indexArray); } } sw.Stop(); Console.WriteLine("Time taken to load model via file:" + sw.ElapsedMilliseconds + "ms"); // SaveModelFile(); // TODO: use this.Content to load your game content here }
private void DrawModelHalf(StandardModel model, Texture texture, Matrix world, Matrix view, Matrix projection) { var worldParam = Half.Parameters["World"]; var viewParam = Half.Parameters["View"]; var projParam = Half.Parameters["Projection"]; var textParam = Half.Parameters["gTex0"]; var camPos = Half.Parameters["CameraPosition"]; camPos.SetValue(new Vector3(0, 3, z)); textParam.SetValue(texture); graphics.GraphicsDevice.SetVertexBuffer(model.VertexBuffer, 0); graphics.GraphicsDevice.Indices = model.IndexBuffer; worldParam.SetValue(world); viewParam.SetValue(view); projParam.SetValue(projection); Half.CurrentTechnique.Passes[0].Apply(); graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, model.VertexCount, 0, model.PrimativeCount); }
/// <summary> /// LoadContent will be called once per game and is the place to load\\ /// all of your content. /// </summary> protected override void LoadContent() { GraphicsDevice.PresentationParameters.MultiSampleCount = 8; // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Standard = Content.Load <Effect>("Standard"); Half = Content.Load <Effect>("Half"); Console.WriteLine("LOADING TEXTURES"); var sw = new Stopwatch(); { sw.Start(); NewModelRocks = ModelLoader.LoadStandardModel(GraphicsDevice, "Assets/Models/rocks.smod"); NewModelPipe = ModelLoader.LoadStandardModel(GraphicsDevice, "Assets/Models/pipe.smod"); sw.Stop(); Console.WriteLine("Time taken to load model via file:" + sw.ElapsedMilliseconds + "ms"); sw.Reset(); } { // This way appears slightly faster (~10%) but I don't think it's worth the trade off of loading the entire chunk into memory var jobScheduler = new JobScheduler(); var chunkReader = new AssetChunkReader(); var chunkFileName = "Assets/Chunk/textures.chunk"; chunkReader.AssociateChunk(chunkFileName); sw.Start(); var fs1 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); var fs2 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); var fs3 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); var fs4 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); var fs5 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); var fs6 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); var fs7 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); var fs8 = new FileStream(chunkFileName, FileMode.Open, FileAccess.Read, FileShare.Read); jobScheduler.QueueJob("Assets/Textures/tex16/walls.tex16", () => { WallTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/walls.tex16", fs1); }); jobScheduler.QueueJob("Assets/Textures/tex16/rocks.tex16", () => { RockTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/rocks.tex16", fs2); }); jobScheduler.QueueJob("Assets/Textures/tex16/doorbars.tex16", () => { DoorBarsTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/doorbars.tex16", fs3); }); jobScheduler.QueueJob("Assets/Textures/tex16/floor.tex16", () => { FloorTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/floor.tex16", fs4); }); jobScheduler.QueueJob("Assets/Textures/tex16/goat.tex16", () => { GoatTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/goat.tex16", fs5); }); jobScheduler.QueueJob("Assets/Textures/tex16/ice.tex16", () => { IceTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/ice.tex16", fs6); }); jobScheduler.QueueJob("Assets/Textures/tex16/torch.tex16", () => { TorchTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/torch.tex16", fs7); }); jobScheduler.QueueJob("Assets/Textures/tex16/water.tex16", () => { WaterTexture = chunkReader.LoadTextureFromStream(GraphicsDevice, "Assets/Textures/tex16/water.tex16", fs8); }); jobScheduler.ExecuteAll(() => { fs1.Close(); fs2.Close(); fs3.Close(); fs4.Close(); fs5.Close(); fs6.Close(); fs7.Close(); fs8.Close(); sw.Stop(); Console.WriteLine("Time taken to load tex16 (multithreaded) from Chunk file: " + sw.ElapsedMilliseconds + "ms"); sw.Reset(); }); } }