Esempio n. 1
0
        private void CreateCamera()
        {
            _xTranslation = FromMeters(Ellipsoid.Wgs84.Radii.X);

            Camera camera = _sceneState.Camera;

            camera.PerspectiveNearPlaneDistance = FromMeters(0.01);
            camera.PerspectiveFarPlaneDistance  = FromMeters(5000000);
            camera.Target = Vector3D.UnitX * _xTranslation;
            camera.Eye    = Vector3D.UnitX * _xTranslation * 1.1;

            if (_camera != null)
            {
                ((IDisposable)_camera).Dispose();
                _camera = null;
            }

            _camera                           = new CameraLookAtPoint(camera, _window, Ellipsoid.UnitSphere);
            _camera.Range                     = (camera.Eye - camera.Target).Magnitude;
            _camera.MinimumZoomRate           = FromMeters(1);
            _camera.MaximumZoomRate           = FromMeters(Double.MaxValue);
            _camera.ZoomFactor                = 10;
            _camera.ZoomRateRangeAdjustment   = 0;
            _camera.MinimumRotateRate         = 1.0;
            _camera.MaximumRotateRate         = 1.0;
            _camera.RotateRateRangeAdjustment = 0.0;
            _camera.RotateFactor              = 0.0;
        }
Esempio n. 2
0
        public GlobeRayCasting()
        {
            Ellipsoid globeShape = Ellipsoid.ScaledWgs84;

            _window              = Device.CreateWindow(800, 600, "Chapter 4:  Globe Ray Casting");
            _window.Resize      += OnResize;
            _window.RenderFrame += OnRenderFrame;
            _sceneState          = new SceneState();
            _camera              = new CameraLookAtPoint(_sceneState.Camera, _window, globeShape);
            _clearState          = new ClearState();

            _window.Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
            {
                if (e.Key == KeyboardKey.P)
                {
                    CenterCameraOnPoint();
                }
                else if (e.Key == KeyboardKey.C)
                {
                    CenterCameraOnGlobeCenter();
                }
            };

            Bitmap bitmap = new Bitmap("NE2_50M_SR_W_4096.jpg");

            _texture = Device.CreateTexture2D(bitmap, TextureFormat.RedGreenBlue8, false);

            _globe         = new RayCastedGlobe(_window.Context);
            _globe.Shape   = globeShape;
            _globe.Texture = _texture;
            _globe.ShowWireframeBoundingBox = true;

            _sceneState.Camera.ZoomToTarget(globeShape.MaximumRadius);
        }
Esempio n. 3
0
        public EllipsoidSurfaceNormals()
        {
            _globeShape = new Ellipsoid(1, 1, _semiMinorAxis);

            _window                   = Device.CreateWindow(800, 600, "Chapter 2:  Ellipsoid Surface Normals");
            _window.Resize           += OnResize;
            _window.RenderFrame      += OnRenderFrame;
            _window.Keyboard.KeyDown += OnKeyDown;
            _sceneState               = new SceneState();
            _camera                   = new CameraLookAtPoint(_sceneState.Camera, _window, _globeShape);

            _instructions         = new HeadsUpDisplay();
            _instructions.Texture = Device.CreateTexture2D(
                Device.CreateBitmapFromText("Up - Increase semi-minor axis\nDown - Decrease semi-minor axis",
                                            new Font("Arial", 24)),
                TextureFormat.RedGreenBlueAlpha8, false);
            _instructions.Color = Color.Black;

            _clearState = new ClearState();

            CreateScene();

            ///////////////////////////////////////////////////////////////////

            _sceneState.Camera.Eye = Vector3D.UnitY;
            _sceneState.Camera.ZoomToTarget(2 * _globeShape.MaximumRadius);
        }
Esempio n. 4
0
        public Multithreading()
        {
            Ellipsoid globeShape = Ellipsoid.ScaledWgs84;

            _workerWindow        = Device.CreateWindow(1, 1);
            _window              = Device.CreateWindow(800, 600, "Chapter 10:  Multithreading");
            _window.Resize      += OnResize;
            _window.RenderFrame += OnRenderFrame;
            _sceneState          = new SceneState();
            _camera              = new CameraLookAtPoint(_sceneState.Camera, _window, globeShape);
            _clearState          = new ClearState();

            Bitmap bitmap = new Bitmap("NE2_50M_SR_W_4096.jpg");

            _texture = Device.CreateTexture2D(bitmap, TextureFormat.RedGreenBlue8, false);

            _globe                 = new RayCastedGlobe(_window.Context);
            _globe.Shape           = globeShape;
            _globe.Texture         = _texture;
            _globe.UseAverageDepth = true;

            ///////////////////////////////////////////////////////////////////

            _doneQueue.MessageReceived += ProcessNewShapefile;

            _requestQueue.MessageReceived += new ShapefileWorker(_workerWindow.Context, globeShape, _doneQueue).Process;

            // 2ND_EDITION:  Draw order
            _requestQueue.Post(new ShapefileRequest("110m_admin_0_countries.shp",
                                                    new ShapefileAppearance()));
            _requestQueue.Post(new ShapefileRequest("110m_admin_1_states_provinces_lines_shp.shp",
                                                    new ShapefileAppearance()));
            _requestQueue.Post(new ShapefileRequest("airprtx020.shp",
                                                    new ShapefileAppearance()
            {
                Bitmap = new Bitmap("paper-plane--arrow.png")
            }));
            _requestQueue.Post(new ShapefileRequest("amtrakx020.shp",
                                                    new ShapefileAppearance()
            {
                Bitmap = new Bitmap("car-red.png")
            }));
            _requestQueue.Post(new ShapefileRequest("110m_populated_places_simple.shp",
                                                    new ShapefileAppearance()
            {
                Bitmap = new Bitmap("032.png")
            }));

#if SINGLE_THREADED
            _requestQueue.ProcessQueue();
#else
            _requestQueue.StartInAnotherThread();
#endif

            ///////////////////////////////////////////////////////////////////

            _sceneState.Camera.ZoomToTarget(globeShape.MaximumRadius);
        }
