Beispiel #1
0
        public BaseGizmo(GraphicsDevice device, Effect effect)
        {
            _effect = effect;
            _device = device;

            // Create the gizmo geometry
            _rotationHelperGeometry = Buffer.Vertex.New <SolidVertex>(device, _rotationTrianglesCount * 3 + 2);
            _cylinder = GeometricPrimitive.Cylinder.New(_device, 1.0f, 1.0f, _lineRadiusTesselation);
            _cube     = GeometricPrimitive.Cube.New(_device, 1.0f);
            _cone     = GeometricPrimitive.Cone.New(_device, 1.0f, 1.3f, 16);

            // Create the rasterizer state for wireframe drawing
            var renderStateDesc = new SharpDX.Direct3D11.RasterizerStateDescription
            {
                CullMode                 = SharpDX.Direct3D11.CullMode.None,
                DepthBias                = 0,
                DepthBiasClamp           = 0,
                FillMode                 = SharpDX.Direct3D11.FillMode.Wireframe,
                IsAntialiasedLineEnabled = true,
                IsDepthClipEnabled       = true,
                IsFrontCounterClockwise  = false,
                IsMultisampleEnabled     = true,
                IsScissorEnabled         = false,
                SlopeScaledDepthBias     = 0
            };

            _rasterizerWireframe = RasterizerState.New(_device, renderStateDesc);
        }
Beispiel #2
0
        public ParticleSystem(GraphicsDevice device, ContentManager content)
        {
            this.device = device;

            // Create vertex buffer used to spawn new particles
            this.particleStart = Buffer.Vertex.New <ParticleVertex>(device, MAX_NEW);

            // Create vertex buffers to use for updating and drawing the particles alternatively
            var vbFlags = BufferFlags.VertexBuffer | BufferFlags.StreamOutput;

            this.particleDrawFrom = Buffer.New <ParticleVertex>(device, MAX_PARTICLES, vbFlags);
            this.particleStreamTo = Buffer.New <ParticleVertex>(device, MAX_PARTICLES, vbFlags);

            this.layout  = VertexInputLayout.FromBuffer(0, this.particleStreamTo);
            this.effect  = content.Load <Effect>("ParticleEffect");
            this.texture = content.Load <Texture2D>("Dot");

            this.viewParameter           = effect.Parameters["_view"];
            this.projParameter           = effect.Parameters["_proj"];
            this.lookAtMatrixParameter   = effect.Parameters["_lookAtMatrix"];
            this.elapsedSecondsParameter = effect.Parameters["_elapsedSeconds"];
            this.camDirParameter         = effect.Parameters["_camDir"];
            this.gravityParameter        = effect.Parameters["_gravity"];
            this.textureParameter        = effect.Parameters["_texture"];
            this.samplerParameter        = effect.Parameters["_sampler"];
            this.updatePass = effect.Techniques["UpdateTeq"].Passes[0];
            this.renderPass = effect.Techniques["RenderTeq"].Passes[0];
        }
Beispiel #3
0
        private void TryProcessFriends()
        {
            var updated = false;
            Tuple <int, int[]> item;

            while (_loadedFriendsQueue.TryDequeue(out item))
            {
                updated = true;

                var userId    = item.Item1;
                var friendIds = item.Item2;
                var user      = _users[userId];

                user.AllFriends.UnionWith(friendIds);
                foreach (var friendId in friendIds)
                {
                    _users[friendId].AllFriends.Add(userId);
                    if (!_users[friendId].Friends.Contains(userId))
                    {
                        user.Friends.Add(friendId);
                    }
                }
            }

            if (!updated)
            {
                return;
            }

            var nodes        = _nodesBuffer.GetData();
            var edgesIndices = new int[_users.Sum(i => i.AllFriends.Count)];
            var edges        = new EdgeVertex[_users.Sum(i => i.Friends.Count)];

            for (int i = 0, j = 0, k = 0; i < _users.Length; i++)
            {
                nodes[i].EdgesStart = j;
                nodes[i].EdgesEnd   = j + _users[i].AllFriends.Count;

                foreach (var friendId in _users[i].AllFriends)
                {
                    edgesIndices[j++] = friendId;
                }

                foreach (var friendId in _users[i].Friends)
                {
                    edges[k++] = new EdgeVertex(i, friendId);
                }
            }

            _nodesBuffer.SetData(nodes);
            _newNodesBuffer.SetData(nodes);

            _edgeIndicesBuffer?.Dispose();
            _edgesBuffer?.Dispose();

            _edgeIndicesBuffer = edgesIndices.Length == 0 ? null : Buffer.Structured.New(GraphicsDevice, edgesIndices);
            _edgesBuffer       = edges.Length == 0 ? null : Buffer.Structured.New(GraphicsDevice, edges.ToArray());

            _shader.Parameters["EdgeIndices"].SetResource(_edgeIndicesBuffer);
        }
Beispiel #4
0
        private void CreateVertexBuffer()
        {
            var vertices = new VertexPositionNormalTexture[_sourceMesh.Positions.Count];

            for (int i = 0; i < _sourceMesh.Positions.Count; i++)
            {
                vertices[i].Position = ConversionUtility.ToSharpDXVector3(_sourceMesh.Positions[i]);
                vertices[i].Normal   = ConversionUtility.ToSharpDXVector3(_sourceMesh.Normals[i]);
                if (_sourceMesh.TextureCoordinates.Count - 1 >= i)
                {
                    vertices[i].TextureCoordinate = new Vector2(_sourceMesh.TextureCoordinates[i].X, _sourceMesh.TextureCoordinates[i].Y);
                }
            }
            _vertexBuffer = Buffer.Vertex.New(_device, vertices);
        }
