Exemple #1
0
        public override void CreateIndexBuffer(List <Face> faces)
        {
            int[] indices = new int[24];
            int   i       = 0;

            for (int n = 0; n < 4; n++)
            {
                indices[i++] = n;
                indices[i++] = (n + 1) % 4;

                indices[i++] = n + 4;
                indices[i++] = (n + 1) % 4 + 4;

                indices[i++] = n;
                indices[i++] = n + 4;
            }

            lock (DX.GlobalLock)
            {
                IndexBuffer = new D3D9.IndexBuffer(DX.Device, sizeof(int) * 24, D3D9.Usage.WriteOnly, D3D9.Pool.Managed, false);
                IndexBuffer.Lock(0, 0, D3D9.LockFlags.None).WriteRange(indices);
                IndexBuffer.Unlock();
            }
            IndexCount = indices.Length;
        }
        public static Model FromScene(Scene scene, Device device)
        {
            VertexDeclaration vertexDeclaration = new VertexDeclaration(device,
                VertexPositionNormalTexture.VertexElements);
            Model result = new Model(scene, device, vertexDeclaration);
            foreach (Mesh mesh in scene.Meshes.Where(x => x.Positions.Any()))
            {
                VertexBuffer vertexBuffer = new VertexBuffer(device,
                    mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes,
                    Usage.WriteOnly, VertexFormat.None, Pool.Default);
                DataStream vertexDataStream = vertexBuffer.Lock(0,
                    mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes,
                    LockFlags.None);
                VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.Positions.Count];
                for (int i = 0; i < vertices.Length; ++i)
                    vertices[i] = new VertexPositionNormalTexture(mesh.Positions[i],
                        (mesh.Normals.Count > i) ? mesh.Normals[i] : Vector3D.Zero,
                        (mesh.TextureCoordinates.Count > i) ? mesh.TextureCoordinates[i].Xy : Point2D.Zero);
                vertexDataStream.WriteRange(vertices);
                vertexBuffer.Unlock();

                IndexBuffer indexBuffer = new IndexBuffer(device, mesh.Indices.Count * sizeof(int),
                    Usage.WriteOnly, Pool.Default, false);
                DataStream indexDataStream = indexBuffer.Lock(0, mesh.Indices.Count * sizeof(int), LockFlags.None);
                indexDataStream.WriteRange(mesh.Indices.ToArray());
                indexBuffer.Unlock();

                ModelMesh modelMesh = new ModelMesh(mesh, device, vertexBuffer,
                    mesh.Positions.Count, indexBuffer, mesh.PrimitiveCount,
                    Matrix3D.Identity, mesh.Material);
                result.Meshes.Add(modelMesh);
            }
            return result;
        }
Exemple #3
0
        internal ModelMesh(Mesh sourceMesh, Device device, VertexBuffer vertexBuffer, int numVertices,
			IndexBuffer indexBuffer, int primitiveCount,
			Matrix3D world, Material material)
        {
            SourceMesh = sourceMesh;
            _device = device;
            _vertexBuffer = vertexBuffer;
            _numVertices = numVertices;
            _indexBuffer = indexBuffer;
            _primitiveCount = primitiveCount;

            _effect = new SimpleEffect(device)
            {
                World = world,
                AmbientLightColor = new ColorRgbF(0.1f, 0.1f, 0.1f),
                DiffuseColor = material.DiffuseColor,
                SpecularColor = material.SpecularColor,
                SpecularPower = material.Shininess,
                Alpha = material.Transparency
            };
            if (!string.IsNullOrEmpty(material.DiffuseTextureName))
                _effect.DiffuseTexture = Texture.FromFile(device, material.DiffuseTextureName, Usage.None, Pool.Default);
            _effect.CurrentTechnique = "RenderScene";
            Opaque = (material.Transparency == 1.0f);
        }