Esempio n. 5
0
        public DepthBufferPrecision()
        {
            _globeShape          = Ellipsoid.Wgs84;
            _nearDistance        = 1;
            _cubeRootFarDistance = 300;

            _window                       = Device.CreateWindow(800, 600, "Chapter 6:  Depth Buffer Precision");
            _window.Resize               += OnResize;
            _window.RenderFrame          += OnRenderFrame;
            _window.Keyboard.KeyUp       += OnKeyUp;
            _window.Keyboard.KeyDown     += OnKeyDown;
            _sceneState                   = new SceneState();
            _sceneState.DiffuseIntensity  = 0.45f;
            _sceneState.SpecularIntensity = 0.05f;
            _sceneState.AmbientIntensity  = 0.5f;

            _camera = new CameraLookAtPoint(_sceneState.Camera, _window, _globeShape);

            _sceneState.Camera.ZoomToTarget(_globeShape.MaximumRadius);

            ///////////////////////////////////////////////////////////////////

            _globe       = new TessellatedGlobe();
            _globe.Shape = _globeShape;
            _globe.NumberOfSlicePartitions = 64;
            _globe.NumberOfStackPartitions = 32;
            _globe.Texture  = Device.CreateTexture2D(new Bitmap("world_topo_bathy_200411_3x5400x2700.jpg"), TextureFormat.RedGreenBlue8, false);
            _globe.Textured = true;

            _plane               = new Plane(_window.Context);
            _plane.XAxis         = 0.6 * _globeShape.MaximumRadius * Vector3D.UnitX;
            _plane.YAxis         = 0.6 * _globeShape.MinimumRadius * Vector3D.UnitZ;
            _plane.OutlineWidth  = 3;
            _cubeRootPlaneHeight = 100.0;
            UpdatePlaneOrigin();

            _viewportQuad = new ViewportQuad(_window.Context, null);

            _framebuffer              = _window.Context.CreateFramebuffer();
            _depthFormatIndex         = 1;
            _depthTestLess            = true;
            _logarithmicDepthConstant = 1;
            UpdatePlanesAndDepthTests();

            _clearState = new ClearState();

            ///////////////////////////////////////////////////////////////////

            _hudFont   = new Font("Arial", 16);
            _hud       = new HeadsUpDisplay();
            _hud.Color = Color.Blue;
            UpdateHUD();
        }
        public ClipmapTerrainOnGlobe()
        {
            _window = Device.CreateWindow(800, 600, "Chapter 13:  Clipmap Terrain on a Globe");

            _ellipsoid = Ellipsoid.Wgs84;

            WorldWindTerrainSource terrainSource = new WorldWindTerrainSource();
            EsriRestImagery        imagery       = new EsriRestImagery();

            _clipmap = new GlobeClipmapTerrain(_window.Context, terrainSource, imagery, _ellipsoid, 511);
            _clipmap.HeightExaggeration = 1.0f;

            _sceneState = new SceneState();
            _sceneState.DiffuseIntensity    = 0.90f;
            _sceneState.SpecularIntensity   = 0.05f;
            _sceneState.AmbientIntensity    = 0.05f;
            _sceneState.Camera.FieldOfViewY = Math.PI / 3.0;

            _clearState       = new ClearState();
            _clearState.Color = Color.White;

            _sceneState.Camera.PerspectiveNearPlaneDistance = 0.000001 * _ellipsoid.MaximumRadius;
            _sceneState.Camera.PerspectiveFarPlaneDistance  = 10.0 * _ellipsoid.MaximumRadius;
            _sceneState.SunPosition = new Vector3D(200000, 300000, 200000) * _ellipsoid.MaximumRadius;

            _lookCamera       = new CameraLookAtPoint(_sceneState.Camera, _window, _ellipsoid);
            _lookCamera.Range = 1.5 * _ellipsoid.MaximumRadius;

            _globe       = new RayCastedGlobe(_window.Context);
            _globe.Shape = _ellipsoid;
            Bitmap bitmap = new Bitmap("NE2_50M_SR_W_4096.jpg");

            _globe.Texture = Device.CreateTexture2D(bitmap, TextureFormat.RedGreenBlue8, false);

            _clearDepth         = new ClearState();
            _clearDepth.Buffers = ClearBuffers.DepthBuffer | ClearBuffers.StencilBuffer;

            _window.Keyboard.KeyDown += OnKeyDown;

            _window.Resize         += OnResize;
            _window.RenderFrame    += OnRenderFrame;
            _window.PreRenderFrame += OnPreRenderFrame;

            _hudFont   = new Font("Arial", 16);
            _hud       = new HeadsUpDisplay();
            _hud.Color = Color.Blue;
            UpdateHUD();
        }
Esempio n. 7
0
        private void SetupVR(Ellipsoid globeShape)
        {
            // 创建一个默认相机以方便计算SceneState对象在VR模式下创建时需要外部提供的变换矩阵
            Camera   camera            = new Camera();
            Matrix4D perspectiveMatrix = Matrix4D.CreatePerspectiveFieldOfView(camera.FieldOfViewY,
                                                                               camera.AspectRatio, camera.PerspectiveNearPlaneDistance, camera.PerspectiveFarPlaneDistance);
            Matrix4D viewMatrix = Matrix4D.LookAt(camera.Eye, camera.Target, camera.Up);

            _leftEyeSceneState = new SceneState(VREye.LeftEye, perspectiveMatrix, viewMatrix);
            _leftEyeCamera     = new CameraLookAtPoint(_leftEyeSceneState.Camera, _window, globeShape);
            _leftEyeSceneState.HmdPoseMatrix = Matrix4D.Identity;

            _rightEyeSceneState = new SceneState(VREye.RightEye, perspectiveMatrix, viewMatrix);
            _rightEyeCamera     = new CameraLookAtPoint(_rightEyeSceneState.Camera, _window, globeShape);
            _rightEyeSceneState.HmdPoseMatrix = Matrix4D.Identity;
        }