Beispiel #5
0
        public void UpdateSceneryVertexBuffer(GraphicsDevice device, VertexList <SceneryVertex> vertexList)
        {
            if (vertexList.Count == 0)
            {
                return;
            }

            if (m_sceneryVertexBuffer == null || m_sceneryVertexBuffer.ElementCount < vertexList.Count)
            {
                Utilities.Dispose(ref m_sceneryVertexBuffer);
                m_sceneryVertexBuffer = Buffer.Vertex.New <SceneryVertex>(device, vertexList.Data.Length);
            }

            m_sceneryVertexBuffer.SetData(vertexList.Data, 0, vertexList.Count);
        }
 public RenderDeferredIndexedCommand(
     Effect effect,
     PrimitiveType primitiveType,
     SharpDX.Toolkit.Graphics.Buffer <T> vertexBuffer,
     int numVertices,
     SharpDX.Toolkit.Graphics.Buffer indexBuffer,
     int numIndices,
     bool is32bit,
     Dictionary <string, GraphicsResource> resources,
     Matrix world,
     BlendState blend = null,
     DepthStencilState depthStencil = null,
     RasterizerState rasterizer     = null,
     string technique = null,
     int startIndex   = 0)
     : base(effect, primitiveType, vertexBuffer, numVertices, indexBuffer, numIndices, is32bit, resources, world, blend, depthStencil, rasterizer, technique)
 {
 }
Beispiel #7
0
        public void UpdateVertexBuffer(GraphicsDevice device, VertexList <TerrainVertex> vertexList)
        {
            if (vertexList.Count == 0)
            {
                return;
            }

            if (m_vertexBuffer == null || m_vertexBuffer.ElementCount < vertexList.Count)
            {
                if (vertexList.Count > m_maxVertices)
                {
                    m_maxVertices = vertexList.Count;
                }

                //System.Diagnostics.Trace.TraceError("Alloc {0}: {1} verts", this.ChunkOffset, m_maxVertices);

                Utilities.Dispose(ref m_vertexBuffer);
                m_vertexBuffer = Buffer.Vertex.New <TerrainVertex>(device, m_maxVertices);
            }

            m_vertexBuffer.SetData(vertexList.Data, 0, vertexList.Count);
        }
Beispiel #8
0
        /// <summary>
        /// Executes once on device creation.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

            // define vertices for single triangle
            var vertices = new[]
            {
                new VertexInputType(new Vector3(-1.0f, -1.0f, 0.0f), Color.Red),
                new VertexInputType(new Vector3(0.0f, 1.0f, 0.0f), Color.Green),
                new VertexInputType(new Vector3(1.0f, -1.0f, 0.0f), Color.Blue),
            };

            // create vertex buffer
            _vertexBuffer      = ToDisposeContent(Buffer.Vertex.New(GraphicsDevice, vertices));
            _vertexBuffer.Name = "triangle_vb";

            // create input layout
            _inputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer);

            // load effect file
            _effect      = ToDisposeContent(Content.Load <Effect>("ShaderGame1"));
            _effect.Name = "triangle_fx";
        }
Beispiel #9
0
        private Buffer <SolidVertex> GetVertexBufferFromBoundingBox(BoundingBox box)
        {
            var p0 = new SolidVertex(new Vector3(box.Minimum.X, box.Minimum.Y, box.Minimum.Z));
            var p1 = new SolidVertex(new Vector3(box.Maximum.X, box.Minimum.Y, box.Minimum.Z));
            var p2 = new SolidVertex(new Vector3(box.Maximum.X, box.Minimum.Y, box.Maximum.Z));
            var p3 = new SolidVertex(new Vector3(box.Minimum.X, box.Minimum.Y, box.Maximum.Z));
            var p4 = new SolidVertex(new Vector3(box.Minimum.X, box.Maximum.Y, box.Minimum.Z));
            var p5 = new SolidVertex(new Vector3(box.Maximum.X, box.Maximum.Y, box.Minimum.Z));
            var p6 = new SolidVertex(new Vector3(box.Maximum.X, box.Maximum.Y, box.Maximum.Z));
            var p7 = new SolidVertex(new Vector3(box.Minimum.X, box.Maximum.Y, box.Maximum.Z));

            var vertices = new[]
            {
                p4, p5, p5, p1, p1, p0, p0, p4,
                p5, p6, p6, p2, p2, p1, p1, p5,
                p2, p6, p6, p7, p7, p3, p3, p2,
                p7, p4, p4, p0, p0, p3, p3, p7,
                p7, p6, p6, p5, p5, p4, p4, p7,
                p0, p1, p1, p2, p2, p3, p3, p0
            };

            return(Buffer.New(_device, vertices, BufferFlags.VertexBuffer, SharpDX.Direct3D11.ResourceUsage.Default));
        }
Beispiel #10
0
        public MeshCamera(Room room, float zoom, Vector2R?pos) : base(room, zoom, pos)
        {
            pendingVertices = new List <SpriteVertex>();
            //permVertices = new List<SpriteVertex>();
            //Perms = new HashSet<SpriteVertex>();
            pendingVertexQueue = new BlockingCollection <List <SpriteVertex> >(3);
            device             = OrbIt.Game.GraphicsDevice;
            Mesh   = Buffer.Vertex.New <SpriteVertex>(OrbIt.Game.GraphicsDevice, 16 * 1024);
            layout = VertexInputLayout.FromBuffer(0, Mesh);

            effect  = OrbIt.Game.Content.Load <Effect>("Effects/MixedShaders2");
            texture = OrbIt.Game.Content.Load <Texture2D>("Textures/spritesheet");

            mvpParam         = effect.Parameters["mvp"];
            spriteCountParam = effect.Parameters["SpriteCount"];
            textureParam     = effect.Parameters["ModelTexture"];

            textureSamplerParameter = effect.Parameters["_sampler"];
            effectPass = effect.Techniques["Render"].Passes[0];
            textureParam.SetResource(texture);

            textureSamplerParameter.SetResource(device.SamplerStates.LinearClamp);
            spriteCountParam.SetValue((float)Enum.GetValues(typeof(Textures)).Length);
        }
