Пример #1
0
        public void CopyFromSystemMemory <T>(
            T[] bufferInSystemMemory,
            int destinationOffsetInBytes,
            int lengthInBytes) where T : struct
        {
            if (destinationOffsetInBytes < 0)
            {
                throw new ArgumentOutOfRangeException("destinationOffsetInBytes",
                                                      "destinationOffsetInBytes must be greater than or equal to zero.");
            }

            if (destinationOffsetInBytes + lengthInBytes > _sizeInBytes)
            {
                throw new ArgumentOutOfRangeException(
                          "destinationOffsetInBytes + lengthInBytes must be less than or equal to SizeInBytes.");
            }

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

            if (lengthInBytes > ArraySizeInBytes.Size(bufferInSystemMemory))
            {
                throw new ArgumentOutOfRangeException("lengthInBytes",
                                                      "lengthInBytes must be less than or equal to the size of bufferInSystemMemory in bytes.");
            }

            Bind();
            GL.BufferSubData <T>(_type,
                                 new IntPtr(destinationOffsetInBytes),
                                 new IntPtr(lengthInBytes),
                                 bufferInSystemMemory);
        }
Пример #2
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);
                        }
                    }
        }
Пример #3
0
        private static VertexBuffer CreateVertexBuffer <T>(IList <T> values, BufferHint usageHint) where T : struct
        {
            T[] valuesArray = new T[values.Count];
            values.CopyTo(valuesArray, 0);

            VertexBuffer vertexBuffer = Device.CreateVertexBuffer(usageHint, ArraySizeInBytes.Size(valuesArray));

            vertexBuffer.CopyFromSystemMemory(valuesArray);
            return(vertexBuffer);
        }
Пример #4
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);
                        }
        }
Пример #5
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);
                            }
        }
Пример #6
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]);
                }
        }
Пример #7
0
        public void Create()
        {
            _context.MakeCurrent();

            int vbSizeInBytes = ArraySizeInBytes.Size(_positions);

            _vertexBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, vbSizeInBytes);
            _vertexBuffer.CopyFromSystemMemory(_positions);
            _positions = null;

            Fence fence = Device.CreateFence();

            while (fence.ClientWait(0) == ClientWaitResult.TimeoutExpired)
            {
                Thread.Sleep(10);
            }
        }
Пример #8
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]);
                }
        }
Пример #9
0
        public CPURelativeToEye(Context context, Vector3D[] positions, byte[] colors)
        {
            _sp = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Examples.CPURelativeToEye.Shaders.VS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Examples.Shaders.FS.glsl"));
            _modelViewPerspectiveMatrixRelativeToEye = (Uniform <Matrix4F>)(_sp.Uniforms["u_modelViewPerspectiveMatrixRelativeToEye"]);
            _pointSize = (Uniform <float>)_sp.Uniforms["u_pointSize"];

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

            _positions = new Vector3D[positions.Length];
            positions.CopyTo(_positions, 0);
            _positionsRelativeToEye = new Vector3F[_positions.Length];
            _eye = Vector3D.Zero;

            //
            // _positionBuffer is dynamic, and is written to when the camera moves.
            //
            _positionBuffer = Device.CreateVertexBuffer(BufferHint.DynamicDraw, ArraySizeInBytes.Size(_positionsRelativeToEye));

            _colorBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, colors.Length);
            _colorBuffer.CopyFromSystemMemory(colors);

            _va = context.CreateVertexArray();
            _va.Attributes[_sp.VertexAttributes["position"].Location] =
                new VertexBufferAttribute(_positionBuffer, ComponentDatatype.Float, 3);
            _va.Attributes[_sp.VertexAttributes["color"].Location] =
                new VertexBufferAttribute(_colorBuffer, ComponentDatatype.UnsignedByte, 3, true, 0, 0);

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

            RenderState renderState = new RenderState();

            renderState.FacetCulling.Enabled = false;
            renderState.DepthTest.Enabled    = false;
            renderState.ProgramPointSize     = ProgramPointSize.Enabled;

            _drawState = new DrawState(renderState, _sp, _va);
        }
Пример #10
0
 public virtual void CopyFromSystemMemory <T>(T[] bufferInSystemMemory, int destinationOffsetInBytes) where T : struct
 {
     CopyFromSystemMemory <T>(bufferInSystemMemory, destinationOffsetInBytes, ArraySizeInBytes.Size(bufferInSystemMemory));
 }
