Exemple #1
0
        public void RenderShadowmap(Shader effect,
                                    GraphicsDevice graphicsDevice,
                                    ShadowRenderer shadowRenderer,
                                    Matrix worldMatrix,
                                    Texture2D tilemap)
        {
            Vector3[] corners    = new Vector3[8];
            Camera    tempCamera = new Camera(World, camera.Target, camera.Position, camera.FOV, camera.AspectRatio, camera.NearPlane, 30);

            tempCamera.GetFrustrum().GetCorners(corners);
            BoundingBox cameraBox = MathFunctions.GetBoundingBox(corners);

            cameraBox          = cameraBox.Expand(1.0f);
            effect.World       = worldMatrix;
            effect.MainTexture = tilemap;
            shadowRenderer.SetupViewProj(cameraBox);
            shadowRenderer.PrepareEffect(effect, false);
            shadowRenderer.BindShadowmapEffect(effect);
            shadowRenderer.BindShadowmap(graphicsDevice);

            List <VoxelChunk> renderListCopy = RenderList.ToArray().ToList();

            foreach (VoxelChunk chunk in renderListCopy)
            {
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    chunk.Render(Graphics);
                }
            }
            shadowRenderer.UnbindShadowmap(graphicsDevice);
            effect.CurrentTechnique        = effect.Techniques[Shader.Technique.Textured];
            effect.SelfIlluminationEnabled = false;
        }
Exemple #2
0
        public void CreateDepths(Camera inputCamera)
        {
            if (inputCamera == null)
            {
                return;
            }

            BoundingFrustum frust   = inputCamera.GetFrustrum();
            BoundingBox     box     = MathFunctions.GetBoundingBox(frust.GetCorners());
            Vector3         forward = frust.Near.Normal;

            Vector3 z = Vector3.Zero;

            foreach (InstanceData instance in Data)
            {
                z = instance.Transform.Translation - inputCamera.Position;
                instance.Depth = z.LengthSquared();

                if (instance.Depth < CullDistance)
                {
                    // Half plane test. Faster. Much less accurate.
                    //if (Vector3.Dot(z, forward) > 0)
                    if (box.Contains(instance.Transform.Translation) != ContainmentType.Contains)
                    {
                        instance.Depth *= 100;
                    }
                }
                else
                {
                    instance.Depth *= 100;
                }
            }
        }
Exemple #3
0
        public void RenderAll(Camera renderCamera, DwarfTime gameTime, GraphicsDevice graphicsDevice, Effect effect, Matrix worldMatrix, Texture2D tilemap)
        {
            effect.Parameters["xIllumination"].SetValue(ChunkData.IllumMap);
            effect.Parameters["xTexture"].SetValue(tilemap);
            effect.Parameters["xSunGradient"].SetValue(ChunkData.SunMap);
            effect.Parameters["xAmbientGradient"].SetValue(ChunkData.AmbientMap);
            effect.Parameters["xTorchGradient"].SetValue(ChunkData.TorchMap);
            effect.Parameters["xTint"].SetValue(new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
            effect.Parameters["SelfIllumination"].SetValue(1);
            effect.Parameters["xEnableShadows"].SetValue(0);

			BoundingFrustum cameraFrustrum = renderCamera.GetFrustrum();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                foreach (KeyValuePair<Point3, VoxelChunk> chunk in ChunkData.ChunkMap)
                {
                    if (cameraFrustrum.Intersects(chunk.Value.GetBoundingBox()))
                    {
                        chunk.Value.Render(Graphics);
                    }
                }
            }
            effect.Parameters["SelfIllumination"].SetValue(0);
        }
