コード例 #1
0
        public void DrawIds(MeshMaterialLibrary meshMat, List <Decal> decals, List <PointLight> pointLights, List <DirectionalLight> dirLights, EnvironmentSample envSample, List <DebugEntity> debug, Matrix viewProjection, Matrix view, EditorLogic.EditorSendData editorData)
        {
            _graphicsDevice.SetRenderTarget(_idRenderTarget2D);
            _graphicsDevice.BlendState = BlendState.Opaque;

            _graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;
            _graphicsDevice.DepthStencilState = DepthStencilState.Default;

            meshMat.Draw(MeshMaterialLibrary.RenderType.IdRender, viewProjection);

            //Now onto the billboards
            DrawBillboards(decals, pointLights, dirLights, envSample, debug, viewProjection, view);

            //Now onto the gizmos
            DrawGizmos(viewProjection, editorData, _assets);

            Rectangle sourceRectangle =
                new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);

            Color[] retrievedColor = new Color[1];

            try
            {
                if (sourceRectangle.X >= 0 && sourceRectangle.Y >= 0 && sourceRectangle.X < _idRenderTarget2D.Width - 2 && sourceRectangle.Y < _idRenderTarget2D.Height - 2)
                {
                    _idRenderTarget2D.GetData(0, sourceRectangle, retrievedColor, 0, 1);
                }
            }
            catch
            {
                //nothing
            }

            HoveredId = IdGenerator.GetIdFromColor(retrievedColor[0]);
        }
コード例 #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //  FUNCTIONS
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //  MAIN FUNCTIONS
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Done after Load
        public void Initialize(Assets assets, Space space, GraphicsDevice graphicsDevice)
        {
            _assets       = assets;
            _physicsSpace = space;

            MeshMaterialLibrary = new MeshMaterialLibrary(graphicsDevice);

            SetUpEditorScene(graphicsDevice);
        }
コード例 #3
0
ファイル: EditorRender.cs プロジェクト: zengqh/DeferredEngine
        public void DrawEditorElements(MeshMaterialLibrary meshMaterialLibrary, List <Decal> decals, List <PointLight> lights, List <DirectionalLight> dirLights, EnvironmentSample envSample, List <DebugEntity> debug, Matrix staticViewProjection, Matrix view, EditorLogic.EditorSendData editorData)
        {
            _graphicsDevice.SetRenderTarget(null);
            _graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;
            _graphicsDevice.DepthStencilState = DepthStencilState.Default;
            _graphicsDevice.BlendState        = BlendState.Opaque;

            DrawGizmo(staticViewProjection, editorData);
            DrawBillboards(decals, lights, dirLights, envSample, debug, staticViewProjection, view, editorData);
        }
コード例 #4
0
        /// <summary>
        /// Create the projection matrices
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="meshMaterialLibrary"></param>
        /// <param name="entities"></param>
        private void UpdateViewProjection(Camera camera, MeshMaterialLibrary meshMaterialLibrary, List <BasicEntity> entities)
        {
            _viewProjectionHasChanged = camera.HasChanged;

            //If the camera didn't do anything we don't need to update this stuff
            if (_viewProjectionHasChanged)
            {
                //We have processed the change, now setup for next frame as false
                camera.HasChanged = false;
                camera.HasMoved   = false;

                //View matrix
                _view        = Matrix.CreateLookAt(camera.Position, camera.Lookat, camera.Up);
                _inverseView = Matrix.Invert(_view);

                _projection = Matrix.CreatePerspectiveFieldOfView(camera.FieldOfView,
                                                                  GameSettings.g_ScreenWidth / (float)GameSettings.g_ScreenHeight, 1, GameSettings.g_FarPlane);

                Shaders.GBufferEffectParameter_Camera.SetValue(camera.Position);

                _viewProjection = _view * _projection;

                //this is the unjittered viewProjection. For some effects we don't want the jittered one
                _staticViewProjection = _viewProjection;

                //Transformation for TAA - from current view back to the old view projection
                _currentViewToPreviousViewProjection = Matrix.Invert(_view) * _previousViewProjection;

                _previousViewProjection = _viewProjection;
                _inverseViewProjection  = Matrix.Invert(_viewProjection);

                if (_boundingFrustum == null)
                {
                    _boundingFrustum = new BoundingFrustum(_staticViewProjection);
                }
                else
                {
                    _boundingFrustum.Matrix = _staticViewProjection;
                }

                Matrix id = Matrix.Identity;
            }

            //We need to update whether or not entities are in our boundingFrustum and then cull them or not!
            meshMaterialLibrary.FrustumCulling(entities, _boundingFrustum, _viewProjectionHasChanged, camera.Position);

            //Performance Profiler
            if (GameSettings.d_profiler)
            {
                long performanceCurrentTime = _performanceTimer.ElapsedTicks;
                GameStats.d_profileUpdateViewProjection = performanceCurrentTime - _performancePreviousTime;

                _performancePreviousTime = performanceCurrentTime;
            }
        }