Beispiel #11
0
        /// <summary>
        /// Executes once on device creation.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

// ================================== NEW CODE START =================================
            // Creates vertices for the cube
            var cubeVertices =
                new[]
            {
                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(0, 0, 1), new Vector2(1, 0)),      // Back
                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, 1.0f), new Vector3(0, 0, 1), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(0, 0, 1), new Vector2(0, 1)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(0, 0, 1), new Vector2(1, 0)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(0, 0, 1), new Vector2(0, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, 1.0f), new Vector3(0, 0, 1), new Vector2(0, 0)),

                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, -1.0f), new Vector3(0, 0, -1), new Vector2(0, 1)),      // Front
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, -1.0f), new Vector3(0, 0, -1), new Vector2(1, 0)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, -1.0f), new Vector3(0, 0, -1), new Vector2(0, 0)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, -1.0f), new Vector3(0, 0, -1), new Vector2(0, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, -1.0f), new Vector3(0, 0, -1), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, -1.0f), new Vector3(0, 0, -1), new Vector2(1, 0)),

                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, 1.0f), new Vector3(0, 1, 0), new Vector2(0, 0)),      // Top
                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, -1.0f), new Vector3(0, 1, 0), new Vector2(0, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, -1.0f), new Vector3(0, 1, 0), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, 1.0f), new Vector3(0, 1, 0), new Vector2(0, 0)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, -1.0f), new Vector3(0, 1, 0), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(0, 1, 0), new Vector2(1, 0)),

                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(0, -1, 0), new Vector2(1, 1)),      // Bottom
                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, -1.0f), new Vector3(0, -1, 0), new Vector2(0, 0)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, -1.0f), new Vector3(0, -1, 0), new Vector2(1, 0)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(0, -1, 0), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, 1.0f), new Vector3(0, -1, 0), new Vector2(0, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, -1.0f), new Vector3(0, -1, 0), new Vector2(0, 0)),

                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(-1, 0, 0), new Vector2(0, 0)),      // Left
                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, -1.0f), new Vector3(-1, 0, 0), new Vector2(0, 1)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, -1.0f), new Vector3(-1, 0, 0), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(-1, 0, 0), new Vector2(0, 0)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, -1.0f), new Vector3(-1, 0, 0), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(-1.0f, 1.0f, 1.0f), new Vector3(-1, 0, 0), new Vector2(1, 0)),

                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, 1.0f), new Vector3(1, 0, 0), new Vector2(1, 1)),      // Right
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, -1.0f), new Vector3(1, 0, 0), new Vector2(0, 0)),
                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, -1.0f), new Vector3(1, 0, 0), new Vector2(1, 0)),
                new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, 1.0f), new Vector3(1, 0, 0), new Vector2(1, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(1, 0, 0), new Vector2(0, 1)),
                new VertexPositionNormalTexture(new Vector3(1.0f, 1.0f, -1.0f), new Vector3(1, 0, 0), new Vector2(0, 0)),
            };

            // create vertex buffer
            _vertexBuffer = ToDisposeContent(Buffer.Vertex.New(GraphicsDevice, cubeVertices));

            _inputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer);
// ================================== NEW CODE END ==================================

            // load effect file
            _effect = ToDisposeContent(Content.Load <Effect>("ShaderGame4"));
        }
Beispiel #12
0
        private void CreateIndexBuffer()
        {
            var indices = _sourceMesh.Indices.Select(x => (short)x).ToArray();

            _indexBuffer = Buffer.Index.New(_device, indices);
        }