Esempio n. 8
0
        public TerrainShading()
        {
            _window                   = Device.CreateWindow(800, 600, "Chapter 11:  Terrain Shading");
            _window.Resize           += OnResize;
            _window.RenderFrame      += OnRenderFrame;
            _window.Keyboard.KeyDown += OnKeyDown;
            _window.Keyboard.KeyUp   += OnKeyUp;
            _sceneState               = new SceneState();
            _sceneState.Camera.PerspectiveFarPlaneDistance = 4096;
            _sceneState.DiffuseIntensity  = 0.9f;
            _sceneState.SpecularIntensity = 0.05f;
            _sceneState.AmbientIntensity  = 0.05f;
            _clearState = new ClearState();

            ///////////////////////////////////////////////////////////////////

            TerrainTile terrainTile = TerrainTile.FromBitmap(new Bitmap("ps-e.lg.png"));

            _tile = new VertexDisplacementMapTerrainTile(_window.Context, terrainTile);
            _tile.HeightExaggeration     = 30;
            _tile.ColorMapTexture        = Device.CreateTexture2D(new Bitmap("ps_texture_1k.png"), TextureFormat.RedGreenBlue8, false);
            _tile.ColorRampHeightTexture = Device.CreateTexture2D(new Bitmap("ColorRamp.jpg"), TextureFormat.RedGreenBlue8, false);
            _tile.ColorRampSlopeTexture  = Device.CreateTexture2D(new Bitmap("ColorRampSlope.jpg"), TextureFormat.RedGreenBlue8, false);
            _tile.BlendRampTexture       = Device.CreateTexture2D(new Bitmap("BlendRamp.jpg"), TextureFormat.Red8, false);
            _tile.GrassTexture           = Device.CreateTexture2D(new Bitmap("Grass.jpg"), TextureFormat.RedGreenBlue8, false);
            _tile.StoneTexture           = Device.CreateTexture2D(new Bitmap("Stone.jpg"), TextureFormat.RedGreenBlue8, false);
            _tile.BlendMaskTexture       = Device.CreateTexture2D(new Bitmap("BlendMask.jpg"), TextureFormat.Red8, false);

            ///////////////////////////////////////////////////////////////////

            double tileRadius = Math.Max(terrainTile.Resolution.X, terrainTile.Resolution.Y) * 0.5;

            _camera                           = new CameraLookAtPoint(_sceneState.Camera, _window, Ellipsoid.UnitSphere);
            _camera.CenterPoint               = new Vector3D(terrainTile.Resolution.X * 0.5, terrainTile.Resolution.Y * 0.5, 0.0);
            _camera.MinimumRotateRate         = 1.0;
            _camera.MaximumRotateRate         = 1.0;
            _camera.RotateRateRangeAdjustment = 0.0;
            _camera.RotateFactor              = 0.0;
            _sceneState.Camera.ZoomToTarget(tileRadius);

            ///////////////////////////////////////////////////////////////////

            _hudFont   = new Font("Arial", 16);
            _hud       = new HeadsUpDisplay();
            _hud.Color = Color.Black;
            UpdateHUD();
        }
Esempio n. 9
0
        public Curves()
        {
            _semiMinorAxis = Ellipsoid.ScaledWgs84.Radii.Z;
            SetShape();

            _window                   = Device.CreateWindow(800, 600, "Chapter 2:  Curves");
            _window.Resize           += OnResize;
            _window.RenderFrame      += OnRenderFrame;
            _window.Keyboard.KeyDown += OnKeyDown;
            _sceneState               = new SceneState();
            _camera                   = new CameraLookAtPoint(_sceneState.Camera, _window, _globeShape);

            _clearState = new ClearState();

            _texture = Device.CreateTexture2D(new Texture2DDescription(1, 1, TextureFormat.RedGreenBlue8));
            WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, 3);

            pixelBuffer.CopyFromSystemMemory(new byte[] { 0, 255, 127 });
            _texture.CopyFromBuffer(pixelBuffer, ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte, 1);

            _instructions       = new HeadsUpDisplay();
            _instructions.Color = Color.Black;

            _sampledPoints                  = new BillboardCollection(_window.Context);
            _sampledPoints.Texture          = Device.CreateTexture2D(Device.CreateBitmapFromPoint(8), TextureFormat.RedGreenBlueAlpha8, false);
            _sampledPoints.DepthTestEnabled = false;

            _ellipsoid         = new RayCastedGlobe(_window.Context);
            _ellipsoid.Texture = _texture;

            _polyline                  = new Polyline();
            _polyline.Width            = 3;
            _polyline.DepthTestEnabled = false;

            _plane              = new Plane(_window.Context);
            _plane.Origin       = Vector3D.Zero;
            _plane.OutlineWidth = 3;

            CreateScene();

            ///////////////////////////////////////////////////////////////////

            _sceneState.Camera.Eye = Vector3D.UnitY;
            _sceneState.Camera.ZoomToTarget(2 * _globeShape.MaximumRadius);
        }
Esempio n. 10
0
        public TerrainRayCasting()
        {
            _window                   = Device.CreateWindow(800, 600, "Chapter 11:  Terrain Ray Casting");
            _window.Resize           += OnResize;
            _window.RenderFrame      += OnRenderFrame;
            _window.Keyboard.KeyDown += OnKeyDown;
            _sceneState               = new SceneState();
            _sceneState.Camera.PerspectiveFarPlaneDistance = 4096;
            _clearState = new ClearState();

            ///////////////////////////////////////////////////////////////////

            // 创建地形高程对象
            TerrainTile terrainTile = TerrainTile.FromBitmap(new Bitmap(@"ps-e.lg.png"));

            // 创建地形图形可渲染对象
            _tile = new RayCastedTerrainTile(terrainTile);
            // 地形高度缩放因子(可以使高山更高)
            _tile.HeightExaggeration = 30;

            ///////////////////////////////////////////////////////////////////

            // 根据地形设置相机位置
            double tileRadius = Math.Max(terrainTile.Resolution.X, terrainTile.Resolution.Y) * 0.5;

            _camera                           = new CameraLookAtPoint(_sceneState.Camera, _window, Ellipsoid.UnitSphere);
            _camera.CenterPoint               = new Vector3D(terrainTile.Resolution.X * 0.5, terrainTile.Resolution.Y * 0.5, 0.0);
            _camera.MinimumRotateRate         = 1.0;
            _camera.MaximumRotateRate         = 1.0;
            _camera.RotateRateRangeAdjustment = 0.0;
            _camera.RotateFactor              = 0.0;
            _sceneState.Camera.ZoomToTarget(tileRadius);

            ///////////////////////////////////////////////////////////////////

            _hudFont   = new Font("Arial", 16);
            _hud       = new HeadsUpDisplay();
            _hud.Color = Color.Black;
            UpdateHUD();
        }