コード例 #5
0
        public void DrawOutlines(MeshMaterialLibrary meshMat, Matrix viewProjection, bool drawAll, int hoveredId, EditorLogic.EditorSendData editorData, bool mouseMoved)
        {
            _graphicsDevice.SetRenderTarget(_idRenderTarget2D);

            if (!mouseMoved)
            {
                _graphicsDevice.Clear(ClearOptions.Target, Color.Black, 0, 0);
            }
            else
            {
                _graphicsDevice.Clear(Color.Black);
            }
            _graphicsDevice.BlendState        = BlendState.Opaque;
            _graphicsDevice.DepthStencilState = DepthStencilState.Default;
            _graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;

            int selectedId = editorData.SelectedObjectId;

            //Selected entity
            if (selectedId != 0)
            {
                //UPdate the size of our outlines!

                if (!drawAll)
                {
                    meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false,
                                 false, selectedId);
                }

                Shaders.IdRenderEffectParameterColorId.SetValue(_selectedColor);
                meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false,
                             outlined: true, outlineId: selectedId);
            }

            if (selectedId != hoveredId && hoveredId != 0 && mouseMoved)
            {
                if (!drawAll)
                {
                    meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false, false, hoveredId);
                }

                Shaders.IdRenderEffectParameterColorId.SetValue(_hoveredColor);
                meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false, outlined: true, outlineId: hoveredId);
            }
        }
コード例 #6
0
        public void Draw(MeshMaterialLibrary meshMat, List <Decal> decals, List <PointLight> pointLights, List <DirectionalLight> dirLights, EnvironmentSample envSample, List <DebugEntity> debug, Matrix viewProjection, Matrix view, EditorLogic.EditorSendData editorData, bool mouseMoved)
        {
            if (editorData.GizmoTransformationMode)
            {
                _graphicsDevice.SetRenderTarget(_idRenderTarget2D);
                _graphicsDevice.Clear(Color.Black);
                return;
            }

            if (mouseMoved)
            {
                DrawIds(meshMat, decals, pointLights, dirLights, envSample, debug, viewProjection, view, editorData);
            }

            if (GameSettings.e_drawoutlines)
            {
                DrawOutlines(meshMat, viewProjection, mouseMoved, HoveredId, editorData, mouseMoved);
            }
        }
コード例 #7
0
        /// <summary>
        /// Draw all our meshes to the GBuffer - albedo, normal, depth - for further computation
        /// </summary>
        /// <param name="meshMaterialLibrary"></param>
        private void DrawTextureBuffer(MeshMaterialLibrary meshMaterialLibrary)
        {
            if (!Input.WasKeyPressed(Keys.C) && !GameSettings.g_UpdateShading)
            {
                return;
            }

            _graphicsDevice.SetRenderTarget(_textureBuffer);
            _graphicsDevice.Clear(Color.TransparentBlack);
            _graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;
            _graphicsDevice.DepthStencilState = DepthStencilState.None;
            meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.TextureBuffer, graphicsDevice: _graphicsDevice, viewProjection: _viewProjection, lightViewPointChanged: true, view: _view);

            //Performance Profiler
            if (GameSettings.d_profiler)
            {
                long performanceCurrentTime = _performanceTimer.ElapsedTicks;
                GameStats.d_profileDrawGBuffer = performanceCurrentTime - _performancePreviousTime;

                _performancePreviousTime = performanceCurrentTime;
            }
        }
コード例 #8
0
        private void DrawObjects(MeshMaterialLibrary meshMaterialLibrary)
        {
            _graphicsDevice.SetRenderTarget(null);
            _graphicsDevice.Clear(GameSettings.g_UpdateShading ? Color.CadetBlue : Color.DarkViolet);
            _graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;
            _graphicsDevice.DepthStencilState = DepthStencilState.Default;
            Shaders.GBufferEffectParameter_Material_Texture.SetValue(GameSettings.g_FixSeams ? _textureBufferSeamFix : _textureBuffer);
            meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.FinalMesh, graphicsDevice: _graphicsDevice, viewProjection: _viewProjection, lightViewPointChanged: true, view: _view);

            _graphicsDevice.RasterizerState = RasterizerState.CullClockwise;
            _graphicsDevice.BlendState      = BlendState.NonPremultiplied;
            //Skybox
            ModelMeshPart part = _assets.Isosphere.Meshes[0].MeshParts[0];

            _graphicsDevice.SetVertexBuffer(part.VertexBuffer);
            _graphicsDevice.Indices = (part.IndexBuffer);
            int primitiveCount = part.PrimitiveCount;
            int vertexOffset   = part.VertexOffset;
            //int vCount = meshLib.GetMesh().NumVertices;
            int startIndex = part.StartIndex;

            Matrix world = Matrix.CreateScale(100);

            Shaders.GBufferEffectParameter_WorldViewProj.SetValue(world * _viewProjection);

            Shaders.GBufferEffectParameter_WorldIT.SetValue(world);

            Shaders.GBufferEffectTechniques_DrawSkybox.Passes[0].Apply();
            _graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, vertexOffset, startIndex, primitiveCount);

            //Performance Profiler
            if (GameSettings.d_profiler)
            {
                long performanceCurrentTime = _performanceTimer.ElapsedTicks;
                GameStats.d_profileDrawGBuffer = performanceCurrentTime - _performancePreviousTime;

                _performancePreviousTime = performanceCurrentTime;
            }
        }