Пример #11
0
        public static VertexArray CreateVertexArray(Context context, int positionLocation)
        {
            Vector4F[]   positions       = new[] { new Vector4F(0, 0, 0, 1) };
            VertexBuffer positionsBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, ArraySizeInBytes.Size(positions));

            positionsBuffer.CopyFromSystemMemory(positions);

            VertexArray va = context.CreateVertexArray();

            va.Attributes[positionLocation] = new VertexBufferAttribute(positionsBuffer, ComponentDatatype.Float, 4);
            va.DisposeBuffers = true;

            return(va);
        }
Пример #12
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);
        }
Пример #13
0
        public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeCollection shaderAttributes, BufferHint usageHint)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

            if (shaderAttributes == null)
            {
                throw new ArgumentNullException("shaderAttributes");
            }

            MeshBuffers meshBuffers = new MeshBuffers();

            if (mesh.Indices != null)
            {
                if (mesh.Indices.Datatype == IndicesType.UnsignedShort)
                {
                    IList <ushort> meshIndices = ((IndicesUnsignedShort)mesh.Indices).Values;

                    ushort[] indices = new ushort[meshIndices.Count];
                    for (int j = 0; j < meshIndices.Count; ++j)
                    {
                        indices[j] = meshIndices[j];
                    }

                    IndexBuffer indexBuffer = Device.CreateIndexBuffer(usageHint, indices.Length * sizeof(ushort));
                    indexBuffer.CopyFromSystemMemory(indices);
                    meshBuffers.IndexBuffer = indexBuffer;
                }
                else if (mesh.Indices.Datatype == IndicesType.UnsignedInt)
                {
                    IList <uint> meshIndices = ((IndicesUnsignedInt)mesh.Indices).Values;

                    uint[] indices = new uint[meshIndices.Count];
                    for (int j = 0; j < meshIndices.Count; ++j)
                    {
                        indices[j] = meshIndices[j];
                    }

                    IndexBuffer indexBuffer = Device.CreateIndexBuffer(usageHint, indices.Length * sizeof(uint));
                    indexBuffer.CopyFromSystemMemory(indices);
                    meshBuffers.IndexBuffer = indexBuffer;
                }
                else
                {
                    throw new NotSupportedException("mesh.Indices.Datatype " +
                                                    mesh.Indices.Datatype.ToString() + " is not supported.");
                }
            }

            //
            // Emulated double precision vectors are a special case:  one mesh vertex attribute
            // yields two shader vertex attributes.  As such, these are handled separately before
            // normal attributes.
            //
            HashSet <string> ignoreAttributes = new HashSet <string>();

            foreach (VertexAttribute attribute in mesh.Attributes)
            {
                if (attribute is VertexAttributeDoubleVector3)
                {
                    VertexAttributeDoubleVector3 emulated = (VertexAttributeDoubleVector3)attribute;

                    int highLocation = -1;
                    int lowLocation  = -1;

                    foreach (ShaderVertexAttribute shaderAttribute in shaderAttributes)
                    {
                        if (shaderAttribute.Name == emulated.Name + "High")
                        {
                            highLocation = shaderAttribute.Location;
                        }
                        else if (shaderAttribute.Name == emulated.Name + "Low")
                        {
                            lowLocation = shaderAttribute.Location;
                        }

                        if ((highLocation != -1) && (lowLocation != -1))
                        {
                            break;
                        }
                    }

                    if ((highLocation == -1) && (lowLocation == -1))
                    {
                        //
                        // The shader did not have either attribute.  No problem.
                        //
                        continue;
                    }
                    else if ((highLocation == -1) || (lowLocation == -1))
                    {
                        throw new ArgumentException("An emulated double vec3 mesh attribute requires both " + emulated.Name + "High and " + emulated.Name + "Low vertex attributes, but the shader only contains one matching attribute.");
                    }

                    //
                    // Copy both high and low parts into a single vertex buffer.
                    //
                    IList <Vector3D> values = ((VertexAttribute <Vector3D>)attribute).Values;

                    Vector3F[] vertices = new Vector3F[2 * values.Count];

                    int j = 0;
                    for (int i = 0; i < values.Count; ++i)
                    {
                        EmulatedVector3D v = new EmulatedVector3D(values[i]);
                        vertices[j++] = v.High;
                        vertices[j++] = v.Low;
                    }

                    VertexBuffer vertexBuffer = Device.CreateVertexBuffer(usageHint, ArraySizeInBytes.Size(vertices));
                    vertexBuffer.CopyFromSystemMemory(vertices);

                    int stride = 2 * SizeInBytes <Vector3F> .Value;
                    meshBuffers.Attributes[highLocation] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 3, false, 0, stride);
                    meshBuffers.Attributes[lowLocation] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 3, false, SizeInBytes <Vector3F> .Value, stride);

                    ignoreAttributes.Add(emulated.Name + "High");
                    ignoreAttributes.Add(emulated.Name + "Low");
                }
            }

            // TODO:  Not tested exhaustively
            foreach (ShaderVertexAttribute shaderAttribute in shaderAttributes)
            {
                if (ignoreAttributes.Contains(shaderAttribute.Name))
                {
                    continue;
                }

                if (!mesh.Attributes.Contains(shaderAttribute.Name))
                {
                    throw new ArgumentException("Shader requires vertex attribute \"" + shaderAttribute.Name + "\", which is not present in mesh.");
                }

                VertexAttribute attribute = mesh.Attributes[shaderAttribute.Name];


                if (attribute.Datatype == VertexAttributeType.EmulatedDoubleVector3)
                {
                    IList <Vector3D> values = ((VertexAttribute <Vector3D>)attribute).Values;

                    Vector3F[] valuesArray = new Vector3F[values.Count];
                    for (int i = 0; i < values.Count; ++i)
                    {
                        valuesArray[i] = values[i].ToVector3F();
                    }

                    VertexBuffer vertexBuffer = Device.CreateVertexBuffer(usageHint, ArraySizeInBytes.Size(valuesArray));
                    vertexBuffer.CopyFromSystemMemory(valuesArray);
                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 3);
                }
                else if (attribute.Datatype == VertexAttributeType.HalfFloat)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <Half>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 1);
                }
                else if (attribute.Datatype == VertexAttributeType.HalfFloatVector2)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <Vector2H>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 2);
                }
                else if (attribute.Datatype == VertexAttributeType.HalfFloatVector3)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <Vector3H>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 3);
                }
                else if (attribute.Datatype == VertexAttributeType.HalfFloatVector4)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <Vector4H>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 4);
                }
                else if (attribute.Datatype == VertexAttributeType.Float)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <float>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 1);
                }
                else if (attribute.Datatype == VertexAttributeType.FloatVector2)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <Vector2F>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 2);
                }
                else if (attribute.Datatype == VertexAttributeType.FloatVector3)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <Vector3F>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 3);
                }
                else if (attribute.Datatype == VertexAttributeType.FloatVector4)
                {
                    VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <Vector4F>)attribute).Values, usageHint);

                    meshBuffers.Attributes[shaderAttribute.Location] =
                        new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 4);
                }
                else if (attribute.Datatype == VertexAttributeType.UnsignedByte)
                {
                    if (attribute is VertexAttributeRGBA)
                    {
                        VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <byte>)attribute).Values, usageHint);

                        meshBuffers.Attributes[shaderAttribute.Location] =
                            new VertexBufferAttribute(vertexBuffer, ComponentDatatype.UnsignedByte, 4, true, 0, 0);
                    }

                    else if (attribute is VertexAttributeRGB)
                    {
                        VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <byte>)attribute).Values, usageHint);

                        meshBuffers.Attributes[shaderAttribute.Location] =
                            new VertexBufferAttribute(vertexBuffer, ComponentDatatype.UnsignedByte, 3, true, 0, 0);
                    }
                    else
                    {
                        VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute <byte>)attribute).Values, usageHint);

                        meshBuffers.Attributes[shaderAttribute.Location] =
                            new VertexBufferAttribute(vertexBuffer, ComponentDatatype.UnsignedByte, 1);
                    }
                }
                else
                {
                    Debug.Fail("attribute.Datatype");
                }
            }

            return(meshBuffers);
        }