Exemple #4
0
 public virtual void CreateIndexBuffer(List <Face> faces)
 {
     lock (DX.GlobalLock)
     {
         IndexBuffer = new D3D9.IndexBuffer(DX.Device, faces.Count * Face.Size, D3D9.Usage.WriteOnly, D3D9.Pool.Managed, true);
         IndexBuffer.Lock(0, 0, D3D9.LockFlags.None).WriteRange(faces.ToArray());
         IndexBuffer.Unlock();
     }
     IndexCount = faces.Count * 3;
 }
 public override void UnloadContent()
 {
     if (m_indexBuffer != null)
     {
         m_indexBuffer.Dispose();
         m_indexBuffer = null;
     }
     if (m_vertexBuffer != null)
     {
         m_vertexBuffer.Dispose();
         m_vertexBuffer = null;
     }
 }
Exemple #6
0
        internal Direct3D9IndexBuffer(SharpDX.Direct3D9.Device device, uint[] data)
        {
            _length = data.Length;
            _buffer = new SharpDX.Direct3D9.IndexBuffer(device, data.Length * sizeof(uint), Usage.WriteOnly, Pool.Managed, false);

            SharpDX.DataStream ds = _buffer.Lock(0, _length * sizeof(uint), LockFlags.None);

            for (int i = 0; i < data.Length; i++)
            {
                ds.Write(data[i]);
            }

            _buffer.Unlock();
        }
 public void Dispose()
 {
     if (VertexBuffer != null)
     {
         VertexBuffer.Dispose();
         VertexBuffer = null;
         VertexCount = 0;
     }
     if (IndexBuffer != null)
     {
         IndexBuffer.Dispose();
         IndexBuffer = null;
         IndexCount = 0;
     }
 }
        public void CreateResources()
        {
            ushort[] indices = new ushort[6 * 20];

            int count = indices.Length / 6;

            for (int i = 0; i < count; i++)
            {
                indices[(i * 6) + 0] = (ushort)((i * 4) + 0);
                indices[(i * 6) + 1] = (ushort)((i * 4) + 1);
                indices[(i * 6) + 2] = (ushort)((i * 4) + 2);
                indices[(i * 6) + 3] = (ushort)((i * 4) + 2);
                indices[(i * 6) + 4] = (ushort)((i * 4) + 1);
                indices[(i * 6) + 5] = (ushort)((i * 4) + 3);
            }

            _maxBatches = indices.Length / 6;
            _vertexDeclaration = new VertexDeclaration(_device, VertexPositionNormalTexture.VertexElements);
            _indexBuffer = new IndexBuffer(_device, sizeof(ushort) * indices.Length, Usage.WriteOnly, Pool.Managed, true);
            _indexBuffer.Lock(0, 0, LockFlags.None).WriteRange(indices, 0, indices.Length);
        }
        public void UnloadContent()
        {
            if (m_vertexBuffer != null)
            {
                m_vertexBuffer.Dispose();
                m_vertexBuffer = null;
                MyPerformanceCounter.PerAppLifetime.ModelVertexBuffersSize -= m_vertexBufferSize;
                m_vertexBufferSize = 0;
            }

            if (m_indexBuffer != null)
            {
                m_indexBuffer.Dispose();
                m_indexBuffer = null;
                MyPerformanceCounter.PerAppLifetime.ModelIndexBuffersSize -= m_indexBufferSize;
                m_indexBufferSize = 0;
            }

            LoadState = Textures.LoadState.Unloaded;

            m_loadedContent = false;
        }