Exemple #4
0
        public void RenderAll(Camera renderCamera, DwarfTime gameTime, GraphicsDevice graphicsDevice, Shader effect, Matrix worldMatrix, Texture2D tilemap)
        {
            effect.SelfIlluminationTexture  = ChunkData.IllumMap;
            effect.MainTexture              = tilemap;
            effect.SunlightGradient         = ChunkData.SunMap;
            effect.AmbientOcclusionGradient = ChunkData.AmbientMap;
            effect.TorchlightGradient       = ChunkData.TorchMap;
            effect.LightRampTint            = Color.White;
            effect.VertexColorTint          = Color.White;
            effect.SelfIlluminationEnabled  = true;
            effect.EnableShadows            = false;

            BoundingFrustum cameraFrustrum = renderCamera.GetFrustrum();

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                foreach (KeyValuePair <Point3, VoxelChunk> chunk in ChunkData.ChunkMap)
                {
                    if (cameraFrustrum.Intersects(chunk.Value.GetBoundingBox()))
                    {
                        chunk.Value.Render(Graphics);
                    }
                }
            }
            effect.SelfIlluminationEnabled = false;
        }
Exemple #5
0
        public void RenderInstances(
            GraphicsDevice Device,
            Shader Effect,
            Camera Camera,
            InstanceRenderer.RenderMode Mode)
        {
            int uniqueInstances = 0;

            RenderPass += 1;
            var frustrum = Camera.GetFrustrum();

            foreach (var item in OctTree.EnumerateItems(frustrum))
            {
                if (!item.Visible)
                {
                    continue;
                }

                if (item.RenderPass < RenderPass)
                {
                    uniqueInstances += 1;
                    Renderer.RenderInstance(item, Device, Effect, Camera, Mode);
                }
                item.RenderPass = RenderPass;
            }

            Renderer.Flush(Device, Effect, Camera, Mode);
            GamePerformance.Instance.TrackValueType("Instances Drawn", uniqueInstances);
        }
Exemple #6
0
 public List <Body> SelectRootBodiesOnScreen(Rectangle selectionRectangle, Camera camera)
 {
     return((from component in RootComponent.Children.OfType <Body>()
             let screenPos = camera.Project(component.GlobalTransform.Translation)
                             where   screenPos.Z > 0 &&
                             (selectionRectangle.Contains((int)screenPos.X, (int)screenPos.Y) || selectionRectangle.Intersects(component.GetScreenRect(camera))) &&
                             camera.GetFrustrum().Contains(component.GlobalTransform.Translation) != ContainmentType.Disjoint &&
                             !PlayState.ChunkManager.ChunkData.CheckOcclusionRay(camera.Position, component.Position)
                             select component).ToList());
 }
Exemple #7
0
        public void Update(DwarfTime gameTime, Camera camera, GraphicsDevice g)
        {
            UpdateRenderList(camera);

            visibilityChunksTimer.Update(gameTime);
            if (visibilityChunksTimer.HasTriggered)
            {
                visibleSet.Clear();
                GetChunksIntersecting(camera.GetFrustrum(), visibleSet);
            }
        }
Exemple #8
0
        public static IEnumerable <IRenderableComponent> EnumerateVisibleRenderables(
            IEnumerable <IRenderableComponent> Renderables,
            ChunkManager chunks,
            Camera Camera)
        {
            var frustrum = Camera.GetFrustrum();

            var visibleComponents = Renderables.Where(r =>
            {
                if (!r.IsVisible)
                {
                    return(false);
                }
                if (r.IsAboveCullPlane(chunks))
                {
                    return(false);
                }
                if (r.FrustrumCull)
                {
                    if ((r.GlobalTransform.Translation - Camera.Position).LengthSquared() >=
                        chunks.DrawDistanceSquared)
                    {
                        return(false);
                    }
                    if (!r.GetBoundingBox().Intersects(frustrum))
                    {
                        return(false);
                    }
                }

                return(true);
            }).ToList();

            // Need to identify transparent entities and draw them last. Alpha masked entities do not
            //   count - they don't blend!
            //visibleComponents.Sort((A, B) =>
            //{
            //    if (A == B) return 0;
            //    return
            //    -(Camera.Position - A.GlobalTransform.Translation).LengthSquared()
            //        .CompareTo((Camera.Position - B.GlobalTransform.Translation).LengthSquared());
            //});

            return(visibleComponents);
        }
