예제 #1
0
        /// <summary>
        /// Function to flush the cache by rendering its contents.
        /// </summary>
        public void Flush()
        {
            GorgonVertexBufferBinding binding = _renderer.Graphics.Input.VertexBuffers[0];

            // Apply the current projection/view matrix.
            if (_renderer.Camera.NeedsUpdate)
            {
                _renderer.Camera.Update();
            }

            // Only advance the cache when we've got something to copy into the buffer.
            switch (binding.VertexBuffer.Settings.Usage)
            {
            case BufferUsage.Dynamic:
                // If we're not at the beginning of the cache, then
                // do a no overwrite lock.  This will help performance.
                var flags = BufferLockFlags.Write
                            | (_currentVertex > 0 ? BufferLockFlags.NoOverwrite : BufferLockFlags.Discard);

                using (GorgonDataStream stream = binding.VertexBuffer.Lock(flags, _renderer.Graphics))
                {
                    stream.Position = _currentVertex * Gorgon2DVertex.SizeInBytes;
                    stream.WriteRange(_vertices, _currentVertex, _verticesWritten);
                    binding.VertexBuffer.Unlock();
                }
                break;

            default:
                binding.VertexBuffer.Update(_vertices,
                                            _currentVertex * Gorgon2DVertex.SizeInBytes,
                                            _renderer.Graphics);
                break;
            }

            // Draw the buffer data.
            switch (_renderer.Graphics.Input.PrimitiveType)
            {
            case PrimitiveType.PointList:
            case PrimitiveType.LineList:
                _renderer.Graphics.Output.Draw(_currentVertex, _verticesWritten);
                break;

            case PrimitiveType.TriangleList:
                if (_renderer.Graphics.Input.IndexBuffer == null)
                {
                    _renderer.Graphics.Output.Draw(_currentVertex, _verticesWritten);
                }
                else
                {
                    _renderer.Graphics.Output.DrawIndexed(_firstIndex, _vertexOffset, _indexCount);
                }
                break;
            }

            _currentVertex   = _nextVertex;
            _firstIndex     += _indexCount;
            _verticesWritten = 0;
            _indexCount      = 0;
            NeedsFlush       = false;
        }