Beispiel #13
0
        protected override void Initialize()
        {
            Window.IsMouseVisible = true;
            Shader = Content.Load <Effect>("Shader");

            DrawNodesPass   = Shader.CurrentTechnique.Passes["DrawNodes"];
            DrawEdgesPass   = Shader.CurrentTechnique.Passes["DrawEdges"];
            UpdateNodesPass = Shader.CurrentTechnique.Passes["UpdateNodes"];

            PhotoAtlas = Texture2D.New(GraphicsDevice, 100, 100, PixelFormat.R8G8B8A8.UNorm, arraySize: 2048);

            ReadUsers();

            var edges       = new List <EdgeVertex>();
            var edgeIndices = new List <int>();
            var vertices    = new NodeVertex[_users.Length];


            var graph = new BidirectionalGraph <NodeType, EdgeType>();

            graph.AddVertexRange(_users);
            graph.AddEdgeRange(_users.SelectMany(i => i.FriendsSet.Select(j => new EdgeType(i, j))));
            var points = new Dictionary <NodeType, Point>();
            var alg    = new GraphSharp.Algorithms.Layout.Simple.FDP.ISOMLayoutAlgorithm <NodeType, EdgeType, GraphType>(graph, points, new ISOMLayoutParameters()
            {
                Width  = 128,
                Height = 78
            });

            for (var i = 0; i < _users.Length; i++)
            {
                var user     = _users[i];
                var angle    = 3.2 * Math.Sqrt(i + 1);
                var position = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * (float)angle * 0.25f;
                points.Add(user, new Point(position.X, position.Y));
            }
            alg.Compute();

            var positions = new Vector2[_users.Length];

            foreach (var point in alg.VertexPositions)
            {
                positions[point.Key.ArrayIndex] = new Vector2((float)point.Value.X, (float)point.Value.Y);
            }

            float dt = 1 / 60f;

            for (var iterCount = 10; iterCount != 0; iterCount--)
            {
                for (var i = 0; i < positions.Length; i++)
                {
                    for (var j = i + 1; j < positions.Length; j++)
                    {
                        var delta    = positions[j] - positions[i];
                        var distance = delta.Length();
                        delta /= distance;

                        var force = dt * delta * (RepulsionForce / ((float)Math.Pow(distance / RepulsionDistance, RepulsionPower) + RepulsionMax));
                        positions[i] -= force;
                        positions[j] += force;
                    }
                }
            }

            for (var i = 0; i < _users.Length; i++)
            {
                var user = _users[i];

                //var position = positions[i];

                //var point = alg.VertexPositions[user];
                //var position = new Vector2((float)point.X, (float)point.Y);

                var angle    = 3.2 * Math.Sqrt(i + 1);
                var position = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * (float)angle * 0.25f;

                //var angle = MathUtil.TwoPi * i / _users.Length;
                //var r = 2 * _users.Length / MathUtil.TwoPi;
                //var position = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * r;

                user.Position = position;
                vertices[i]   = new NodeVertex(position, i, edgeIndices.Count, user.AllFriends.Length);
                edgeIndices.AddRange(user.AllFriends.Select(j => j.ArrayIndex));
                edges.AddRange(user.Friends.Select(j => new EdgeVertex(i, j.ArrayIndex)));
            }

            NodesBuffer       = Buffer.Structured.New(GraphicsDevice, vertices, true);
            NewNodesBuffer    = Buffer.Structured.New(GraphicsDevice, vertices, true);
            EdgesBuffer       = Buffer.Structured.New(GraphicsDevice, edges.ToArray());
            EdgeIndicesBuffer = Buffer.Structured.New(GraphicsDevice, edgeIndices.ToArray());

            _scale    = 1 / vertices.Last().Position.Length();
            _position = Vector2.Zero;
            _rotation = 0;

            Shader.Parameters["Edges"].SetResource(EdgesBuffer);
            Shader.Parameters["EdgeIndices"].SetResource(EdgeIndicesBuffer);
            Shader.Parameters["atlas"].SetResource(PhotoAtlas);
            Shader.Parameters["atlasSampler"].SetResource(GraphicsDevice.SamplerStates.LinearWrap);
            //Shader.Parameters["scale"].SetValue(Scale2);
            //Shader.Parameters["camPos"].SetValue(_position);

            Layout();
        }
