Example #1
0
 public virtual void CopyFromBuffer(
     WritePixelBuffer pixelBuffer,
     ImageFormat format,
     ImageDatatype dataType)
 {
     CopyFromBuffer(pixelBuffer, 0, 0, Description.Width, Description.Height, format, dataType, 4);
 }
Example #2
0
 public abstract void CopyFromBuffer(
     WritePixelBuffer pixelBuffer,
     int xOffset,
     int yOffset,
     int width,
     int height,
     ImageFormat format,
     ImageDatatype dataType,
     int rowAlignment);
Example #3
0
        private void Update(Context context, SceneState sceneState)
        {
            if (_meshBuffers != null)
            {
                if (_drawState.VertexArray != null)
                {
                    _drawState.VertexArray.Dispose();
                    _drawState.VertexArray = null;
                }

                _drawState.VertexArray = context.CreateVertexArray(_meshBuffers);
                _meshBuffers           = null;
            }

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

            int width        = (int)Math.Ceiling(Width * sceneState.HighResolutionSnapScale);
            int outlineWidth = (int)Math.Ceiling(OutlineWidth * sceneState.HighResolutionSnapScale);

            int textureWidth = width + outlineWidth + outlineWidth + 2;

            if ((_texture == null) || (_texture.Description.Width != textureWidth))
            {
                int textureResolution = textureWidth * 2;

                float[] texels = new float[textureResolution];

                int k = 3;
                for (int i = 1; i < textureWidth - 1; ++i)
                {
                    texels[k] = 1;                  // Alpha (stored in Green channel)
                    k        += 2;
                }

                int j = (outlineWidth + 1) * 2;
                for (int i = 0; i < width; ++i)
                {
                    texels[j] = 1;                  // Fill/Outline (stored in Red channel)
                    j        += 2;
                }

                WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                             sizeof(float) * textureResolution);
                pixelBuffer.CopyFromSystemMemory(texels);

                if (_texture != null)
                {
                    _texture.Dispose();
                    _texture = null;
                }

                // TODO:  Why does only Float or HalfFloat work here?
                _texture = Device.CreateTexture2D(new Texture2DDescription(textureWidth, 1, TextureFormat.RedGreen8));
                _texture.CopyFromBuffer(pixelBuffer, ImageFormat.RedGreen, ImageDatatype.Float);
            }
        }
Example #4
0
        public override void CopyFromBuffer(
            WritePixelBuffer pixelBuffer,
            int xOffset,
            int yOffset,
            int width,
            int height,
            ImageFormat format,
            ImageDatatype dataType,
            int rowAlignment)
        {
            if (pixelBuffer.SizeInBytes < TextureUtility.RequiredSizeInBytes(
                    width, height, format, dataType, rowAlignment))
            {
                throw new ArgumentException("Pixel buffer is not big enough for provided width, height, format, and datatype.");
            }

            if (xOffset < 0)
            {
                throw new ArgumentOutOfRangeException("xOffset", "xOffset must be greater than or equal to zero.");
            }

            if (yOffset < 0)
            {
                throw new ArgumentOutOfRangeException("yOffset", "yOffset must be greater than or equal to zero.");
            }

            if (xOffset + width > _description.Width)
            {
                throw new ArgumentOutOfRangeException("xOffset + width must be less than or equal to Description.Width");
            }

            if (yOffset + height > _description.Height)
            {
                throw new ArgumentOutOfRangeException("yOffset + height must be less than or equal to Description.Height");
            }

            VerifyRowAlignment(rowAlignment);

            WritePixelBufferGL3x bufferObjectGL = (WritePixelBufferGL3x)pixelBuffer;

            bufferObjectGL.Bind();
            BindToLastTextureUnit();
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, rowAlignment);
            GL.TexSubImage2D(_target, 0,
                             xOffset,
                             yOffset,
                             width,
                             height,
                             TypeConverterGL3x.To(format),
                             TypeConverterGL3x.To(dataType),
                             new IntPtr());

            GenerateMipmaps();
        }
        private Texture2D PostsToTexture(float[] posts)
        {
            Texture2DDescription description = new Texture2DDescription(TileWidth, TileHeight, TextureFormat.Red32f, false);
            Texture2D            texture     = Device.CreateTexture2DRectangle(description);

            using (WritePixelBuffer wpb = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, TileWidth * TileHeight * sizeof(float)))
            {
                wpb.CopyFromSystemMemory(posts);
                texture.CopyFromBuffer(wpb, ImageFormat.Red, ImageDatatype.Float);
            }

            return(texture);
        }
