Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
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;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Discard is valid only for dynamic buffers
        /// </summary>
        public static void SetData <T>(this IndexBuffer indexBuffer, T[] data, LockFlags flags = LockFlags.None)
            where T : struct
        {
            int itemSize = Utilities.SizeOf <T>();
            var ptr      = indexBuffer.LockToPointer(0, itemSize * data.Length, flags);

            Utilities.Write(ptr, data, 0, data.Length);
            indexBuffer.Unlock();
        }
Ejemplo n.º 4
0
Archivo: Mesh.cs Proyecto: Sadral/TRRM
 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;
 }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        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];
        }