Beispiel #14
0
        protected override void OnDraw()
        {
            // To make sure things are in a defined state for legacy rendering...
            ((TombLib.Rendering.DirectX11.Dx11RenderingSwapChain)SwapChain).BindForce();
            ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState();

            _device.SetDepthStencilState(_device.DepthStencilStates.Default);
            _device.SetRasterizerState(_device.RasterizerStates.CullBack);
            _device.SetBlendState(_device.BlendStates.Opaque);

            Matrix4x4 viewProjection = Camera.GetViewProjectionMatrix(ClientSize.Width, ClientSize.Height);

            Effect solidEffect = _deviceManager.___LegacyEffects["Solid"];

            if (Static != null)
            {
                var model = _wadRenderer.GetStatic(Static);

                var effect = _deviceManager.___LegacyEffects["StaticModel"];

                var world = GizmoTransform;

                effect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                effect.Parameters["Color"].SetValue(Vector4.One);
                effect.Parameters["Texture"].SetResource(_wadRenderer.Texture);
                effect.Parameters["TextureSampler"].SetResource(_device.SamplerStates.Default);

                for (int i = 0; i < model.Meshes.Count; i++)
                {
                    var mesh = model.Meshes[i];

                    _device.SetVertexBuffer(0, mesh.VertexBuffer);
                    _device.SetIndexBuffer(mesh.IndexBuffer, true);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, mesh.VertexBuffer));

                    effect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                    effect.Techniques[0].Passes[0].Apply();

                    foreach (var submesh in mesh.Submeshes)
                    {
                        _device.Draw(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.BaseIndex);
                    }

                    //foreach (var submesh in mesh.Submeshes)
                    //    _device.DrawIndexed(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.MeshBaseIndex);
                }

                _device.SetRasterizerState(_rasterizerWireframe);

                // Draw boxes
                if (DrawVisibilityBox || DrawCollisionBox)
                {
                    if (DrawVisibilityBox)
                    {
                        _vertexBufferVisibility?.Dispose();
                        _vertexBufferVisibility = GetVertexBufferFromBoundingBox(Static.VisibilityBox);

                        _device.SetVertexBuffer(_vertexBufferVisibility);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _vertexBufferVisibility));
                        _device.SetIndexBuffer(null, false);

                        solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
                        solidEffect.CurrentTechnique.Passes[0].Apply();

                        _device.Draw(PrimitiveType.LineList, _vertexBufferVisibility.ElementCount);
                    }

                    if (DrawCollisionBox)
                    {
                        _vertexBufferCollision?.Dispose();
                        _vertexBufferCollision = GetVertexBufferFromBoundingBox(Static.CollisionBox);

                        _device.SetVertexBuffer(_vertexBufferCollision);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _vertexBufferCollision));
                        _device.SetIndexBuffer(null, false);

                        solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(1.0f, 0.0f, 0.0f, 1.0f));
                        solidEffect.CurrentTechnique.Passes[0].Apply();

                        _device.Draw(PrimitiveType.LineList, _vertexBufferCollision.ElementCount);
                    }
                }

                // Draw normals
                if (DrawNormals)
                {
                    var lines = new List <SolidVertex>();
                    for (int i = 0; i < Static.Mesh.VerticesNormals.Count; i++)
                    {
                        var p = Vector3.Transform(Static.Mesh.VerticesPositions[i], world);
                        var n = Vector3.TransformNormal(Static.Mesh.VerticesNormals[i] /
                                                        Static.Mesh.VerticesNormals[i].Length(), world);

                        var v = new SolidVertex();
                        v.Position = p;
                        v.Color    = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
                        lines.Add(v);

                        v          = new SolidVertex();
                        v.Position = p + n * 256.0f;
                        v.Color    = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
                        lines.Add(v);
                    }

                    Buffer <SolidVertex> bufferLines = Buffer.New(_device, lines.ToArray(), BufferFlags.VertexBuffer, SharpDX.Direct3D11.ResourceUsage.Default);
                    _device.SetVertexBuffer(bufferLines);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, bufferLines));
                    _device.SetIndexBuffer(null, false);

                    solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                    solidEffect.Parameters["Color"].SetValue(new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
                    solidEffect.CurrentTechnique.Passes[0].Apply();

                    _device.Draw(PrimitiveType.LineList, bufferLines.ElementCount);
                }
            }

            if (DrawGrid)
            {
                _device.SetRasterizerState(_rasterizerWireframe);

                // Draw the grid
                _device.SetVertexBuffer(0, _plane.VertexBuffer);
                _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _plane.VertexBuffer));
                _device.SetIndexBuffer(_plane.IndexBuffer, true);

                solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                solidEffect.Parameters["Color"].SetValue(Vector4.One);
                solidEffect.Techniques[0].Passes[0].Apply();

                _device.Draw(PrimitiveType.LineList, _plane.VertexBuffer.ElementCount);
            }

            if (DrawLights)
            {
                _device.SetRasterizerState(_rasterizerWireframe);

                foreach (var light in Static.Lights)
                {
                    // Draw the little sphere
                    _device.SetVertexBuffer(0, _littleSphere.VertexBuffer);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _littleSphere.VertexBuffer));
                    _device.SetIndexBuffer(_littleSphere.IndexBuffer, false);

                    var world = Matrix4x4.CreateTranslation(light.Position);
                    solidEffect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                    solidEffect.Parameters["Color"].SetValue(new Vector4(1.0f, 1.0f, 0.0f, 1.0f));
                    solidEffect.Techniques[0].Passes[0].Apply();

                    _device.DrawIndexed(PrimitiveType.TriangleList, _littleSphere.IndexBuffer.ElementCount);

                    if (SelectedLight == light)
                    {
                        _device.SetVertexBuffer(0, _sphere.VertexBuffer);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _sphere.VertexBuffer));
                        _device.SetIndexBuffer(_sphere.IndexBuffer, false);

                        world = Matrix4x4.CreateScale(light.Radius * 2.0f) * Matrix4x4.CreateTranslation(light.Position);
                        solidEffect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
                        solidEffect.Techniques[0].Passes[0].Apply();

                        _device.DrawIndexed(PrimitiveType.TriangleList, _sphere.IndexBuffer.ElementCount);
                    }
                }

                _device.SetRasterizerState(_device.RasterizerStates.CullBack);
            }

            if (DrawGizmo)
            {
                // Draw the gizmo
                SwapChain.ClearDepth();
                _gizmo.Draw(viewProjection);
            }

            if (SelectedLight != null)
            {
                // Draw the gizmo of selected light
                SwapChain.ClearDepth();
                _gizmoLight.Draw(viewProjection);
            }

            // Draw debug strings
            ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState(); // To make sure SharpDx.Toolkit didn't change settings.
            SwapChain.RenderText(new Text
            {
                Font      = _fontDefault,
                PixelPos  = new Vector2(5, 5),
                Alignment = new Vector2(0, 0),
                String    =
                    "Position: " + StaticPosition +
                    "\nRotation: " + StaticRotation.X * (180 / Math.PI) +
                    "\nScale: " + StaticScale
            });
        }
