Ejemplo n.º 1
0
        public void Texture2D()
        {
            BlittableRGBA[] pixels = new BlittableRGBA[]
            {
                new BlittableRGBA(Color.Red),
                new BlittableRGBA(Color.Green)
            };
            int sizeInBytes = ArraySizeInBytes.Size(pixels);
            Texture2DDescription description = new Texture2DDescription(2, 1, TextureFormat.RedGreenBlueAlpha8, false);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                    using (Texture2D texture = Device.CreateTexture2D(description))
                    {
                        writePixelBuffer.CopyFromSystemMemory(pixels);

                        //
                        // Create texture with pixel buffer
                        //
                        texture.CopyFromBuffer(writePixelBuffer, BlittableRGBA.Format, BlittableRGBA.Datatype);

                        //
                        // Read back pixels
                        //
                        using (ReadPixelBuffer readPixelBuffer = texture.CopyToBuffer(BlittableRGBA.Format, BlittableRGBA.Datatype))
                        {
                            BlittableRGBA[] readPixels = readPixelBuffer.CopyToSystemMemory <BlittableRGBA>();

                            Assert.AreEqual(sizeInBytes, readPixelBuffer.SizeInBytes);
                            Assert.AreEqual(pixels[0], readPixels[0]);
                            Assert.AreEqual(pixels[1], readPixels[1]);
                            Assert.AreEqual(description, texture.Description);
                        }
                    }
        }
Ejemplo n.º 2
0
        public void RenderTexturedPoint()
        {
            string fs =
                @"#version 330
                 
                  uniform sampler2D textureUnit;
                  out vec4 FragColor;

                  void main()
                  {
                      FragColor = texture(textureUnit, vec2(0, 0));
                  }";

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), fs))
                        using (Texture2D texture = TestUtility.CreateTexture(new BlittableRGBA(Color.FromArgb(0, 255, 0, 0))))
                            using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                            {
                                Uniform <int> textureUniform = (Uniform <int>)sp.Uniforms["textureUnit"];
                                textureUniform.Value = 0;

                                window.Context.TextureUnits[0].Texture        = texture;
                                window.Context.TextureUnits[0].TextureSampler = Device.TextureSamplers.NearestClamp;
                                window.Context.Framebuffer = framebuffer;
                                window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());

                                TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="camera">The renderer camera that is to be manipulated by the new instance.</param>
        /// <param name="window">The window in which the scene is drawn.</param>
        /// <param name="ellipsoid">The ellipsoid defining the shape of the globe.</param>
        public CameraLookAtPoint(Camera camera, GraphicsWindow window, Ellipsoid ellipsoid)
        {
            if (camera == null)
            {
                throw new ArgumentNullException("camera");
            }
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }

            _camera = camera;
            _window = window;

            _centerPoint = camera.Target;

            _zoomFactor = 5.0;
            _zoomRateRangeAdjustment = ellipsoid.MaximumRadius;
            _maximumZoomRate = Double.MaxValue;
            _minimumZoomRate = ellipsoid.MaximumRadius / 100.0;

            _rotateFactor = 1.0 / ellipsoid.MaximumRadius;
            _rotateRateRangeAdjustment = ellipsoid.MaximumRadius;
            _maximumRotateRate = 1.0;
            _minimumRotateRate = 1.0 / 5000.0;

            // TODO: Should really be:
            // _range = (camera.Eye - camera.Target).Magnitude;
            _range = ellipsoid.MaximumRadius * 2.0;

            MouseEnabled = true;
        }
Ejemplo n.º 4
0
        public void ClearColorDepth()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (Texture2D depthTexture = Device.CreateTexture2D(new Texture2DDescription(1, 1, TextureFormat.Depth32f, false)))
                    {
                        framebuffer.DepthAttachment = depthTexture;

                        window.Context.Framebuffer = framebuffer;
                        window.Context.Clear(new ClearState()
                        {
                            Buffers = ClearBuffers.All, Color = Color.Red, Depth = 0.5f
                        });
                        TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                        ValidateDepth(framebuffer.DepthAttachment, 0.5f);

                        //
                        // Scissor out window and verify clear doesn't modify contents
                        //
                        ScissorTest scissorTest = new ScissorTest();
                        scissorTest.Enabled   = true;
                        scissorTest.Rectangle = new Rectangle(0, 0, 0, 0);

                        window.Context.Clear(new ClearState()
                        {
                            ScissorTest = scissorTest, Buffers = ClearBuffers.All, Color = Color.Blue, Depth = 1
                        });
                        TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                        ValidateDepth(framebuffer.DepthAttachment, 0.5f);
                    }
        }