Exemple #9
0
        public PolygonDrawCommand(Camera camera, IEnumerable <Vector3> points, bool isClosed, Color lineColor, int lineWidth, Viewport viewport)
        {
            Points = new List <Vector2>();

            BoundingFrustum cameraFrustrum = camera.GetFrustrum();

            foreach (Vector3 point in points)
            {
                if (cameraFrustrum.Contains(point) == ContainmentType.Contains)
                {
                    Vector3 screenVec = viewport.Project(point, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);
                    Points.Add(new Vector2(screenVec.X, screenVec.Y));
                }
            }

            IsClosed  = isClosed;
            LineColor = lineColor;
            LineWidth = lineWidth;
        }
        public static IEnumerable <IRenderableComponent> EnumerateVisibleRenderables(
            IEnumerable <IRenderableComponent> Renderables,
            ChunkManager chunks,
            Camera Camera)
        {
            var frustrum = Camera.GetFrustrum();

            // Todo: Use an octtree for this.
            var visibleComponents = Renderables.Where(r =>
            {
                if (!r.IsVisible)
                {
                    return(false);
                }
                if (chunks.IsAboveCullPlane(r.GetBoundingBox()))
                {
                    return(false);
                }
                if (r.FrustumCull)
                {
                    if ((r.GlobalTransform.Translation - Camera.Position).LengthSquared() >=
                        chunks.DrawDistanceSquared)
                    {
                        return(false);
                    }
                    if (!r.GetBoundingBox().Intersects(frustrum))
                    {
                        return(false);
                    }
                }

                return(true);
            }).ToList();

            return(visibleComponents);
        }
Exemple #11
0
        public List <Body> FrustrumCullLocatableComponents(Camera camera)
        {
            List <Body> visible = CollisionManager.GetVisibleObjects <Body>(camera.GetFrustrum(), CollisionManager.CollisionType.Dynamic | CollisionManager.CollisionType.Static);

            return(visible);
        }
Exemple #12
0
        public void Update(DwarfTime gameTime, Camera camera, GraphicsDevice g)
        {
            UpdateRenderList(camera);

            if (waterUpdateTimer.Update(gameTime))
            {
                WaterUpdateEvent.Set();
            }

            UpdateRebuildList();

            generateChunksTimer.Update(gameTime);
            if (generateChunksTimer.HasTriggered)
            {
                if (ToGenerate.Count > 0)
                {
                    NeedsGenerationEvent.Set();
                    ChunkData.RecomputeNeighbors();
                }

                foreach (VoxelChunk chunk in GeneratedChunks)
                {
                    if (!ChunkData.ChunkMap.ContainsKey(chunk.ID))
                    {
                        ChunkData.AddChunk(chunk);
                        ChunkGen.GenerateVegetation(chunk, Components, Content, Graphics);
                        ChunkGen.GenerateFauna(chunk, Components, Content, Graphics, PlayState.ComponentManager.Factions);
                        List <VoxelChunk> adjacents = ChunkData.GetAdjacentChunks(chunk);
                        foreach (VoxelChunk c in adjacents)
                        {
                            c.ShouldRecalculateLighting = true;
                            c.ShouldRebuild             = true;
                        }
                        RecalculateBounds();
                    }
                }

                while (GeneratedChunks.Count > 0)
                {
                    VoxelChunk gen = null;
                    if (!GeneratedChunks.TryDequeue(out gen))
                    {
                        break;
                    }
                }
            }


            visibilityChunksTimer.Update(gameTime);
            if (visibilityChunksTimer.HasTriggered)
            {
                visibleSet.Clear();
                GetChunksIntersecting(camera.GetFrustrum(), visibleSet);
                //RemoveDistantBlocks(camera);
            }


            foreach (VoxelChunk chunk in ChunkData.ChunkMap.Values)
            {
                chunk.Update(gameTime);
            }

            Water.Splash(gameTime);
            Water.HandleTransfers(gameTime);

            HashSet <VoxelChunk> affectedChunks = new HashSet <VoxelChunk>();

            foreach (Voxel voxel in KilledVoxels)
            {
                affectedChunks.Add(voxel.Chunk);
                voxel.Chunk.NotifyDestroyed(new Point3(voxel.GridPosition));
                if (!voxel.IsInterior)
                {
                    foreach (KeyValuePair <Point3, VoxelChunk> n in voxel.Chunk.Neighbors)
                    {
                        affectedChunks.Add(n.Value);
                    }
                }
            }

            if (GameSettings.Default.FogofWar)
            {
                ChunkData.Reveal(KilledVoxels);
            }

            lock (RebuildList)
            {
                foreach (VoxelChunk affected in affectedChunks)
                {
                    affected.NotifyTotalRebuild(false);
                }
            }
            KilledVoxels.Clear();
        }
