/* * 更新雷达中的可见区域贴图 * * * 分为以下几个步骤: * * <1>获得可能在雷达范围中的所有遮挡物 * * <2>将遮挡物的边界点坐标转换到雷达空间中,并在已经栅格化的雷达空间中填充边界点的深度信息,获得深度图。 * * <3>用显卡计算出可见区域贴图 * * */ private void CalRaderMap(Rader rader, IEnumerable <IShelterObj>[] shelterObjGroup) { // 获得可能在雷达范围中的所有遮挡物。 List <IShelterObj> sheltersInRader = new List <IShelterObj>(16); foreach (IEnumerable <IShelterObj> group in shelterObjGroup) { foreach (IShelterObj shelter in group) { if (shelter.BoundingBox.Intersects(rader.BoundBox)) { sheltersInRader.Add(shelter); } } } // 将遮挡物的边界点坐标转换到雷达空间中,并在已经栅格化的雷达空间中填充边界点的深度信息,获得深度图。 rader.depthMap.ClearMap(1); foreach (IShelterObj shelter in sheltersInRader) { rader.depthMap.ApplyShelterObj(rader, shelter); } // 用显卡计算出可见区域贴图 raderDrawer.DrawRader(rader); // 更新各项数据 rader.UpdateTexture(); }
public void ApplyShelterObj(Rader rader, IShelterObj obj) { Matrix worldMatrix = obj.WorldTrans; CircleList <BorderPoint> border = obj.BorderData; CircleListNode <BorderPoint> lastB = border.First.pre; Vector2 pInRader = rader.TranslateToRaderSpace(Vector2.Transform(ConvertHelper.PointToVector2(lastB.value.p), worldMatrix)); int lastIndex = IndexOf(pInRader.X); float lastDepth = pInRader.Y; CircleListNode <BorderPoint> cur = border.First; for (int i = 0; i < border.Length; i++) { pInRader = rader.TranslateToRaderSpace(Vector2.Transform(ConvertHelper.PointToVector2(cur.value.p), worldMatrix)); int curIndex = IndexOf(pInRader.X); float curDepth = pInRader.Y; if (pInRader.X >= -1.1f && pInRader.X <= 1.3f) { //this[pInRader.X] = Math.Min( this[pInRader.X], pInRader.Y ); SetValueAtIndex(curIndex, pInRader.Y, obj, i, cur.value.p); if (curIndex - lastIndex > 1) { int overIndex = lastIndex + 1; while (overIndex != curIndex) { float lerp = MathHelper.Lerp(lastDepth, curDepth, (curIndex - overIndex) / (curIndex - lastIndex)); SetValueAtIndex(overIndex, lerp, obj, i, cur.value.p); overIndex++; } } else if (curIndex - lastIndex < -1) { int overIndex = lastIndex - 1; while (overIndex != curIndex) { float lerp = MathHelper.Lerp(lastDepth, curDepth, (curIndex - overIndex) / (curIndex - lastIndex)); SetValueAtIndex(overIndex, lerp, obj, i, cur.value.p); overIndex--; } } } lastIndex = curIndex; lastDepth = curDepth; cur = cur.next; } }
private static ObjVisiBorder CalNonShelterVisiBorder( IHasBorderObj obj, Rader rader ) { CircleListNode<BorderPoint> curNode = obj.BorderData.First; CircleList<VisiBordPoint> points = new CircleList<VisiBordPoint>(); for (int i = 0; i < obj.BorderData.Length; i++) { if (rader.PointInRader( Vector2.Transform( ConvertHelper.PointToVector2( curNode.value.p ), obj.WorldTrans ) )) { points.AddLast( new VisiBordPoint( i, curNode.value.p ) ); } curNode = curNode.next; } if (points.Length != 0) return new ObjVisiBorder( obj, points ); else return null; }
public void DrawRader( Rader rader ) { try { BaseGame.Device.RenderState.DepthBufferEnable = false; BaseGame.Device.RenderState.DepthBufferWriteEnable = false; BaseGame.Device.RenderState.AlphaBlendEnable = false; Texture2D depthMapTex = rader.depthMap.GetMapTexture(); depthMapPara.SetValue( depthMapTex ); raderAngPara.SetValue( rader.Ang ); raderColorPara.SetValue( ColorHelper.ToFloat4( rader.RaderColor ) ); raderRotaMatrixPara.SetValue( rader.RotaMatrix ); renderRaderEffect.CommitChanges(); DepthStencilBuffer old = BaseGame.Device.DepthStencilBuffer; BaseGame.Device.SetRenderTarget( 0, rader.target ); BaseGame.Device.DepthStencilBuffer = depthBuffer; BaseGame.Device.Clear( Color.TransparentBlack ); renderRaderEffect.CurrentTechnique = renderRaderEffect.Techniques[0]; renderRaderEffect.Begin(); renderRaderEffect.CurrentTechnique.Passes[0].Begin(); BaseGame.Device.VertexDeclaration = del; BaseGame.Device.Vertices[0].SetSource( sectorBuffer, 0, del.GetVertexStrideSize( 0 ) ); BaseGame.Device.DrawPrimitives( PrimitiveType.TriangleFan, 0, partSum ); renderRaderEffect.CurrentTechnique.Passes[0].End(); renderRaderEffect.End(); BaseGame.Device.SetRenderTarget( 0, rader.targetSmall ); BaseGame.Device.DepthStencilBuffer = depthBufferSmall; BaseGame.Device.Clear( Color.TransparentBlack ); renderRaderEffect.CurrentTechnique = renderRaderEffect.Techniques[0]; renderRaderEffect.Begin(); renderRaderEffect.CurrentTechnique.Passes[0].Begin(); BaseGame.Device.VertexDeclaration = del; BaseGame.Device.Vertices[0].SetSource( sectorBuffer, 0, del.GetVertexStrideSize( 0 ) ); BaseGame.Device.DrawPrimitives( PrimitiveType.TriangleFan, 0, partSum ); renderRaderEffect.CurrentTechnique.Passes[0].End(); renderRaderEffect.End(); BaseGame.Device.SetRenderTarget( 0, null ); BaseGame.Device.DepthStencilBuffer = old; BaseGame.Device.RenderState.DepthBufferEnable = true; BaseGame.Device.RenderState.DepthBufferWriteEnable = true; } catch (Exception ex) { Log.Write( ex.Message ); } }
public void DrawRader(Rader rader) { try { BaseGame.Device.RenderState.DepthBufferEnable = false; BaseGame.Device.RenderState.DepthBufferWriteEnable = false; BaseGame.Device.RenderState.AlphaBlendEnable = false; Texture2D depthMapTex = rader.depthMap.GetMapTexture(); depthMapPara.SetValue(depthMapTex); raderAngPara.SetValue(rader.Ang); raderColorPara.SetValue(ColorHelper.ToFloat4(rader.RaderColor)); raderRotaMatrixPara.SetValue(rader.RotaMatrix); renderRaderEffect.CommitChanges(); DepthStencilBuffer old = BaseGame.Device.DepthStencilBuffer; BaseGame.Device.SetRenderTarget(0, rader.target); BaseGame.Device.DepthStencilBuffer = depthBuffer; BaseGame.Device.Clear(Color.TransparentBlack); renderRaderEffect.CurrentTechnique = renderRaderEffect.Techniques[0]; renderRaderEffect.Begin(); renderRaderEffect.CurrentTechnique.Passes[0].Begin(); BaseGame.Device.VertexDeclaration = del; BaseGame.Device.Vertices[0].SetSource(sectorBuffer, 0, del.GetVertexStrideSize(0)); BaseGame.Device.DrawPrimitives(PrimitiveType.TriangleFan, 0, partSum); renderRaderEffect.CurrentTechnique.Passes[0].End(); renderRaderEffect.End(); BaseGame.Device.SetRenderTarget(0, rader.targetSmall); BaseGame.Device.DepthStencilBuffer = depthBufferSmall; BaseGame.Device.Clear(Color.TransparentBlack); renderRaderEffect.CurrentTechnique = renderRaderEffect.Techniques[0]; renderRaderEffect.Begin(); renderRaderEffect.CurrentTechnique.Passes[0].Begin(); BaseGame.Device.VertexDeclaration = del; BaseGame.Device.Vertices[0].SetSource(sectorBuffer, 0, del.GetVertexStrideSize(0)); BaseGame.Device.DrawPrimitives(PrimitiveType.TriangleFan, 0, partSum); renderRaderEffect.CurrentTechnique.Passes[0].End(); renderRaderEffect.End(); BaseGame.Device.SetRenderTarget(0, null); BaseGame.Device.DepthStencilBuffer = old; BaseGame.Device.RenderState.DepthBufferEnable = true; BaseGame.Device.RenderState.DepthBufferWriteEnable = true; } catch (Exception ex) { Log.Write(ex.Message); } }
/* * 更新雷达中的可见区域贴图 * * * 分为以下几个步骤: * * <1>获得可能在雷达范围中的所有遮挡物 * * <2>将遮挡物的边界点坐标转换到雷达空间中,并在已经栅格化的雷达空间中填充边界点的深度信息,获得深度图。 * * <3>用显卡计算出可见区域贴图 * * */ private void CalRaderMap( Rader rader, IEnumerable<IShelterObj>[] shelterObjGroup ) { // 获得可能在雷达范围中的所有遮挡物。 List<IShelterObj> sheltersInRader = new List<IShelterObj>( 16 ); foreach (IEnumerable<IShelterObj> group in shelterObjGroup) { foreach (IShelterObj shelter in group) { if (shelter.BoundingBox.Intersects( rader.BoundBox )) sheltersInRader.Add( shelter ); } } // 将遮挡物的边界点坐标转换到雷达空间中,并在已经栅格化的雷达空间中填充边界点的深度信息,获得深度图。 rader.depthMap.ClearMap( 1 ); foreach (IShelterObj shelter in sheltersInRader) { rader.depthMap.ApplyShelterObj( rader, shelter ); } // 用显卡计算出可见区域贴图 raderDrawer.DrawRader( rader ); // 更新各项数据 rader.UpdateTexture(); }
public void ApplyShelterObj( Rader rader, IShelterObj obj ) { Matrix worldMatrix = obj.WorldTrans; CircleList<BorderPoint> border = obj.BorderData; CircleListNode<BorderPoint> lastB = border.First.pre; Vector2 pInRader = rader.TranslateToRaderSpace( Vector2.Transform( ConvertHelper.PointToVector2( lastB.value.p ), worldMatrix ) ); int lastIndex = IndexOf( pInRader.X ); float lastDepth = pInRader.Y; CircleListNode<BorderPoint> cur = border.First; for (int i = 0; i < border.Length; i++) { pInRader = rader.TranslateToRaderSpace( Vector2.Transform( ConvertHelper.PointToVector2( cur.value.p ), worldMatrix ) ); int curIndex = IndexOf( pInRader.X ); float curDepth = pInRader.Y; if (pInRader.X >= -1.1f && pInRader.X <= 1.3f) { //this[pInRader.X] = Math.Min( this[pInRader.X], pInRader.Y ); SetValueAtIndex( curIndex, pInRader.Y, obj, i, cur.value.p ); if (curIndex - lastIndex > 1) { int overIndex = lastIndex + 1; while (overIndex != curIndex) { float lerp = MathHelper.Lerp( lastDepth, curDepth, (curIndex - overIndex) / (curIndex - lastIndex) ); SetValueAtIndex( overIndex, lerp, obj, i, cur.value.p ); overIndex++; } } else if (curIndex - lastIndex < -1) { int overIndex = lastIndex - 1; while (overIndex != curIndex) { float lerp = MathHelper.Lerp( lastDepth, curDepth, (curIndex - overIndex) / (curIndex - lastIndex) ); SetValueAtIndex( overIndex, lerp, obj, i, cur.value.p ); overIndex--; } } } lastIndex = curIndex; lastDepth = curDepth; cur = cur.next; } }