Ejemplo n.º 5
0
        public void Texture2DRowAlignment()
        {
            //
            // Create pixel buffer - RGB, 4 byte aligned, 255 is padding
            //
            byte[] pixels = new byte[]
            {
                1, 2, 3, 255,
                4, 5, 6, 255
            };
            int sizeInBytes = pixels.Length * sizeof(byte);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                    using (Texture2D texture = Device.CreateTexture2D(new Texture2DDescription(1, 2, TextureFormat.RedGreenBlue8, false)))
                    {
                        writePixelBuffer.CopyFromSystemMemory(pixels);
                        texture.CopyFromBuffer(writePixelBuffer, ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte);

                        //
                        // Read back pixels
                        //
                        using (ReadPixelBuffer readPixelBuffer = texture.CopyToBuffer(ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte, 1))
                        {
                            byte[] readPixels = readPixelBuffer.CopyToSystemMemory <byte>();
                            Assert.AreEqual(1, readPixels[0]);
                            Assert.AreEqual(2, readPixels[1]);
                            Assert.AreEqual(3, readPixels[2]);
                            Assert.AreEqual(4, readPixels[3]);
                            Assert.AreEqual(5, readPixels[4]);
                            Assert.AreEqual(6, readPixels[5]);
                        }
                    }
        }
Ejemplo n.º 6
0
        public void WritePixelBufferBitmap()
        {
            using (Bitmap bitmap = new Bitmap(1, 1))
                using (GraphicsWindow window = Device.CreateWindow(1, 1))
                    using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                                        BitmapAlgorithms.SizeOfPixelsInBytes(bitmap)))
                    {
                        Color color = Color.FromArgb(0, 1, 2, 3);
                        bitmap.SetPixel(0, 0, color);

                        pixelBuffer.CopyFromBitmap(bitmap);

                        //
                        // Verify read back - comes back BGRA
                        //
                        BlittableBGRA[] readBackPixel = pixelBuffer.CopyToSystemMemory <BlittableBGRA>();
                        Assert.AreEqual(color.R, readBackPixel[0].R);
                        Assert.AreEqual(color.G, readBackPixel[0].G);
                        Assert.AreEqual(color.B, readBackPixel[0].B);
                        Assert.AreEqual(color.A, readBackPixel[0].A);

                        //
                        // Verify read back into bitmap
                        //
                        using (Bitmap readBackBitmap = pixelBuffer.CopyToBitmap(1, 1, PixelFormat.Format32bppArgb))
                        {
                            Assert.AreEqual(color, readBackBitmap.GetPixel(0, 0));
                        }
                    }
        }
Ejemplo n.º 7
0
        public void RenderPointMultipleColorAttachments()
        {
            Texture2DDescription description = new Texture2DDescription(1, 1, TextureFormat.RedGreenBlue8, false);

            string fs =
                @"#version 330
                 
                  out vec3 RedColor;
                  out vec3 GreenColor;

                  void main()
                  {
                      RedColor = vec3(1.0, 0.0, 0.0);
                      GreenColor = vec3(0.0, 1.0, 0.0);
                  }";

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = window.Context.CreateFramebuffer())
                    using (Texture2D redTexture = Device.CreateTexture2D(description))
                        using (Texture2D greenTexture = Device.CreateTexture2D(description))
                            using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), fs))
                                using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                                {
                                    Assert.AreNotEqual(sp.FragmentOutputs["RedColor"], sp.FragmentOutputs["GreenColor"]);
                                    framebuffer.ColorAttachments[sp.FragmentOutputs["RedColor"]]   = redTexture;
                                    framebuffer.ColorAttachments[sp.FragmentOutputs["GreenColor"]] = greenTexture;

                                    window.Context.Framebuffer = framebuffer;
                                    window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());

                                    TestUtility.ValidateColor(redTexture, 255, 0, 0);
                                    TestUtility.ValidateColor(greenTexture, 0, 255, 0);
                                }
        }
Ejemplo n.º 8
0
        public void RenderPointWithStencil()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (Texture2D depthStencilTexture = Device.CreateTexture2D(new Texture2DDescription(1, 1, TextureFormat.Depth24Stencil8, false)))
                        using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), ShaderSources.PassThroughFragmentShader()))
                            using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                            {
                                framebuffer.DepthStencilAttachment = depthStencilTexture;

                                StencilTest stencilTest = new StencilTest();
                                stencilTest.Enabled = true;
                                stencilTest.FrontFace.DepthFailStencilPassOperation = StencilOperation.Replace;
                                stencilTest.FrontFace.DepthPassStencilPassOperation = StencilOperation.Replace;
                                stencilTest.FrontFace.StencilFailOperation          = StencilOperation.Replace;
                                stencilTest.FrontFace.ReferenceValue = 2;

                                RenderState renderState = new RenderState();
                                renderState.StencilTest = stencilTest;

                                window.Context.Framebuffer = framebuffer;
                                window.Context.Clear(new ClearState());
                                window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(renderState, sp, va), new SceneState());

                                TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);

                                using (ReadPixelBuffer readPixelBuffer = depthStencilTexture.CopyToBuffer(ImageFormat.DepthStencil, ImageDatatype.UnsignedInt248, 1))
                                {
                                    byte[] depthStencil = readPixelBuffer.CopyToSystemMemory <byte>();
                                    Assert.AreEqual(stencilTest.FrontFace.ReferenceValue, depthStencil[0]);
                                }
                            }
        }