Esempio n. 11
0
        public Curves()
        {
            _semiMinorAxis = Ellipsoid.ScaledWgs84.Radii.Z;
            // 根据椭球对象的尺寸计算表面设定点的三维坐标
            SetShape();

            // 创建主窗口
            _window = Device.CreateWindow(800, 600, "Chapter 2:  Curves");
            // 设置主窗口回调函数
            _window.Resize           += OnResize;
            _window.RenderFrame      += OnRenderFrame;
            _window.Keyboard.KeyDown += OnKeyDown;

            // 创建主场景OpenGL状态存储对象
            _sceneState = new SceneState();

            // 设置主场景照相机类型(固定观察一点的照相机)
            _camera = new CameraLookAtPoint(_sceneState.Camera, _window, _globeShape);

            // 创建状态(scissor/color/depth/stencil)清除对象
            _clearState = new ClearState();

            // 创建单色纹理对象(用于对椭球着色)
            // 因为RayCastedGlobe对象的颜色只能通过其Texture接口设置
            _texture = Device.CreateTexture2D(new Texture2DDescription(1, 1, TextureFormat.RedGreenBlue8));
            WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, 3);

            pixelBuffer.CopyFromSystemMemory(new byte[] { 0, 255, 127 });
            _texture.CopyFromBuffer(pixelBuffer, ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte, 1);

            // 创建窗口表面的信息显示对象
            _instructions = new HeadsUpDisplay();
            // 设置信息显示前景色
            _instructions.Color = Color.Black;

            // 创建椭球表面采样点billboard显示集合对象
            _sampledPoints = new BillboardCollection(_window.Context);
            // 创建并设置billboard显示纹理为直径8个窗口像素的点
            _sampledPoints.Texture = Device.CreateTexture2D(Device.CreateBitmapFromPoint(8), TextureFormat.RedGreenBlueAlpha8, false);
            // 关闭采样点billboard集合显示对象的深度测试(使其在任何角度都可见)
            _sampledPoints.DepthTestEnabled = false;

            // 创建RayCastedGlobe椭球绘制对象并设置纹理
            _ellipsoid         = new RayCastedGlobe(_window.Context);
            _ellipsoid.Texture = _texture;

            // 创建椭球表面曲线显示对象
            _polyline = new Polyline();
            // 设置线宽并关闭深度测试
            _polyline.Width            = 3;
            _polyline.DepthTestEnabled = false;

            // 创建参考面显示对象
            _plane              = new Plane(_window.Context);
            _plane.Origin       = Vector3D.Zero;
            _plane.OutlineWidth = 3;

            // 创建主场景
            CreateScene();

            ///////////////////////////////////////////////////////////////////

            // 设置主照相机观察方向
            _sceneState.Camera.Eye = Vector3D.UnitY;
            // 根据视场重新调整相机位置
            _sceneState.Camera.ZoomToTarget(2 * _globeShape.MaximumRadius);
        }
Esempio n. 12
0
        public SubdivisionSphere1()
        {
            _window              = Device.CreateWindow(800, 600, "Chapter 4:  Subdivision Sphere 1");
            _window.Resize      += OnResize;
            _window.RenderFrame += OnRenderFrame;
            _sceneState          = new SceneState();
            _camera              = new CameraLookAtPoint(_sceneState.Camera, _window, Ellipsoid.UnitSphere);
            _clearState          = new ClearState();

            string vs =
                @"#version 330

                  layout(location = og_positionVertexLocation) in vec4 position;
                  out vec3 worldPosition;
                  out vec3 positionToLight;
                  out vec3 positionToEye;

                  uniform mat4 og_modelViewPerspectiveMatrix;
                  uniform vec3 og_cameraEye;
                  uniform vec3 og_cameraLightPosition;

                  void main()                     
                  {
                        gl_Position = og_modelViewPerspectiveMatrix * position; 

                        worldPosition = position.xyz;
                        positionToLight = og_cameraLightPosition - worldPosition;
                        positionToEye = og_cameraEye - worldPosition;
                  }";

            string fs =
                @"#version 330
                 
                  in vec3 worldPosition;
                  in vec3 positionToLight;
                  in vec3 positionToEye;
                  out vec3 fragmentColor;

                  uniform vec4 og_diffuseSpecularAmbientShininess;
                  uniform sampler2D og_texture0;

                  float LightIntensity(vec3 normal, vec3 toLight, vec3 toEye, vec4 diffuseSpecularAmbientShininess)
                  {
                      vec3 toReflectedLight = reflect(-toLight, normal);

                      float diffuse = max(dot(toLight, normal), 0.0);
                      float specular = max(dot(toReflectedLight, toEye), 0.0);
                      specular = pow(specular, diffuseSpecularAmbientShininess.w);

                      return (diffuseSpecularAmbientShininess.x * diffuse) +
                             (diffuseSpecularAmbientShininess.y * specular) +
                              diffuseSpecularAmbientShininess.z;
                  }

                  vec2 ComputeTextureCoordinates(vec3 normal)
                  {
                      return vec2(atan(normal.y, normal.x) * og_oneOverTwoPi + 0.5, asin(normal.z) * og_oneOverPi + 0.5);
                  }

                  void main()
                  {
                      vec3 normal = normalize(worldPosition);
                      float intensity = LightIntensity(normal,  normalize(positionToLight), normalize(positionToEye), og_diffuseSpecularAmbientShininess);
                      fragmentColor = intensity * texture(og_texture0, ComputeTextureCoordinates(normal)).rgb;
                  }";
            ShaderProgram sp = Device.CreateShaderProgram(vs, fs);

            ///////////////////////////////////////////////////////////////////

            Mesh        mesh = SubdivisionSphereTessellatorSimple.Compute(5);
            VertexArray va   = _window.Context.CreateVertexArray(mesh, sp.VertexAttributes, BufferHint.StaticDraw);

            _primitiveType = mesh.PrimitiveType;

            ///////////////////////////////////////////////////////////////////

            RenderState renderState = new RenderState();

            //_renderState.RasterizationMode = RasterizationMode.Line;
            renderState.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder;

            _drawState = new DrawState(renderState, sp, va);

            ///////////////////////////////////////////////////////////////////

            Bitmap bitmap = new Bitmap("NE2_50M_SR_W_4096.jpg");

            //Bitmap bitmap = new Bitmap("world_topo_bathy_200411_3x5400x2700.jpg");
            //Bitmap bitmap = new Bitmap("world.topo.200412.3x5400x2700.jpg");
            _texture = Device.CreateTexture2D(bitmap, TextureFormat.RedGreenBlue8, false);

            _sceneState.Camera.ZoomToTarget(1);
        }