Beispiel #15
0
        protected override void OnDraw()
        {
            // To make sure things are in a defined state for legacy rendering...
            ((TombLib.Rendering.DirectX11.Dx11RenderingSwapChain)SwapChain).BindForce();
            ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState();

            _device.SetDepthStencilState(_device.DepthStencilStates.Default);
            _device.SetRasterizerState(_device.RasterizerStates.CullBack);
            _device.SetBlendState(_device.BlendStates.Opaque);

            Matrix4x4 viewProjection = Camera.GetViewProjectionMatrix(ClientSize.Width, ClientSize.Height);

            /*if (Level != null && Room != null)
             * {
             *  Effect roomsEffect = _deviceManager.Effects["Room"];
             *
             *  roomsEffect.Parameters["TextureEnabled"].SetValue(true);
             *  roomsEffect.Parameters["DrawSectorOutlinesAndUseEditorUV"].SetValue(false);
             *  roomsEffect.Parameters["Highlight"].SetValue(false);
             *  roomsEffect.Parameters["Dim"].SetValue(false);
             *  roomsEffect.Parameters["Color"].SetValue(new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
             *  roomsEffect.Parameters["Texture"].SetResource(_textureAtlas);
             *  roomsEffect.Parameters["TextureSampler"].SetResource(_device.SamplerStates.AnisotropicWrap);
             *  roomsEffect.Parameters["UseVertexColors"].SetValue(true);
             *  roomsEffect.Parameters["TextureAtlasRemappingSize"].SetValue(_textureAtlasRemappingSize);
             *  roomsEffect.Parameters["TextureCoordinateFactor"].SetValue(_textureAtlas == null ? new Vector2(0) : new Vector2(1.0f / _textureAtlas.Width, 1.0f / _textureAtlas.Height));
             *
             *  _device.SetVertexBuffer(0, Room.VertexBuffer);
             *  _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, Room.VertexBuffer));
             *
             *  var world = Matrix4x4.CreateTranslation(new Vector3(-RoomPosition.X, 0, -RoomPosition.Z));
             *
             *  roomsEffect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
             *
             *  roomsEffect.Techniques[0].Passes[0].Apply();
             *  _device.Draw(PrimitiveType.TriangleList, Room.VertexBuffer.ElementCount);
             *
             * }*/

            Effect solidEffect = _deviceManager.___LegacyEffects["Solid"];

            if (_model != null)
            {
                var skin   = (_skinModel != null ? _skinModel : _model);
                var effect = _deviceManager.___LegacyEffects["Model"];

                effect.Parameters["Texture"].SetResource(_wadRenderer.Texture);
                effect.Parameters["TextureSampler"].SetResource(_device.SamplerStates.Default);

                // Build animation transforms
                var matrices = new List <Matrix4x4>();
                if (_editor.ValidAnimationAndFrames)
                {
                    for (var b = 0; b < _model.Meshes.Count; b++)
                    {
                        matrices.Add(_model.AnimationTransforms[b]);
                    }
                }
                else
                {
                    foreach (var bone in _model.Bones)
                    {
                        matrices.Add(bone.GlobalTransform);
                    }
                }

                for (int i = 0; i < skin.Meshes.Count; i++)
                {
                    var mesh = skin.Meshes[i];
                    if (mesh.Vertices.Count == 0)
                    {
                        continue;
                    }

                    _device.SetVertexBuffer(0, mesh.VertexBuffer);
                    _device.SetIndexBuffer(mesh.IndexBuffer, true);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, mesh.VertexBuffer));

                    if (SelectedMesh == _model.Meshes[i] && _editor.ValidAnimationAndFrames)
                    {
                        effect.Parameters["Color"].SetValue(new Vector4(1.0f, 0.0f, 0.0f, 1.0f));
                    }
                    else
                    {
                        effect.Parameters["Color"].SetValue(Vector4.One);
                    }

                    effect.Parameters["ModelViewProjection"].SetValue((matrices[i] * viewProjection).ToSharpDX());

                    effect.Techniques[0].Passes[0].Apply();

                    foreach (var submesh in mesh.Submeshes)
                    {
                        _device.Draw(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.BaseIndex);
                    }

                    //foreach (var submesh in mesh.Submeshes)
                    //   _device.DrawIndexed(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.MeshBaseIndex);
                }

                if (_editor.ValidAnimationAndFrames)
                {
                    _device.SetRasterizerState(_rasterizerWireframe);

                    // Draw selection box
                    if (SelectedMesh != null)
                    {
                        if (_vertexBufferVisibility != null)
                        {
                            _vertexBufferVisibility.Dispose();
                        }
                        int meshIndex = _model.Meshes.IndexOf(SelectedMesh);
                        _vertexBufferVisibility = GetVertexBufferFromBoundingBox(Skin.Meshes[meshIndex].BoundingBox);

                        _device.SetVertexBuffer(_vertexBufferVisibility);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _vertexBufferVisibility));
                        _device.SetIndexBuffer(null, false);

                        solidEffect.Parameters["ModelViewProjection"].SetValue((_model.AnimationTransforms[meshIndex] * viewProjection).ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
                        solidEffect.CurrentTechnique.Passes[0].Apply();

                        _device.Draw(PrimitiveType.LineList, _vertexBufferVisibility.ElementCount);
                    }

                    // Draw collision box
                    if (Configuration.AnimationEditor_ShowCollisionBox)
                    {
                        if (_vertexBufferVisibility != null)
                        {
                            _vertexBufferVisibility.Dispose();
                        }
                        _vertexBufferVisibility = GetVertexBufferFromBoundingBox(_editor.CurrentKeyFrame.BoundingBox);

                        _device.SetVertexBuffer(_vertexBufferVisibility);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _vertexBufferVisibility));
                        _device.SetIndexBuffer(null, false);

                        solidEffect.Parameters["ModelViewProjection"].SetValue((viewProjection).ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(0.0f, 0.0f, 1.0f, 1.0f));
                        solidEffect.CurrentTechnique.Passes[0].Apply();

                        _device.Draw(PrimitiveType.LineList, _vertexBufferVisibility.ElementCount);
                    }
                }
            }

            if (Configuration.AnimationEditor_ShowGrid)
            {
                // Draw the grid
                _device.SetVertexBuffer(0, _plane.VertexBuffer);
                _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _plane.VertexBuffer));
                _device.SetIndexBuffer(_plane.IndexBuffer, true);
                _device.SetRasterizerState(_rasterizerWireframe);

                var shift = Matrix4x4.CreateTranslation(new Vector3(-GridPosition.X, GridPosition.Y, -GridPosition.Z));
                solidEffect.Parameters["ModelViewProjection"].SetValue((shift * viewProjection).ToSharpDX());
                solidEffect.Parameters["Color"].SetValue(Vector4.One);
                solidEffect.Techniques[0].Passes[0].Apply();

                _device.Draw(PrimitiveType.LineList, _plane.VertexBuffer.ElementCount);
            }

            if (Configuration.AnimationEditor_ShowGizmo &&
                SelectedMesh != null && _editor.ValidAnimationAndFrames)
            {
                // Draw the gizmo
                SwapChain.ClearDepth();
                _gizmo.Draw(viewProjection);
            }

            if (_editor.CurrentAnim != null &&
                Configuration.RenderingItem_ShowDebugInfo)
            {
                ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState(); // To make sure SharpDx.Toolkit didn't change settings.
                string debugMessage = "Frame: " + (_editor.CurrentFrameIndex + 1) + "/" + _editor.CurrentAnim.DirectXAnimation.KeyFrames.Count;
                if (SelectedMesh != null)
                {
                    debugMessage += "\nMesh: " + SelectedMesh.Name;
                    debugMessage += "\nBone: " + _model.Bones[_model.Meshes.IndexOf(SelectedMesh)].Name;
                    debugMessage += "\nRotation: " + _editor.CurrentKeyFrame.Rotations[Model.Meshes.IndexOf(SelectedMesh)];
                }
                SwapChain.RenderText(new Text
                {
                    Font      = _fontDefault,
                    PixelPos  = new Vector2(10, 25),
                    Alignment = new Vector2(0, 0),
                    String    = debugMessage
                });
            }
        }