Ejemplo n.º 9
0
        public void EnumeratevertexBufferAttributes()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (VertexArray va = window.Context.CreateVertexArray())
                    using (VertexBuffer vb0 = Device.CreateVertexBuffer(BufferHint.StaticDraw, 4))
                        using (VertexBuffer vb1 = Device.CreateVertexBuffer(BufferHint.DynamicDraw, 4))
                            using (VertexBuffer vb2 = Device.CreateVertexBuffer(BufferHint.StreamDraw, 4))
                            {
                                va.Attributes[0] = new VertexBufferAttribute(vb0, ComponentDatatype.Float, 1);
                                va.Attributes[1] = new VertexBufferAttribute(vb1, ComponentDatatype.Float, 1);
                                va.Attributes[2] = new VertexBufferAttribute(vb2, ComponentDatatype.Float, 1);
                                Assert.AreEqual(3, va.Attributes.Count);

                                va.Attributes[1] = null;
                                Assert.AreEqual(2, va.Attributes.Count);

                                va.Attributes[1] = new VertexBufferAttribute(vb1, ComponentDatatype.Float, 1);
                                Assert.AreEqual(3, va.Attributes.Count);

                                int count = 0;
                                foreach (VertexBufferAttribute vb in va.Attributes)
                                {
                                    Assert.IsNotNull(vb.VertexBuffer);
                                    ++count;
                                }
                                Assert.AreEqual(va.Attributes.Count, count);
                            }
        }
Ejemplo n.º 10
0
        public void Matrix32()
        {
            string fs =
                @"#version 330

                  uniform mat3x2 exampleMat32;
                  out vec3 FragColor;

                  void main()
                  {
                      FragColor = vec3(exampleMat32[1].x, exampleMat32[2].x, 0.0);
                  }";

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), fs))
                        using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                        {
                            Matrix32 <float> m32 = new Matrix32 <float>(
                                0.0f, 1.0f, 1.0f,
                                0.0f, 0.0f, 0.0f);
                            Uniform <Matrix32 <float> > exampleMat32 = (Uniform <Matrix32 <float> >)sp.Uniforms["exampleMat32"];
                            Assert.AreEqual("exampleMat32", exampleMat32.Name);
                            Assert.AreEqual(UniformType.FloatMatrix32, exampleMat32.Datatype);
                            Assert.AreEqual(new Matrix32 <float>(), exampleMat32.Value);
                            exampleMat32.Value = m32;
                            Assert.AreEqual(m32, exampleMat32.Value);

                            window.Context.Framebuffer = framebuffer;
                            window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());
                            TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 255, 0);
                        }
        }
Ejemplo n.º 11
0
        public void LinkAutomaticUniforms()
        {
            string fs =
                @"#version 330
                 
                  uniform sampler2D og_texture0;
                  uniform sampler2D og_texture1;
                  uniform sampler2D og_texture2;
                  uniform sampler2D og_texture3;
                  out vec4 FragColor;

                  void main()
                  {
                      FragColor = 
                          texture(og_texture0, vec2(0, 0)) + 
                          texture(og_texture1, vec2(0, 0)) + 
                          texture(og_texture2, vec2(0, 0)) + 
                          texture(og_texture3, vec2(0, 0));
                  }";

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), fs))
                {
                    Assert.AreEqual(0, ((Uniform <int>)sp.Uniforms["og_texture0"]).Value);
                    Assert.AreEqual(1, ((Uniform <int>)sp.Uniforms["og_texture1"]).Value);
                    Assert.AreEqual(2, ((Uniform <int>)sp.Uniforms["og_texture2"]).Value);
                    Assert.AreEqual(3, ((Uniform <int>)sp.Uniforms["og_texture3"]).Value);
                }
        }
Ejemplo n.º 12
0
        public void UniformBuffer()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (ShaderProgram sp = Device.CreateShaderProgram(
                           ShaderSources.PassThroughVertexShader(),
                           ShaderSources.RedUniformBlockFragmentShader()))
                    using (UniformBuffer uniformBuffer = Device.CreateUniformBuffer(BufferHint.DynamicDraw, sp.UniformBlocks["RedBlock"].SizeInBytes))
                    {
                        Assert.IsFalse(sp.Log.Contains("warning"));

                        UniformBlock       redBlock = sp.UniformBlocks["RedBlock"];
                        UniformBlockMember red      = redBlock.Members["red"];

                        //
                        // Verify creating uniform buffer
                        //
                        Assert.IsNotNull(uniformBuffer);
                        Assert.AreEqual(BufferHint.DynamicDraw, uniformBuffer.UsageHint);
                        Assert.AreEqual(redBlock.SizeInBytes, uniformBuffer.SizeInBytes);

                        redBlock.Bind(uniformBuffer);

                        //
                        // Verify copying into red member
                        //
                        float redIntensity = 0.5f;
                        uniformBuffer.CopyFromSystemMemory(new[] { redIntensity }, red.OffsetInBytes);

                        float[] redIntensity2 = uniformBuffer.CopyToSystemMemory <float>(red.OffsetInBytes, sizeof(float));
                        Assert.AreEqual(redIntensity, redIntensity2[0]);
                    }
        }
