public Bitmap TerrainToBitmap(Bitmap mapBmp)
        {
            mapBmp = null;
            // debug
            //var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            //long startMemory = currentProcess.WorkingSet64;

            List<string> renderers = RenderingLoader.ListRenderers(Util.ExecutingDirectory());
            if (renderers.Count > 0)
            {
                m_primMesher = RenderingLoader.LoadRenderer(renderers[0]);
                MainConsole.Instance.Debug("[MAPTILE]: Loaded prim mesher " + m_primMesher);
            }
            else
            {
                MainConsole.Instance.Info("[MAPTILE]: No prim mesher loaded, prim rendering will be disabled");
            }

            bool drawPrimVolume = true;
            bool textureTerrain = true;

            try
            {
                IConfig startupConfig = m_config.Configs["Startup"];
                drawPrimVolume = startupConfig.GetBoolean("DrawPrimOnMapTile", drawPrimVolume);
                textureTerrain = startupConfig.GetBoolean("TextureOnMapTile", textureTerrain);
            }
            catch
            {
                MainConsole.Instance.Warn("[MAPTILE]: Failed to load StartupConfig");
            }

            m_texturePrims = m_config.Configs["MapModule"].GetBoolean("WarpTexturePrims", false);

            int scaledRemovalFactor = m_scene.RegionInfo.RegionSizeX/(Constants.RegionSize/2);
            Vector3 camPos = new Vector3(m_scene.RegionInfo.RegionSizeX/2 - 0.5f,
                                         m_scene.RegionInfo.RegionSizeY/2 - 0.5f, 221.7025033688163f);
            Viewport viewport = new Viewport(camPos, -Vector3.UnitZ, 1024f, 0.1f,
                                             m_scene.RegionInfo.RegionSizeX - scaledRemovalFactor,
                                             m_scene.RegionInfo.RegionSizeY - scaledRemovalFactor,
                                             m_scene.RegionInfo.RegionSizeX - scaledRemovalFactor,
                                             m_scene.RegionInfo.RegionSizeY - scaledRemovalFactor);

            int width = viewport.Width;
            int height = viewport.Height;

            WarpRenderer renderer = new WarpRenderer();
            //warp_Object terrainObj;
            renderer.CreateScene(width, height);
            renderer.Scene.autoCalcNormals = false;

            #region Camera

            warp_Vector pos = ConvertVector(viewport.Position);
            pos.z -= 0.001f; // Works around an issue with the Warp3D camera
            warp_Vector lookat = warp_Vector.add(ConvertVector(viewport.Position), ConvertVector(viewport.LookDirection));

            renderer.Scene.defaultCamera.setPos(pos);
            renderer.Scene.defaultCamera.lookAt(lookat);

            if (viewport.Orthographic)
            {
                renderer.Scene.defaultCamera.isOrthographic = true;
                renderer.Scene.defaultCamera.orthoViewWidth = viewport.OrthoWindowWidth;
                renderer.Scene.defaultCamera.orthoViewHeight = viewport.OrthoWindowHeight;
            }
            else
            {
                viewport.Orthographic = false;
                float fov = 256;
                //fov *= 1.75f; // FIXME: ???
                renderer.Scene.defaultCamera.setFov(fov);
            }

            #endregion Camera

            renderer.Scene.addLight("Light1", new warp_Light(new warp_Vector(1.0f, 0.5f, 1f), 0xffffff, 0, 320, 40));
            renderer.Scene.addLight("Light2", new warp_Light(new warp_Vector(-1f, -1f, 1f), 0xffffff, 0, 100, 40));

            try
            {
                CreateWater(renderer);

                CreateTerrain(renderer, textureTerrain);

                if (drawPrimVolume && m_primMesher != null)
                {
                    foreach (ISceneChildEntity part in m_scene.Entities.GetEntities().SelectMany(ent => ent.ChildrenEntities()))
                        CreatePrim(renderer, part);
                }

            }
            catch (Exception ex)
            {
                MainConsole.Instance.Warn("[Warp3D]: Exception in the map generation, " + ex);
            }

            renderer.Render();
            Bitmap bitmap = renderer.Scene.getImage();
            bitmap = ImageUtils.ResizeImage(bitmap, Constants.RegionSize, Constants.RegionSize);
            foreach (var o in renderer.Scene.objectData.Values)
            {
                warp_Object obj = (warp_Object) o;
                obj.vertexData = null;
                obj.triangleData = null;
            }

            renderer.Reset ();
            //renderer.Scene.removeAllObjects();
            // renderer = null;
            // viewport = null;
            m_primMesher = null;
            ///terrainObj.fastvertex = null;
            ///terrainObj.fasttriangle = null;
            ///terrainObj = null;
            SaveCache();
            m_colors.Clear();

            //Force GC to try to clean this mess up
            GC.Collect();

            // debug
            //currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            //var endMemory = currentProcess.WorkingSet64;
            //MainConsole.Instance.InfoFormat ("[Warp3D]:Render:  Memory Start: {0}, end: {1}, Diff: {2}", startMemory, endMemory, (endMemory-startMemory));

            return bitmap;
        }