Пример #14
0
        public SceneGPURelativeToEyeLOD(Context context, Vector3D[] positions, byte[] colors)
        {
            _spHigh = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Examples.GPURelativeToEyeLOD.Shaders.HighPrecisionVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Examples.Shaders.FS.glsl"));
            _cameraEyeHigh = (Uniform <Vector3F>)_spHigh.Uniforms["u_cameraEyeHigh"];
            _cameraEyeLow  = (Uniform <Vector3F>)_spHigh.Uniforms["u_cameraEyeLow"];
            _modelViewPerspectiveMatrixRelativeToEye = (Uniform <Matrix4F>)(_spHigh.Uniforms["u_modelViewPerspectiveMatrixRelativeToEye"]);
            _pointSizeHigh = (Uniform <float>)_spHigh.Uniforms["u_pointSize"];

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

            Vector3F[] positionsHigh = new Vector3F[positions.Length];
            Vector3F[] positionsLow  = new Vector3F[positions.Length];

            for (int i = 0; i < positions.Length; ++i)
            {
                Vector3DToTwoVector3F(positions[i], out positionsHigh[i], out positionsLow[i]);
            }
            _center = positions[6];

            _positionBufferHigh = Device.CreateVertexBuffer(BufferHint.StaticDraw, ArraySizeInBytes.Size(positions));
            _positionBufferLow  = Device.CreateVertexBuffer(BufferHint.StaticDraw, ArraySizeInBytes.Size(positions));
            _colorBuffer        = Device.CreateVertexBuffer(BufferHint.StaticDraw, colors.Length);

            _positionBufferHigh.CopyFromSystemMemory(positionsHigh);
            _positionBufferLow.CopyFromSystemMemory(positionsLow);
            _colorBuffer.CopyFromSystemMemory(colors);

            _vaHigh = context.CreateVertexArray();
            _vaHigh.Attributes[_spHigh.VertexAttributes["positionHigh"].Location] =
                new VertexBufferAttribute(_positionBufferHigh, ComponentDatatype.Float, 3);
            _vaHigh.Attributes[_spHigh.VertexAttributes["positionLow"].Location] =
                new VertexBufferAttribute(_positionBufferLow, ComponentDatatype.Float, 3);
            _vaHigh.Attributes[_spHigh.VertexAttributes["color"].Location] =
                new VertexBufferAttribute(_colorBuffer, ComponentDatatype.UnsignedByte, 3, true, 0, 0);

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

            RenderState renderState = new RenderState();

            renderState.FacetCulling.Enabled = false;
            renderState.DepthTest.Enabled    = false;
            renderState.ProgramPointSize     = ProgramPointSize.Enabled;

            _drawStateHigh = new DrawState(renderState, _spHigh, _vaHigh);

            ///////////////////////////////////////////////////////////////////
            // Low Precision

            _spLow = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Examples.GPURelativeToEyeLOD.Shaders.LowPrecisionVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Examples.Shaders.FS.glsl"));
            _pointSizeLow = (Uniform <float>)_spLow.Uniforms["u_pointSize"];

            _vaLow = context.CreateVertexArray();
            _vaLow.Attributes[_spLow.VertexAttributes["position"].Location] =
                new VertexBufferAttribute(_positionBufferHigh, ComponentDatatype.Float, 3);
            _vaLow.Attributes[_spLow.VertexAttributes["color"].Location] =
                new VertexBufferAttribute(_colorBuffer, ComponentDatatype.UnsignedByte, 3, true, 0, 0);

            _drawStateLow = new DrawState(renderState, _spLow, _vaLow);
        }