Ejemplo n.º 13
0
        public void EnumerateColorAttachments()
        {
            Texture2DDescription description = new Texture2DDescription(1, 1, TextureFormat.RedGreenBlue8, false);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = window.Context.CreateFramebuffer())
                    using (Texture2D color0 = Device.CreateTexture2D(description))
                        using (Texture2D color1 = Device.CreateTexture2D(description))
                            using (Texture2D color2 = Device.CreateTexture2D(description))
                            {
                                framebuffer.ColorAttachments[0] = color0;
                                framebuffer.ColorAttachments[1] = color1;
                                framebuffer.ColorAttachments[2] = color2;
                                Assert.AreEqual(3, framebuffer.ColorAttachments.Count);

                                framebuffer.ColorAttachments[1] = null;
                                Assert.AreEqual(2, framebuffer.ColorAttachments.Count);

                                framebuffer.ColorAttachments[1] = color1;
                                Assert.AreEqual(3, framebuffer.ColorAttachments.Count);

                                int count = 0;
                                foreach (Texture2D texture in framebuffer.ColorAttachments)
                                {
                                    Assert.AreEqual(description, texture.Description);
                                    ++count;
                                }
                                Assert.AreEqual(framebuffer.ColorAttachments.Count, count);
                            }
        }
Ejemplo n.º 14
0
        public void TextureUnits()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Texture2D texture = Device.CreateTexture2D(new Texture2DDescription(1, 1, TextureFormat.RedGreenBlueAlpha8, false)))
                    using (TextureSampler sampler = Device.CreateTexture2DSampler(
                               TextureMinificationFilter.Nearest,
                               TextureMagnificationFilter.Nearest,
                               TextureWrap.Clamp,
                               TextureWrap.Clamp,
                               2))
                    {
                        window.Context.TextureUnits[0].Texture        = texture;
                        window.Context.TextureUnits[0].TextureSampler = Device.TextureSamplers.LinearRepeat;

                        Assert.AreEqual(texture, window.Context.TextureUnits[0].Texture);
                        Assert.AreEqual(Device.TextureSamplers.LinearRepeat, window.Context.TextureUnits[0].TextureSampler);

                        //
                        // Assign same texture with different filter
                        //
                        window.Context.TextureUnits[0].Texture        = texture;
                        window.Context.TextureUnits[0].TextureSampler = sampler;
                        Assert.AreEqual(texture, window.Context.TextureUnits[0].Texture);
                        Assert.AreEqual(sampler, window.Context.TextureUnits[0].TextureSampler);
                    }
        }
Ejemplo n.º 15
0
        public void Texture2DSubImage()
        {
            float[] pixels = new float[]
            {
                1, 2,
                3, 4
            };
            int sizeInBytes = pixels.Length * sizeof(float);
            Texture2DDescription description = new Texture2DDescription(2, 2, TextureFormat.Red32f, true);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                    using (Texture2D texture = Device.CreateTexture2D(description))
                    {
                        //
                        // Copy from system memory to texture via pixel buffer
                        //
                        writePixelBuffer.CopyFromSystemMemory(pixels);
                        texture.CopyFromBuffer(writePixelBuffer, ImageFormat.Red, ImageDatatype.Float, 1);

                        //
                        // Read back pixels
                        //
                        using (ReadPixelBuffer readPixelBuffer = texture.CopyToBuffer(ImageFormat.Red, ImageDatatype.Float, 1))
                        {
                            float[] readPixels = readPixelBuffer.CopyToSystemMemory <float>();

                            //
                            // Verify
                            //
                            Assert.AreEqual(sizeInBytes, readPixelBuffer.SizeInBytes);
                            Assert.AreEqual(pixels[0], readPixels[0]);
                            Assert.AreEqual(pixels[1], readPixels[1]);
                            Assert.AreEqual(pixels[2], readPixels[2]);
                            Assert.AreEqual(pixels[3], readPixels[3]);
                            Assert.AreEqual(description, texture.Description);

                            //
                            // Update sub image
                            //
                            float modifiedPixel = 9;
                            writePixelBuffer.CopyFromSystemMemory(new[] { modifiedPixel });
                            texture.CopyFromBuffer(writePixelBuffer, 1, 1, 1, 1, ImageFormat.Red, ImageDatatype.Float, 1);
                            using (ReadPixelBuffer readPixelBuffer2 = texture.CopyToBuffer(ImageFormat.Red, ImageDatatype.Float, 1))
                            {
                                float[] readPixels2 = readPixelBuffer2.CopyToSystemMemory <float>();

                                Assert.AreEqual(sizeInBytes, readPixelBuffer2.SizeInBytes);
                                Assert.AreEqual(pixels[0], readPixels2[0]);
                                Assert.AreEqual(pixels[1], readPixels2[1]);
                                Assert.AreEqual(pixels[2], readPixels2[2]);
                                Assert.AreEqual(modifiedPixel, readPixels2[3]);
                            }
                        }
                    }
        }
