internal void Clear()
        {
            Bitmask = 0;

            if (Frustum != null)
            {
                DeallocateFrustum(Frustum);
                Frustum = null;
            }

            List.Clear();
            IsInsideList.SetSize(0);

            List2.Clear();
            IsInsideList2.SetSize(0);

            SmallObjects = null;
            Type = MyFrustumEnum.Unassigned;
            Index = 0;
            Ignored = null;
        }
Example #2
0
        public void UpdateScreenSize()
        {
            Viewport = MySandboxGame.ScreenViewport;

            PreviousPosition = Vector3D.Zero;
            BoundingFrustum = new BoundingFrustumD(MatrixD.Identity);

            ForwardAspectRatio = (float)Viewport.Width / (float)Viewport.Height;
        }
 void DebugDrawFrustum(MatrixD camera, Color color)
 {
     BoundingFrustumD frustum = new BoundingFrustumD(camera);
     MyDebugDraw.DrawBoundingFrustum(frustum, color);
 }
Example #4
0
        internal static void PrepareEntitiesForLargeDraw(ref BoundingFrustumD frustum, Vector3D cameraPosition, MyOcclusionQueryID queryID, List<MyElement> renderObjectListForDraw, ref int occludedItemsStats)
        {
            GetRenderProfiler().StartProfilingBlock("PrepareEntitiesForDrawFr()");

            if (queryID != MyOcclusionQueryID.MAIN_RENDER)
            {             
                return;
            }

            renderObjectListForDraw.Clear();

            GetRenderProfiler().StartProfilingBlock("m_prunningStructure.OverlapAllFrustum");
            m_farObjectsPrunningStructure.OverlapAllFrustum(ref frustum, renderObjectListForDraw);

            //AssertRenderObjects(renderObjectListForDraw);
            GetRenderProfiler().EndProfilingBlock();
          
            GetRenderProfiler().StartProfilingBlock("Get from cullobjects - part 2");

            int c = 0;

            if (queryID == MyOcclusionQueryID.MAIN_RENDER)
            {
                //int ii = 0;
                while (c < renderObjectListForDraw.Count)
                {
                    MyRenderLight renderLight = renderObjectListForDraw[c] as MyRenderLight;
                    if (renderLight != null && ((renderLight.LightOn || renderLight.GlareOn)))
                    {
                        continue;
                    }

                    MyRenderObject ro = renderObjectListForDraw[c] as MyRenderObject;

                    if (ro.NearFlag)
                    {
                        renderObjectListForDraw.RemoveAtFast(c);
                        continue;
                    }


                    if (!ro.SkipIfTooSmall)
                    {
                        c++;
                        continue;
                    }

                    Vector3D entityPosition = ro.WorldVolume.Center;

                    Vector3D.Distance(ref cameraPosition, ref entityPosition, out ro.Distance);

                    float cullRatio = MyRenderConstants.DISTANCE_CULL_RATIO;

                    if (ro.WorldVolume.Radius < ro.Distance / cullRatio)
                    {
                        renderObjectListForDraw.RemoveAtFast(c);
                        continue;
                    }
                    c++;
                }
            }

            GetRenderProfiler().EndProfilingBlock();

            GetRenderProfiler().EndProfilingBlock();
        }