Esempio n. 13
0
        public NightLights()
        {
            _window              = Device.CreateWindow(800, 600, "Chapter 4:  Night Lights");
            _window.Resize      += OnResize;
            _window.RenderFrame += OnRenderFrame;
            _sceneState          = new SceneState();
            _camera              = new CameraLookAtPoint(_sceneState.Camera, _window, Ellipsoid.ScaledWgs84);
            _clearState          = new ClearState();

            string vs =
                @"#version 330

                  layout(location = og_positionVertexLocation) in vec4 position;
                  out vec3 worldPosition;
                  out vec3 positionToLight;
                  out vec3 positionToEye;

                  uniform mat4 og_modelViewPerspectiveMatrix;
                  uniform vec3 og_cameraEye;
                  uniform vec3 og_sunPosition;

                  void main()                     
                  {
                        gl_Position = og_modelViewPerspectiveMatrix * position; 

                        worldPosition = position.xyz;
                        positionToLight = og_sunPosition - worldPosition;
                        positionToEye = og_cameraEye - worldPosition;
                  }";

            string fs =
                @"#version 330
                 
                  in vec3 worldPosition;
                  in vec3 positionToLight;
                  in vec3 positionToEye;
                  out vec3 fragmentColor;

                  uniform vec4 og_diffuseSpecularAmbientShininess;
                  uniform sampler2D og_texture0;                    // Day
                  uniform sampler2D og_texture1;                    // Night

                  uniform float u_blendDuration;
                  uniform float u_blendDurationScale;

                  float LightIntensity(vec3 normal, vec3 toLight, vec3 toEye, float diffuseDot, vec4 diffuseSpecularAmbientShininess)
                  {
                      vec3 toReflectedLight = reflect(-toLight, normal);

                      float diffuse = max(diffuseDot, 0.0);
                      float specular = max(dot(toReflectedLight, toEye), 0.0);
                      specular = pow(specular, diffuseSpecularAmbientShininess.w);

                      return (diffuseSpecularAmbientShininess.x * diffuse) +
                             (diffuseSpecularAmbientShininess.y * specular) +
                              diffuseSpecularAmbientShininess.z;
                  }

                  vec2 ComputeTextureCoordinates(vec3 normal)
                  {
                      return vec2(atan(normal.y, normal.x) * og_oneOverTwoPi + 0.5, asin(normal.z) * og_oneOverPi + 0.5);
                  }

                  vec3 NightColor(vec3 normal)
                  {
                      return texture(og_texture1, ComputeTextureCoordinates(normal)).rgb;
                  }

                  vec3 DayColor(vec3 normal, vec3 toLight, vec3 toEye, float diffuseDot, vec4 diffuseSpecularAmbientShininess)
                  {
                      float intensity = LightIntensity(normal, toLight, toEye, diffuseDot, diffuseSpecularAmbientShininess);
                      return intensity * texture(og_texture0, ComputeTextureCoordinates(normal)).rgb;
                  }

                  void main()
                  {
                      vec3 normal = normalize(worldPosition);
                      vec3 toLight = normalize(positionToLight);
                      float diffuse = dot(toLight, normal);

                      if (diffuse > u_blendDuration)
                      {
                          fragmentColor = DayColor(normal, toLight, normalize(positionToEye), diffuse, og_diffuseSpecularAmbientShininess);
                      }
                      else if (diffuse < -u_blendDuration)
                      {
                          fragmentColor = NightColor(normal);
                      }
                      else
                      {
                          vec3 night = NightColor(normal);
                          vec3 day = DayColor(normal, toLight, normalize(positionToEye), diffuse, og_diffuseSpecularAmbientShininess);
                          fragmentColor = mix(night, day, (diffuse + u_blendDuration) * u_blendDurationScale);
                      }
                  }";
            ShaderProgram sp = Device.CreateShaderProgram(vs, fs);

            float blendDurationScale = 0.1f;

            ((Uniform <float>)sp.Uniforms["u_blendDuration"]).Value      = blendDurationScale;
            ((Uniform <float>)sp.Uniforms["u_blendDurationScale"]).Value = 1 / (2 * blendDurationScale);

            Mesh        mesh = SubdivisionEllipsoidTessellator.Compute(Ellipsoid.ScaledWgs84, 5, SubdivisionEllipsoidVertexAttributes.Position);
            VertexArray va   = _window.Context.CreateVertexArray(mesh, sp.VertexAttributes, BufferHint.StaticDraw);

            _primitiveType = mesh.PrimitiveType;

            RenderState renderState = new RenderState();

            renderState.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder;

            _drawState = new DrawState(renderState, sp, va);

            Bitmap dayBitmap = new Bitmap("world.topo.200412.3x5400x2700.jpg");

            _dayTexture = Device.CreateTexture2D(dayBitmap, TextureFormat.RedGreenBlue8, false);
            Bitmap nightBitmap = new Bitmap("land_ocean_ice_lights_2048.jpg");

            _nightTexture = Device.CreateTexture2D(nightBitmap, TextureFormat.RedGreenBlue8, false);

            _sceneState.DiffuseIntensity  = 0.5f;
            _sceneState.SpecularIntensity = 0.15f;
            _sceneState.AmbientIntensity  = 0.35f;
            _sceneState.Camera.ZoomToTarget(1);
        }