Ejemplo n.º 16
0
 public void RenderPoint()
 {
     using (GraphicsWindow window = Device.CreateWindow(1, 1))
         using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
             using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), ShaderSources.PassThroughFragmentShader()))
                 using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                 {
                     window.Context.Framebuffer = framebuffer;
                     window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());
                     TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                 }
 }
Ejemplo n.º 17
0
        public void RenderTriangle()
        {
            Vector4F[] positions = new[]
            {
                new Vector4F(-0.5f, -0.5f, 0, 1),
                new Vector4F(0.5f, -0.5f, 0, 1),
                new Vector4F(0.5f, 0.5f, 0, 1)
            };

            ushort[] indices = new ushort[]
            {
                0, 1, 2
            };

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), ShaderSources.PassThroughFragmentShader()))
                        using (VertexBuffer positionsBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, ArraySizeInBytes.Size(positions)))
                            using (IndexBuffer indexBuffer = Device.CreateIndexBuffer(BufferHint.StaticDraw, indices.Length * sizeof(ushort)))
                                using (VertexArray va = window.Context.CreateVertexArray())
                                {
                                    positionsBuffer.CopyFromSystemMemory(positions);
                                    indexBuffer.CopyFromSystemMemory(indices);

                                    va.Attributes[sp.VertexAttributes["position"].Location] =
                                        new VertexBufferAttribute(positionsBuffer, ComponentDatatype.Float, 4);
                                    va.IndexBuffer = indexBuffer;

                                    window.Context.Framebuffer = framebuffer;
                                    window.Context.Draw(PrimitiveType.Triangles, 0, 3, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());
                                    TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);

                                    //
                                    // Verify detach
                                    //
                                    window.Context.Clear(new ClearState()
                                    {
                                        Buffers = ClearBuffers.ColorBuffer, Color = Color.FromArgb(0, 255, 0)
                                    });
                                    va.Attributes[sp.VertexAttributes["position"].Location] = null;
                                    va.IndexBuffer = null;
                                    window.Context.Draw(PrimitiveType.Triangles, 0, 0, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());
                                    TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 0, 255, 0);

                                    //
                                    // Verify rendering without indices
                                    //
                                    va.Attributes[sp.VertexAttributes["position"].Location] =
                                        new VertexBufferAttribute(positionsBuffer, ComponentDatatype.Float, 4);
                                    window.Context.Draw(PrimitiveType.Triangles, 0, 3, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());
                                    TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                                }
        }
Ejemplo n.º 18
0
        public void HighResolutionSnapFramebuffer()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (HighResolutionSnapFramebuffer snap = new HighResolutionSnapFramebuffer(window.Context, 2, 10, 2))
                {
                    Assert.AreEqual(2, snap.AspectRatio);
                    Assert.AreEqual(10, snap.DotsPerInch);

                    Assert.AreEqual(2, snap.WidthInInches);
                    Assert.AreEqual(20, snap.WidthInPixels);

                    Assert.AreEqual(1, snap.HeightInInches);
                    Assert.AreEqual(10, snap.HeightInPixels);

                    window.Context.Framebuffer = snap.Framebuffer;
                    window.Context.Viewport    = new Rectangle(0, 0, snap.WidthInPixels, snap.HeightInPixels);
                    window.Context.Clear(new ClearState()
                    {
                        Buffers = ClearBuffers.ColorAndDepthBuffer, Color = Color.Red, Depth = 0.5f
                    });

                    string colorFile = "color.bmp";
                    string depthFile = "depth.bmp";

                    snap.SaveColorBuffer(colorFile);
                    snap.SaveDepthBuffer(depthFile);

                    try
                    {
                        using (Bitmap colorBitmap = new Bitmap(colorFile))
                        {
                            Assert.AreEqual(snap.WidthInPixels, colorBitmap.Width);
                            Assert.AreEqual(snap.HeightInPixels, colorBitmap.Height);
                        }

                        using (Bitmap depthBitmap = new Bitmap(depthFile))
                        {
                            Assert.AreEqual(snap.WidthInPixels, depthBitmap.Width);
                            Assert.AreEqual(snap.HeightInPixels, depthBitmap.Height);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        File.Delete(colorFile);
                        File.Delete(depthFile);
                    }
                }
        }