コード例 #9
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        //  RENDER FUNCTIONS
        ////////////////////////////////////////////////////////////////////////////////////////////////////////

        #region MAIN DRAW FUNCTIONS
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        //  MAIN DRAW FUNCTIONS
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Main Draw function of the game
        /// </summary>
        /// <param name="camera">view point of the renderer</param>
        /// <param name="meshMaterialLibrary">a class that has stored all our mesh data</param>
        /// <param name="entities">entities and their properties</param>
        /// <param name="pointLights"></param>
        /// <param name="directionalLights"></param>
        /// <param name="editorData">The data passed from our editor logic</param>
        /// <param name="gameTime"></param>
        /// <returns></returns>
        public void Draw(Camera camera, MeshMaterialLibrary meshMaterialLibrary, List <BasicEntity> entities, List <PointLightSource> pointLights, List <DirectionalLightSource> directionalLights, EditorLogic.EditorSendData editorData, GameTime gameTime)
        {
            //Reset the stat counter, so we can count stats/information for this frame only
            ResetStats();

            //Update the mesh data for changes in physics etc.
            meshMaterialLibrary.FrustumCullingStartFrame(entities);

            //Check if we changed some drastic stuff for which we need to reload some elements
            CheckRenderChanges(directionalLights);

            //Render ShadowMaps
            //DrawShadowMaps(meshMaterialLibrary, entities, pointLights, directionalLights, camera);

            //Update our view projection matrices if the camera moved
            UpdateViewProjection(camera, meshMaterialLibrary, entities);

            GS.BeginMark("DrawTextureBuffer", Color.Red);
            //Draw our meshes to the G Buffer
            DrawTextureBuffer(meshMaterialLibrary);

            GS.EndMark("DrawTextureBuffer");

            FixSeams();

            GS.BeginMark("DrawMeshes", Color.Blue);
            DrawObjects(meshMaterialLibrary);
            GS.EndMark("DrawMeshes");
            //Draw the final rendered image, change the output based on user input to show individual buffers/rendertargets
            RenderMode();

            //Draw (debug) lines
            LineHelperManager.Draw(_graphicsDevice, _staticViewProjection);

            //Set up the frustum culling for the next frame
            meshMaterialLibrary.FrustumCullingFinalizeFrame(entities);
        }