Beispiel #16
0
        protected override void OnDraw()
        {
            // To make sure things are in a defined state for legacy rendering...
            ((TombLib.Rendering.DirectX11.Dx11RenderingSwapChain)SwapChain).BindForce();
            ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState();

            _device.SetDepthStencilState(_device.DepthStencilStates.Default);
            _device.SetRasterizerState(_device.RasterizerStates.CullBack);
            _device.SetBlendState(_device.BlendStates.Opaque);

            Matrix4x4 viewProjection = Camera.GetViewProjectionMatrix(ClientSize.Width, ClientSize.Height);
            Effect    solidEffect    = _deviceManager.___LegacyEffects["Solid"];

            _wadRenderer.Dispose();
            if (Skeleton != null)
            {
                var effect = _deviceManager.___LegacyEffects["Model"];

                effect.Parameters["TextureSampler"].SetResource(_device.SamplerStates.Default);

                foreach (var node in Skeleton)
                {
                    // TODO Keep data on GPU, optimize data upload
                    // Use new renderer
                    var mesh = _wadRenderer.GetStatic(new WadStatic(new WadStaticId(0))
                    {
                        Mesh = node.WadMesh
                    });

                    effect.Parameters["Texture"].SetResource(_wadRenderer.Texture);
                    effect.Parameters["ModelViewProjection"].SetValue((node.GlobalTransform * viewProjection).ToSharpDX());
                    effect.Techniques[0].Passes[0].Apply();

                    foreach (var mesh_ in mesh.Meshes)
                    {
                        _device.SetVertexBuffer(0, mesh_.VertexBuffer);
                        _device.SetIndexBuffer(mesh_.IndexBuffer, true);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, mesh_.VertexBuffer));

                        foreach (var submesh in mesh_.Submeshes)
                        {
                            _device.Draw(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.BaseIndex);
                        }
                    }
                    //foreach (var submesh in mesh_.Submeshes)
                    //    _device.DrawIndexed(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.MeshBaseIndex);
                }

                // Draw box
                if (SelectedNode != null)
                {
                    _vertexBufferVisibility?.Dispose();
                    _vertexBufferVisibility = GetVertexBufferFromBoundingBox(SelectedNode.WadMesh.BoundingBox);

                    _device.SetVertexBuffer(_vertexBufferVisibility);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _vertexBufferVisibility));
                    _device.SetIndexBuffer(null, false);

                    solidEffect.Parameters["ModelViewProjection"].SetValue((SelectedNode.GlobalTransform * viewProjection).ToSharpDX());
                    solidEffect.Parameters["Color"].SetValue(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
                    solidEffect.CurrentTechnique.Passes[0].Apply();

                    _device.Draw(PrimitiveType.LineList, _vertexBufferVisibility.ElementCount);
                }
            }

            if (DrawGrid)
            {
                _device.SetRasterizerState(_rasterizerWireframe);

                // Draw the grid
                _device.SetVertexBuffer(0, _plane.VertexBuffer);
                _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _plane.VertexBuffer));
                _device.SetIndexBuffer(_plane.IndexBuffer, true);

                solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                solidEffect.Parameters["Color"].SetValue(Vector4.One);
                solidEffect.Techniques[0].Passes[0].Apply();

                _device.Draw(PrimitiveType.LineList, _plane.VertexBuffer.ElementCount);
            }

            if (DrawGizmo && SelectedNode != null)
            {
                // Draw the gizmo
                SwapChain.ClearDepth();
                _gizmo.Draw(viewProjection);
            }

            // Draw debug strings
            if (SelectedNode != null)
            {
                ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState(); // To make sure SharpDx.Toolkit didn't change settings.
                Matrix4x4 worldViewProjection = SelectedNode.GlobalTransform * viewProjection;
                SwapChain.RenderText(new Text
                {
                    Font            = _fontDefault,
                    Pos             = worldViewProjection.TransformPerspectively(SelectedNode.Centre - Vector3.UnitY * 128.0f).To2(),
                    TextAlignment   = new Vector2(0, 0),
                    ScreenAlignment = new Vector2(0.5f, 0.5f),
                    String          =
                        "Name: " + SelectedNode.Bone.Name +
                        "\nLocal offset: " + SelectedNode.Bone.Translation
                });
            }
        }
Beispiel #17
0
        private void SurfaceOnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_mouseMoved)
            {
                return;
            }

            var surface = (FrameworkElement)sender;
            var point   = e.GetPosition(surface);

            var origin = _position + new Vector2(
                (float)((point.X / surface.ActualWidth * 2 - 1) / _scale * Aspect),
                (float)((1 - point.Y / surface.ActualHeight * 2) / _scale))
                         .Rotate(-_rotation);

            var prevSelectedId = _selectedUserId;

            var minDistance = float.PositiveInfinity;

            _selectedUserId = -1;
            var nodes = _nodesBuffer.GetData();

            for (var i = 0; i < _users.Length; i++)
            {
                var distance = (nodes[i].Position - origin).Length();
                if (distance < PhotoRadius && distance < minDistance)
                {
                    minDistance     = distance;
                    _selectedUserId = i;
                }
            }

            if (prevSelectedId == _selectedUserId)
            {
                return;
            }

            for (var i = 0; i < _users.Length; i++)
            {
                nodes[i].Width = 1f;
            }

            _selectedEdgesBuffer?.Dispose();
            _selectedEdgesBuffer = null;

            if (_selectedUserId != -1)
            {
                nodes[_selectedUserId].Width = SelectedPhotoWidth;
                foreach (var friend in _users[_selectedUserId].AllFriends)
                {
                    nodes[friend].Width = SelectedFriendsPhotoWidth;
                }

                var edges = _users[_selectedUserId].AllFriends.Select(i => new EdgeVertex(_selectedUserId, i)).ToArray();
                if (edges.Length != 0)
                {
                    _selectedEdgesBuffer = Buffer.Structured.New(GraphicsDevice, edges);
                }

                UserSelected?.Invoke(_users[_selectedUserId].Model);
            }

            _nodesBuffer.SetData(nodes);
            _newNodesBuffer.SetData(nodes);
        }