예제 #2
0
        public void BouncyBalls3D()
        {
            Gorgon.Run(_form, () =>
            {
                _swap.Clear(Color.Black);
                for (int i = 0; i < Count; i++)
                {
                    _balls[i].Angle += _balls[i].AngleDelta * GorgonTiming.Delta;

                    if (_balls[i].Angle > 360.0f)
                    {
                        _balls[i].Angle      = _balls[i].Angle - 360.0f;
                        _balls[i].AngleDelta = GorgonRandom.RandomSingle() * 90.0f;
                    }

                    if ((_balls[i].ScaleBouce) || (!_balls[i].ZBounce))
                    {
                        _balls[i].Color.W -= _balls[i].Velocity.Z * GorgonTiming.Delta;
                    }
                    else
                    {
                        _balls[i].Color.W += _balls[i].Velocity.Z * GorgonTiming.Delta;
                    }

                    if (_balls[i].Color.W > 1.0f)
                    {
                        _balls[i].Color.W = 1.0f;
                    }

                    if (_balls[i].Color.W < 0.0f)
                    {
                        _balls[i].Color.W = 0.0f;
                        _balls[i].Color   = new Vector4((GorgonRandom.RandomSingle() * 0.961f) + 0.039f,
                                                        (GorgonRandom.RandomSingle() * 0.961f) + 0.039f,
                                                        (GorgonRandom.RandomSingle() * 0.961f) + 0.039f, 0.0f);
                    }

                    if (_balls[i].YBounce)
                    {
                        _balls[i].Position.Y -= (_balls[i].Velocity.Y * GorgonTiming.Delta);
                    }
                    else
                    {
                        _balls[i].Position.Y += (_balls[i].Velocity.Y * GorgonTiming.Delta);
                    }

                    if (_balls[i].XBounce)
                    {
                        _balls[i].Position.X -= (_balls[i].Velocity.X * GorgonTiming.Delta);
                    }
                    else
                    {
                        _balls[i].Position.X += (_balls[i].Velocity.X * GorgonTiming.Delta);
                    }
                    if (_balls[i].ZBounce)
                    {
                        _balls[i].Position.Z -= (_balls[i].Velocity.Z * GorgonTiming.Delta);
                    }
                    else
                    {
                        _balls[i].Position.Z += (_balls[i].Velocity.Z * GorgonTiming.Delta);
                    }

                    if (_balls[i].Position.X > (1.0f * _aspect))
                    {
                        _balls[i].Position.X = (1.0f * _aspect);
                        _balls[i].Velocity.X = (GorgonRandom.RandomSingle() * 0.5f);
                        _balls[i].XBounce    = !_balls[i].XBounce;
                    }
                    if (_balls[i].Position.Y > (1.0f * _aspect))
                    {
                        _balls[i].Position.Y = (1.0f * _aspect);
                        _balls[i].Velocity.Y = (GorgonRandom.RandomSingle() * 0.5f);
                        _balls[i].YBounce    = !_balls[i].YBounce;
                    }

                    if (_balls[i].Position.X < (-1.0f * _aspect))
                    {
                        _balls[i].Position.X = (-1.0f * _aspect);
                        _balls[i].Velocity.X = GorgonRandom.RandomSingle() * 0.5f;
                        _balls[i].XBounce    = !_balls[i].XBounce;
                    }
                    if (_balls[i].Position.Y < (-1.0f * _aspect))
                    {
                        _balls[i].Position.Y = (-1.0f * _aspect);
                        _balls[i].Velocity.Y = GorgonRandom.RandomSingle() * 0.5f;
                        _balls[i].YBounce    = !_balls[i].YBounce;
                    }


                    if (_balls[i].Position.Z < -1.0f)
                    {
                        _balls[i].Position.Z = -1.0f;
                        _balls[i].Velocity.Z = GorgonRandom.RandomSingle() * 0.5f;
                        _balls[i].ZBounce    = !_balls[i].ZBounce;
                    }

                    if (!(_balls[i].Position.Z > 1.0f))
                    {
                        continue;
                    }

                    _balls[i].Position.Z = 1.0f;
                    _balls[i].Velocity.Z = GorgonRandom.RandomSingle() * 0.5f;
                    _balls[i].ZBounce    = !_balls[i].ZBounce;
                }

                var sortPos = _balls.OrderByDescending(item => item.Position.Z).ToArray();

                for (int i = 0; i < Count * 4; i += 4)
                {
                    int arrayindex = i / 4;

                    Matrix world;
                    if (_3d)
                    {
                        Quaternion rot = Quaternion.RotationYawPitchRoll(sortPos[arrayindex].Angle.Cos(), -sortPos[arrayindex].Angle.Sin() * 2.0f, -sortPos[arrayindex].Angle);
                        Matrix.RotationQuaternion(ref rot, out world);
                    }
                    else
                    {
                        world = Matrix.RotationZ(-sortPos[arrayindex].Angle);
                    }
                    world = world * (Matrix.Scaling(sortPos[arrayindex].Scale, sortPos[arrayindex].Scale, 1.0f)) *
                            Matrix.Translation(sortPos[arrayindex].Position.X, sortPos[arrayindex].Position.Y,
                                               sortPos[arrayindex].Position.Z);
                    Matrix trans;
                    Matrix.Multiply(ref world, ref pvw, out trans);

                    _sprite[i].Color = new GorgonColor(sortPos[arrayindex].Color);

                    _sprite[i + 1].Color = _sprite[i].Color;
                    _sprite[i + 2].Color = _sprite[i].Color;
                    _sprite[i + 3].Color = _sprite[i].Color;

                    _sprite[i].Position     = Vector3.Transform(new Vector3(-0.1401f, 0.1401f, 0.0f), trans);
                    _sprite[i + 1].Position = Vector3.Transform(new Vector3(0.1401f, 0.1401f, 0.0f), trans);
                    _sprite[i + 2].Position = Vector3.Transform(new Vector3(-0.1401f, -0.1401f, 0.0f), trans);
                    _sprite[i + 3].Position = Vector3.Transform(new Vector3(0.1401f, -0.1401f, 0.0f), trans);

                    if (sortPos[arrayindex].Checkered)
                    {
                        _sprite[i].UV     = new Vector2(0.503f, 0.0f);
                        _sprite[i + 1].UV = new Vector2(1.0f, 0.0f);
                        _sprite[i + 2].UV = new Vector2(0.503f, 0.5f);
                        _sprite[i + 3].UV = new Vector2(1.0f, 0.5f);
                    }
                    else
                    {
                        _sprite[i].UV     = new Vector2(0.0f, 0.503f);
                        _sprite[i + 1].UV = new Vector2(0.5f, 0.503f);
                        _sprite[i + 2].UV = new Vector2(0.0f, 1.0f);
                        _sprite[i + 3].UV = new Vector2(0.5f, 1.0f);
                    }
                }

                using (GorgonDataStream vstream = _vertices.Lock(BufferLockFlags.Write | BufferLockFlags.Discard))
                {
                    vstream.WriteRange(_sprite);
                    _vertices.Unlock();
                }

                _graphics.Output.DrawIndexed(0, 0, 6 * Count);

                _swap.Flip();

                _form.Text = string.Format("FPS: {0:0.0}  DT: {1:0.000}ms", GorgonTiming.FPS, GorgonTiming.Delta * 1000);

                return(true);
            });

            Assert.IsTrue(_form.TestResult == DialogResult.Yes);
        }