コード例 #10
0
ファイル: EditorLogic.cs プロジェクト: zengqh/DeferredEngine
        /// <summary>
        /// Main Logic for the editor part
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="entities"></param>
        /// <param name="data"></param>
        public void Update(GameTime gameTime,
                           List <BasicEntity> entities,
                           List <Decal> decals,
                           List <PointLight> pointLights,
                           List <DirectionalLight> dirLights,
                           EnvironmentSample envSample,
                           List <DebugEntity> debugEntities,
                           EditorReceivedData data,
                           MeshMaterialLibrary meshMaterialLibrary)
        {
            if (!GameSettings.e_enableeditor)
            {
                return;
            }

            if (!DebugScreen.ConsoleOpen)
            {
                if (Input.WasKeyPressed(Keys.R))
                {
                    GameStats.e_gizmoMode = GizmoModes.Rotation;
                }
                if (Input.WasKeyPressed(Keys.T))
                {
                    GameStats.e_gizmoMode = GizmoModes.Translation;
                }
                if (Input.WasKeyPressed(Keys.Z))
                {
                    GameStats.e_gizmoMode = GizmoModes.Scale;
                }
            }

            _gizmoMode = GameStats.e_gizmoMode;

            int hoveredId = data.HoveredId;

            if (_gizmoTransformationMode)
            {
                if (Input.mouseState.LeftButton == ButtonState.Pressed)
                {
                    GizmoControl(_gizmoId, data);
                }
                else
                {
                    _gizmoTransformationMode = false;
                }
            }
            else if (Input.WasLMBClicked() && !GUIControl.UIWasUsed)
            {
                previousMouseX = Input.mouseState.X;
                previousMouseY = Input.mouseState.Y;

                //Gizmos
                if (hoveredId >= 1 && hoveredId <= 3)
                {
                    _gizmoId = hoveredId;
                    GizmoControl(_gizmoId, data);
                    return;
                }

                if (hoveredId <= 0)
                {
                    SelectedObject = null;
                    return;
                }

                bool foundnew = false;
                //Get the selected entity!
                for (int index = 0; index < entities.Count; index++)
                {
                    var VARIABLE = entities[index];
                    if (VARIABLE.Id == hoveredId)
                    {
                        SelectedObject = VARIABLE;
                        foundnew       = true;
                        break;
                    }
                }
                if (foundnew == false)
                {
                    for (int index = 0; index < decals.Count; index++)
                    {
                        Decal decal = decals[index];
                        if (decal.Id == hoveredId)
                        {
                            SelectedObject = decal;
                            break;
                        }
                    }

                    for (int index = 0; index < pointLights.Count; index++)
                    {
                        PointLight pointLight = pointLights[index];
                        if (pointLight.Id == hoveredId)
                        {
                            SelectedObject = pointLight;
                            break;
                        }
                    }

                    for (int index = 0; index < dirLights.Count; index++)
                    {
                        DirectionalLight directionalLight = dirLights[index];
                        if (directionalLight.Id == hoveredId)
                        {
                            SelectedObject = directionalLight;
                            break;
                        }
                    }

                    {
                        if (envSample.Id == hoveredId)
                        {
                            SelectedObject = envSample;
                        }
                    }

                    for (int index = 0; index < debugEntities.Count; index++)
                    {
                        DirectionalLight debugEntity = dirLights[index];
                        if (debugEntity.Id == hoveredId)
                        {
                            SelectedObject = debugEntity;
                            break;
                        }
                    }
                }
            }

            //Controls

            if (Input.WasKeyPressed(Keys.Delete))
            {
                //Find object
                if (SelectedObject is BasicEntity)
                {
                    entities.Remove((BasicEntity)SelectedObject);
                    meshMaterialLibrary.DeleteFromRegistry((BasicEntity)SelectedObject);

                    SelectedObject = null;
                }
                else if (SelectedObject is Decal)
                {
                    decals.Remove((Decal)SelectedObject);

                    SelectedObject = null;
                }
                else if (SelectedObject is PointLight)
                {
                    pointLights.Remove((PointLight)SelectedObject);

                    SelectedObject = null;
                }
                else if (SelectedObject is DirectionalLight)
                {
                    dirLights.Remove((DirectionalLight)SelectedObject);

                    SelectedObject = null;
                }
            }

            if (Input.WasKeyPressed(Keys.Insert) || (Input.keyboardState.IsKeyDown(Keys.LeftControl) && Input.WasKeyPressed(Keys.C)))
            {
                if (SelectedObject is BasicEntity)
                {
                    BasicEntity copy = (BasicEntity)SelectedObject.Clone;
                    copy.RegisterInLibrary(meshMaterialLibrary);

                    entities.Add(copy);
                }
                else if (SelectedObject is Decal)
                {
                    Decal copy = (Decal)SelectedObject.Clone;
                    decals.Add(copy);
                }
                else if (SelectedObject is PointLight)
                {
                    PointLight copy = (PointLight)SelectedObject.Clone;
                    pointLights.Add(copy);
                }
                else if (SelectedObject is DirectionalLight)
                {
                    DirectionalLight copy = (DirectionalLight)SelectedObject.Clone;
                    dirLights.Add(copy);
                }
            }
        }
コード例 #11
0
 public void Dispose(MeshMaterialLibrary library)
 {
     library.DeleteFromRegistry(this);
 }
コード例 #12
0
 public void RegisterInLibrary(MeshMaterialLibrary library)
 {
     library.Register(Material, Model, WorldTransform);
 }
コード例 #13
0
        public BasicEntity(Model model, MaterialEffect material, Vector3 position, double angleZ, double angleX, double angleY, Vector3 scale, MeshMaterialLibrary library = null, Entity physicsObject = null)
        {
            Id             = IdGenerator.GetNewId();
            WorldTransform = new TransformMatrix(Matrix.Identity, Id);

            Position = position;
            AngleZ   = angleZ;
            AngleX   = angleX;
            AngleY   = angleY;
            Scale    = scale;

            RotationMatrix = Matrix.CreateRotationX((float)AngleX) * Matrix.CreateRotationY((float)AngleY) *
                             Matrix.CreateRotationZ((float)AngleZ);

            Material = material;
            Model    = model;

            if (library != null)
            {
                RegisterInLibrary(library);
            }

            if (physicsObject != null)
            {
                RegisterPhysics(physicsObject);
            }
        }