Esempio n. 14
0
        public VectorData()
        {
            Ellipsoid globeShape = Ellipsoid.ScaledWgs84;

            _window                   = Device.CreateWindow(800, 600, "Chapter 7:  Vector Data");
            _window.Resize           += OnResize;
            _window.RenderFrame      += OnRenderFrame;
            _window.Keyboard.KeyDown += OnKeyDown;
            _window.Keyboard.KeyUp   += OnKeyUp;
            _sceneState               = new SceneState();
            _camera                   = new CameraLookAtPoint(_sceneState.Camera, _window, globeShape);

            Context context = _window.Context;

            // 创建帧缓冲对象
            _framebuffer = context.CreateFramebuffer();

            _clearBlack       = new ClearState();
            _clearBlack.Color = Color.Black;

            _clearWhite       = new ClearState();
            _clearWhite.Color = Color.White;

            // 创建视口矩形
            _quad = new DayNightViewportQuad(context);

            _globe                 = new DayNightGlobe(context);
            _globe.Shape           = globeShape;
            _globe.UseAverageDepth = true;
            _globe.DayTexture      = Device.CreateTexture2D(new Bitmap("NE2_50M_SR_W_4096.jpg"), TextureFormat.RedGreenBlue8, false);
            _globe.NightTexture    = Device.CreateTexture2D(new Bitmap("land_ocean_ice_lights_2048.jpg"), TextureFormat.RedGreenBlue8, false);

            _countries = new ShapefileRenderer("110m_admin_0_countries.shp", context, globeShape,
                                               new ShapefileAppearance()
            {
                PolylineWidth        = 1.0,
                PolylineOutlineWidth = 1.0
            });
            _states = new ShapefileRenderer("110m_admin_1_states_provinces_lines_shp.shp", context, globeShape,
                                            new ShapefileAppearance()
            {
                PolylineWidth        = 1.0,
                PolylineOutlineWidth = 1.0
            });
            _rivers = new ShapefileRenderer("50m-rivers-lake-centerlines.shp", context, globeShape,
                                            new ShapefileAppearance()
            {
                PolylineColor        = Color.LightBlue,
                PolylineOutlineColor = Color.LightBlue,
                PolylineWidth        = 1.0,
                PolylineOutlineWidth = 0.0
            });

            _populatedPlaces = new ShapefileRenderer("110m_populated_places_simple.shp", context, globeShape,
                                                     new ShapefileAppearance()
            {
                Bitmap = new Bitmap("032.png")
            });
            _airports = new ShapefileRenderer("airprtx020.shp", context, globeShape,
                                              new ShapefileAppearance()
            {
                Bitmap = new Bitmap("car-red.png")
            });
            _amtrakStations = new ShapefileRenderer("amtrakx020.shp", context, globeShape,
                                                    new ShapefileAppearance()
            {
                Bitmap = new Bitmap("paper-plane--arrow.png")
            });

            _hudFont   = new Font("Arial", 16);
            _hud       = new HeadsUpDisplay();
            _hud.Color = Color.Blue;

            _showVectorData = true;

            _sceneState.DiffuseIntensity  = 0.5f;
            _sceneState.SpecularIntensity = 0.1f;
            _sceneState.AmbientIntensity  = 0.4f;
            _sceneState.Camera.ZoomToTarget(globeShape.MaximumRadius);

            UpdateHUD();
        }
