private bool Idle() { if (GorgonTiming.SecondsSinceStart > 30) { GorgonDialogs.InfoBox(this, string.Format("{0} text objects created. None survived.", _textCount)); return(false); } ++_textCount; // This is for testing, for the love of all that is good and holy, do NOT do this in your code. GorgonText text = _2d.Renderables.CreateText("Test", _font, string.Format("This is a test. When object #{0} goes out of scope, the event should unbind because of WeakEventManager.", _textCount)); _swap.Clear(Color.CornflowerBlue); text.Position = new SlimMath.Vector2(50, 50); text.Color = Color.Yellow; text.Draw(); if (_debug.Objects.Count > 512) { _debug.GcTest(); } _debug.Objects.Add(new WeakReference <GorgonText>(text)); _2d.Render(); _swap.Flip(1); return(true); }
/// <summary> /// Function to handle idle time for the application. /// </summary> /// <returns>TRUE to continue processing, FALSE to stop.</returns> private static bool Idle() { // Clear the swap chain. _swap.Clear(_clearColor); // Animate our clear color so we have something to look at instead of a blank screen. _clearColor = new GorgonColor( _clearColor.Red + (GorgonTiming.Delta * _colorDirection * 0.0125f), _clearColor.Green + (GorgonTiming.Delta * _colorDirection * 0.0125f), _clearColor.Blue + (GorgonTiming.Delta * _colorDirection * 0.0125f) ); if (((_clearColor.Red > (250.0f / 255.0f)) || (_clearColor.Red < (25.0f / 255.0f))) && ((_clearColor.Green > (245.0f / 255.0f)) || (_clearColor.Green < (24.5f / 255.0f))) && ((_clearColor.Blue > (220.0f / 255.0f)) || (_clearColor.Blue < (22.0f / 255.0f)))) { _colorDirection *= -1; // Ensure that we don't get stuck. _clearColor = _colorDirection < 0 ? new GorgonColor(250.0f / 255.0f, 245f / 255.0f, 220f / 255.0f) : new GorgonColor(25.0f / 255.0f, 24.5f / 255.0f, 22.0f / 255.0f); } // Now we flip our buffers on the swap chain. // We need to this or we won't see anything. // Note that we can limit this to flip on a specified number of vertical retraces. This // will enable us to lock the frame rate to that of the refresh rate of the monitor. _swap.Flip(); return(true); }
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); }
/// <summary> /// Main application loop. /// </summary> /// <returns>TRUE to continue processing, FALSE to stop.</returns> private static bool Idle() { Matrix world; _swapChain.Clear(Color.CornflowerBlue, 1.0f); _cloudRotation += 2.0f * GorgonTiming.Delta; _objRotation += 50.0f * GorgonTiming.Delta; if (_cloudRotation > 359.9f) { _cloudRotation -= 359.9f; } if (_objRotation > 359.9f) { _objRotation -= 359.9f; } ProcessKeys(); _material.UVOffset = new Vector2(0, _material.UVOffset.Y - 0.125f * GorgonTiming.Delta); _graphics.Shaders.PixelShader.Current = _waterShader; if (_material.UVOffset.Y < 0.0f) { _material.UVOffset = new Vector2(0, 1.0f + _material.UVOffset.Y); } _materialBuffer.Update(ref _material); world = _triangle.World; _wvp.UpdateWorldMatrix(ref world); _graphics.Shaders.PixelShader.Resources[0] = _triangle.Texture; _graphics.Shaders.PixelShader.Resources[1] = _normalMap; _graphics.Shaders.PixelShader.Resources[2] = _specMap; _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_triangle.VertexBuffer, Vertex3D.Size); _graphics.Input.IndexBuffer = _triangle.IndexBuffer; _graphics.Input.PrimitiveType = _triangle.PrimitiveType; _graphics.Output.DrawIndexed(0, 0, _triangle.IndexCount); //_plane.Rotation = new Vector3(_objRotation, 0, 0); world = _plane.World; _wvp.UpdateWorldMatrix(ref world); _graphics.Shaders.PixelShader.Resources[0] = _plane.Texture; _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_plane.VertexBuffer, Vertex3D.Size); _graphics.Input.IndexBuffer = _plane.IndexBuffer; _graphics.Input.PrimitiveType = _plane.PrimitiveType; _graphics.Output.DrawIndexed(0, 0, _plane.IndexCount); var worldRot = _icoSphere.Rotation; worldRot.Y += 4.0f * GorgonTiming.Delta; _icoSphere.Rotation = worldRot; world = _icoSphere.World; _wvp.UpdateWorldMatrix(ref world); _graphics.Shaders.PixelShader.Current = _bumpShader; _graphics.Shaders.PixelShader.Resources[0] = _icoSphere.Texture; _graphics.Shaders.PixelShader.Resources[1] = _normalEarfMap; _graphics.Shaders.PixelShader.Resources[2] = _specEarfMap; _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_icoSphere.VertexBuffer, Vertex3D.Size); _graphics.Input.IndexBuffer = _icoSphere.IndexBuffer; _graphics.Input.PrimitiveType = _icoSphere.PrimitiveType; _graphics.Output.DrawIndexed(0, 0, _icoSphere.IndexCount); var cubeMat = new Material { UVOffset = Vector2.Zero, SpecularPower = 0.0f }; _materialBuffer.Update(ref cubeMat); _cube.Rotation = new Vector3(_objRotation, _objRotation, _objRotation); world = _cube.World; _wvp.UpdateWorldMatrix(ref world); _graphics.Shaders.PixelShader.Resources[0] = _cube.Texture; _graphics.Shaders.PixelShader.Resources[1] = _gorgNrm; _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_cube.VertexBuffer, Vertex3D.Size); _graphics.Input.IndexBuffer = _cube.IndexBuffer; _graphics.Input.PrimitiveType = _cube.PrimitiveType; _graphics.Output.DrawIndexed(0, 0, _cube.IndexCount); var sphereMat = new Material { UVOffset = Vector2.Zero, SpecularPower = 0.75f }; _materialBuffer.Update(ref sphereMat); _yPos = (_objRotation.Radians().Sin().Abs() * 2.0f) - 1.10f; _sphere.Position = new Vector3(-2.0f, _yPos, 0.75f); _sphere.Rotation = new Vector3(_objRotation, _objRotation, 0); world = _sphere.World; _wvp.UpdateWorldMatrix(ref world); _graphics.Shaders.PixelShader.Current = _pixelShader; _graphics.Shaders.PixelShader.Resources[0] = _sphere.Texture; _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_sphere.VertexBuffer, Vertex3D.Size); _graphics.Input.IndexBuffer = _sphere.IndexBuffer; _graphics.Input.PrimitiveType = _sphere.PrimitiveType; _graphics.Output.DrawIndexed(0, 0, _sphere.IndexCount); sphereMat = new Material { UVOffset = Vector2.Zero, SpecularPower = 0.0f }; _materialBuffer.Update(ref sphereMat); _graphics.Shaders.PixelShader.Resources[0] = _clouds.Texture; _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_clouds.VertexBuffer, Vertex3D.Size); _graphics.Input.IndexBuffer = _clouds.IndexBuffer; _graphics.Input.PrimitiveType = _clouds.PrimitiveType; _clouds.Rotation = new Vector3(0, _cloudRotation, 0); world = _clouds.World; _wvp.UpdateWorldMatrix(ref world); _graphics.Output.BlendingState.States = GorgonBlendStates.AdditiveBlending; _graphics.Output.DrawIndexed(0, 0, _clouds.IndexCount); _graphics.Output.BlendingState.States = GorgonBlendStates.NoBlending; world = _sphere.World; _wvp.UpdateWorldMatrix(ref world); _graphics.Input.Layout = _normalVertexLayout; _graphics.Input.PrimitiveType = PrimitiveType.LineList; _graphics.Shaders.PixelShader.Current = _normalPixelShader; _graphics.Shaders.VertexShader.Current = _normalVertexShader; _graphics.Input.IndexBuffer = null; _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_sphere.Normals, Vector4.SizeInBytes); _graphics.Output.Draw(0, _sphere.VertexCount * 2); _graphics.Input.Layout = _vertexLayout; _graphics.Shaders.VertexShader.Current = _vertexShader; var state = _renderer2D.Begin2D(); _renderer2D.Drawing.DrawString(_font, string.Format( "FPS: {0:0.0}, Delta: {1:0.000} ms Tris: {3:0} CamRot: {2} Mouse: {4:0}x{5:0} Sensitivity: {6:0.0##}", GorgonTiming.FPS, GorgonTiming.Delta * 1000, _cameraRotation, (_triangle.TriangleCount) + (_plane.TriangleCount) + (_cube.TriangleCount) + (_sphere.TriangleCount) + (_icoSphere.TriangleCount) + (_clouds.TriangleCount), _mouse.Position.X, _mouse.Position.Y, _sensitivity), Vector2.Zero, Color.White); _renderer2D.Flush(); _renderer2D.End2D(state); _swapChain.Flip(); return(true); }
/// <summary> /// Function to handle idle time for the application. /// </summary> /// <returns>TRUE to continue processing, FALSE to stop.</returns> private static bool Idle() { // Clear to our gray color. _swap.Clear(Color.FromArgb(173, 173, 173), 1.0f); // Animate the ball. UpdateBall(); // Draw our planes. foreach (var plane in _planes) { plane.Draw(); } // Draw the main sphere. _sphere.Draw(); // Draw the sphere shadow first. var spherePosition = _sphere.Position; Graphics.Output.DepthStencilState.States = _noDepth; Graphics.Shaders.PixelShader.Current = _pixelShaderShadow; _sphere.Position = new Vector3(spherePosition.X + 0.25f, spherePosition.Y - 0.125f, spherePosition.Z); _sphere.Draw(); // Reset our sphere position, pixel shader and depth writing state. _sphere.Position = spherePosition; Graphics.Output.DepthStencilState.States = _depth; Graphics.Shaders.PixelShader.Current = _pixelShader; // Draw our text. // Use this to show how incredibly slow and terrible my 3D code is. // Tell the 2D renderer to remember the current state of the 3D scene. _3DState = _2D.Begin2D(); _2D.Drawing.FilledRectangle(new RectangleF(0, 0, _mainForm.ClientSize.Width - 1.0f, 38.0f), Color.FromArgb(128, 0, 0, 0)); _2D.Drawing.DrawRectangle(new RectangleF(0, 0, _mainForm.ClientSize.Width, 38.0f), Color.White); _2D.Drawing.DrawString(Graphics.Fonts.DefaultFont, "FPS: " + GorgonTiming.AverageFPS.ToString("0.0") + "\nDelta: " + (GorgonTiming.AverageDelta * 1000.0f).ToString("0.0##") + " milliseconds", new Vector2(3.0f, 0.0f), GorgonColor.White); // Draw our logo because I'm insecure. _2D.Drawing.Blit(Graphics.Textures.GorgonLogo, new RectangleF(_mainForm.ClientSize.Width - Graphics.Textures.GorgonLogo.Settings.Width, _mainForm.ClientSize.Height - Graphics.Textures.GorgonLogo.Settings.Height, Graphics.Textures.GorgonLogo.Settings.Width, Graphics.Textures.GorgonLogo.Settings.Height)); // Note that we're rendering here but not flipping the buffer (the 'false' parameter). This just delays the page // flipping until later. Technically, we don't need to do this here because it's the last thing we're doing, but // if we had more rendering to do after, we'd have to flip manually. _2D.Flush(); // Restore the 3D scene state. _2D.End2D(_3DState); // Now we flip our buffers. // We need to this or we won't see anything. _swap.Flip(); return(true); }