コード例 #14
0
        /// <summary>
        /// Only one shadow map needed for a directional light
        /// </summary>
        /// <param name="light"></param>
        /// <param name="shadowResolution"></param>
        /// <param name="meshMaterialLibrary"></param>
        /// <param name="entities"></param>
        private void CreateShadowMapDirectionalLight(GraphicsDevice graphicsDevice, DirectionalLight light, int shadowResolution, MeshMaterialLibrary meshMaterialLibrary, List <BasicEntity> entities)
        {
            //Create a renderTarget if we don't have one yet
            if (light.ShadowMap == null)
            {
                //if (lightSource.ShadowFiltering != DirectionalLightSource.ShadowFilteringTypes.VSM)
                //{
                light.ShadowMap = new RenderTarget2D(graphicsDevice, shadowResolution, shadowResolution, false,
                                                     SurfaceFormat.Single, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents);
                //}
                //else //For a VSM shadowMap we need 2 components
                //{
                //    lightSource.ShadowMap = new RenderTarget2D(_graphicsDevice, shadowResolution, shadowResolution, false,
                //       SurfaceFormat.Vector2, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents);
                //}
            }

            if (light.HasChanged)
            {
                Matrix lightProjection = Matrix.CreateOrthographic(light.ShadowSize, light.ShadowSize,
                                                                   -light.ShadowDepth, light.ShadowDepth);
                Matrix lightView = Matrix.CreateLookAt(light.Position, light.Position + light.Direction, Vector3.Down);

                light.LightView           = lightView;
                light.LightViewProjection = lightView * lightProjection;

                _boundingFrustumShadow = new BoundingFrustum(light.LightViewProjection);

                graphicsDevice.SetRenderTarget(light.ShadowMap);
                graphicsDevice.Clear(ClearOptions.DepthBuffer, Color.White, 1, 0);

                meshMaterialLibrary.FrustumCulling(entities, _boundingFrustumShadow, true, light.Position);

                // Rendering!
                _FarClip.SetValue(light.ShadowDepth);
                _SizeBias.SetValue(GameSettings.ShadowBias * 2048 / light.ShadowResolution);

                meshMaterialLibrary.Draw(MeshMaterialLibrary.RenderType.ShadowLinear,
                                         light.LightViewProjection, light.HasChanged, false, false, 0, light.LightView, renderModule: this);
            }
            else
            {
                _boundingFrustumShadow = new BoundingFrustum(light.LightViewProjection);

                bool hasAnyObjectMoved = meshMaterialLibrary.FrustumCulling(entities: entities, boundingFrustrum: _boundingFrustumShadow, hasCameraChanged: false, cameraPosition: light.Position);

                if (!hasAnyObjectMoved)
                {
                    return;
                }

                meshMaterialLibrary.FrustumCulling(entities: entities, boundingFrustrum: _boundingFrustumShadow, hasCameraChanged: true, cameraPosition: light.Position);

                graphicsDevice.SetRenderTarget(light.ShadowMap);
                graphicsDevice.Clear(ClearOptions.DepthBuffer, Color.White, 1, 0);

                _FarClip.SetValue(light.ShadowDepth);
                _SizeBias.SetValue(GameSettings.ShadowBias * 2048 / light.ShadowResolution);

                meshMaterialLibrary.Draw(MeshMaterialLibrary.RenderType.ShadowLinear,
                                         light.LightViewProjection, false, true, false, 0, light.LightView, renderModule: this);
            }

            //Blur!
            //if (lightSource.ShadowFiltering == DirectionalLightSource.ShadowFilteringTypes.VSM)
            //{
            //    lightSource.ShadowMap = _gaussianBlur.DrawGaussianBlur(lightSource.ShadowMap);
            //}
        }
コード例 #15
0
        public void Draw(GraphicsDevice graphicsDevice, MeshMaterialLibrary meshMaterialLibrary, List <BasicEntity> entities, List <PointLight> pointLights, List <DirectionalLight> dirLights, Camera camera)
        {
            _pass = Passes.Omnidirectional;

            //Go through all our point lights
            for (int index = 0; index < pointLights.Count; index++)
            {
                PointLight light = pointLights[index];

                if (!light.IsEnabled)
                {
                    continue;
                }

                //If we don't see the light we shouldn't update. This is actually wrong, can lead to mistakes,
                //if we implement it like this we should rerender once we enter visible space again.
                //if (_boundingFrustum.Contains(light.BoundingSphere) == ContainmentType.Disjoint)
                //{
                //    continue;
                //}

                if (light.CastShadows)
                {
                    //A poing light has 6 shadow maps, add that to our stat counter. These are total shadow maps, not updated ones
                    GameStats.shadowMaps += 6;

                    //Update if we didn't initialize yet or if we are dynamic
                    if (!light.StaticShadows || light.ShadowMap == null)
                    {
                        CreateShadowCubeMap(graphicsDevice, light, light.ShadowResolution, meshMaterialLibrary, entities);

                        light.HasChanged  = false;
                        camera.HasChanged = true;
                    }
                }
            }

            _pass = Passes.Directional;

            int dirLightShadowedWithSSBlur = 0;

            for (int index = 0; index < dirLights.Count; index++)
            {
                DirectionalLight light = dirLights[index];
                if (!light.IsEnabled)
                {
                    continue;
                }

                if (light.CastShadows)
                {
                    GameStats.shadowMaps += 1;

                    CreateShadowMapDirectionalLight(graphicsDevice, light, light.ShadowResolution, meshMaterialLibrary, entities);

                    camera.HasChanged = true;
                    light.HasChanged  = false;

                    if (light.ScreenSpaceShadowBlur)
                    {
                        dirLightShadowedWithSSBlur++;
                    }
                }

                if (dirLightShadowedWithSSBlur > 1)
                {
                    throw new NotImplementedException(
                              "Only one shadowed DirectionalLight with screen space blur is supported right now");
                }
            }
        }