Esempio n. 15
0
        public SubdivisionSphere2()
        {
            _window              = Device.CreateWindow(800, 600, "Chapter 4:  Subdivision Sphere 2");
            _window.Resize      += OnResize;
            _window.RenderFrame += OnRenderFrame;
            _sceneState          = new SceneState();
            _camera              = new CameraLookAtPoint(_sceneState.Camera, _window, Ellipsoid.UnitSphere);
            _clearState          = new ClearState();

            string vs =
                @"#version 330

                  layout(location = og_positionVertexLocation) in vec4 position;
                  layout(location = og_normalVertexLocation) in vec3 normal;
                  layout(location = og_textureCoordinateVertexLocation) in vec2 textureCoordinate;

                  out vec3 positionToLight;
                  out vec3 positionToEye;
                  out vec3 surfaceNormal;
                  out vec2 surfaceTextureCoordinate;

                  uniform mat4 og_modelViewPerspectiveMatrix;
                  uniform vec3 og_cameraEye;
                  uniform vec3 og_cameraLightPosition;

                  void main()                     
                  {
                        gl_Position = og_modelViewPerspectiveMatrix * position; 

                        positionToLight = og_cameraLightPosition - position.xyz;
                        positionToEye = og_cameraEye - position.xyz;

                        surfaceNormal = normal;
                        surfaceTextureCoordinate = textureCoordinate;
                  }";
            string fs =
                @"#version 330
                 
                  in vec3 positionToLight;
                  in vec3 positionToEye;
                  in vec3 surfaceNormal;
                  in vec2 surfaceTextureCoordinate;

                  out vec3 fragmentColor;

                  uniform vec4 og_diffuseSpecularAmbientShininess;
                  uniform sampler2D og_texture0;

                  float LightIntensity(vec3 normal, vec3 toLight, vec3 toEye, vec4 diffuseSpecularAmbientShininess)
                  {
                      vec3 toReflectedLight = reflect(-toLight, normal);

                      float diffuse = max(dot(toLight, normal), 0.0);
                      float specular = max(dot(toReflectedLight, toEye), 0.0);
                      specular = pow(specular, diffuseSpecularAmbientShininess.w);

                      return (diffuseSpecularAmbientShininess.x * diffuse) +
                             (diffuseSpecularAmbientShininess.y * specular) +
                              diffuseSpecularAmbientShininess.z;
                  }

                  void main()
                  {
                      vec3 normal = normalize(surfaceNormal);
                      float intensity = LightIntensity(normal,  normalize(positionToLight), normalize(positionToEye), og_diffuseSpecularAmbientShininess);
                      fragmentColor = intensity * texture(og_texture0, surfaceTextureCoordinate).rgb;
                  }";
            ShaderProgram sp = Device.CreateShaderProgram(vs, fs);

            Mesh        mesh = SubdivisionSphereTessellator.Compute(5, SubdivisionSphereVertexAttributes.All);
            VertexArray va   = _window.Context.CreateVertexArray(mesh, sp.VertexAttributes, BufferHint.StaticDraw);

            _primitiveType = mesh.PrimitiveType;

            RenderState renderState = new RenderState();

            renderState.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder;

            _drawState = new DrawState(renderState, sp, va);

            Bitmap bitmap = new Bitmap("NE2_50M_SR_W_4096.jpg");

            _texture = Device.CreateTexture2D(bitmap, TextureFormat.RedGreenBlue8, false);
        }
 private void OnKeyDown(object sender, KeyboardKeyEventArgs e)
 {
     if (e.Key == KeyboardKey.U)
     {
         _sceneState.SunPosition = _sceneState.Camera.Eye;
     }
     else if (e.Key == KeyboardKey.W)
     {
         _clipmap.Wireframe = !_clipmap.Wireframe;
         UpdateHUD();
     }
     else if (e.Key == KeyboardKey.B)
     {
         if (!_clipmap.BlendRegionsEnabled)
         {
             _clipmap.BlendRegionsEnabled = true;
             _clipmap.ShowBlendRegions    = false;
         }
         else if (_clipmap.ShowBlendRegions)
         {
             _clipmap.BlendRegionsEnabled = false;
         }
         else
         {
             _clipmap.ShowBlendRegions = true;
         }
         UpdateHUD();
     }
     else if (e.Key == KeyboardKey.L)
     {
         _clipmap.LodUpdateEnabled = !_clipmap.LodUpdateEnabled;
         UpdateHUD();
     }
     else if (e.Key == KeyboardKey.C)
     {
         _clipmap.ColorClipmapLevels = !_clipmap.ColorClipmapLevels;
         if (_clipmap.ColorClipmapLevels)
         {
             _clipmap.ShowImagery = false;
             _clipmap.Lighting    = true;
         }
         UpdateHUD();
     }
     else if (e.Key == KeyboardKey.I)
     {
         _clipmap.ShowImagery = !_clipmap.ShowImagery;
         _clipmap.Lighting    = !_clipmap.ShowImagery;
         if (_clipmap.ShowImagery)
         {
             _clipmap.ColorClipmapLevels = false;
         }
         UpdateHUD();
     }
     else if (e.Key == KeyboardKey.S)
     {
         _clipmap.Lighting = !_clipmap.Lighting;
         UpdateHUD();
     }
     else if (e.Key == KeyboardKey.Z)
     {
         if (_lookCamera != null)
         {
             double     longitude = -119.5326056;
             double     latitude  = 37.74451389;
             Geodetic3D halfDome  = new Geodetic3D(Trig.ToRadians(longitude), Trig.ToRadians(latitude), 2700.0);
             _lookCamera.ViewPoint(_ellipsoid, halfDome);
             _lookCamera.Azimuth   = 0.0;
             _lookCamera.Elevation = Trig.ToRadians(30.0);
             _lookCamera.Range     = 10000.0;
         }
     }
     else if (e.Key == KeyboardKey.F)
     {
         if (_lookCamera != null)
         {
             _lookCamera.Dispose();
             _lookCamera             = null;
             _flyCamera              = new CameraFly(_sceneState.Camera, _window);
             _flyCamera.MovementRate = 1200.0;
         }
         else if (_flyCamera != null)
         {
             _flyCamera.Dispose();
             _flyCamera = null;
             _sceneState.Camera.Target = new Vector3D(0.0, 0.0, 0.0);
             _lookCamera = new CameraLookAtPoint(_sceneState.Camera, _window, _ellipsoid);
             _lookCamera.UpdateParametersFromCamera();
         }
         UpdateHUD();
     }
     else if (_flyCamera != null && (e.Key == KeyboardKey.Plus || e.Key == KeyboardKey.KeypadPlus))
     {
         _flyCamera.MovementRate *= 2.0;
         UpdateHUD();
     }
     else if (_flyCamera != null && (e.Key == KeyboardKey.Minus || e.Key == KeyboardKey.KeypadMinus))
     {
         _flyCamera.MovementRate *= 0.5;
         UpdateHUD();
     }
     else if (e.Key == KeyboardKey.E)
     {
         if (_clipmap.Ellipsoid.MaximumRadius == _clipmap.Ellipsoid.MinimumRadius)
         {
             _clipmap.Ellipsoid = Ellipsoid.Wgs84;
             _globe.Shape       = Ellipsoid.Wgs84;
         }
         else
         {
             double radius = Ellipsoid.Wgs84.MaximumRadius;
             _clipmap.Ellipsoid = new Ellipsoid(radius, radius, radius);
             _globe.Shape       = _clipmap.Ellipsoid;
         }
     }
 }
