Exemple #1
0
        public void ExampleSetVertexArraysIndexedElements(GraphicsContext ctx, ShaderProgram shaderProgram)
        {
            VertexArrays vao = new VertexArrays();

            // ... create and setup buffers ...

            ElementBuffer <int> indexBuffer = new ElementBuffer <int>(BufferUsage.StaticDraw);

            indexBuffer.Create(new int[] { 0, 1, 2, 3, 2, 1 });

            // Draw the first 6 deferenced vertices as GL_TRIANGLES
            int idx1 = vao.SetElementArray(PrimitiveType.Triangles, indexBuffer);
            // Draw the first two deferenced vertices as GL_LINES
            int idx2 = vao.SetElementArray(PrimitiveType.Lines, indexBuffer, 0, 2);

            // Create/update all resources linked to this instance
            vao.Create(ctx);

            // Draw all elements
            vao.Draw(ctx, shaderProgram);
            // Draw only the triangles
            vao.Draw(ctx, shaderProgram, idx1);
        }
Exemple #2
0
        public void ExampleSetVertexArraysElements(GraphicsContext ctx, ShaderProgram shaderProgram)
        {
            VertexArrays vao = new VertexArrays();

            // ... create and setup buffers ...

            // Draw the first 4 vertices as GL_LINES
            int idx1 = vao.SetElementArray(PrimitiveType.Lines, 0, 4);
            // Draw all other vertices using GL_TRIANGLES
            int idx2 = vao.SetElementArray(PrimitiveType.Triangles, 4, vao.ArrayLength);
            // Draw all vertices using GL_POINTS
            int idx3 = vao.SetElementArray(PrimitiveType.Points);

            // Create/update all resources linked to this instance
            vao.Create(ctx);

            // Draw all elements
            vao.Draw(ctx, shaderProgram);
            // Draw only the triangles
            vao.Draw(ctx, shaderProgram, idx2);
            // Draw using IElement interface (idx1 and idx3)
            vao.Draw(ctx, shaderProgram, vao.GetElementArray(idx1), vao.GetElementArray(idx3));
        }
Exemple #3
0
        /// <summary>
        /// Update framebuffer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ObjectsControl_Render(object sender, GlControlEventArgs e)
        {
            float deltaTime = (float)_Crono.Elapsed.TotalSeconds * _TimeSpeed;

            _Crono.Restart();

            // Update force
            _ComputeEnergy.SetStorageBuffer(_Context, "MassBuffer", _MassBuffer);

            _ComputeEnergy.SetUniform(_Context, "glo_MassCount", (int)_MassBuffer.ItemsCount);
            _ComputeEnergy.SetUniform(_Context, "glo_Gravity", _Gravity);
            _ComputeEnergy.SetUniform(_Context, "glo_MinDistanceForGravity", 0.1f);

            _ComputeEnergy.SetUniform(_Context, "glo_DeltaTime", deltaTime);

            _ComputeEnergy.Compute(_Context, 1024);
            _ComputeEnergy.MemoryBarrier(MemoryBarrierMask.ShaderStorageBarrierBit);

            // Update positions
            _ComputePosition.SetStorageBuffer(_Context, "MassBuffer", _MassBuffer);

            _ComputePosition.SetUniform(_Context, "glo_MassCount", (int)_MassBuffer.ItemsCount);
            _ComputePosition.SetUniform(_Context, "glo_Gravity", _Gravity);
            _ComputePosition.SetUniform(_Context, "glo_MinDistanceForGravity", 0.1f);

            _ComputePosition.SetUniform(_Context, "glo_DeltaTime", deltaTime);
            _ComputePosition.Compute(_Context, 1024);
            _ComputePosition.MemoryBarrier(MemoryBarrierMask.VertexAttribArrayBarrierBit);

            // Draw scene
            GlControl senderControl     = (GlControl)sender;
            float     senderAspectRatio = (float)senderControl.Width / senderControl.Height;

            // Clear
            Gl.Viewport(0, 0, senderControl.Width, senderControl.Height);
            Gl.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            Matrix4x4f mvp = Matrix4x4f.Perspective(60.0f, senderAspectRatio, 0.1f, 16535.0f) * Matrix4x4f.Translated(-128.0f, -128.0f, -256.0f);

            _DrawMass.SetUniform(_Context, "glo_ModelViewProjection", mvp);

            _MassArrays.Draw(_Context, _DrawMass);
        }