Exemple #10
0
        public RenderResources(Device device)
        {
            Vertex[] rectangle = new Vertex[]{
                new Vertex(0,0,0),
                new Vertex(1,0,0),
                new Vertex(0,1,0),
                new Vertex(1,1,0),
            };

            int[] rectanglei = new int[] { 0, 1, 1, 3, 3, 2, 2, 0 };

            RectangleVBuffer = new VertexBuffer(device, rectangle.Length * Vertex.SizeInBytes, Usage.WriteOnly, VertexFormat.None, Pool.Default);
            var dsv = RectangleVBuffer.Lock(0, 0, LockFlags.Discard);
            dsv.WriteRange(rectangle);
            RectangleVBuffer.Unlock();

            RecangleIBuffer = new IndexBuffer(device, sizeof(int) * rectanglei.Length, Usage.WriteOnly, Pool.Default, false);
            var dsi = RecangleIBuffer.Lock(0, 0, LockFlags.Discard);
            dsi.WriteRange(rectanglei);
            RecangleIBuffer.Unlock();

            int[] fillrecti = new int[] { 0, 1, 2, 3, 2, 1 };

            FillRectangleIBuffer = new IndexBuffer(device, sizeof(int) * fillrecti.Length, Usage.WriteOnly, Pool.Default, false);
            dsi = FillRectangleIBuffer.Lock(0, 0, LockFlags.Discard);
            dsi.WriteRange(fillrecti);
            FillRectangleIBuffer.Unlock();

            Vertex[] circle = new Vertex[CircleQuality + 1];
            for (int la = 0; la < circle.Length;la++)
            {
                var angle = O3DHelper.Scale(la,0,circle.Length,0,MathUtil.TwoPi);
                float x = (float)Math.Sin(angle) * 0.5f + 0.5f;
                float y = (float)Math.Cos(angle) * 0.5f + 0.5f;
                circle[la] = new Vertex(x, y, 0);
            }
            circle[CircleQuality] = new Vertex(0.5f, 0.5f, 0);

            CircleVBuffer = new VertexBuffer(device, circle.Length * Vertex.SizeInBytes, Usage.WriteOnly, VertexFormat.None, Pool.Default);
            dsv = CircleVBuffer.Lock(0, 0, LockFlags.Discard);
            dsv.WriteRange(circle);
            CircleVBuffer.Unlock();

            int[] circlei = new int[CircleQuality * 2];
            for (int la = 0; la < CircleQuality;la++)
            {
                circlei[la * 2 + 0] = la;
                circlei[la * 2 + 1] = (la + 1) % CircleQuality;
            }

            CircleIBuffer = new IndexBuffer(device, sizeof(int) * circlei.Length, Usage.WriteOnly, Pool.Default, false);
            dsi = CircleIBuffer.Lock(0, 0, LockFlags.Discard);
            dsi.WriteRange(circlei);
            CircleIBuffer.Unlock();

            int[] fcircle = new int[CircleQuality * 3];
            for (int la = 0; la < CircleQuality; la++)
            {
                fcircle[la * 3 + 0] = (la + 1) % CircleQuality;
                fcircle[la * 3 + 1] = la;
                fcircle[la * 3 + 2] = CircleQuality;
            }

            FillCircleIBuffer = new IndexBuffer(device, sizeof(int) * fcircle.Length, Usage.WriteOnly, Pool.Default, false);
            dsi = FillCircleIBuffer.Lock(0, 0, LockFlags.Discard);
            dsi.WriteRange(fcircle);
            FillCircleIBuffer.Unlock();

            Shader = Effect.FromString(device, Resources.Shader, ShaderFlags.None);
        }
        void CreateIndexBuffer()
        {
            MyDebug.AssertDebug(m_indexBuffer == null);

            if (m_Indices != null)
            {
                m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, m_Indices.Length * sizeof(int), Usage.WriteOnly, Pool.Default, false);
                m_indexBuffer.SetData(m_Indices);
                m_indexBuffer.Tag = this;
                m_indexBufferSize = m_Indices.Length * sizeof(int);
            }
            else if (m_Indices_16bit != null)
            {
                m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, m_Indices_16bit.Length * sizeof(short), Usage.WriteOnly, Pool.Default, true);
                m_indexBuffer.SetData(m_Indices_16bit);
                m_indexBuffer.Tag = this;
                m_indexBufferSize = m_Indices_16bit.Length * sizeof(short);
            }

            MyPerformanceCounter.PerAppLifetime.ModelIndexBuffersSize += m_indexBufferSize;

            SignResource(m_indexBuffer);
        }
 private void SignResource(IndexBuffer indexBuffer)
 {
     indexBuffer.DebugName = m_assetName + "_ib";
 }
        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            m_meshContainer.Clear();
            m_vertexBuffer.Dispose();
            m_vertexBuffer = null;
            MyPerformanceCounter.PerAppLifetime.ModelVertexBuffersSize -= m_vertexBufferSize;
            m_vertexBufferSize = 0;

            m_indexBuffer.Dispose();
            m_indexBuffer = null;
            MyPerformanceCounter.PerAppLifetime.ModelIndexBuffersSize -= m_indexBufferSize;
            m_indexBufferSize = 0;
        }
        public void LoadBuffers(MyModelData modelData, string assetName = null)
        {
            System.Diagnostics.Debug.Assert(modelData.Sections.Count > 0, "Invalid object");
            if (modelData.Sections.Count == 0)
                return;

            // create index buffer
            {
                m_trianglesCount = modelData.Indices.Count / 3;
                m_Indices_16bit = new ushort[modelData.Indices.Count];

                for (int i = 0; i < modelData.Indices.Count; ++i)
                    m_Indices_16bit[i] = (ushort)modelData.Indices[i];

                m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, m_Indices_16bit.Length * sizeof(short), Usage.WriteOnly, Pool.Default, true);
                m_indexBuffer.SetData(m_Indices_16bit);
                m_indexBuffer.Tag = this;
                m_indexBufferSize = m_Indices_16bit.Length * sizeof(short);

                SignResource(m_indexBuffer);
            }

            // create vertex buffer
            {
                m_verticesCount = modelData.Positions.Count;
                m_vertices      = new MyCompressedVertexNormal[m_verticesCount];
                var vertexArray = new MyVertexFormatPositionNormalTextureTangent[m_verticesCount];

                for (int i = 0; i < modelData.Positions.Count; ++i)
                {
                    vertexArray[i].Position = modelData.Positions[i];
                    vertexArray[i].Normal   = modelData.Normals[i];
                    vertexArray[i].Tangent  = modelData.Tangents[i];
                    vertexArray[i].TexCoord = modelData.TexCoords[i];

                    m_vertices[i] = new MyCompressedVertexNormal()
                    {
                        Position = vertexArray[i].PositionPacked,
                        Normal   = vertexArray[i].NormalPacked
                    };
                }

                m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangent.VertexDeclaration;
                m_vertexStride      = MyVertexFormatPositionNormalTextureTangent.Stride;
                m_vertexBufferSize  = vertexArray.Length * m_vertexStride;
                m_vertexBuffer      = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default);
                m_vertexBuffer.SetData(vertexArray);
                m_vertexBuffer.Tag = this;

                SignResource(m_vertexBuffer);
            }

            m_meshContainer.Clear();

            // apply materials here
            for (int s = 0; s < modelData.Sections.Count; ++s)
            {
                var mpi            = new MyMeshPartInfo();
                mpi.Technique      = m_drawTechnique;
                
                // Disabled, because it assert always when models are loaded before materials
                //System.Diagnostics.Debug.Assert(MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName), "Mesh material not present!");
                
                if (MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName))
                    mpi.m_MaterialDesc = MyRenderModels.Materials[modelData.Sections[s].MaterialName];

                var start = modelData.Sections[s].IndexStart;
                var end   = start + modelData.Sections[s].TriCount*3;

                for (int i = start; i < end; ++i)
                    mpi.m_indices.Add(modelData.Indices[i]);

                m_meshContainer.Add(new MyRenderMesh(mpi, null)
                {
                    IndexStart = modelData.Sections[s].IndexStart,
                    TriCount   = modelData.Sections[s].TriCount
                });
            }

            // store properties of this model
            {
                BoundingBox    = modelData.AABB;
                BoundingSphere = new BoundingSphere(modelData.AABB.Center, modelData.AABB.HalfExtents.Length());

                BoundingBoxSize     = BoundingBox.Size;
                BoundingBoxSizeHalf = BoundingBox.HalfExtents;

                ModelInfo = new MyModelInfo(m_trianglesCount, m_verticesCount, BoundingBoxSize);

                PreloadTextures(LoadingMode.Immediate);
                LoadState = Textures.LoadState.Loaded;

                m_loadedContent = true;
                m_loadedData    = true;
            }
        }