Beispiel #18
0
        /// <summary>
        /// Executes once on device creation.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

// ================================== NEW CODE START =================================

            //var colors = new[] { Color.Red, Color.Red, Color.Green, Color.Green, Color.Blue, Color.Blue };
            //var colors = new[] { Color.Orange, Color.Orange, Color.OrangeRed, Color.OrangeRed, Color.DarkOrange, Color.DarkOrange };
            var colors = new[] { Color.Orange, Color.Orange, Color.Orange, Color.Orange, Color.Orange, Color.Orange };

            // Creates vertices for the cube
            var cubeVertices =
                new[]
            {
                new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), colors[0]),         // Back
                new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), colors[0]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), colors[0]),
                new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), colors[0]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), colors[0]),
                new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), colors[0]),

                new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), colors[1]),         // Front
                new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), colors[1]),
                new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), colors[1]),
                new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), colors[1]),
                new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), colors[1]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), colors[1]),

                new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), colors[2]),         // Top
                new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), colors[2]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), colors[2]),
                new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), colors[2]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), colors[2]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), colors[2]),

                new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), colors[3]),         // Bottom
                new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), colors[3]),
                new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), colors[3]),
                new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), colors[3]),
                new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), colors[3]),
                new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), colors[3]),

                new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), colors[4]),         // Left
                new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), colors[4]),
                new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), colors[4]),
                new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), colors[4]),
                new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), colors[4]),
                new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), colors[4]),

                new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), colors[5]),         // Right
                new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), colors[5]),
                new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), colors[5]),
                new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), colors[5]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), colors[5]),
                new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), colors[5]),
            };

            // create vertex buffer
            _vertexBuffer = ToDisposeContent(Buffer.Vertex.New(GraphicsDevice, cubeVertices));

            _inputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer);
// ================================== NEW CODE END ==================================

            // load effect file
            _effect = ToDisposeContent(Content.Load <Effect>("ShaderGame2"));
        }
Beispiel #19
0
        public void InitializeNodes(Api api, User[] users)
        {
            _users     = users;
            _usersById = users.ToDictionary(i => i.Model.Id);

            var nodes = new NodeVertex[_users.Length];

            for (var i = 0; i < _users.Length; i++)
            {
                var angle    = 3.2 * Math.Sqrt(i + 1);
                var position = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * (float)angle * 0.25f;

                nodes[i] = new NodeVertex(position, i, 1, 0, 0);
            }

            _nodesBuffer?.Dispose();
            _newNodesBuffer?.Dispose();

            _nodesBuffer    = Buffer.Structured.New(GraphicsDevice, nodes, true);
            _newNodesBuffer = Buffer.Structured.New(GraphicsDevice, nodes, true);

            _edgesBuffer?.Dispose();
            _edgesBuffer = null;
            _selectedEdgesBuffer?.Dispose();
            _selectedEdgesBuffer = null;
            _edgeIndicesBuffer?.Dispose();
            _edgeIndicesBuffer = null;

            _shader.Parameters["Edges"].SetResource(_edgesBuffer);
            _shader.Parameters["EdgeIndices"].SetResource(_edgeIndicesBuffer);

            if (_photoAtlases != null)
            {
                foreach (var atlas in _photoAtlases)
                {
                    atlas.Dispose();
                }
            }
            _photoAtlases = new Texture2D[(_users.Length + (PhotoAtlasSize - 1)) / PhotoAtlasSize];
            for (var i = 0; i < _photoAtlases.Length; i++)
            {
                var size = Math.Min(PhotoAtlasSize, _users.Length - i * PhotoAtlasSize);
                _photoAtlases[i] = Texture2D.New(GraphicsDevice, PhotoAtlasWidth, PhotoAtlasWidth, PixelFormat.R8G8B8A8.UNorm, arraySize: size);
                for (var j = 0; j < size; j++)
                {
                    GraphicsDevice.Copy(_defaultPhoto, 0, _photoAtlases[i], j);
                }
            }

            _scale    = 1 / nodes.Last().Position.Length();
            _position = Vector2.Zero;
            _rotation = 0;

            _dimx = (_users.Length + (BlockSize - 1)) / BlockSize;
            _shader.Parameters["numParticles"].SetValue(_users.Length);
            _shader.Parameters["dimx"].SetValue(_dimx);

            _loadedPhotosQueue  = new ConcurrentQueue <Tuple <int, byte[]> >();
            _loadedFriendsQueue = new ConcurrentQueue <Tuple <int, int[]> >();

            if (_userLoadingCts != null)
            {
                _userLoadingCts.Cancel();
                _userLoadingCts.Dispose();
            }

            _userLoadingCts = new CancellationTokenSource();
            Task.Factory.StartNew(() => StartPhotoLoadingTask(_userLoadingCts.Token), _userLoadingCts.Token);
            Task.Factory.StartNew(() => StartFriendsLoadingTask(api, _userLoadingCts.Token), _userLoadingCts.Token);
        }