Exemple #13
0
        public void Update(DwarfTime gameTime, Camera camera, GraphicsDevice g)
        {
            UpdateRenderList(camera);

            if (waterUpdateTimer.Update(gameTime))
            {
                WaterUpdateEvent.Set();
            }

            UpdateRebuildList();

            generateChunksTimer.Update(gameTime);
            if(generateChunksTimer.HasTriggered)
            {
                if(ToGenerate.Count > 0)
                {
                    NeedsGenerationEvent.Set();
                    ChunkData.RecomputeNeighbors();
                }

                foreach(VoxelChunk chunk in GeneratedChunks)
                {
                    if(!ChunkData.ChunkMap.ContainsKey(chunk.ID))
                    {
                        ChunkData.AddChunk(chunk);
                        ChunkGen.GenerateVegetation(chunk, Components, Content, Graphics);
                        ChunkGen.GenerateFauna(chunk, Components, Content, Graphics, PlayState.ComponentManager.Factions);
                        List<VoxelChunk> adjacents = ChunkData.GetAdjacentChunks(chunk);
                        foreach(VoxelChunk c in adjacents)
                        {
                            c.ShouldRecalculateLighting = true;
                            c.ShouldRebuild = true;
                        }
                        RecalculateBounds();
                    }
                }

                while(GeneratedChunks.Count > 0)
                {
                    VoxelChunk gen = null;
                    if(!GeneratedChunks.TryDequeue(out gen))
                    {
                        break;
                    }
                }
            }

            visibilityChunksTimer.Update(gameTime);
            if(visibilityChunksTimer.HasTriggered)
            {
                visibleSet.Clear();
                GetChunksIntersecting(camera.GetFrustrum(), visibleSet);
                //RemoveDistantBlocks(camera);
            }

            foreach(VoxelChunk chunk in ChunkData.ChunkMap.Values)
            {
                chunk.Update(gameTime);
            }

            Water.Splash(gameTime);
            Water.HandleTransfers(gameTime);

            HashSet<VoxelChunk> affectedChunks = new HashSet<VoxelChunk>();
            foreach (Voxel voxel in KilledVoxels)
            {
                affectedChunks.Add(voxel.Chunk);
                voxel.Chunk.NotifyDestroyed(new Point3(voxel.GridPosition));
                if (!voxel.IsInterior)
                {
                    foreach (KeyValuePair<Point3, VoxelChunk> n in voxel.Chunk.Neighbors)
                    {
                        affectedChunks.Add(n.Value);
                    }
                }
            }

            lock (RebuildList)
            {
                foreach (VoxelChunk affected in affectedChunks)
                {
                    affected.NotifyTotalRebuild(false);
                }
            }
            KilledVoxels.Clear();
        }