Exemple #15
0
 protected void RecreateBuffers()
 {
     Pool pool;
     if (viewportImage.GraphicsDeviceService.UseDeviceEx == true) pool = Pool.Default;
     else pool = Pool.Managed;
     if (!thickLines)
     {
         // Prepare for using single-pixel lines
         if ((vertices == null) || (vertices.Length != Points.Count) || (vertexBuffer == null))
         {
             if (vertexBuffer != null) vertexBuffer.Dispose();
             if ((vertices == null) || (vertices.Length != Points.Count)) vertices = new VertexPositionColor[Points.Count];
             vertexBuffer = new VertexBuffer(graphicsDevice, vertices.Length * VertexPositionColor.SizeInBytes,
                 Usage.WriteOnly, VertexFormat.Position | VertexFormat.Diffuse, pool);
         }
         if ((indices == null) || (indices.Length != Points.Count) || (indexBuffer == null))
         {
             if (indexBuffer != null) indexBuffer.Dispose();
             if ((indices == null) || (indices.Length != Points.Count)) indices = new short[Points.Count];
             indexBuffer = new IndexBuffer(graphicsDevice, indices.Length * Marshal.SizeOf(typeof(short)),
                 Usage.WriteOnly, pool, true);
         }
     }
     else
     {
         // Prepare for thick lines. 4 vertices and 6 indices per line.
         if ((thickVertices == null) || (thickVertices.Length != Points.Count * 4) || (vertexBuffer == null))
         {
             if (vertexBuffer != null) vertexBuffer.Dispose();
             if ((thickVertices == null) || (thickVertices.Length != Points.Count * 4)) thickVertices = new ThickLinesVertex[Points.Count * 4];
             vertexBuffer = new VertexBuffer(graphicsDevice, thickVertices.Length * ThickLinesVertex.SizeInBytes,
                 Usage.WriteOnly, VertexFormat.Position | VertexFormat.Texture0 | VertexFormat.Texture1 | VertexFormat.Diffuse, pool);
         }
         if ((indices == null) || (indices.Length != Points.Count * 6) || (indexBuffer == null))
         {
             if (indexBuffer != null) indexBuffer.Dispose();
             if ((indices == null) || (indices.Length != Points.Count * 6)) indices = new short[6 * Points.Count];
             indexBuffer = new IndexBuffer(graphicsDevice, indices.Length * Marshal.SizeOf(typeof(short)),
                 Usage.WriteOnly, pool, true);
         }
     }
 }
        /// <summary>
        /// Loads the content.
        /// </summary>
        public override void LoadContent()
        {
            MyRender.Log.WriteLine("TransparentGeometry.LoadContent() - START");
            MyRender.Log.IncreaseIndent();
            MyRender.GetRenderProfiler().StartProfilingBlock("TransparentGeometry.LoadContent");

            MyRender.Log.WriteLine(string.Format("MyTransparentGeometry.LoadData - START"));

            m_sortedTransparentGeometry.Clear();

            m_preparedTransparentGeometry.Clear();
            m_lowresTransparentGeometry.Clear();

            //  Max count of all particles should be less or equal than max count of billboards
            MyDebug.AssertRelease(MyTransparentGeometryConstants.MAX_PARTICLES_COUNT <= MyTransparentGeometryConstants.MAX_TRANSPARENT_GEOMETRY_COUNT);
            MyDebug.AssertRelease(MyTransparentGeometryConstants.MAX_COCKPIT_PARTICLES_COUNT <= MyTransparentGeometryConstants.MAX_TRANSPARENT_GEOMETRY_COUNT);

            PrepareAtlasMaterials();

            m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, MyVertexFormatTransparentGeometry.Stride * MyTransparentGeometryConstants.MAX_TRANSPARENT_GEOMETRY_COUNT * MyTransparentGeometryConstants.VERTICES_PER_TRANSPARENT_GEOMETRY, Usage.WriteOnly | Usage.Dynamic, VertexFormat.None, Pool.Default);
            m_startOffsetInVertexBuffer = 0;
            m_endOffsetInVertexBuffer = 0;
            m_vertexBuffer.DebugName = "TransparentGeometry";

            m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, MyTransparentGeometryConstants.MAX_TRANSPARENT_GEOMETRY_INDICES * sizeof(int), Usage.WriteOnly, Pool.Default, false);
            m_indexBuffer.SetData(m_indices);

            MyRender.GetRenderProfiler().EndProfilingBlock();

            MyRender.Log.DecreaseIndent();
            MyRender.Log.WriteLine("TransparentGeometry.LoadContent() - END");
        }