Example #6
0
        /// <summary>
        /// Creates a 1x1 RGBA8 texture
        /// </summary>
        public static Texture2D CreateTexture(BlittableRGBA rgba)
        {
            Texture2DDescription description = new Texture2DDescription(1, 1, TextureFormat.RedGreenBlueAlpha8, false);
            Texture2D            texture     = Device.CreateTexture2D(description);

            BlittableRGBA[] pixels = new BlittableRGBA[] { rgba };

            using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, ArraySizeInBytes.Size(pixels)))
            {
                writePixelBuffer.CopyFromSystemMemory(pixels);
                texture.CopyFromBuffer(writePixelBuffer, ImageFormat.RedGreenBlueAlpha, ImageDatatype.UnsignedByte);
            }

            return(texture);
        }
Example #7
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);
        }
        public RayCastedTerrainTile(TerrainTile tile)
        {
            ShaderProgram sp = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.RayCastedTerrainTile.TerrainVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.RayCastedTerrainTile.TerrainFS.glsl"));

            _tileResolution     = tile.Resolution;
            _tileMinimumHeight  = tile.MinimumHeight;
            _tileMaximumHeight  = tile.MaximumHeight;
            _tileAABBLowerLeft  = Vector3D.Zero;            // TEXEL_SPACE_TODO
            _tileAABBUpperRight = new Vector3D(tile.Resolution.X, tile.Resolution.Y,
                                               tile.MaximumHeight - tile.MinimumHeight);

            _heightExaggeration = (Uniform <float>)sp.Uniforms["u_heightExaggeration"];
            _minimumHeight      = (Uniform <float>)sp.Uniforms["u_minimumHeight"];
            _maximumHeight      = (Uniform <float>)sp.Uniforms["u_maximumHeight"];
            _aabbLowerLeft      = (Uniform <Vector3F>)sp.Uniforms["u_aabbLowerLeft"];
            _aabbUpperRight     = (Uniform <Vector3F>)sp.Uniforms["u_aabbUpperRight"];
            _shadingAlgorithm   = (Uniform <int>)sp.Uniforms["u_shadingAlgorithm"];

            HeightExaggeration = 1;

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

            _drawState = new DrawState();
            _drawState.RenderState.FacetCulling.Face = CullFace.Front;
            _drawState.ShaderProgram = sp;

            //
            // Upload height map as a one channel floating point texture
            //
            WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                         sizeof(float) * tile.Heights.Length);

            pixelBuffer.CopyFromSystemMemory(tile.Heights);

            _texture = Device.CreateTexture2DRectangle(new Texture2DDescription(
                                                           tile.Resolution.X, tile.Resolution.Y, TextureFormat.Red32f));
            _texture.CopyFromBuffer(pixelBuffer, ImageFormat.Red, ImageDatatype.Float);

            ShowTerrain = true;
        }
Example #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;

            // 创建主场景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);
        }