Exemple #4
0
        private void VisionControl_Render(object sender, OpenGL.GlControlEventArgs e)
        {
            #region Draw Basic Picture

            // Update image input
            _Framebuffer.BindDraw(_GraphicsContext);
            Gl.Viewport(0, 0, (int)_Framebuffer.Width, (int)_Framebuffer.Height);
            _Framebuffer.Clear(_GraphicsContext, ClearBufferMask.ColorBufferBit);
            {                   // Draw a quad
                Matrix4x4f quadProj  = Matrix4x4f.Ortho2D(-1.0f, +1.0f, -1.0f, +1.0f);
                Matrix4x4f quadModel = new Matrix4x4f();

                _Angle += 1.0f;

                quadModel.RotateZ(10.0f * (float)Math.Cos(Angle.ToRadians(_Angle)));

                _GraphicsContext.Bind(_ProgramStd);
                _ProgramStd.SetUniform(_GraphicsContext, "glo_ModelViewProjection", quadProj * quadModel);
                _ProgramStd.SetUniform(_GraphicsContext, "glo_UniformColor", Vertex4f.One);

                _ArraysQuad.Draw(_GraphicsContext, _ProgramStd);
            }
            _Framebuffer.UnbindDraw(_GraphicsContext);

            #endregion

            #region Track Corners

            // Read back image input pixels
            using (OpenGL.Objects.Image imageInput = _FramebufferTexture.Get(_GraphicsContext, PixelLayout.RGB24, 0)) {
                // Copy the input RGB frame from OpenGL to OpenVX
                Rectangle cv_rgb_image_region = new Rectangle();
                cv_rgb_image_region.StartX = 0;
                cv_rgb_image_region.StartY = 0;
                cv_rgb_image_region.EndX   = imageInput.Width;
                cv_rgb_image_region.EndY   = imageInput.Height;

                ImagePatchAddressing cv_rgb_image_layout = new ImagePatchAddressing();
                cv_rgb_image_layout.StrideX = 3;
                cv_rgb_image_layout.StrideY = (int)imageInput.Stride;

                VX.CopyImagePatch(_ImageInput, ref cv_rgb_image_region, 0, ref cv_rgb_image_layout, imageInput.ImageBuffer, Accessor.WriteOnly, MemoryType.Host);
            }

            // Now that input RGB image is ready, just run a graph.
            // Run Harris at the beginning to initialize the previous keypoints,
            // on other frames run the tracking graph.
            VX.ProcessGraph(_DetectCorners ? _GraphHarris : _GraphTrack);

            _DetectCorners = false;

            #endregion

            #region Store Markers on GPU

            // To mark the keypoints in display, you need to access the output
            // keypoint array and draw each item on the output window using gui.DrawArrow().
            UIntPtr num_corners  = UIntPtr.Zero;
            uint    num_tracking = 0;

            _KeypointsPrevious = VX.GetReferenceFromDelay(_KeypointsDelay, -1);
            _KeypointsCurrent  = VX.GetReferenceFromDelay(_KeypointsDelay, 0);

            VX.Query(_KeypointsPrevious, ArrayAttribute.Numitems, out num_corners);
            if (num_corners.ToUInt64() > 0)
            {
                UIntPtr kp_old_stride = UIntPtr.Zero, kp_new_stride = UIntPtr.Zero;
                MapId   kp_old_map = new MapId(), kp_new_map = new MapId();
                IntPtr  kp_old_buf, kp_new_buf;

                VX.MapArrayRange(_KeypointsPrevious, (UIntPtr)0, num_corners, ref kp_old_map, ref kp_old_stride, out kp_old_buf, Accessor.ReadOnly, MemoryType.Host, 0);
                VX.MapArrayRange(_KeypointsCurrent, (UIntPtr)0, num_corners, ref kp_new_map, ref kp_new_stride, out kp_new_buf, Accessor.ReadOnly, MemoryType.Host, 0);

                _BufferOpticalMarkers.Map(_GraphicsContext, BufferAccess.WriteOnly);

                for (uint i = 0; i < num_corners.ToUInt64(); i++)
                {
                    KeyPoint kp_old = VX.ArrayItem <KeyPoint>(kp_old_buf, i, kp_old_stride);
                    KeyPoint kp_new = VX.ArrayItem <KeyPoint>(kp_new_buf, i, kp_new_stride);

                    if (kp_new.TrackingStatus != 0)
                    {
                        Vertex2f vOld = new Vertex2f(kp_old.X / 1024.0f, kp_old.Y / 1024.0f);
                        Vertex2f vNew = new Vertex2f(kp_new.X / 1024.0f, kp_new.Y / 1024.0f);

                        _BufferOpticalMarkers.SetElement(vOld, (num_tracking * 2) + 0, 0);
                        _BufferOpticalMarkers.SetElement(vNew, (num_tracking * 2) + 1, 0);

                        num_tracking++;
                    }
                }

                _BufferOpticalMarkers.Unmap(_GraphicsContext);

                VX.UnmapArrayRange(_KeypointsPrevious, kp_old_map);
                VX.UnmapArrayRange(_KeypointsCurrent, kp_new_map);
            }

            #endregion

            Gl.Viewport(0, 0, VisionControl.Width, VisionControl.Height);
            Gl.ClearColor(1.0f, 0.0f, 0.0f, 0.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit);

            #region Draw Input Image

            _GraphicsContext.Bind(_ProgramStdTex);
            _ProgramStdTex.SetUniform(_GraphicsContext, "glo_ModelViewProjection", Matrix4x4f.Ortho2D(0.0f, 1.0f, 0.0f, 1.0f));
            _ProgramStdTex.SetUniform(_GraphicsContext, "glo_Texture", _FramebufferTexture);

            _ArraysPostQuad.Draw(_GraphicsContext, _ProgramStdTex);

            #endregion

            #region Draw Markers

            if (num_tracking > 0)
            {
                _GraphicsContext.Bind(_ProgramStd);
                _ProgramStd.SetUniform(_GraphicsContext, "glo_ModelViewProjection", Matrix4x4f.Ortho2D(0.0f, 1.0f, 0.0f, 1.0f));
                _ProgramStd.SetUniform(_GraphicsContext, "glo_UniformColor", new Vertex4f(1.0f, 0.0f, 0.0f, 1.0f));

                _ArraysOpticalMarkers.Draw(_GraphicsContext, _ProgramStd, 0, 0, num_tracking * 2);
            }

            #endregion

            // Increase the age of the delay objects to make the current entry become previous entry
            VX.AgeDelay(_PyramidDelay);
            VX.AgeDelay(_KeypointsDelay);
        }