Exemple #17
0
        protected void RecreateBuffers()
        {
            if (viewportImage == null) return;
            lock (updateLocker)
            {
                Pool pool;
                if (viewportImage.GraphicsDeviceService.UseDeviceEx == true) pool = Pool.Default;
                else pool = Pool.Managed;
                if ((vertexBufferLength != vertices.Length) || (vertexBuffer == null))
                {
                    if (vertexBuffer != null) vertexBuffer.Dispose();
                    vertexBuffer = new VertexBuffer(graphicsDevice, vertices.Length * VertexPositionNormalColor.SizeInBytes,
                    Usage.WriteOnly, VertexFormat.Position | VertexFormat.Normal | VertexFormat.Diffuse, pool);
                    vertexBufferLength = vertices.Length;
                }

                using (DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None))
                {
                    stream.WriteRange(vertices);
                    vertexBuffer.Unlock();
                }

                graphicsDevice.VertexFormat = VertexFormat.Position | VertexFormat.Normal | VertexFormat.Diffuse;

                if ((indexBufferLength != indices.Length) || (indexBuffer == null))
                {
                    if (indexBuffer != null) indexBuffer.Dispose();
                    indexBuffer = new IndexBuffer(graphicsDevice, indices.Length * Marshal.SizeOf(typeof(int)),
                        Usage.WriteOnly, pool, false);
                    indexBufferLength = indices.Length;
                }

                using (DataStream streamIndex = indexBuffer.Lock(0, 0, LockFlags.None))
                {
                    streamIndex.WriteRange(indices);
                    indexBuffer.Unlock();
                }
            }
        }
        private NormalBuffers GetNormalBuffers(ModelMesh mesh)
        {
            if (!_normals.ContainsKey(mesh))
            {
                NormalBuffers normalBuffers = new NormalBuffers();

                Line3D[] normalLines = NormalLinesGenerator.Generate(mesh.SourceMesh);
                normalBuffers.PrimitiveCount = normalLines.Length;
                normalBuffers.VertexCount = normalLines.Length * 2;

                VertexBuffer vertexBuffer = new VertexBuffer(_device,
                    normalBuffers.VertexCount * VertexPositionColor.SizeInBytes,
                    Usage.WriteOnly, VertexFormat.None, Pool.Default);
                DataStream vertexDataStream = vertexBuffer.Lock(0,
                    normalBuffers.VertexCount * VertexPositionColor.SizeInBytes,
                    LockFlags.None);
                VertexPositionColor[] vertices = new VertexPositionColor[normalBuffers.VertexCount];
                int counter = 0;
                for (int i = 0; i < normalLines.Length; ++i)
                {
                    Vector3D normalColor = Vector3D.Normalize(normalLines[i].Direction);
                    normalColor += Vector3D.One;
                    normalColor *= 0.5f;
                    vertices[counter++] = new VertexPositionColor(normalLines[i].Start, normalColor);
                    vertices[counter++] = new VertexPositionColor(normalLines[i].End, normalColor);
                }
                vertexDataStream.WriteRange(vertices);
                vertexBuffer.Unlock();
                normalBuffers.Vertices = vertexBuffer;

                IndexBuffer indexBuffer = new IndexBuffer(_device, normalBuffers.VertexCount * sizeof(int),
                    Usage.WriteOnly, Pool.Default, false);
                DataStream indexDataStream = indexBuffer.Lock(0, normalBuffers.VertexCount * sizeof(int), LockFlags.None);
                indexDataStream.WriteRange(Enumerable.Range(0, normalBuffers.VertexCount).ToArray());
                indexBuffer.Unlock();
                normalBuffers.Indices = indexBuffer;

                _normals.Add(mesh, normalBuffers);
            }
            return _normals[mesh];
        }
        private void CreateIndexBuffer(int triangleCount)
        {
            int indexSize = sizeof(int);
            if (m_indexBuffer == null || m_indexBuffer.Description.Size < triangleCount * 3 * indexSize)
            {
                if (m_indexBuffer != null)
                    m_indexBuffer.Dispose();

                m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, triangleCount * 3 * indexSize, Usage.WriteOnly, Pool.Default, false);
            }

            int indexOffset = 0;
            m_meshes.Clear();
            foreach (var pair in m_materialGroups)
            {
                var mesh = new MyRenderGroup() { IndexStart = indexOffset, Material = pair.Key };

                foreach (var meshInfo in pair.Value)
                {
                    // Copy indices
                    var compoundIb = m_indexBuffer.Lock(indexOffset * indexSize, meshInfo.Mesh.TriCount * 3 * indexSize, LockFlags.None);

                    var format = meshInfo.Model.IndexBuffer.Description.Format;
                    Debug.Assert(format == Format.Index16 || format == Format.Index32);
                    int modelIndexSize = format == Format.Index16 ? sizeof(short) : sizeof(int);
                    var modelIb = meshInfo.Model.IndexBuffer.Lock(meshInfo.Mesh.IndexStart * modelIndexSize, meshInfo.Mesh.TriCount * 3 * modelIndexSize, LockFlags.ReadOnly);

                    while (modelIb.RemainingLength > 0)
                    {
                        if (format == Format.Index16)
                        {
                            int index = modelIb.Read<short>() + meshInfo.VertexOffset;
                            compoundIb.Write(index);
                        }
                        else
                        {
                            int index = modelIb.Read<int>() + meshInfo.VertexOffset;
                            compoundIb.Write(index);
                        }
                    }
                    m_indexBuffer.Unlock();
                    meshInfo.Model.IndexBuffer.Unlock();

                    mesh.TriangleCount += meshInfo.Mesh.TriCount;
                    indexOffset += meshInfo.Mesh.TriCount * 3;
                }

                m_meshes.Add(mesh);
            }
        }