Example #10
0
        public VertexDisplacementMapTerrainTile(Context context, TerrainTile tile)
        {
            //
            // Upload height map as a one channel floating point texture
            //
            WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                         sizeof(float) * tile.Heights.Length);

            pixelBuffer.CopyFromSystemMemory(tile.Heights);

            _texture = Device.CreateTexture2DRectangle(new Texture2DDescription(
                                                           tile.Resolution.X, tile.Resolution.Y, TextureFormat.Red32f));
            _texture.CopyFromBuffer(pixelBuffer, ImageFormat.Red, ImageDatatype.Float);

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

            ShaderProgram spTerrain = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.TerrainVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.TerrainFS.glsl"));

            _heightExaggerationUniform = (Uniform <float>)spTerrain.Uniforms["u_heightExaggeration"];
            ((Uniform <Vector2F>)spTerrain.Uniforms["u_positionToTextureCoordinate"]).Value = new Vector2F(
                (float)(1.0 / (double)(tile.Resolution.X)),
                (float)(1.0 / (double)(tile.Resolution.Y)));
            ((Uniform <Vector2F>)spTerrain.Uniforms["u_positionToRepeatTextureCoordinate"]).Value = new Vector2F(
                (float)(4.0 / (double)tile.Resolution.X),
                (float)(4.0 / (double)tile.Resolution.Y));

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

            ShaderProgram spNormals = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsGS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsFS.glsl"));

            _heightExaggerationNormals = (Uniform <float>)spNormals.Uniforms["u_heightExaggeration"];
            _fillDistanceNormals       = (Uniform <float>)spNormals.Uniforms["u_fillDistance"];
            ((Uniform <Vector3F>)spNormals.Uniforms["u_color"]).Value = Vector3F.Zero;

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

            ShaderProgram spWireframe = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeGS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeFS.glsl"));

            _lineWidthWireframe          = (Uniform <float>)spWireframe.Uniforms["u_halfLineWidth"];
            _heightExaggerationWireframe = (Uniform <float>)spWireframe.Uniforms["u_heightExaggeration"];
            ((Uniform <Vector3F>)spWireframe.Uniforms["u_color"]).Value = Vector3F.Zero;

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

            Mesh mesh = RectangleTessellator.Compute(new RectangleD(new Vector2D(0.5, 0.5),
                                                                    new Vector2D((double)tile.Resolution.X - 0.5, (double)tile.Resolution.Y - 0.5)),
                                                     tile.Resolution.X - 1, tile.Resolution.Y - 1);

            _va            = context.CreateVertexArray(mesh, spWireframe.VertexAttributes, BufferHint.StaticDraw);
            _primitiveType = mesh.PrimitiveType;

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

            RenderState rsTerrain = new RenderState();

            rsTerrain.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder;

            RenderState rsWireframe = new RenderState();

            rsWireframe.Blending.Enabled                   = true;
            rsWireframe.Blending.SourceRGBFactor           = SourceBlendingFactor.SourceAlpha;
            rsWireframe.Blending.SourceAlphaFactor         = SourceBlendingFactor.SourceAlpha;
            rsWireframe.Blending.DestinationRGBFactor      = DestinationBlendingFactor.OneMinusSourceAlpha;
            rsWireframe.Blending.DestinationAlphaFactor    = DestinationBlendingFactor.OneMinusSourceAlpha;
            rsWireframe.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder;
            rsWireframe.DepthTest.Function                 = DepthTestFunction.LessThanOrEqual;

            RenderState rsNormals = new RenderState();

            rsNormals.FacetCulling.Enabled            = false;
            rsNormals.Blending.Enabled                = true;
            rsNormals.Blending.SourceRGBFactor        = SourceBlendingFactor.SourceAlpha;
            rsNormals.Blending.SourceAlphaFactor      = SourceBlendingFactor.SourceAlpha;
            rsNormals.Blending.DestinationRGBFactor   = DestinationBlendingFactor.OneMinusSourceAlpha;
            rsNormals.Blending.DestinationAlphaFactor = DestinationBlendingFactor.OneMinusSourceAlpha;

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

            _drawStateTerrain   = new DrawState(rsTerrain, spTerrain, _va);
            _drawStateWireframe = new DrawState(rsWireframe, spWireframe, _va);
            _drawStateNormals   = new DrawState(rsNormals, spNormals, _va);

            _tileMinimumHeight = tile.MinimumHeight;
            _tileMaximumHeight = tile.MaximumHeight;

            _heightExaggeration = 1;
            _normalsAlgorithm   = TerrainNormalsAlgorithm.ForwardDifference;
            _showTerrain        = true;
            _dirty = true;
        }
Example #11
0
        public override void CopyFromBuffer(
            WritePixelBuffer pixelBuffer,
            int xOffset,
            int yOffset,
            int width,
            int height,
            ImageFormat format,
            ImageDatatype dataType,
            int rowAlignment)
        {
            if (pixelBuffer.SizeInBytes < TextureUtility.RequiredSizeInBytes(
                width, height, format, dataType, rowAlignment))
            {
                throw new ArgumentException("Pixel buffer is not big enough for provided width, height, format, and datatype.");
            }
            
            if (xOffset < 0)
            {
                throw new ArgumentOutOfRangeException("xOffset", "xOffset must be greater than or equal to zero.");
            }

            if (yOffset < 0)
            {
                throw new ArgumentOutOfRangeException("yOffset", "yOffset must be greater than or equal to zero.");
            }

            if (xOffset + width > _description.Width)
            {
                throw new ArgumentOutOfRangeException("xOffset + width must be less than or equal to Description.Width");
            }
            
            if (yOffset + height > _description.Height)
            {
                throw new ArgumentOutOfRangeException("yOffset + height must be less than or equal to Description.Height");
            }
            
            VerifyRowAlignment(rowAlignment);

            WritePixelBufferGL3x bufferObjectGL = (WritePixelBufferGL3x)pixelBuffer;

            bufferObjectGL.Bind();
            BindToLastTextureUnit();
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, rowAlignment);
            GL.TexSubImage2D(_target, 0,
                xOffset,
                yOffset,
                width,
                height,
                TypeConverterGL3x.To(format),
                TypeConverterGL3x.To(dataType),
                new IntPtr());

            GenerateMipmaps();
        }