public HeightmapRenderer(WorldRenderer In, HeightMap For)
 {
     _in = In;
     _for = For;
     _p2d = new Perlin2D(100);
     _renchunks = new RenderChunk[NUM_RENDER_CHUNKS] [];
     for (int i = 0; i < _renchunks.Length; ++i) {
         _renchunks [i] = new RenderChunk[NUM_RENDER_CHUNKS];
     }
 }
        void Render(WorldRenderer WR, float X, float Y, int Pass)
        {
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.NormalArray);

            int mincx = Floor(X / CHUNK_SIZE) - VIEW_DIST;
            int mincy = Floor(Y / CHUNK_SIZE) - VIEW_DIST;

            for (int cx = mincx; cx < mincx + VIEW_DIST + VIEW_DIST + 1; ++cx) {
                for (int cy = mincy; cy < mincy + VIEW_DIST + VIEW_DIST + 1; ++cy) {
                    if (InFrustrum(cx, cy)) {
                        int cox = cx - mincx;
                        int coy = cy - mincy;

                        cox = Abs(cox - VIEW_DIST);
                        coy = Abs(coy - VIEW_DIST);
                        int d = 1;//(int)(Math.Sqrt(cox * cox + coy * coy));

                        int LOD = 32;
                        LOD >>= d;
                        if (LOD < 1)
                            LOD = 1;

                        int x = cx % NUM_RENDER_CHUNKS;
                        int y = cy % NUM_RENDER_CHUNKS;
                        if (x < 0)
                            x += NUM_RENDER_CHUNKS;
                        if (y < 0)
                            y += NUM_RENDER_CHUNKS;
                        if (_renchunks [x] [y] == null || _renchunks [x] [y].X != cx || _renchunks [x] [y].Y != cy || _renchunks [x] [y].LOD != LOD)
                            _renchunks [x] [y] = new RenderChunk(_for, _p2d, cx, cy, LOD);
                        switch (Pass) {
                        case PASS_GROUND:
                            _renchunks [x] [y].Render(WR);
                            break;
                        case PASS_WATER:
                            _renchunks [x] [y].RenderWater(WR);
                            break;
                        default:
                            throw new ArgumentException("Pass " + Pass + " out of range");
                        }
                    }
                }
            }
            GL.DisableClientState(ArrayCap.VertexArray);
            GL.DisableClientState(ArrayCap.NormalArray);
            GL.DisableClientState(ArrayCap.ColorArray);
        }