Exemple #14
0
        public void RenderAll(Camera renderCamera, DwarfTime gameTime, GraphicsDevice graphicsDevice, Effect effect, Matrix worldMatrix, Texture2D tilemap)
        {
            effect.Parameters["xIllumination"].SetValue(ChunkData.IllumMap);
            effect.Parameters["xTexture"].SetValue(tilemap);
            effect.Parameters["xSunGradient"].SetValue(ChunkData.SunMap);
            effect.Parameters["xAmbientGradient"].SetValue(ChunkData.AmbientMap);
            effect.Parameters["xTorchGradient"].SetValue(ChunkData.TorchMap);
            effect.Parameters["xTint"].SetValue(new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
            effect.Parameters["SelfIllumination"].SetValue(1);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                foreach (KeyValuePair<Point3, VoxelChunk> chunk in ChunkData.ChunkMap)
                {
                    if (renderCamera.GetFrustrum().Intersects(chunk.Value.GetBoundingBox()))
                    {
                        chunk.Value.Render(tilemap, ChunkData.IllumMap, ChunkData.SunMap, ChunkData.AmbientMap,
                            ChunkData.TorchMap, graphicsDevice, effect, worldMatrix);
                    }
                }
            }
            effect.Parameters["SelfIllumination"].SetValue(0);
        }
        public void CreateDepths(Camera inputCamera)
        {
            if (inputCamera == null)
            {
                return;
            }

            BoundingFrustum frust = inputCamera.GetFrustrum();
            BoundingBox box = MathFunctions.GetBoundingBox(frust.GetCorners());
            Vector3 forward = frust.Near.Normal;

            Vector3 z = Vector3.Zero;
            foreach (InstanceData instance in Data)
            {
                z = instance.Transform.Translation - inputCamera.Position;
                instance.Depth = z.LengthSquared();

                if (instance.Depth < CullDistance)
                {

                    // Half plane test. Faster. Much less accurate.
                    //if (Vector3.Dot(z, forward) > 0)
                    if(box.Contains(instance.Transform.Translation) != ContainmentType.Contains)
                    {
                        instance.Depth *= 100;
                    }
                }
                else
                {
                    instance.Depth *= 100;
                }
            }
        }
        public void Render(DwarfTime gameTime,
                           ChunkManager chunks,
                           Camera camera,
                           SpriteBatch spriteBatch,
                           GraphicsDevice graphicsDevice,
                           Shader effect,
                           WaterRenderType waterRenderMode, float waterLevel)
        {
            bool renderForWater = (waterRenderMode != WaterRenderType.None);

            if (!renderForWater)
            {
                visibleComponents.Clear();

                BoundingFrustum frustrum = camera.GetFrustrum();

                foreach (IRenderableComponent b in RenderableComponents)
                {
                    if (!b.IsVisible)
                    {
                        continue;
                    }
                    if (b.IsAboveCullPlane)
                    {
                        continue;
                    }

                    if (b.FrustrumCull)
                    {
                        if ((b.GlobalTransform.Translation - camera.Position).LengthSquared() >= chunks.DrawDistanceSquared)
                        {
                            continue;
                        }
                        if (!(b.GetBoundingBox().Intersects(frustrum)))
                        {
                            continue;
                        }
                    }

                    System.Diagnostics.Debug.Assert(!visibleComponents.Contains(b));
                    visibleComponents.Add(b);
                }

                Camera = camera;
            }

            effect.EnableLighting          = GameSettings.Default.CursorLightEnabled;
            graphicsDevice.RasterizerState = RasterizerState.CullNone;

            visibleComponents.Sort(CompareZDepth);
            foreach (IRenderableComponent bodyToDraw in visibleComponents)
            {
                if (waterRenderMode == WaterRenderType.Reflective &&
                    !(bodyToDraw.GetBoundingBox().Min.Y > waterLevel - 2))
                {
                    continue;
                }
                bodyToDraw.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderForWater);
            }
            effect.EnableLighting = false;
        }