Ejemplo n.º 19
0
 public void DepthStencilAttachment()
 {
     using (GraphicsWindow window = Device.CreateWindow(1, 1))
         using (Framebuffer framebuffer = window.Context.CreateFramebuffer())
             using (Texture2D depthStencilTexture = Device.CreateTexture2D(new Texture2DDescription(1, 1, TextureFormat.Depth32fStencil8, false)))
             {
                 Assert.IsNull(framebuffer.DepthStencilAttachment);
                 framebuffer.DepthStencilAttachment = depthStencilTexture;
                 Assert.AreEqual(depthStencilTexture, framebuffer.DepthStencilAttachment);
                 framebuffer.DepthStencilAttachment = null;
                 Assert.IsNull(framebuffer.DepthStencilAttachment);
             }
 }
Ejemplo n.º 20
0
        public HighResolutionSnap(GraphicsWindow window, SceneState sceneState)
        {
            _window = window;
            _sceneState = sceneState;

            window.Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
            {
                if (e.Key == KeyboardKey.Space)
                {
                    Enable(true);
                }
            };
        }
Ejemplo n.º 21
0
        public HighResolutionSnap(GraphicsWindow window, SceneState sceneState)
        {
            _window     = window;
            _sceneState = sceneState;

            window.Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
            {
                if (e.Key == KeyboardKey.Space)
                {
                    Enable(true);
                }
            };
        }
Ejemplo n.º 22
0
 public void EnumerateTextureUnits()
 {
     using (GraphicsWindow window = Device.CreateWindow(1, 1))
     {
         int count = 0;
         foreach (TextureUnit unit in window.Context.TextureUnits)
         {
             Assert.IsNull(unit.Texture);
             Assert.IsNull(unit.TextureSampler);
             ++count;
         }
         Assert.AreEqual(count, window.Context.TextureUnits.Count);
     }
 }
Ejemplo n.º 23
0
        public void RenderNonInterleavedVertexBuffer()
        {
            Vector4F[]      positions = new[] { new Vector4F(0, 0, 0, 1) };
            BlittableRGBA[] colors    = new[] { new BlittableRGBA(Color.Red) };
            string          vs        =
                @"#version 330

                  layout(location = og_positionVertexLocation) in vec4 position;               
                  layout(location = og_colorVertexLocation) in vec4 color;
                  out vec4 fsColor;

                  void main()                     
                  {
                      gl_Position = position; 
                      fsColor = color;
                  }";
            string fs =
                @"#version 330
                 
                  in vec4 fsColor;
                  out vec4 FragColor;

                  void main()
                  {
                      FragColor = fsColor;
                  }";

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (ShaderProgram sp = Device.CreateShaderProgram(vs, fs))
                        using (VertexBuffer vertexBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw,
                                                                                     ArraySizeInBytes.Size(positions) + ArraySizeInBytes.Size(colors)))
                            using (VertexArray va = window.Context.CreateVertexArray())
                            {
                                int colorsOffset = ArraySizeInBytes.Size(positions);
                                vertexBuffer.CopyFromSystemMemory(positions);
                                vertexBuffer.CopyFromSystemMemory(colors, colorsOffset);

                                va.Attributes[sp.VertexAttributes["position"].Location] =
                                    new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 4);
                                va.Attributes[sp.VertexAttributes["color"].Location] =
                                    new VertexBufferAttribute(vertexBuffer, ComponentDatatype.UnsignedByte, 4, true, colorsOffset, 0);

                                window.Context.Framebuffer = framebuffer;
                                window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());

                                TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                            }
        }
Ejemplo n.º 24
0
        public void VertexArray()
        {
            Vector3F[] positions = new Vector3F[]
            {
                new Vector3F(0, 0, 0),
                new Vector3F(1, 0, 0),
                new Vector3F(0, 1, 0)
            };
            int vbSizeInBytes = ArraySizeInBytes.Size(positions);

            uint[] indices       = new uint[] { 0, 1, 2 };
            int    ibSizeInBytes = indices.Length * sizeof(uint);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (VertexBuffer vertexBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, vbSizeInBytes))
                    using (IndexBuffer indexBuffer = Device.CreateIndexBuffer(BufferHint.StreamDraw, ibSizeInBytes))
                        using (VertexArray va = window.Context.CreateVertexArray())
                        {
                            vertexBuffer.CopyFromSystemMemory(positions);
                            indexBuffer.CopyFromSystemMemory(indices);

                            //
                            // Create and verify vertex buffer attribute
                            //
                            VertexBufferAttribute vertexBufferAttribute = new VertexBufferAttribute(
                                vertexBuffer, ComponentDatatype.Float, 3, false, 0, 0);
                            Assert.AreEqual(vertexBuffer, vertexBufferAttribute.VertexBuffer);
                            Assert.AreEqual(ComponentDatatype.Float, vertexBufferAttribute.ComponentDatatype);
                            Assert.AreEqual(3, vertexBufferAttribute.NumberOfComponents);
                            Assert.IsFalse(vertexBufferAttribute.Normalize);
                            Assert.AreEqual(0, vertexBufferAttribute.OffsetInBytes);
                            Assert.AreEqual(SizeInBytes <Vector3F> .Value, vertexBufferAttribute.StrideInBytes);

                            //
                            // Verify vertex array
                            //
                            va.Attributes[0] = vertexBufferAttribute;
                            va.IndexBuffer   = indexBuffer;

                            Assert.AreEqual(vertexBufferAttribute, va.Attributes[0]);
                            Assert.AreEqual(indexBuffer, va.IndexBuffer);

                            va.Attributes[0] = null;
                            va.IndexBuffer   = null;

                            Assert.IsNull(va.Attributes[0]);
                            Assert.IsNull(va.IndexBuffer);
                        }
        }