Esempio n. 2
0
        public Bitmap TerrainBitmap(Viewport viewport, bool threeD)
        {
            // AntiAliasing
            int width  = viewport.Width * 2;
            int height = viewport.Height * 2;

            WarpRenderer renderer = new WarpRenderer();

            renderer.CreateScene(width, height);
            renderer.Scene.autoCalcNormals = false;
            if (threeD)
            {
                renderer.SetBackgroundColor(SKYCOLOR);
            }

            #region Camera

            warp_Vector pos = ConvertVector(viewport.Position);
            pos.z -= 0.001f; // Works around an issue with the Warp3D camera
            warp_Vector lookat = warp_Vector.add(ConvertVector(viewport.Position), ConvertVector(viewport.LookDirection));

            renderer.Scene.defaultCamera.setPos(pos);
            renderer.Scene.defaultCamera.lookAt(lookat);

            if (viewport.Orthographic)
            {
                renderer.Scene.defaultCamera.isOrthographic = true;
                if (viewport.OrthoWindowWidth <= viewport.OrthoWindowHeight)
                {
                    renderer.Scene.defaultCamera.orthoViewWidth  = viewport.OrthoWindowWidth;
                    renderer.Scene.defaultCamera.orthoViewHeight = viewport.OrthoWindowWidth;
                }
                else
                {
                    renderer.Scene.defaultCamera.orthoViewWidth  = viewport.OrthoWindowHeight;
                    renderer.Scene.defaultCamera.orthoViewHeight = viewport.OrthoWindowHeight;
                }
            }
            else
            {
                viewport.Orthographic = false;
                float fov = viewport.FieldOfView;
                renderer.Scene.defaultCamera.setFov(fov);
            }

            #endregion Camera

            renderer.Scene.addLight("Light1", new warp_Light(new warp_Vector(1.0f, 0.5f, 1f), 0xffffff, 0, 320, 40));
            renderer.Scene.addLight("Light2", new warp_Light(new warp_Vector(-1f, -1f, 1f), 0xffffff, 0, 100, 40));


            try
            {
                CreateWater(renderer, threeD);
                CreateTerrain(renderer, m_textureTerrain);

                if (m_drawPrimVolume && m_primMesher != null)
                {
                    foreach (ISceneChildEntity part in m_scene.Entities.GetEntities().SelectMany(ent => ent.ChildrenEntities()))
                    {
                        CreatePrim(renderer, part);
                    }
                }
            }
            catch (Exception ex)
            {
                MainConsole.Instance.Warn("[Warp3D]: Exception in the map generation, " + ex);
            }

            renderer.Render();
            Bitmap bitmap = renderer.Scene.getImage();

            // AntiAliasing
            using (Bitmap origBitmap = bitmap)
                bitmap = ImageUtils.ResizeImage(origBitmap, viewport.Width, viewport.Height);


            // Clean up
            SaveCache();
            foreach (var o in renderer.Scene.objectData.Values)
            {
                warp_Object obj = (warp_Object)o;
                obj.vertexData   = null;
                obj.triangleData = null;
            }

            renderer.Scene.removeAllObjects();
            renderer.Reset();
            m_colors.Clear();

            //Force GC to try to clean this mess up
            GC.Collect();

            return(bitmap);
        }