Exemple #20
0
 protected override void DisposeDisposables()
 {
     base.DisposeDisposables();
     if (vertexBuffer != null) vertexBuffer.Dispose();
     if (indexBuffer != null) indexBuffer.Dispose();
     vertexBuffer = null; indexBuffer = null;
 }
        public override void LoadContent()
        {
            int vbSize;
            unsafe { vbSize = sizeof(MyVertexFormatPositionNormalTextureTangent) * m_vertices.Length; }
            int ibSize = sizeof(ushort) * m_indices.Length;

            m_vertexBuffer = new VertexBuffer(MyRender.Device, vbSize, Usage.Dynamic | Usage.WriteOnly, VertexFormat.None, Pool.Default);
            m_vertexBuffer.Tag = this;
            m_vertexBuffer.SetData(m_vertices, LockFlags.Discard);

            m_indexBuffer = new IndexBuffer(MyRender.Device, ibSize, Usage.Dynamic | Usage.WriteOnly, Pool.Default, true);
            m_indexBuffer.Tag = this;
            m_indexBuffer.SetData(m_indices, LockFlags.Discard);

            m_ropeMaterial.PreloadTexture();

            base.LoadContent();
        }
        /// <summary>
        /// Unloads the content.
        /// </summary>
        public override void UnloadContent()
        {
            MyRender.Log.WriteLine("TransparentGeometry.UnloadContent - START");
            MyRender.Log.IncreaseIndent();


            if (m_indexBuffer != null)
            {
                m_indexBuffer.Dispose();
                m_indexBuffer = null;
            }

            if (m_vertexBuffer != null)
            {
                m_vertexBuffer.Dispose();
                m_vertexBuffer = null;
            }

            MyRender.Log.DecreaseIndent();
            MyRender.Log.WriteLine("TransparentGeometry.UnloadContent - END");

        }
        public override void UnloadContent()
        {
            m_vertexBuffer.Dispose();
            m_indexBuffer.Dispose();

            m_vertexBuffer = null;
            m_indexBuffer = null;

            base.UnloadContent();
        }