Axiom.Components.Terrain.TerrainMaterialGenerator.RenderCompositeMap C# (CSharp) Method

RenderCompositeMap() public method

Helper method to render a composite map.
public RenderCompositeMap ( int size, Rectangle rect, Material mat, Axiom.Core.Texture destCompositeMap ) : void
size int The requested composite map size
rect Axiom.Core.Rectangle The region of the composite map to update, in image space
mat Axiom.Graphics.Material The material to use to render the map
destCompositeMap Axiom.Core.Texture
return void
        public virtual void RenderCompositeMap(int size, Rectangle rect,
            Material mat, Texture destCompositeMap)
        {
            //return;
            if (mCompositeMapSM == null)
            {
                //dedicated SceneManager

                mCompositeMapSM = Root.Instance.CreateSceneManager(SceneType.ExteriorClose, "TerrainMaterialGenerator_SceneManager");
                float camDist = 100;
                float halfCamDist = camDist * 0.5f;
                mCompositeMapCam = mCompositeMapSM.CreateCamera("TerrainMaterialGenerator_Camera");
                mCompositeMapCam.Position = new Vector3(0, 0, camDist);
                //mCompositeMapCam.LookAt(Vector3.Zero);
                mCompositeMapCam.ProjectionType = Projection.Orthographic;
                mCompositeMapCam.Near = 10;
                mCompositeMapCam.Far = 999999* 3;
                //mCompositeMapCam.AspectRatio = camDist / camDist;
                mCompositeMapCam.SetOrthoWindow(camDist, camDist);
                // Just in case material relies on light auto params
                mCompositeMapLight = mCompositeMapSM.CreateLight("TerrainMaterialGenerator_Light");
                mCompositeMapLight.Type = LightType.Directional;

                RenderSystem rSys = Root.Instance.RenderSystem;
                float hOffset = rSys.HorizontalTexelOffset / (float)size;
                float vOffset = rSys.VerticalTexelOffset / (float)size;

                //setup scene
                mCompositeMapPlane = mCompositeMapSM.CreateManualObject("TerrainMaterialGenerator_ManualObject");
                mCompositeMapPlane.Begin(mat.Name, OperationType.TriangleList);
                mCompositeMapPlane.Position(-halfCamDist, halfCamDist, 0);
                mCompositeMapPlane.TextureCoord(0 - hOffset, 0 - vOffset);
                mCompositeMapPlane.Position(-halfCamDist, -halfCamDist, 0);
                mCompositeMapPlane.TextureCoord(0 - hOffset, 1 - vOffset);
                mCompositeMapPlane.Position(halfCamDist, -halfCamDist, 0);
                mCompositeMapPlane.TextureCoord(1 - hOffset, 1 - vOffset);
                mCompositeMapPlane.Position(halfCamDist, halfCamDist, 0);
                mCompositeMapPlane.TextureCoord(1 - hOffset, 0 - vOffset);
                mCompositeMapPlane.Quad(0, 1, 2, 3);
                mCompositeMapPlane.End();
                mCompositeMapSM.RootSceneNode.AttachObject(mCompositeMapPlane);
            }//end if

            // update
            mCompositeMapPlane.SetMaterialName(0, mat.Name);
            mCompositeMapLight.Direction = TerrainGlobalOptions.LightMapDirection;
            mCompositeMapLight.Diffuse = TerrainGlobalOptions.CompositeMapDiffuse;
            mCompositeMapSM.AmbientLight =TerrainGlobalOptions.CompositeMapAmbient;
            

            //check for size change (allow smaller to be reused)
            if (mCompositeMapRTT != null && size != mCompositeMapRTT.Width)
            {
                TextureManager.Instance.Remove(mCompositeMapRTT);
                mCompositeMapRTT = null;
            }
            if (mCompositeMapRTT == null)
            {
                mCompositeMapRTT = TextureManager.Instance.CreateManual(
                    mCompositeMapSM.Name + "/compRTT",
                    ResourceGroupManager.DefaultResourceGroupName,
                    TextureType.TwoD,
                    size,
                    size,
                    0,
                    PixelFormat.BYTE_RGBA,
                    TextureUsage.RenderTarget);

                RenderTarget rtt = mCompositeMapRTT.GetBuffer().GetRenderTarget();
                // don't render all the time, only on demand
                rtt.IsAutoUpdated = false;
                Viewport vp = rtt.AddViewport(mCompositeMapCam);
                // don't render overlays
                vp.ShowOverlays = false;
            }

            // calculate the area we need to update
            float vpleft = (float)rect.Left / (float)size;
            float vptop = (float)rect.Top / (float)size;
            float vpright = (float)rect.Right / (float)size;
            float vpbottom = (float)rect.Bottom / (float)size;
            float vpwidth = (float)rect.Width / (float)size;
            float vpheight = (float)rect.Height / (float)size;

            RenderTarget rtt2 = mCompositeMapRTT.GetBuffer().GetRenderTarget();
            Viewport vp2 = rtt2.GetViewport(0);
            mCompositeMapCam.SetWindow(vpleft, vptop, vpright, vpbottom);
            rtt2.Update();
            vp2.Update();
            // We have an RTT, we want to copy the results into a regular texture
            // That's because in non-update scenarios we don't want to keep an RTT
            // around. We use a single RTT to serve all terrain pages which is more
            // efficient.
            BasicBox box = new BasicBox((int)rect.Left, (int)rect.Top, (int)rect.Right, (int)rect.Bottom);
            destCompositeMap.GetBuffer().Blit(mCompositeMapRTT.GetBuffer(), box, box);
        }
        public void Dispose()

Usage Example

Example #1
0
            /// <summary>
            /// Update the composite map for a terrain
            /// </summary>
            /// <param name="terrain"></param>
            /// <param name="rect"></param>
            public virtual void UpdateCompositeMap(Terrain terrain, Rectangle rect)
            {
                // convert point-space rect into image space
                int     compSize = terrain.CompositeMap.Width;
                var     imgRect = new Rectangle();
                Vector3 inVec = Vector3.Zero, outVec = Vector3.Zero;

                inVec.x = rect.Left;
                inVec.y = rect.Bottom - 1;                 // this is 'top' in image space, also make inclusive
                terrain.ConvertPosition(Space.PointSpace, inVec, Space.TerrainSpace, ref outVec);
                float left = (outVec.x * compSize);
                float top  = ((1.0f - outVec.y) * compSize);

                ;
                imgRect.Left = (long)left;
                imgRect.Top  = (long)top;
                inVec.x      = rect.Right - 1;
                inVec.y      = rect.Top;            // this is 'bottom' in image space
                terrain.ConvertPosition(Space.PointSpace, inVec, Space.TerrainSpace, ref outVec);
                float right = (outVec.x * (float)compSize + 1);

                imgRect.Right = (long)right;
                float bottom = ((1.0f - outVec.y) * compSize + 1);

                imgRect.Bottom = (long)bottom;

                imgRect.Left   = System.Math.Max(0L, imgRect.Left);
                imgRect.Top    = System.Math.Max(0L, imgRect.Top);
                imgRect.Right  = System.Math.Min((long)compSize, imgRect.Right);
                imgRect.Bottom = System.Math.Min((long)compSize, imgRect.Bottom);

#warning enable rendercompositemap
#if false
                mParent.RenderCompositeMap(compSize, imgRect,
                                           terrain.CompositeMapMaterial,
                                           terrain.CompositeMap);
#endif
                update = true;
            }