コード例 #1
0
        public void doPaint()
        {
            DateTime start = DateTime.Now;

            if (this.DesignMode)
            {
                return;
            }

            if (area.LocationMiddle.Lat == 0 && area.LocationMiddle.Lng == 0)
            {
                return;
            }

            core.Position = center;

            double heightscale = 1; //(step/90.0)*5;

            var campos = convertCoords(center);

            cameraX = campos[0];
            cameraY = campos[1];
            cameraZ = (campos[2] < srtm.getAltitude(center.Lat, center.Lng).alt)
                ? (srtm.getAltitude(center.Lat, center.Lng).alt + 1) * heightscale
                : center.Alt * heightscale; // (srtm.getAltitude(lookZ, lookX, 20) + 100) * heighscale;

            lookX = campos[0] + Math.Sin(MathHelper.Radians(rpy.Z)) * 100;
            lookY = campos[1] + Math.Cos(MathHelper.Radians(rpy.Z)) * 100;
            lookZ = cameraZ;

            var size = 5000;

            // in front
            PointLatLngAlt front = center.newpos(rpy.Z, size);
            // behind
            PointLatLngAlt behind = center.newpos(rpy.Z, -50);
            // left : 90 allows for 180 degree viewing angle
            PointLatLngAlt left = center.newpos(rpy.Z - 45, size);
            // right
            PointLatLngAlt right = center.newpos(rpy.Z + 45, size);

            double maxlat = Math.Max(left.Lat, Math.Max(right.Lat, Math.Max(front.Lat, behind.Lat)));
            double minlat = Math.Min(left.Lat, Math.Min(right.Lat, Math.Min(front.Lat, behind.Lat)));

            double maxlng = Math.Max(left.Lng, Math.Max(right.Lng, Math.Max(front.Lng, behind.Lng)));
            double minlng = Math.Min(left.Lng, Math.Min(right.Lng, Math.Min(front.Lng, behind.Lng)));

            area = RectLatLng.FromLTRB(minlng, maxlat, maxlng, minlat);

            if (!Context.IsCurrent)
            {
                MakeCurrent();
            }

            GL.MatrixMode(MatrixMode.Projection);

            OpenTK.Matrix4 projection = OpenTK.Matrix4.CreatePerspectiveFieldOfView((float)(90 * MathHelper.deg2rad),
                                                                                    (float)Width / Height, 0.1f,
                                                                                    (float)20000);
            GL.LoadMatrix(ref projection);

            /*Console.WriteLine("cam: {0} {1} {2} lookat: {3} {4} {5}", (float) cameraX, (float) cameraY, (float) cameraZ,
             *  (float) lookX,
             *  (float) lookY, (float) lookZ);
             */
            Matrix4 modelview = Matrix4.LookAt((float)cameraX, (float)cameraY, (float)cameraZ + 100f * 0,
                                               (float)lookX,
                                               (float)lookY, (float)lookZ, 0, 0, 1);

            GL.MatrixMode(MatrixMode.Modelview);

            // roll
            modelview = Matrix4.Mult(modelview, Matrix4.CreateRotationZ((float)(rpy.X * MathHelper.deg2rad)));
            // pitch
            modelview = Matrix4.Mult(modelview, Matrix4.CreateRotationX((float)(rpy.Y * -MathHelper.deg2rad)));

            GL.LoadMatrix(ref modelview);

            GL.Viewport(0, 0, Width, Height);

            GL.ClearColor(Color.CornflowerBlue);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.AccumBufferBit);


            GL.Disable(EnableCap.Fog);
            GL.Enable(EnableCap.Fog);
            GL.Disable(EnableCap.Lighting);

            Lighting.SetupAmbient(0.1f);
            Lighting.SetDefaultMaterial(1f);
            //      Lighting.SetupLightZero(new Vector3d(cameraX, cameraY, cameraZ + 100), 0f);


            GL.Fog(FogParameter.FogColor, new float[] { 100 / 255.0f, 149 / 255.0f, 237 / 255.0f, 1f });
            GL.Fog(FogParameter.FogDensity, 0.1f);
            GL.Fog(FogParameter.FogMode, (int)FogMode.Linear);
            GL.Fog(FogParameter.FogStart, (float)300);
            GL.Fog(FogParameter.FogEnd, (float)2000);

            GL.Disable(EnableCap.DepthTest);
            //GL.DepthFunc(DepthFunction.Always);

            var texlist = textureid.ToArray().ToSortedList((a, b) => { return(a.Value.zoom.CompareTo(b.Value.zoom)); });

            int textureload = 0;

            foreach (var tidict in texlist)
            {
                if (!tidict.Value.textureReady)
                {
                    if (textureload < 1)
                    {
                        textureload++;
                    }
                    else
                    {
                        continue;
                    }
                }

                long xr = tidict.Key.X * prj.TileSize.Width;
                long yr = tidict.Key.Y * prj.TileSize.Width;

                long x2 = (tidict.Key.X + 1) * prj.TileSize.Width;
                long y2 = (tidict.Key.Y + 1) * prj.TileSize.Width;

                GL.Clear(ClearBufferMask.DepthBufferBit);

                GL.Enable(EnableCap.DepthTest);

                if (tidict.Value.texture.Count != 0)
                {
                    tidict.Value.Draw();
                }

                GL.Disable(EnableCap.Texture2D);
                GL.BindTexture(TextureTarget.Texture2D, 0);

                {
                    if (GCSViews.FlightPlanner.instance.pointlist.Count > 1)
                    {
                        GL.Color3(Color.Red);

                        GL.LineWidth(3);

                        // render wps
                        GL.Begin(PrimitiveType.LineStrip);

                        foreach (var point in GCSViews.FlightPlanner.instance.pointlist)
                        {
                            if (point == null)
                            {
                                continue;
                            }
                            var co = convertCoords(point);
                            GL.Vertex3(co[0], co[1], co[2]);
                        }

                        GL.End();
                    }

                    if (green == 0)
                    {
                        green = generateTexture(GMap.NET.Drawing.Properties.Resources.green.ToBitmap());
                    }

                    GL.Enable(EnableCap.DepthTest);
                    GL.DepthMask(false);
                    GL.Enable(EnableCap.Blend);
                    GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
                    GL.Enable(EnableCap.Texture2D);
                    GL.BindTexture(TextureTarget.Texture2D, green);
                    var list = GCSViews.FlightPlanner.instance.pointlist.ToList();
                    if (MainV2.comPort.MAV.cs.TargetLocation != PointLatLngAlt.Zero)
                    {
                        list.Add(MainV2.comPort.MAV.cs.TargetLocation);
                    }
                    foreach (var point in list)
                    {
                        if (point == null)
                        {
                            continue;
                        }
                        var co = convertCoords(point);
                        GL.Begin(PrimitiveType.TriangleStrip);

                        GL.Color3(Color.Red); //tr
                        GL.TexCoord2(0, 0);
                        GL.Vertex3(Math.Sin(MathHelper.Radians(rpy.Z + 90)) * 2 + co[0], Math.Cos(MathHelper.Radians(rpy.Z + 90)) * 2 + co[1], co[2] + 10);
                        GL.Color3(Color.Green); //tl
                        GL.TexCoord2(1, 0);
                        GL.Vertex3(co[0] - Math.Sin(MathHelper.Radians(rpy.Z + 90)) * 2, co[1] - Math.Cos(MathHelper.Radians(rpy.Z + 90)) * 2, co[2] + 10);
                        GL.Color3(Color.Blue); // br
                        GL.TexCoord2(0, 1);
                        GL.Vertex3(co[0] + Math.Sin(MathHelper.Radians(rpy.Z + 90)) * 2, co[1] + Math.Cos(MathHelper.Radians(rpy.Z + 90)) * 2, co[2] - 1);
                        GL.Color3(Color.Yellow); // bl
                        GL.TexCoord2(1, 1);
                        GL.Vertex3(co[0] - Math.Sin(MathHelper.Radians(rpy.Z + 90)) * 2, co[1] - Math.Cos(MathHelper.Radians(rpy.Z + 90)) * 2, co[2] - 1);

                        GL.End();
                    }
                    GL.Disable(EnableCap.Blend);
                    GL.DepthMask(true);


                    /*
                     * WPs.ForEach(a =>
                     * {
                     *  var co = convertCoords(new PointLatLngAlt(a.lat, a.lng, a.alt));
                     *  GL.Vertex3(co[0], co[1], co[2]);
                     * });*/
                }
            }

            try
            {
                this.SwapBuffers();


                //Context.MakeCurrent(null);
            }
            catch
            {
            }

            //this.Invalidate();

            var delta = DateTime.Now - start;

            Console.Write("OpenGLTest2 {0}\r", delta.TotalMilliseconds);
        }