Ejemplo n.º 25
0
        public static void Execute(string filename, GraphicsWindow window, Camera camera)
        {
            if (File.Exists(filename))
            {
                camera.LoadView(filename);
            }

            window.Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
            {
                if (e.Key == KeyboardKey.Space)
                {
                    camera.SaveView(filename);
                }
            };
        }
Ejemplo n.º 26
0
        public static void Execute(string filename, GraphicsWindow window, Camera camera)
        {
            if (File.Exists(filename))
            {
                camera.LoadView(filename);
            }

            window.Keyboard.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
            {
                if (e.Key == KeyboardKey.Space)
                {
                    camera.SaveView(filename);
                }
            };
        }
Ejemplo n.º 27
0
        public CameraFly(Camera camera, GraphicsWindow window)
        {
            if (camera == null)
            {
                throw new ArgumentNullException("camera");
            }
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }

            _camera = camera;
            _window = window;

            InputEnabled = true;
        }
Ejemplo n.º 28
0
        public void Find()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
            {
                ShaderCache cache = new ShaderCache();

                ShaderProgram sp = cache.FindOrAdd("PassThrough",
                                                   ShaderSources.PassThroughVertexShader(),
                                                   ShaderSources.PassThroughFragmentShader());
                ShaderProgram sp2 = cache.Find("PassThrough");

                Assert.AreEqual(sp, sp2);

                cache.Release("PassThrough");
                cache.Release("PassThrough");
            }
        }
Ejemplo n.º 29
0
        private void RenderPoint(string vs)
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (ShaderProgram sp = Device.CreateShaderProgram(vs, ShaderSources.PassThroughFragmentShader()))
                        using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                        {
                            SceneState sceneState = new SceneState();
                            sceneState.Camera.Eye    = 2 * Vector3D.UnitX;
                            sceneState.Camera.Target = Vector3D.Zero;
                            sceneState.Camera.Up     = Vector3D.UnitZ;

                            window.Context.Framebuffer = framebuffer;
                            window.Context.Draw(PrimitiveType.Points, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), sceneState);
                            TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                        }
        }
Ejemplo n.º 30
0
        public void VertexBuffer()
        {
            Vector3F[] positions = new Vector3F[]
            {
                Vector3F.Zero,
                new Vector3F(1, 0, 0),
                new Vector3F(0, 1, 0)
            };
            int sizeInBytes = ArraySizeInBytes.Size(positions);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (VertexBuffer vertexBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, sizeInBytes))
                {
                    //
                    // Verify creating vertex buffer
                    //
                    Assert.IsNotNull(vertexBuffer);
                    Assert.AreEqual(BufferHint.StaticDraw, vertexBuffer.UsageHint);
                    Assert.AreEqual(sizeInBytes, vertexBuffer.SizeInBytes);

                    //
                    // Verify copying entire buffer between system memory and vertex buffer
                    //
                    vertexBuffer.CopyFromSystemMemory(positions);

                    Vector3F[] positions2 = vertexBuffer.CopyToSystemMemory <Vector3F>(0, vertexBuffer.SizeInBytes);
                    Assert.AreEqual(positions[0], positions2[0]);
                    Assert.AreEqual(positions[1], positions2[1]);
                    Assert.AreEqual(positions[2], positions2[2]);

                    //
                    // Verify modiying a subset of the vertex buffer
                    //
                    Vector3F[] modifiedPositions = new Vector3F[]
                    {
                        new Vector3F(0, 1, 0),
                        Vector3F.Zero
                    };
                    vertexBuffer.CopyFromSystemMemory(modifiedPositions, SizeInBytes <Vector3F> .Value, SizeInBytes <Vector3F> .Value);

                    Vector3F[] positions3 = vertexBuffer.CopyToSystemMemory <Vector3F>(0, vertexBuffer.SizeInBytes);
                    Assert.AreEqual(positions[0], positions3[0]);
                    Assert.AreEqual(modifiedPositions[0], positions3[1]);
                    Assert.AreEqual(positions[2], positions3[2]);
                }
        }
Ejemplo n.º 31
0
 public void Clear()
 {
     using (GraphicsWindow window = Device.CreateWindow(1, 1))
     {
         window.Context.Clear(new ClearState {
             Buffers = ClearBuffers.ColorBuffer
         });
         window.Context.Clear(new ClearState {
             Buffers = ClearBuffers.DepthBuffer
         });
         window.Context.Clear(new ClearState {
             Buffers = ClearBuffers.StencilBuffer
         });
         window.Context.Clear(new ClearState {
             Buffers = ClearBuffers.All
         });
     }
 }