Example #5
0
        internal static void PrepareEntitiesForDraw(ref BoundingFrustumD frustum, Vector3D cameraPosition, MyOcclusionQueryID queryID, List<MyElement> renderObjectListForDraw, List<MyRenderLight> renderLightsForDraw, List<MyElement> cullObjectListForDraw, List<MyElement> manualCullObjectListForDraw, List<MyOcclusionQueryIssue> renderOcclusionQueries, ref int occludedItemsStats)
        {
            GetRenderProfiler().StartProfilingBlock("PrepareEntitiesForDrawFr()");

            if (queryID != MyOcclusionQueryID.MAIN_RENDER)
            {
                m_shadowPrunningStructure.OverlapAllFrustum(ref frustum, renderObjectListForDraw);

                GetRenderProfiler().StartProfilingBlock("m_manualCullingStructure.OverlapAllFrustum");
                m_manualCullingStructure.OverlapAllFrustum(ref frustum, manualCullObjectListForDraw);
                GetRenderProfiler().EndProfilingBlock();

                GetRenderProfiler().StartProfilingBlock("Get from manual cullobjects");
                foreach (MyCullableRenderObject cullableObject in manualCullObjectListForDraw)
                {
                    cullableObject.CulledObjects.GetAll(renderObjectListForDraw, false);
                }
                GetRenderProfiler().EndProfilingBlock();

                foreach (var nearObject in m_nearObjects)
                {
                    renderObjectListForDraw.Add(nearObject);
                }

                GetRenderProfiler().EndProfilingBlock();
                return;
            }

            GetRenderProfiler().StartProfilingBlock("m_cullingStructure.OverlapAllFrustum");
            m_cullingStructure.OverlapAllFrustum(ref frustum, cullObjectListForDraw);
            GetRenderProfiler().EndProfilingBlock();

            GetRenderProfiler().StartProfilingBlock("m_manualCullingStructure.OverlapAllFrustum");
            m_manualCullingStructure.OverlapAllFrustum(ref frustum, manualCullObjectListForDraw);
            GetRenderProfiler().EndProfilingBlock();

            
            if (renderOcclusionQueries != null)
            {
                //Process only big cull object for queries
                renderOcclusionQueries.Clear();

                GetRenderProfiler().StartProfilingBlock("PrepareObjectQueries");
                PrepareObjectQueries(queryID, cullObjectListForDraw, renderOcclusionQueries, ref occludedItemsStats);
                GetRenderProfiler().EndProfilingBlock();

                GetRenderProfiler().StartProfilingBlock("PrepareObjectQueries 2");
                PrepareObjectQueries(queryID, manualCullObjectListForDraw, renderOcclusionQueries, ref occludedItemsStats);
                GetRenderProfiler().EndProfilingBlock();
            }

            renderObjectListForDraw.Clear();
            renderLightsForDraw.Clear();

            GetRenderProfiler().StartProfilingBlock("m_prunningStructure.OverlapAllFrustum");
            m_prunningStructure.OverlapAllFrustum(ref frustum, renderObjectListForDraw);

            //AssertRenderObjects(renderObjectListForDraw);
            GetRenderProfiler().EndProfilingBlock();


            GetRenderProfiler().StartProfilingBlock("Get from cullobjects - part 1");

            foreach (MyCullableRenderObject cullableObject in cullObjectListForDraw)
            {
                if (frustum.Contains(cullableObject.WorldAABB) == VRageMath.ContainmentType.Contains)
                {
                    cullableObject.CulledObjects.GetAll(renderObjectListForDraw, false);
                }
                else
                {
                    cullableObject.CulledObjects.OverlapAllFrustum(ref frustum, renderObjectListForDraw, false);
                }
            }

            GetRenderProfiler().EndProfilingBlock();

            GetRenderProfiler().StartProfilingBlock("Get from manual cullobjects");

            foreach (MyCullableRenderObject cullableObject in manualCullObjectListForDraw)
            {
                cullableObject.CulledObjects.GetAll(renderObjectListForDraw, false);
                MyRenderProxy.VisibleObjectsWrite.Add(cullableObject.ID);
            }

            GetRenderProfiler().EndProfilingBlock();

            GetRenderProfiler().StartProfilingBlock("Get from cullobjects - part 2");

            int c = 0;

            if (queryID == MyOcclusionQueryID.MAIN_RENDER)
            {
                //int ii = 0;
                while (c < renderObjectListForDraw.Count)
                {
                    MyRenderLight renderLight = renderObjectListForDraw[c] as MyRenderLight;
                    if (renderLight != null && ((renderLight.LightOn || renderLight.GlareOn)))
                    {
                        renderLightsForDraw.Add(renderLight);
                        renderObjectListForDraw.RemoveAtFast(c);
                        continue;
                    }

                    MyRenderObject ro = renderObjectListForDraw[c] as MyRenderObject;

                    if (ro.NearFlag)
                    {
                        renderObjectListForDraw.RemoveAtFast(c);
                        continue;
                    }


                    if (!ro.SkipIfTooSmall)
                    {
                        c++;
                        continue;
                    }

            
                    Vector3D entityPosition = ro.WorldVolume.Center;

                    Vector3D.Distance(ref cameraPosition, ref entityPosition, out ro.Distance);
                    
                    float cullRatio = MyRenderConstants.DISTANCE_CULL_RATIO;

                    if (ro.WorldVolume.Radius < ro.Distance / cullRatio)
                    {
                        renderObjectListForDraw.RemoveAtFast(c);
                        continue;
                    }
                    

                    c++;
                }


                MyLights.UpdateLightsForEffect(m_renderLightsForDraw);
            }

            GetRenderProfiler().EndProfilingBlock();

            GetRenderProfiler().EndProfilingBlock();
        }
        internal void Render()
        {
            var foliageComponents = MyFoliageComponents.ActiveComponents;
            if (foliageComponents.Count <= 0)
                return;

            ViewProjection = MyRender11.Environment.ViewProjectionAt0;
            Viewport = new MyViewport(MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);

            PerFrame();
            Begin();

            var viewFrustum = new BoundingFrustumD(MyRender11.Environment.ViewProjectionD);

            foreach (var foliageComponent in foliageComponents)
            {
                var renderableComponent = foliageComponent.Owner.GetRenderable();
                bool removeDitheringInProgress = renderableComponent.m_objectDithering > 0 && renderableComponent.m_objectDithering < 2;
                if (!removeDitheringInProgress && foliageComponent.Owner.CalculateCameraDistance() < MyRender11.RenderSettings.FoliageDetails.GrassDrawDistance())
                {
                    if (viewFrustum.Contains(foliageComponent.Owner.Aabb) != ContainmentType.Disjoint)
                        foliageComponent.Render(this);
                }
            }

            End();
        }
        internal static void Render()
        {
            m_instance.ViewProjection = MyEnvironment.ViewProjectionAt0;
            m_instance.Viewport = new MyViewport(MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
            //m_instance.DepthBuffer = MyRender.MainGbuffer.DepthBuffer.DepthStencil;
            //m_instance.RTs = MyRender.MainGbuffer.GbufferTargets;

            m_instance.PerFrame();
            m_instance.Begin();

            var viewFrustum = new BoundingFrustumD(MyEnvironment.ViewProjectionD);
			var foliageComponents = MyComponentFactory<MyFoliageComponent>.GetAll();
            foreach(var foliageComponent in foliageComponents)
            {
				if (foliageComponent.m_owner.CalculateCameraDistance() < MyRender11.RenderSettings.FoliageDetails.GrassDrawDistance()
					&& viewFrustum.Contains(foliageComponent.m_owner.Aabb) != VRageMath.ContainmentType.Disjoint)
                {
					foliageComponent.Render(m_instance);
                }
            }

            m_instance.End();
        }
Example #8
0
        public static void DrawBoundingFrustum(BoundingFrustumD boundingFrustum, Color color)
        {
            boundingFrustum.GetCorners(m_frustumCorners);
            m_lineBatch.Begin();

            // near face            
            m_lineBatch.DrawLine(m_frustumCorners[0], m_frustumCorners[1], color);
            m_lineBatch.DrawLine(m_frustumCorners[1], m_frustumCorners[2], color);
            m_lineBatch.DrawLine(m_frustumCorners[2], m_frustumCorners[3], color);
            m_lineBatch.DrawLine(m_frustumCorners[3], m_frustumCorners[0], color);

            // far face            
            m_lineBatch.DrawLine(m_frustumCorners[4], m_frustumCorners[5], color);
            m_lineBatch.DrawLine(m_frustumCorners[5], m_frustumCorners[6], color);
            m_lineBatch.DrawLine(m_frustumCorners[6], m_frustumCorners[7], color);
            m_lineBatch.DrawLine(m_frustumCorners[7], m_frustumCorners[4], color);

            // top,right,bottom,left face            
            m_lineBatch.DrawLine(m_frustumCorners[0], m_frustumCorners[4], color);
            m_lineBatch.DrawLine(m_frustumCorners[1], m_frustumCorners[5], color);
            m_lineBatch.DrawLine(m_frustumCorners[2], m_frustumCorners[6], color);
            m_lineBatch.DrawLine(m_frustumCorners[3], m_frustumCorners[7], color);

            m_lineBatch.End();
        }
 internal void DeallocateFrustum(BoundingFrustumD frustum)
 {
     m_frustumPool.Deallocate(frustum);
 }
 /// <summary>
 /// Base constructor
 /// </summary>
 public MyBaseShadowCamera()
 {
     m_boundingFrustum = new BoundingFrustumD(m_viewProjMatrix);
     m_worldMatrix = MatrixD.Identity;
     m_viewMatrix = MatrixD.Identity;
 }
Example #11
0
        public void UpdateScreenSize(MyViewport currentScreenViewport)
        {
            Viewport = currentScreenViewport; // MySandboxGame.ScreenViewport; // no dependency on sandbox!

            PreviousPosition = Vector3D.Zero;
            BoundingFrustum = new BoundingFrustumD(MatrixD.Identity);

            ForwardAspectRatio = (float)Viewport.Width / (float)Viewport.Height;
        }
Example #12
0
        public void CalculateLines()
        {
            DsWatch.Start("CLines");
            try
            {
                if (closestPlanet != null)
                {
                    var planetAtmosphereAltitude = closestPlanet.AtmosphereAltitude;

                    var cameraUp      = new Vector3D(Camera.Position - planetCentre);
                    var cameraForward = Vector3D.CalculatePerpendicularVector(cameraUp);
                    frustumMatrix = MatrixD.CreateWorld(Camera.Position, cameraForward, cameraUp);
                    var offset = Vector3.Zero;

                    var frustum = new BoundingFrustumD(Camera.ViewMatrix * customProjectionMatrix);
                    frustumBBox = BoundingBoxD.CreateInvalid();
                    frustumBBox.Include(ref frustum);

                    if (cameraAltitude < (planetAtmosphereAltitude / 2))
                    {
                        var lineAmount = 100;
                        for (int i = 0; i < lineAmount; i++) // Line calculation LOOP
                        {
                            lineThickness = MyUtils.GetRandomFloat(0.01f, 0.05f);

                            offset.Y = (float)frustumBBox.Extents.Y;
                            offset.X = MyUtils.GetRandomInt(-60, 60);
                            offset.Z = MyUtils.GetRandomInt(-60, 60);

                            if (offset.X >= 0 && offset.X < 1)
                            {
                                offset.X = offset.X + 1;
                            }

                            if (offset.Z >= 0 && offset.Z < 1)
                            {
                                offset.Z = offset.Z + 1;
                            }

                            Vector3D lineStartPoint = Vector3D.Transform(offset, frustumMatrix);
                            Vector3D lineEndPoint   = planetCentre;

                            var   length    = frustumBBox.HalfExtents.Y * 0.25; // Shorten line length by 1/4
                            LineD lineCheck = new LineD(lineStartPoint, lineEndPoint, length);

                            Vector3D finalHitPos    = lineEndPoint;
                            Vector3D hitPos         = lineEndPoint;
                            double?  hitDist        = double.MaxValue;
                            double   finalHitDistSq = double.MaxValue;
                            var      checkVoxel     = true;
                            var      isVoxel        = false;

                            lineIntersectedGrids.Clear();
                            lineIntersectedVoxels.Clear();

                            if (frustumBBox.Intersects(ref lineCheck))
                            {
                                for (int j = 0; j < rainImpactEntities.Count; j++) // Line calculation LOOP
                                {
                                    var rainedOnEnt = rainImpactEntities[j];
                                    var grid        = rainedOnEnt as IMyCubeGrid;
                                    if (grid != null && grid.Physics != null)
                                    {
                                        lineIntersectedGrids.Add(rainedOnEnt);
                                    }
                                    else if (rainedOnEnt is MyVoxelBase)
                                    {
                                        lineIntersectedVoxels.Add(rainedOnEnt);
                                    }
                                }

                                for (int k = 0; k < lineIntersectedGrids.Count; k++)
                                {
                                    var intersectedGrid = lineIntersectedGrids[k];
                                    var cubeGrid        = intersectedGrid as IMyCubeGrid;
                                    if (cubeGrid != null && cubeGrid.Physics != null)
                                    {
                                        MyOrientedBoundingBoxD gridOBB = new MyOrientedBoundingBoxD(cubeGrid.LocalAABB, cubeGrid.WorldMatrix);
                                        //DrawOBB(gridOBB, whiteColor, MySimpleObjectRasterizer.Wireframe, 0.01f);

                                        // If we don't intersect a grid continue.
                                        if (!gridOBB.Intersects(ref lineCheck).HasValue)
                                        {
                                            continue;
                                        }

                                        hitDist = GridHitCheck(cubeGrid, lineCheck, lineStartPoint, lineEndPoint);

                                        if (hitDist != null)
                                        {
                                            hitPos = lineStartPoint + (lineCheck.Direction * hitDist.Value);
                                            if (finalHitDistSq > hitDist.Value)
                                            {
                                                finalHitPos    = hitPos;
                                                finalHitDistSq = hitDist.Value;
                                                checkVoxel     = false;
                                            }
                                        }

                                        //LogGridBlockHits(finalHitDistSq, finalHitPos, cubeGrid, blk, lineColor);
                                    }
                                }

                                /*
                                 * if (checkVoxel)
                                 * {
                                 *  for (int l = 0; l < lineIntersectedVoxels.Count; l++)
                                 *  {
                                 *      var intersectedVoxel = lineIntersectedVoxels[l];
                                 *      var voxelHitName = intersectedVoxel as MyVoxelBase;
                                 *      if (voxelHitName != null)
                                 *      {
                                 *          var voxelCheck = VoxelHitCheck(voxelHitName, closestPlanet, lineStartPoint, lineEndPoint, lineCheck);
                                 *          if (voxelCheck != Vector3D.Zero && voxelCheck != null)
                                 *          {
                                 *              finalHitPos = voxelCheck;
                                 *              hitDist = Vector3D.Distance(lineStartPoint, finalHitPos);
                                 *              //LogVoxelHits(hitDist, voxelHitName, finalHitPos, lineCheck.Length);
                                 *              isVoxel = true;
                                 *          }
                                 *      }
                                 *  }
                                 * }
                                 */

                                /*
                                 * // Log Loop sizes
                                 * if (_updateCount % 100 == 0)
                                 * {
                                 *  Logging.Instance.WriteLine(rainImpactEntities.Count.ToString() + " " +
                                 *                             lineIntersectedGrids.Count.ToString() + " " +
                                 *                             lineIntersectedVoxels.Count.ToString());
                                 * }
                                 */


                                //Logging.Instance.WriteLine(isVoxel.ToString());
                                float distanceTotal   = 0f;
                                var   rainDropSize    = MyUtils.GetRandomFloat(0.8f, 1.5f);
                                var   randSkip        = MyUtils.GetRandomInt(8);
                                var   hasHit          = hitDist.Value > 0.001 && (hitDist.Value < lineCheck.Length || isVoxel);
                                var   dropsInDistance = hasHit ? hitDist.Value / rainDropSize : lineCheck.Length / rainDropSize;

                                //var nextStart = hasHit ? finalHitPos : lineStartPoint;
                                //var dir = hasHit ? -lineCheck.Direction : lineCheck.Direction;

                                var nextStart = hasHit ? finalHitPos : finalHitPos;
                                var dir       = hasHit ? -lineCheck.Direction : -lineCheck.Direction;

                                //var nextStart = hasHit && !checkVoxel ? finalHitPos : finalHitPos;
                                //var dir = hasHit && !checkVoxel ? -lineCheck.Direction : -lineCheck.Direction;

                                lineColor = checkVoxel ? Color.Green : Color.White;

                                if (checkVoxel && voxelHitName != null && _updateCount % 300 == 0)
                                {
                                    //LogVoxelHits(hitDist, voxelHitName, finalHitPos, lineCheck.Length);
                                }

                                while (distanceTotal < dropsInDistance)
                                {
                                    if (randSkip-- <= 0)
                                    {
                                        Droplet droplet;
                                        Droplets.AllocateOrCreate(out droplet);

                                        droplet.StartPoint = nextStart;
                                        droplet.Direction  = dir;
                                        droplet.DrawLength = rainDropSize;
                                        droplet.LineColor  = lineColor;
                                        randSkip           = MyUtils.GetRandomInt(8);
                                    }

                                    distanceTotal += rainDropSize;
                                    nextStart     += (dir * rainDropSize);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logging.Instance.WriteLine(e.ToString());
            }
            DsWatch.Complete(true);
        }
Example #13
0
        void DebugDrawFrustum(MatrixD camera, Color color)
        {
            BoundingFrustumD frustum = new BoundingFrustumD(camera);

            MyDebugDraw.DrawBoundingFrustum(frustum, color);
        }
 internal void DeallocateFrustum(BoundingFrustumD frustum)
 {
     m_frustumPool.Deallocate(frustum);
 }
Example #15
0
        internal static void PreparePointLights()
        {
            var activePointlights = 0;

            MyLights.Update();
            BoundingFrustumD viewFrustumClippedD = MyRender11.Environment.ViewFrustumClippedD;

            if (MyStereoRender.Enable)
            {
                if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT)
                {
                    viewFrustumClippedD = MyStereoRender.EnvMatricesLeftEye.ViewFrustumClippedD;
                }
                else if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT)
                {
                    viewFrustumClippedD = MyStereoRender.EnvMatricesRightEye.ViewFrustumClippedD;
                }
            }
            MyLights.PointlightsBvh.OverlapAllFrustum(ref viewFrustumClippedD, VisiblePointlights);

            bool visiblePointlights = VisiblePointlights.Count != 0;

            if (!visiblePointlights && !m_lastFrameVisiblePointlights)
            {
                return;
            }

            m_lastFrameVisiblePointlights = visiblePointlights;

            if (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS)
            {
                VisiblePointlights.Sort((x, y) => x.ViewerDistanceSquared.CompareTo(y.ViewerDistanceSquared));

                while (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS)
                {
                    VisiblePointlights.RemoveAtFast(VisiblePointlights.Count - 1);
                }
            }

            foreach (var light in VisiblePointlights)
            {
                MyLights.WritePointlightConstants(light, ref m_pointlightsCullBuffer[activePointlights]);

                activePointlights++;
                Debug.Assert(activePointlights <= MyRender11Constants.MAX_POINT_LIGHTS);
            }
            for (int lightIndex = activePointlights; lightIndex < MyRender11Constants.MAX_POINT_LIGHTS; ++lightIndex)
            {
                MyLights.WritePointlightConstants(LightId.NULL, ref m_pointlightsCullBuffer[lightIndex]);
            }

            var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16));

            mapping.WriteAndPosition(ref activePointlights);
            mapping.Unmap();

            mapping = MyMapping.MapDiscard(m_pointlightCullHwBuffer.Buffer);
            mapping.WriteAndPosition(m_pointlightsCullBuffer, 0, MyRender11Constants.MAX_POINT_LIGHTS);
            mapping.Unmap();

            if (!MyStereoRender.Enable)
            {
                RC.CSSetCB(0, MyCommon.FrameConstants);
            }
            else
            {
                MyStereoRender.CSBindRawCB_FrameConstants(RC);
            }
            RC.CSSetCB(1, MyCommon.GetObjectCB(16));

            //RC.BindUAV(0, MyScreenDependants.m_test);
            RC.BindUAV(0, MyScreenDependants.m_tileIndices);
            RC.BindGBufferForRead(0, MyGBuffer.Main);
            RC.CSBindRawSRV(MyCommon.POINTLIGHT_SLOT, m_pointlightCullHwBuffer);
            RC.SetCS(m_preparePointLights);
            Vector2I tiles = new Vector2I(MyScreenDependants.TilesX, MyScreenDependants.TilesY);

            if (MyStereoRender.Enable && MyStereoRender.RenderRegion != MyStereoRegion.FULLSCREEN)
            {
                tiles.X /= 2;
            }

            RC.DeviceContext.Dispatch(tiles.X, tiles.Y, 1);
            RC.SetCS(null);
        }