コード例 #2
0
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            DateTime start = DateTime.Now;

            if (this.DesignMode)
            {
                return;
            }

            if (area.LocationMiddle.Lat == 0 && area.LocationMiddle.Lng == 0)
            {
                return;
            }

            try
            {
                base.OnPaint(e);
            }
            catch
            {
                return;
            }

            utmzone = center.GetUTMZone();

            double heightscale = 1;//(step/90.0)*5;

            var campos = convertCoords(center);

            cameraX = campos[0];
            cameraY = campos[1];
            cameraZ = (campos[2] < srtm.getAltitude(center.Lat, center.Lng).alt)
                ? (srtm.getAltitude(center.Lat, center.Lng).alt + 1) * heightscale
                : center.Alt * heightscale; // (srtm.getAltitude(lookZ, lookX, 20) + 100) * heighscale;

            lookX = campos[0] + Math.Sin(MathHelper.Radians(rpy.Z)) * 100;
            lookY = campos[1] + Math.Cos(MathHelper.Radians(rpy.Z)) * 100;
            lookZ = cameraZ;

            var size = 20000;

            // in front
            PointLatLngAlt front = center.newpos(rpy.Z, size);
            // behind
            PointLatLngAlt behind = center.newpos(rpy.Z, -50);
            // left : 90 allows for 180 degree viewing angle
            PointLatLngAlt left = center.newpos(rpy.Z - 45, size);
            // right
            PointLatLngAlt right = center.newpos(rpy.Z + 45, size);

            double maxlat = Math.Max(left.Lat, Math.Max(right.Lat, Math.Max(front.Lat, behind.Lat)));
            double minlat = Math.Min(left.Lat, Math.Min(right.Lat, Math.Min(front.Lat, behind.Lat)));

            double maxlng = Math.Max(left.Lng, Math.Max(right.Lng, Math.Max(front.Lng, behind.Lng)));
            double minlng = Math.Min(left.Lng, Math.Min(right.Lng, Math.Min(front.Lng, behind.Lng)));

            area = RectLatLng.FromLTRB(minlng, maxlat, maxlng, minlat);

            zoom = 20;


            float screenscale = 1;//this.Width/(float) this.Height*1f;

            //if(!Context.IsCurrent)
            MakeCurrent();

            GL.MatrixMode(MatrixMode.Projection);

            OpenTK.Matrix4 projection = OpenTK.Matrix4.CreatePerspectiveFieldOfView((float)(90 * MathHelper.deg2rad), screenscale, 0.00000001f,
                                                                                    (float)20000);
            GL.LoadMatrix(ref projection);

            Console.WriteLine("cam: {0} {1} {2} lookat: {3} {4} {5}", (float)cameraX, (float)cameraY, (float)cameraZ,
                              (float)lookX,
                              (float)lookY, (float)lookZ);

            Matrix4 modelview = Matrix4.LookAt((float)cameraX, (float)cameraY, (float)cameraZ + 100f * 0, (float)lookX,
                                               (float)lookY, (float)lookZ, 0, 0, 1);

            GL.MatrixMode(MatrixMode.Modelview);

            // roll
            modelview = Matrix4.Mult(modelview, Matrix4.CreateRotationZ((float)(rpy.X * MathHelper.deg2rad)));
            // pitch
            modelview = Matrix4.Mult(modelview, Matrix4.CreateRotationX((float)(rpy.Y * -MathHelper.deg2rad)));

            GL.LoadMatrix(ref modelview);

            GL.ClearColor(Color.CornflowerBlue);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.AccumBufferBit);

            GL.LightModel(LightModelParameter.LightModelAmbient, new float[] { 1f, 1f, 1f, 1f });

            GL.Disable(EnableCap.Fog);
            GL.Enable(EnableCap.Fog);
            //GL.Enable(EnableCap.Lighting);
            //GL.Enable(EnableCap.Light0);

            GL.Fog(FogParameter.FogColor, new float[] { 100 / 255.0f, 149 / 255.0f, 237 / 255.0f, 1f });
            //GL.Fog(FogParameter.FogDensity,0.1f);
            GL.Fog(FogParameter.FogMode, (int)FogMode.Linear);
            GL.Fog(FogParameter.FogStart, (float)4000);
            GL.Fog(FogParameter.FogEnd, (float)size);

            GL.Disable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Always);

            /*
             * GL.Begin(BeginMode.LineStrip);
             *
             * GL.Color3(Color.White);
             * GL.Vertex3(0, 0, 0);
             *
             * GL.Color3(Color.Red);
             * GL.Vertex3(area.Bottom, 0, area.Left);
             *
             * GL.Color3(Color.Yellow);
             * GL.Vertex3(lookX, lookY, lookZ);
             *
             * GL.Color3(Color.Green);
             * GL.Vertex3(cameraX, cameraY, cameraZ);
             *
             * GL.End();
             */
            /*
             * GL.PointSize(10);
             * GL.Color4(Color.Yellow);
             * GL.LineWidth(5);
             *
             *
             * GL.Begin(PrimitiveType.LineStrip);
             *
             * GL.Vertex3(area.LocationTopLeft.Lng, area.LocationTopLeft.Lat, (float)cameraZ);
             * GL.Vertex3(area.LocationTopLeft.Lng, area.LocationRightBottom.Lat, (float)cameraZ);
             * GL.Vertex3(area.LocationRightBottom.Lng, area.LocationRightBottom.Lat, (float)cameraZ);
             * GL.Vertex3(area.LocationRightBottom.Lng, area.LocationTopLeft.Lat, (float)cameraZ);
             * GL.Vertex3(area.LocationTopLeft.Lng, area.LocationTopLeft.Lat, (float)cameraZ);
             *
             * GL.End();
             *
             * GL.PointSize((float) (step*1));
             * GL.Color3(Color.Blue);
             * GL.Begin(PrimitiveType.Points);
             * GL.Vertex3(new Vector3((float) center.Lng, (float) center.Lat, (float) cameraZ));
             * GL.End();
             */

            //GL.ClampColor(ClampColorTarget.ClampReadColor, ClampColorMode.True);

            /*
             * GL.Enable(EnableCap.Blend);
             * GL.DepthMask(false);
             * GL.BlendFunc(BlendingFactorSrc.Zero, BlendingFactorDest.Src1Color);
             * GL.DepthMask(true);
             * GL.Disable(EnableCap.Blend);
             */
            // textureid.Clear();

            core.fillEmptyTiles = true;

            core.LevelsKeepInMemmory = 20;

            core.Provider = type;
            core.Position = center;

            //core.ReloadMap();

            List <tileZoomArea> tileArea = new List <tileZoomArea>();

            for (int a = 10; a <= zoom; a++)
            {
                core.Zoom = a;

                var area2 = new RectLatLng(center.Lat, center.Lng, 0, 0);

                // 200m at max zoom
                // step at 0 zoom
                var distm = MathHelper.map(a, 0, zoom, size, 50);

                var offset = center.newpos(rpy.Z, distm);

                area2.Inflate(Math.Abs(center.Lat - offset.Lat), Math.Abs(center.Lng - offset.Lng));

                var extratile = 0;

                if (a == zoom)
                {
                    extratile = 1;
                }

                var tiles = new tileZoomArea()
                {
                    zoom = a, points = prj.GetAreaTileList(area2, a, extratile), area = area2
                };

                tileArea.Add(tiles);
            }

            //tileArea.Reverse();

            while (textureid.Count > 250)
            {
                var first = textureid.Keys.First();
                GL.DeleteTexture(textureid[first]);
                textureid.Remove(first);
            }

            // get tiles & combine into one
            foreach (var tilearea in tileArea)
            {
                foreach (var p in tilearea.points)
                {
                    core.tileDrawingListLock.AcquireReaderLock();
                    core.Matrix.EnterReadLock();
                    try
                    {
                        GMap.NET.Internals.Tile t = core.Matrix.GetTileWithNoLock(tilearea.zoom, p);

                        if (t.NotEmpty)
                        {
                            foreach (GMapImage img in t.Overlays)
                            {
                                if (img.IsParent)
                                {
                                }

                                if (!textureid.ContainsKey(p))
                                {
                                    try
                                    {
                                        generateTexture(p, (Bitmap)img.Img);
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                        else
                        {
                        }
                    }
                    finally
                    {
                        core.Matrix.LeaveReadLock();
                        core.tileDrawingListLock.ReleaseReaderLock();
                    }

                    if (textureid.ContainsKey(p))
                    {
                        int texture = textureid[p];

                        GL.Enable(EnableCap.Texture2D);
                        GL.BindTexture(TextureTarget.Texture2D, texture);
                    }
                    else
                    {
                        //Console.WriteLine("Missing tile");
                        GL.Disable(EnableCap.Texture2D);
                        continue;
                    }

                    long xr = p.X * prj.TileSize.Width;
                    long yr = p.Y * prj.TileSize.Width;

                    long x2 = (p.X + 1) * prj.TileSize.Width;
                    long y2 = (p.Y + 1) * prj.TileSize.Width;

                    GL.LineWidth(4);
                    GL.Color3(Color.White);

                    GL.Clear(ClearBufferMask.DepthBufferBit);

                    GL.Enable(EnableCap.DepthTest);

                    // generate terrain
                    GL.Begin(PrimitiveType.Points);

                    GL.PointSize((float)(20));

                    //GL.Begin(PrimitiveType.Points);
                    GL.Color3(Color.Blue);

                    var latlng = prj.FromPixelToLatLng(xr, yr, tilearea.zoom);
                    var utm    = convertCoords(latlng);
                    utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;

                    GL.TexCoord2(0, 0);
                    GL.Vertex3(utm[0], utm[1], utm[2]);

                    // next down
                    latlng = prj.FromPixelToLatLng(xr, y2, tilearea.zoom);
                    utm    = convertCoords(latlng);
                    utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;

                    GL.TexCoord2(0, 1);
                    GL.Vertex3(utm[0], utm[1], utm[2]);

                    // next right
                    latlng = prj.FromPixelToLatLng(x2, yr, tilearea.zoom);
                    utm    = convertCoords(latlng);
                    utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;

                    GL.TexCoord2(1, 0);
                    GL.Vertex3(utm[0], utm[1], utm[2]);

                    // next right down
                    latlng = prj.FromPixelToLatLng(x2, y2, tilearea.zoom);
                    utm    = convertCoords(latlng);
                    utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;

                    GL.TexCoord2(1, 1);
                    GL.Vertex3(utm[0], utm[1], utm[2]);

                    GL.End();

                    var dist = LocationCenter.GetDistance(latlng);

                    var pxstep = 128;

                    if (dist < 500)
                    {
                        pxstep = 32;
                    }

                    double[] oldutm = null;
                    GL.Begin(PrimitiveType.TriangleStrip);
                    for (long x = xr; x < x2; x += pxstep)
                    {
                        long xnext = x + pxstep;
                        //GL.Begin(PrimitiveType.LineStrip);
                        for (long y = yr; y < y2; y += pxstep)
                        {
                            long ynext = y + pxstep;

                            //GL.Begin(PrimitiveType.Lines);
                            var latlng1 = prj.FromPixelToLatLng(x, y, tilearea.zoom);
                            var utm1    = convertCoords(latlng1);
                            utm1[2] = srtm.getAltitude(latlng1.Lat, latlng1.Lng).alt;

                            var imgx = MathHelper.map(x, xr, x2, 0, 1);
                            var imgy = MathHelper.map(y, yr, y2, 0, 1);

                            GL.TexCoord2(imgx, imgy);
                            GL.Vertex3(utm1[0], utm1[1], utm1[2]);

                            //
                            var latlng2 = prj.FromPixelToLatLng(x, ynext, tilearea.zoom);
                            var utm2    = convertCoords(latlng2);
                            utm2[2] = srtm.getAltitude(latlng2.Lat, latlng2.Lng).alt;

                            imgx = MathHelper.map(x, xr, x2, 0, 1);
                            imgy = MathHelper.map(ynext, yr, y2, 0, 1);

                            GL.TexCoord2(imgx, imgy);
                            GL.Vertex3(utm2[0], utm2[1], utm2[2]);

                            //
                            latlng2 = prj.FromPixelToLatLng(xnext, y, tilearea.zoom);
                            utm2    = convertCoords(latlng2);
                            utm2[2] = srtm.getAltitude(latlng2.Lat, latlng2.Lng).alt;

                            imgx = MathHelper.map(xnext, xr, x2, 0, 1);
                            imgy = MathHelper.map(y, yr, y2, 0, 1);

                            GL.TexCoord2(imgx, imgy);
                            GL.Vertex3(utm2[0], utm2[1], utm2[2]);

                            //
                            latlng2 = prj.FromPixelToLatLng(xnext, ynext, tilearea.zoom);
                            utm2    = convertCoords(latlng2);
                            utm2[2] = srtm.getAltitude(latlng2.Lat, latlng2.Lng).alt;

                            imgx = MathHelper.map(xnext, xr, x2, 0, 1);
                            imgy = MathHelper.map(ynext, yr, y2, 0, 1);

                            GL.TexCoord2(imgx, imgy);
                            GL.Vertex3(utm2[0], utm2[1], utm2[2]);
                        }
                    }

                    GL.End();
                    GL.Disable(EnableCap.Texture2D);
                }
            }

            GL.Flush();

            try
            {
                this.SwapBuffers();


                Context.MakeCurrent(null);
            }
            catch
            {
            }

            //this.Invalidate();

            var delta = DateTime.Now - start;

            Console.WriteLine("OpenGLTest2 {0}", delta.TotalMilliseconds);
        }
コード例 #3
0
        public void doPaint()
        {
            DateTime start = DateTime.Now;

            if (this.DesignMode)
            {
                return;
            }

            if (area.LocationMiddle.Lat == 0 && area.LocationMiddle.Lng == 0)
            {
                return;
            }

            core.Position = center;

            double heightscale = 1; //(step/90.0)*5;

            var campos = convertCoords(center);

            cameraX = campos[0];
            cameraY = campos[1];
            cameraZ = (campos[2] < srtm.getAltitude(center.Lat, center.Lng).alt)
                ? (srtm.getAltitude(center.Lat, center.Lng).alt + 1) * heightscale
                : center.Alt * heightscale; // (srtm.getAltitude(lookZ, lookX, 20) + 100) * heighscale;

            lookX = campos[0] + Math.Sin(MathHelper.Radians(rpy.Z)) * 100;
            lookY = campos[1] + Math.Cos(MathHelper.Radians(rpy.Z)) * 100;
            lookZ = cameraZ;

            var size = 5000;

            // in front
            PointLatLngAlt front = center.newpos(rpy.Z, size);
            // behind
            PointLatLngAlt behind = center.newpos(rpy.Z, -50);
            // left : 90 allows for 180 degree viewing angle
            PointLatLngAlt left = center.newpos(rpy.Z - 45, size);
            // right
            PointLatLngAlt right = center.newpos(rpy.Z + 45, size);

            double maxlat = Math.Max(left.Lat, Math.Max(right.Lat, Math.Max(front.Lat, behind.Lat)));
            double minlat = Math.Min(left.Lat, Math.Min(right.Lat, Math.Min(front.Lat, behind.Lat)));

            double maxlng = Math.Max(left.Lng, Math.Max(right.Lng, Math.Max(front.Lng, behind.Lng)));
            double minlng = Math.Min(left.Lng, Math.Min(right.Lng, Math.Min(front.Lng, behind.Lng)));

            area = RectLatLng.FromLTRB(minlng, maxlat, maxlng, minlat);

            float screenscale = 1; //this.Width/(float) this.Height*1f;

            if (!Context.IsCurrent)
            {
                MakeCurrent();
            }

            GL.MatrixMode(MatrixMode.Projection);

            OpenTK.Matrix4 projection = OpenTK.Matrix4.CreatePerspectiveFieldOfView((float)(90 * MathHelper.deg2rad),
                                                                                    (float)Width / Height, 0.1f,
                                                                                    (float)20000);
            GL.LoadMatrix(ref projection);

            Console.WriteLine("cam: {0} {1} {2} lookat: {3} {4} {5}", (float)cameraX, (float)cameraY, (float)cameraZ,
                              (float)lookX,
                              (float)lookY, (float)lookZ);

            Matrix4 modelview = Matrix4.LookAt((float)cameraX, (float)cameraY, (float)cameraZ + 100f * 0,
                                               (float)lookX,
                                               (float)lookY, (float)lookZ, 0, 0, 1);

            GL.MatrixMode(MatrixMode.Modelview);

            // roll
            modelview = Matrix4.Mult(modelview, Matrix4.CreateRotationZ((float)(rpy.X * MathHelper.deg2rad)));
            // pitch
            modelview = Matrix4.Mult(modelview, Matrix4.CreateRotationX((float)(rpy.Y * -MathHelper.deg2rad)));

            GL.LoadMatrix(ref modelview);

            GL.Viewport(0, 0, Width, Height);

            GL.ClearColor(Color.CornflowerBlue);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.AccumBufferBit);

            GL.LightModel(LightModelParameter.LightModelAmbient, new float[] { 1f, 1f, 1f, 1f });

            GL.Disable(EnableCap.Fog);
            GL.Enable(EnableCap.Fog);
            //GL.Enable(EnableCap.Lighting);
            //GL.Enable(EnableCap.Light0);

            GL.Fog(FogParameter.FogColor, new float[] { 100 / 255.0f, 149 / 255.0f, 237 / 255.0f, 1f });
            //GL.Fog(FogParameter.FogDensity,0.1f);
            GL.Fog(FogParameter.FogMode, (int)FogMode.Linear);
            GL.Fog(FogParameter.FogStart, (float)300);
            GL.Fog(FogParameter.FogEnd, (float)2000);

            GL.Disable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Always);

            var texlist = textureid.ToArray().ToSortedList((a, b) => { return(a.Value.zoom.CompareTo(b.Value.zoom)); });

            int textureload = 0;

            foreach (var tidict in texlist)
            {
                if (!tidict.Value.textureReady)
                {
                    if (textureload < 1)
                    {
                        textureload++;
                    }
                    else
                    {
                        continue;
                    }
                }

                long xr = tidict.Key.X * prj.TileSize.Width;
                long yr = tidict.Key.Y * prj.TileSize.Width;

                long x2 = (tidict.Key.X + 1) * prj.TileSize.Width;
                long y2 = (tidict.Key.Y + 1) * prj.TileSize.Width;

                GL.Clear(ClearBufferMask.DepthBufferBit);

                //GL.Enable(EnableCap.Texture2D);
                //GL.BindTexture(TextureTarget.Texture2D, tidict.Value.idtexture);



                GL.Enable(EnableCap.DepthTest);


                /*
                 *
                 *              // generate terrain
                 *              GL.Begin(PrimitiveType.TriangleStrip);
                 *              GL.PointSize((float) (20));
                 *
                 *              //GL.Begin(PrimitiveType.Points);
                 *              GL.Color3(Color.Blue);
                 *
                 *              var latlng = prj.FromPixelToLatLng(xr, yr, tidict.Value.zoom);
                 *              var utm = convertCoords(latlng);
                 *              utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;
                 *
                 *              GL.TexCoord2(0.0, 0.0);
                 *              GL.Vertex3(utm[0], utm[1], utm[2]);
                 *
                 *              // next down
                 *              latlng = prj.FromPixelToLatLng(xr, y2, tidict.Value.zoom);
                 *              utm = convertCoords(latlng);
                 *              utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;
                 *
                 *              GL.TexCoord2(0, 1);
                 *              GL.Vertex3(utm[0], utm[1], utm[2]);
                 *
                 *              // next right
                 *              latlng = prj.FromPixelToLatLng(x2, yr, tidict.Value.zoom);
                 *              utm = convertCoords(latlng);
                 *              utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;
                 *
                 *              GL.TexCoord2(1, 0);
                 *              GL.Vertex3(utm[0], utm[1], utm[2]);
                 *
                 *              // next right down
                 *              latlng = prj.FromPixelToLatLng(x2, y2, tidict.Value.zoom);
                 *              utm = convertCoords(latlng);
                 *              utm[2] = srtm.getAltitude(latlng.Lat, latlng.Lng).alt;
                 *
                 *              GL.TexCoord2((float)1, 1);
                 *              GL.Vertex3(utm[0], utm[1], utm[2]);
                 */
                /*
                 * GL.Begin(PrimitiveType.TriangleStrip);
                 * for (int i = 0; i < tidict.Value.vertex.Count; i++)
                 * {
                 *  GL.TexCoord2((double)tidict.Value.texture[i].X, tidict.Value.texture[i].Y);
                 *  GL.Vertex3((double)tidict.Value.vertex[i].X, tidict.Value.vertex[i].Y, tidict.Value.vertex[i].Z);
                 * }
                 *
                 * GL.End();
                 */


                //var dist = LocationCenter.GetDistance(latlng);



                //if (dist < 500)
                //    pxstep = 32;

                //GL.Begin(PrimitiveType.TriangleStrip);

                if (tidict.Value.texture.Count != 0)
                {
                    tidict.Value.Draw();
                }

                GL.Disable(EnableCap.Texture2D);

                {
                    // render wps
                    GL.Begin(PrimitiveType.LineStrip);

                    GL.Color3(Color.Red);

                    GL.LineWidth(3);

                    foreach (var point in GCSViews.FlightPlanner.instance.pointlist)
                    {
                        if (point == null)
                        {
                            continue;
                        }
                        var co = convertCoords(point);
                        GL.Vertex3(co[0], co[1], co[2]);
                    }

                    /*
                     * WPs.ForEach(a =>
                     * {
                     *  var co = convertCoords(new PointLatLngAlt(a.lat, a.lng, a.alt));
                     *  GL.Vertex3(co[0], co[1], co[2]);
                     * });*/

                    GL.End();
                }
            }

            try
            {
                this.SwapBuffers();


                Context.MakeCurrent(null);
            }
            catch
            {
            }

            //this.Invalidate();

            var delta = DateTime.Now - start;

            Console.WriteLine("OpenGLTest2 {0}", delta.TotalMilliseconds);
        }