Ejemplo n.º 32
0
        public void RenderMultitexturedPoint()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), ShaderSources.MultitextureFragmentShader()))
                        using (Texture2D texture0 = TestUtility.CreateTexture(new BlittableRGBA(Color.FromArgb(0, 255, 0, 0))))
                            using (Texture2D texture1 = TestUtility.CreateTexture(new BlittableRGBA(Color.FromArgb(0, 0, 255, 0))))
                                using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                                {
                                    window.Context.TextureUnits[0].Texture        = texture0;
                                    window.Context.TextureUnits[0].TextureSampler = Device.TextureSamplers.NearestClamp;
                                    window.Context.TextureUnits[1].Texture        = texture1;
                                    window.Context.TextureUnits[1].TextureSampler = Device.TextureSamplers.NearestClamp;
                                    window.Context.Framebuffer = framebuffer;
                                    window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());

                                    TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 255, 0);
                                }
        }
Ejemplo n.º 33
0
 public void Texture2DSampler()
 {
     using (GraphicsWindow window = Device.CreateWindow(1, 1))
     {
         using (TextureSampler filter = Device.CreateTexture2DSampler(
                    TextureMinificationFilter.Linear,
                    TextureMagnificationFilter.Nearest,
                    TextureWrap.MirroredRepeat,
                    TextureWrap.Repeat,
                    2))
         {
             Assert.AreEqual(TextureMinificationFilter.Linear, filter.MinificationFilter);
             Assert.AreEqual(TextureMagnificationFilter.Nearest, filter.MagnificationFilter);
             Assert.AreEqual(TextureWrap.MirroredRepeat, filter.WrapS);
             Assert.AreEqual(TextureWrap.Repeat, filter.WrapT);
             Assert.AreEqual(2, filter.MaximumAnisotropic);
         }
     }
 }
Ejemplo n.º 34
0
        public void WritePixelBuffer()
        {
            BlittableRGBA[] pixels = new BlittableRGBA[]
            {
                new BlittableRGBA(Color.Red),
                new BlittableRGBA(Color.Green),
                new BlittableRGBA(Color.Blue),
                new BlittableRGBA(Color.White)
            };
            int sizeInBytes = ArraySizeInBytes.Size(pixels);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                {
                    //
                    // Verify creating pixel buffer
                    //
                    Assert.IsNotNull(pixelBuffer);
                    Assert.AreEqual(PixelBufferHint.Stream, pixelBuffer.UsageHint);
                    Assert.AreEqual(sizeInBytes, pixelBuffer.SizeInBytes);

                    //
                    // Verify copying entire buffer between system memory and pixel buffer
                    //
                    pixelBuffer.CopyFromSystemMemory(pixels);

                    BlittableRGBA[] pixels2 = pixelBuffer.CopyToSystemMemory <BlittableRGBA>(0, pixelBuffer.SizeInBytes);
                    Assert.AreEqual(pixels[0], pixels2[0]);
                    Assert.AreEqual(pixels[1], pixels2[1]);
                    Assert.AreEqual(pixels[2], pixels2[2]);

                    //
                    // Verify modiying a subset of the vertex buffer
                    //
                    BlittableRGBA modifiedPixel = new BlittableRGBA(Color.Black);
                    pixelBuffer.CopyFromSystemMemory(new[] { modifiedPixel }, SizeInBytes <BlittableRGBA> .Value);

                    BlittableRGBA[] pixels3 = pixelBuffer.CopyToSystemMemory <BlittableRGBA>(0, pixelBuffer.SizeInBytes);
                    Assert.AreEqual(pixels[0], pixels3[0]);
                    Assert.AreEqual(modifiedPixel, pixels3[1]);
                    Assert.AreEqual(pixels[2], pixels3[2]);
                }
        }
Ejemplo n.º 35
0
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name="camera">The renderer camera that is to be manipulated by the new instance.</param>
 /// <param name="window">The window in which the scene is drawn.</param>
 /// <param name="ellipsoid">The ellipsoid defining the shape of the globe.</param>
 public CameraLookAtPoint(Camera camera, GraphicsWindow window) :
     this(camera, window, Ellipsoid.UnitSphere)
 {
 }
Ejemplo n.º 36
0
        private void myButton1_Click(object sender, EventArgs e)
        {
            _window = Device.CreateWindow(800, 600, "Chapter 13:  Clipmap Terrain on a Globe");

            _ellipsoid = Ellipsoid.Wgs84;

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

            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)));

            _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);
            //_globe.GridResolutions = new GridResolutionCollection(gridResolutions);

            _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;

            //_flyCamera = new CameraFly(_sceneState.Camera, _window);
            //_flyCamera.MovementRate = 1200.0;
            //_flyCamera.InputEnabled = true;

            _sceneState.Camera.Target = new Vector3D(115, -35, 100.0);

            _window.Run(30);
        }