コード例 #16
0
        /// <summary>
        /// Draw forward shaded, alpha blended materials. Very basic and unoptimized algorithm. Can be improved to use tiling in future.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="output"></param>
        /// <param name="meshMat"></param>
        /// <param name="viewProjection"></param>
        /// <param name="camera"></param>
        /// <param name="pointLights"></param>
        /// <param name="frustum"></param>
        /// <returns></returns>
        public RenderTarget2D Draw(GraphicsDevice graphicsDevice, RenderTarget2D output, MeshMaterialLibrary meshMat, Matrix viewProjection, Camera camera, List <PointLight> pointLights, BoundingFrustum frustum)
        {
            graphicsDevice.DepthStencilState = DepthStencilState.Default;

            SetupLighting(camera, pointLights, frustum);

            //Draw Frustum debug test
            //Matrix view = Matrix.CreateLookAt(new Vector3(-88, -11f, 4), new Vector3(38, 8, 32), Vector3.UnitZ);
            //Matrix projection = Matrix.CreatePerspectiveFieldOfView((float)(Math.PI / 2), 1.6f, 1, 100);
            //BoundingFrustumEx frustum2 = new BoundingFrustumEx(view * projection);
            //LineHelperManager.AddFrustum(frustum2, 1, Color.Red);

            //Vector3[] corners = frustum.GetCorners();
            //BoundingFrustumEx frustum3 = new BoundingFrustumEx(ref corners);

            //TiledLighting(frustum, pointLights, 20, 10);

            meshMat.Draw(MeshMaterialLibrary.RenderType.Forward, viewProjection, renderModule: this);

            return(output);
        }
コード例 #17
0
ファイル: BasicEntity.cs プロジェクト: zengqh/DeferredEngine
        public BasicEntity(ModelDefinition modelbb, MaterialEffect material, Vector3 position, double angleZ, double angleX, double angleY, Vector3 scale, MeshMaterialLibrary library = null, Entity physicsObject = null)
        {
            Id                  = IdGenerator.GetNewId();
            Name                = GetType().Name + " " + Id;
            WorldTransform      = new TransformMatrix(Matrix.Identity, Id);
            ModelDefinition     = modelbb;
            Model               = modelbb.Model;
            BoundingBox         = modelbb.BoundingBox;
            BoundingBoxOffset   = modelbb.BoundingBoxOffset;
            SignedDistanceField = modelbb.SDF;

            Material = material;
            Position = position;
            Scale    = scale;

            RotationMatrix = Matrix.CreateRotationX((float)angleX) * Matrix.CreateRotationY((float)angleY) *
                             Matrix.CreateRotationZ((float)angleZ);

            if (library != null)
            {
                RegisterInLibrary(library);
            }

            if (physicsObject != null)
            {
                RegisterPhysics(physicsObject);
            }

            WorldTransform.World        = Matrix.CreateScale(Scale) * RotationMatrix * Matrix.CreateTranslation(Position);
            WorldTransform.Scale        = Scale;
            WorldTransform.InverseWorld = Matrix.Invert(Matrix.CreateTranslation(BoundingBoxOffset * Scale) * RotationMatrix * Matrix.CreateTranslation(Position));
        }
コード例 #18
0
        /// <summary>
        /// Main Logic for the editor part
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="entities"></param>
        /// <param name="data"></param>
        public void Update(GameTime gameTime, List <BasicEntity> entities, List <PointLightSource> pointLights, List <DirectionalLightSource> dirLights, EditorReceivedData data, MeshMaterialLibrary meshMaterialLibrary)
        {
            if (Input.WasKeyPressed(Keys.R))
            {
                _gizmoMode = GizmoModes.Rotation;
            }
            if (Input.WasKeyPressed(Keys.T))
            {
                _gizmoMode = GizmoModes.Translation;
            }

            int hoveredId = data.HoveredId;

            if (_gizmoTransformationMode)
            {
                if (Input.mouseState.LeftButton == ButtonState.Pressed)
                {
                    GizmoControl(_gizmoId, data);
                }
                else
                {
                    _gizmoTransformationMode = false;
                }
            }
            else if (Input.WasLMBPressed())
            {
                //Gizmos
                if (hoveredId >= 1 && hoveredId <= 3)
                {
                    _gizmoId = hoveredId;
                    GizmoControl(_gizmoId, data);
                    return;
                }

                if (hoveredId <= 0)
                {
                    SelectedObject = null;
                    return;
                }

                bool foundnew = false;
                //Get the selected entity!
                for (int index = 0; index < entities.Count; index++)
                {
                    var VARIABLE = entities[index];
                    if (VARIABLE.Id == hoveredId)
                    {
                        SelectedObject = VARIABLE;
                        foundnew       = true;
                        break;
                    }
                }
                if (foundnew == false)
                {
                    for (int index = 0; index < pointLights.Count; index++)
                    {
                        PointLightSource pointLightSource = pointLights[index];
                        if (pointLightSource.Id == hoveredId)
                        {
                            SelectedObject = pointLightSource;
                            break;
                        }
                    }

                    for (int index = 0; index < dirLights.Count; index++)
                    {
                        DirectionalLightSource directionalLightSource = dirLights[index];
                        if (directionalLightSource.Id == hoveredId)
                        {
                            SelectedObject = directionalLightSource;
                            break;
                        }
                    }
                }
            }

            //Controls

            if (Input.WasKeyPressed(Keys.Delete))
            {
                //Find object
                if (SelectedObject is BasicEntity)
                {
                    entities.Remove((BasicEntity)SelectedObject);
                    meshMaterialLibrary.DeleteFromRegistry((BasicEntity)SelectedObject);

                    SelectedObject = null;
                }

                else if (SelectedObject is PointLightSource)
                {
                    pointLights.Remove((PointLightSource)SelectedObject);

                    SelectedObject = null;
                }
                else if (SelectedObject is DirectionalLightSource)
                {
                    dirLights.Remove((DirectionalLightSource)SelectedObject);

                    SelectedObject = null;
                }
            }

            if (Input.WasKeyPressed(Keys.Insert))
            {
                if (SelectedObject is BasicEntity)
                {
                    BasicEntity copy = (BasicEntity)SelectedObject.Clone;
                    copy.RegisterInLibrary(meshMaterialLibrary);

                    entities.Add(copy);
                }
                else if (SelectedObject is PointLightSource)
                {
                    PointLightSource copy = (PointLightSource)SelectedObject.Clone;
                    pointLights.Add(copy);
                }
                else if (SelectedObject is DirectionalLightSource)
                {
                    DirectionalLightSource copy = (DirectionalLightSource)SelectedObject.Clone;
                    dirLights.Add(copy);
                }
            }

            if (SelectedObject != null)
            {
                DebugScreen.AddString(SelectedObject.Position.ToString());
            }
        }