Пример #15
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);
                                }
        }
Пример #16
0
        public void RenderInterleavedVertexBuffer()
        {
            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;
                  }";

            InterleavedVertex[] vertices = new InterleavedVertex[]
            {
                new InterleavedVertex()
                {
                    Position = new Vector4F(0, 0, 0, 1),
                    Color    = new BlittableRGBA(Color.Red)
                },
                new InterleavedVertex()
                {
                    Position = new Vector4F(0, 0, 0, 1),
                    Color    = new BlittableRGBA(Color.FromArgb(255, 0, 255, 0))
                }
            };

            GraphicsWindow window       = Device.CreateWindow(1, 1);
            Framebuffer    framebuffer  = TestUtility.CreateFramebuffer(window.Context);
            ShaderProgram  sp           = Device.CreateShaderProgram(vs, fs);
            VertexBuffer   vertexBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, ArraySizeInBytes.Size(vertices));
            VertexArray    va           = window.Context.CreateVertexArray();
            {
                int colorOffset = SizeInBytes <Vector4F> .Value;
                vertexBuffer.CopyFromSystemMemory(vertices);

                va.Attributes[sp.VertexAttributes["position"].Location] =
                    new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 4, false, 0, SizeInBytes <InterleavedVertex> .Value);
                va.Attributes[sp.VertexAttributes["color"].Location] =
                    new VertexBufferAttribute(vertexBuffer, ComponentDatatype.UnsignedByte, 4, true, colorOffset, SizeInBytes <InterleavedVertex> .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, 0, 0);

                window.Context.Draw(PrimitiveType.Points, 1, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), sp, va), new SceneState());
                TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 0, 255, 0);
            }
        }