Exemple #1
0
        private void RenderLoop()
        {
            while (_isRendering)
            {
                _gameTime.BeginUpdate();

                _controller.Update(_gameTime);

                foreach (IGameObject gameObject in _gameObjects)
                {
                    gameObject.Update(_gameTime, _controller);
                }

                CastRays(_gameTime);

                float maxIntensity = 25f;

                for (int r = 0; r < CellRows; r++)
                {
                    for (int c = 0; c < CellCols; c++)
                    {
                        float cappedIntensity = Math.Min(_grid[r, c].Intensity / maxIntensity, 1);

                        float logIntensity = (float)Math.Log(100 * cappedIntensity + 1, 100);

                        float smoothedIntensity = Lerp(_smoothedIntensities[r, c], logIntensity, 0.25f);

                        _smoothedIntensities[r, c] = Math.Max(smoothedIntensity, 0.035f);
                    }
                }

                _gameTime.StartEvent("Shading");

                using (Graphics graphics = Graphics.FromImage(_background))
                {
                    graphics.PixelOffsetMode    = PixelOffsetMode.HighSpeed;
                    graphics.InterpolationMode  = InterpolationMode.NearestNeighbor;
                    graphics.CompositingQuality = CompositingQuality.HighSpeed;
                    graphics.Clear(Color.Black);

                    graphics.FillRectangle(_caveBrush, 0, 0, 1280, 720);

                    foreach (IGameObject gameObject in _gameObjects)
                    {
                        gameObject.Draw(graphics);
                    }

                    _shadowShader.Run(_shadowQuality, _frameBuffer, _background, _smoothedIntensities);
                }

                _gameTime.EndEvent("Shading");

                foreach (Cell cell in _grid)
                {
                    cell.Reset();
                }

                using (Graphics graphics = Graphics.FromImage(_frameBuffer))
                {
                    _gameTime.Draw(graphics, Width, Height);

                    string raysPerSecond = (_rayTracer.RaysCasted * _gameTime.CurrentFps).ToString("###,###,###");

                    graphics.DrawString($"{raysPerSecond} Rays/s", new Font("Arial", 16), new SolidBrush(Color.White), 20, Height - 50);
                }

                SetCaptureSource(_frameBuffer);

                _gameTime.FinalizeUpdate();
            }
        }