コード例 #19
0
        public void Draw(GraphicsDevice _graphicsDevice, RenderTargetBinding[] _renderTargetBinding, MeshMaterialLibrary meshMaterialLibrary, Matrix _viewProjection, Matrix _view)
        {
            _graphicsDevice.SetRenderTargets(_renderTargetBinding);

            //Clear the GBuffer
            if (GameSettings.g_ClearGBuffer)
            {
                _graphicsDevice.RasterizerState   = RasterizerState.CullNone;
                _graphicsDevice.BlendState        = BlendState.Opaque;
                _graphicsDevice.DepthStencilState = DepthStencilState.Default;

                _clearGBufferPass.Apply();
                _fullScreenTriangle.Draw(_graphicsDevice);
            }

            //Draw the Gbuffer!

            meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.Opaque, viewProjection: _viewProjection, lightViewPointChanged: true, view: _view, renderModule: this);
        }
コード例 #20
0
ファイル: EditorRender.cs プロジェクト: zengqh/DeferredEngine
 public void DrawIds(MeshMaterialLibrary meshMaterialLibrary, List <Decal> decals, List <PointLight> lights, List <DirectionalLight> dirLights, EnvironmentSample envSample, List <DebugEntity> debug, Matrix staticViewProjection, Matrix view, EditorLogic.EditorSendData editorData)
 {
     _idAndOutlineRenderer.Draw(meshMaterialLibrary, decals, lights, dirLights, envSample, debug, staticViewProjection, view, editorData, _mouseMovement);
 }
コード例 #21
0
        public RenderTarget2D Draw(GraphicsDevice graphicsDevice, RenderTarget2D input, RenderTarget2D output, MeshMaterialLibrary meshMat, Matrix viewProjection)
        {
            graphicsDevice.SetRenderTarget(output);
            graphicsDevice.DepthStencilState = DepthStencilState.Default;

            meshMat.Draw(MeshMaterialLibrary.RenderType.SubsurfaceScattering, viewProjection, renderModule: this);

            return(output);
        }