Esempio n. 17
0
        public LinesOnTerrain()
        {
            _window                   = Device.CreateWindow(800, 600, "Research:  Lines on Terrain");
            _window.Resize           += OnResize;
            _window.RenderFrame      += OnRenderFrame;
            _window.Keyboard.KeyDown += OnKeyDown;
            _sceneState               = new SceneState();
            _sceneState.Camera.PerspectiveFarPlaneDistance  = 4096;
            _sceneState.Camera.PerspectiveNearPlaneDistance = 10;

            _instructions         = new HeadsUpDisplay();
            _instructions.Texture = Device.CreateTexture2D(
                Device.CreateBitmapFromText(
                    "u - Use silhouette\ns - Show silhouette\n",
                    new Font("Arial", 24)),
                TextureFormat.RedGreenBlueAlpha8, false);
            _instructions.Color = Color.LightBlue;

            ///////////////////////////////////////////////////////////////////

            TerrainTile terrainTile = TerrainTile.FromBitmap(new Bitmap(@"ps-e.lg.png"));

            _tile = new TriangleMeshTerrainTile(_window.Context, terrainTile);
            _tile.HeightExaggeration = 30.0f;

            ///////////////////////////////////////////////////////////////////

            double tileRadius = Math.Max(terrainTile.Resolution.X, terrainTile.Resolution.Y) * 0.5;

            _camera             = new CameraLookAtPoint(_sceneState.Camera, _window, Ellipsoid.UnitSphere);
            _camera.CenterPoint = new Vector3D(terrainTile.Resolution.X * 0.5, terrainTile.Resolution.Y * 0.5, 0.0);
            _sceneState.Camera.ZoomToTarget(tileRadius);
            _sceneState.Camera.Eye = new Vector3D(_xPos, 256, 0);

            //
            // Positions
            //
            IList <Vector3D> positions = new List <Vector3D>();
            double           temp      = 1.2 * _tile.HeightExaggeration;

            positions.Add(new Vector3D(0.0, 0.0, -temp));
            positions.Add(new Vector3D(0.0, 0.0, temp));
            positions.Add(new Vector3D(100.0, 100.0, -temp));
            positions.Add(new Vector3D(100.0, 100.0, temp));
            positions.Add(new Vector3D(200.0, 100.0, -temp));
            positions.Add(new Vector3D(200.0, 100.0, temp));
            positions.Add(new Vector3D(256.0, 256.0, -temp));
            positions.Add(new Vector3D(256.0, 256.0, temp));
            positions.Add(new Vector3D(512.0, 512.0, -temp));
            positions.Add(new Vector3D(512.0, 512.0, temp));

            //
            // junk
            _polylineOnTerrain = new PolylineOnTerrain();
            _polylineOnTerrain.Set(_window.Context, positions);


            _clearState = new ClearState();

            // junk
            string fs =
                @"#version 330

                uniform sampler2D og_texture0;
                in vec2 fsTextureCoordinates;
                out vec4 fragmentColor;

                void main()
                {
                    if (texture(og_texture0, fsTextureCoordinates).r == 0.0)
                    {
                        fragmentColor = vec4(0.0, 0.0, 0.0, 1.0);
                    }
                    else
                    {
                        discard;
                    }
                }";

            _viewportQuad = new ViewportQuad(_window.Context, fs);
        }
        public LatitudeLongitudeGrid()
        {
            Ellipsoid globeShape = Ellipsoid.Wgs84;

            _window              = Device.CreateWindow(800, 600, "Chapter 4:  Latitude Longitude Grid");
            _window.Resize      += OnResize;
            _window.RenderFrame += OnRenderFrame;
            _sceneState          = new SceneState();
            _camera              = new CameraLookAtPoint(_sceneState.Camera, _window, globeShape);
            _clearState          = new ClearState();

            _sceneState.Camera.PerspectiveNearPlaneDistance = 0.01 * globeShape.MaximumRadius;
            _sceneState.Camera.PerspectiveFarPlaneDistance  = 10.0 * globeShape.MaximumRadius;
            _sceneState.Camera.ZoomToTarget(globeShape.MaximumRadius);

            ///////////////////////////////////////////////////////////////////

            IList <GridResolution> gridResolutions = new List <GridResolution>();

            gridResolutions.Add(new GridResolution(
                                    new Interval(0, 1000000, IntervalEndpoint.Closed, IntervalEndpoint.Open),
                                    new Vector2D(0.005, 0.005)));
            gridResolutions.Add(new GridResolution(
                                    new Interval(1000000, 2000000, IntervalEndpoint.Closed, IntervalEndpoint.Open),
                                    new Vector2D(0.01, 0.01)));
            gridResolutions.Add(new GridResolution(
                                    new Interval(2000000, 20000000, IntervalEndpoint.Closed, IntervalEndpoint.Open),
                                    new Vector2D(0.05, 0.05)));
            gridResolutions.Add(new GridResolution(
                                    new Interval(20000000, double.MaxValue, IntervalEndpoint.Closed, IntervalEndpoint.Open),
                                    new Vector2D(0.1, 0.1)));

            _globe                 = new LatitudeLongitudeGridGlobe(_window.Context);
            _globe.Texture         = Device.CreateTexture2D(new Bitmap("NE2_50M_SR_W_4096.jpg"), TextureFormat.RedGreenBlue8, false);
            _globe.Shape           = globeShape;
            _globe.GridResolutions = new GridResolutionCollection(gridResolutions);

            ///////////////////////////////////////////////////////////////////

            Vector3D vancouver = globeShape.ToVector3D(new Geodetic3D(Trig.ToRadians(-123.06), Trig.ToRadians(49.13), 0));

            TextureAtlas atlas = new TextureAtlas(new Bitmap[]
            {
                new Bitmap("building.png"),
                Device.CreateBitmapFromText("Vancouver", new Font("Arial", 24))
            });

            _vancouverLabel                  = new BillboardCollection(_window.Context);
            _vancouverLabel.Texture          = Device.CreateTexture2D(atlas.Bitmap, TextureFormat.RedGreenBlueAlpha8, false);
            _vancouverLabel.DepthTestEnabled = false;
            _vancouverLabel.Add(new Billboard()
            {
                Position           = vancouver,
                TextureCoordinates = atlas.TextureCoordinates[0]
            });
            _vancouverLabel.Add(new Billboard()
            {
                Position           = vancouver,
                TextureCoordinates = atlas.TextureCoordinates[1],
                HorizontalOrigin   = HorizontalOrigin.Left
            });

            atlas.Dispose();
        }