コード例 #22
0
        /// <summary>
        /// Create the shadow map for each cubemapside, then combine into one cubemap
        /// </summary>
        /// <param name="light"></param>
        /// <param name="size"></param>
        /// <param name="meshMaterialLibrary"></param>
        /// <param name="entities"></param>
        private void CreateShadowCubeMap(GraphicsDevice graphicsDevice, PointLight light, int size, MeshMaterialLibrary meshMaterialLibrary, List <BasicEntity> entities)
        {
            //For VSM we need 2 channels, -> Vector2
            //todo: check if we need preserve contents
            if (light.ShadowMap == null)
            {
                light.ShadowMap = new RenderTarget2D(graphicsDevice, size, size * 6, false, SurfaceFormat.HalfSingle, DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents);
            }

            Matrix      lightViewProjection = new Matrix();
            CubeMapFace cubeMapFace; // = CubeMapFace.NegativeX;

            if (light.HasChanged)
            {
                graphicsDevice.SetRenderTarget(light.ShadowMap);

                Matrix lightProjection = Matrix.CreatePerspectiveFieldOfView((float)(Math.PI / 2), 1, 1, light.Radius);
                Matrix lightView; // = identity

                //Reset the blur array
                light.faceBlurCount = new int[6];

                graphicsDevice.SetRenderTarget(light.ShadowMap);
                graphicsDevice.Clear(Color.Black);

                for (int i = 0; i < 6; i++)
                {
                    // render the scene to all cubemap faces
                    cubeMapFace = (CubeMapFace)i;
                    switch (cubeMapFace)
                    {
                    case CubeMapFace.PositiveX:
                    {
                        lightView           = Matrix.CreateLookAt(light.Position, light.Position + Vector3.UnitX, Vector3.UnitZ);
                        lightViewProjection = lightView * lightProjection;
                        light.LightViewProjectionPositiveX = lightViewProjection;
                        break;
                    }

                    case CubeMapFace.NegativeX:
                    {
                        lightView           = Matrix.CreateLookAt(light.Position, light.Position - Vector3.UnitX, Vector3.UnitZ);
                        lightViewProjection = lightView * lightProjection;
                        light.LightViewProjectionNegativeX = lightViewProjection;
                        break;
                    }

                    case CubeMapFace.PositiveY:
                    {
                        lightView           = Matrix.CreateLookAt(light.Position, light.Position + Vector3.UnitY, Vector3.UnitZ);
                        lightViewProjection = lightView * lightProjection;
                        light.LightViewProjectionPositiveY = lightViewProjection;
                        break;
                    }

                    case CubeMapFace.NegativeY:
                    {
                        lightView           = Matrix.CreateLookAt(light.Position, light.Position - Vector3.UnitY, Vector3.UnitZ);
                        lightViewProjection = lightView * lightProjection;
                        light.LightViewProjectionNegativeY = lightViewProjection;
                        break;
                    }

                    case CubeMapFace.PositiveZ:
                    {
                        lightView           = Matrix.CreateLookAt(light.Position, light.Position + Vector3.UnitZ, Vector3.UnitX);
                        lightViewProjection = lightView * lightProjection;
                        light.LightViewProjectionPositiveZ = lightViewProjection;
                        break;
                    }

                    case CubeMapFace.NegativeZ:
                    {
                        lightView           = Matrix.CreateLookAt(light.Position, light.Position - Vector3.UnitZ, Vector3.UnitX);
                        lightViewProjection = lightView * lightProjection;
                        light.LightViewProjectionNegativeZ = lightViewProjection;
                        break;
                    }
                    }

                    if (_boundingFrustumShadow != null)
                    {
                        _boundingFrustumShadow.Matrix = lightViewProjection;
                    }
                    else
                    {
                        _boundingFrustumShadow = new BoundingFrustum(lightViewProjection);
                    }

                    meshMaterialLibrary.FrustumCulling(entities, _boundingFrustumShadow, true, light.Position);

                    // Rendering!

                    graphicsDevice.Viewport = new Viewport(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution);
                    //_graphicsDevice.ScissorRectangle = new Rectangle(0, light.ShadowResolution* (int) cubeMapFace,  light.ShadowResolution, light.ShadowResolution);

                    _FarClip.SetValue(light.Radius);
                    _LightPositionWS.SetValue(light.Position);

                    graphicsDevice.ScissorRectangle = new Rectangle(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution);

                    meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.ShadowOmnidirectional,
                                             viewProjection: lightViewProjection,
                                             lightViewPointChanged: true,
                                             hasAnyObjectMoved: light.HasChanged,
                                             renderModule: this);
                }
            }
            else
            {
                bool draw = false;

                for (int i = 0; i < 6; i++)
                {
                    // render the scene to all cubemap faces
                    cubeMapFace = (CubeMapFace)i;

                    switch (cubeMapFace)
                    {
                    case CubeMapFace.NegativeX:
                        lightViewProjection = light.LightViewProjectionNegativeX;
                        break;

                    case CubeMapFace.NegativeY:
                        lightViewProjection = light.LightViewProjectionNegativeY;
                        break;

                    case CubeMapFace.NegativeZ:
                        lightViewProjection = light.LightViewProjectionNegativeZ;
                        break;

                    case CubeMapFace.PositiveX:
                        lightViewProjection = light.LightViewProjectionPositiveX;
                        break;

                    case CubeMapFace.PositiveY:
                        lightViewProjection = light.LightViewProjectionPositiveY;
                        break;

                    case CubeMapFace.PositiveZ:
                        lightViewProjection = light.LightViewProjectionPositiveZ;
                        break;
                    }

                    if (_boundingFrustumShadow != null)
                    {
                        _boundingFrustumShadow.Matrix = lightViewProjection;
                    }
                    else
                    {
                        _boundingFrustumShadow = new BoundingFrustum(lightViewProjection);
                    }

                    bool hasAnyObjectMoved = meshMaterialLibrary.FrustumCulling(entities, _boundingFrustumShadow, false, light.Position);

                    if (!hasAnyObjectMoved)
                    {
                        continue;
                    }

                    if (!draw)
                    {
                        graphicsDevice.SetRenderTarget(light.ShadowMap);
                        draw = true;
                    }

                    graphicsDevice.Viewport = new Viewport(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution);

                    //_graphicsDevice.Clear(Color.TransparentBlack);
                    //_graphicsDevice.Clear(ClearOptions.DepthBuffer, Color.White, 0, 0);
                    graphicsDevice.ScissorRectangle = new Rectangle(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution);

                    meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.ShadowOmnidirectional,
                                             viewProjection: lightViewProjection,
                                             lightViewPointChanged: light.HasChanged,
                                             hasAnyObjectMoved: true,
                                             renderModule: this);
                }
            }
        }