Ejemplo n.º 1
0
        private void RenderControl_Render_ES(object sender, GlControlEventArgs e)
        {
            Control control = (Control)sender;
            OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
            ModelMatrix           modelMatrix      = new ModelMatrix();

            // Animate triangle
            modelMatrix.RotateZ(_Angle);

            Gl.Viewport(0, 0, control.Width, control.Height);
            Gl.Clear(ClearBufferMask.ColorBufferBit);

            Gl.UseProgram(_Es2_Program);

            using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition))
                using (MemoryLock arrayColor = new MemoryLock(_ArrayColor))
                {
                    Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address);
                    Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition);

                    Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address);
                    Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor);

                    Gl.UniformMatrix4(_Es2_Program_Location_uMVP, false, (projectionMatrix * modelMatrix).ToArray());

                    Gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
                }
        }
Ejemplo n.º 2
0
        private float[] calcMVP(float w, float h)
        {
            OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, w, 0.0f, h, 0.0f, 1000000.0f);
            ModelMatrix           modelMatrix      = new ModelMatrix();

            return((projectionMatrix * modelMatrix).ToArray());
        }
Ejemplo n.º 3
0
        public void TestGetFrustumLeftPlane()
        {
            ProjectionMatrix projectionMatrix;
            ModelMatrix      modelMatrix;
            Matrix4x4        mvp;
            Plane            plane;

            // Orthographic projection
            projectionMatrix = new OrthoProjectionMatrix(-2.0f, +2.0f, -2.0f, +2.0f);
            plane            = Plane.GetFrustumLeftPlane(projectionMatrix);
            Assert.AreEqual(Vertex3f.UnitX, plane.Normal);
            Assert.AreEqual(-2.0f, plane.Distance);

            // Rotate by 90 deg on Y axis
            modelMatrix = new ModelMatrix();
            modelMatrix.RotateY(90.0f);
            mvp = projectionMatrix * modelMatrix;

            plane = Plane.GetFrustumLeftPlane(mvp);
            Assert.AreEqual(Vertex3f.UnitZ, plane.Normal);
            Assert.AreEqual(-2.0f, plane.Distance);

            // Rotate by 180 deg on Y axis
            modelMatrix = new ModelMatrix();
            modelMatrix.RotateY(180.0f);
            mvp = projectionMatrix * modelMatrix;

            plane = Plane.GetFrustumLeftPlane(mvp);
            Assert.AreEqual(-Vertex3f.UnitX, plane.Normal);
            Assert.AreEqual(-2.0f, plane.Distance);
        }
Ejemplo n.º 4
0
        public void TestGetFrustumFarPlane()
        {
            ProjectionMatrix projectionMatrix;
            Plane            plane;

            // Orthographic projection
            projectionMatrix = new OrthoProjectionMatrix(-2.0f, +2.0f, -2.0f, +2.0f, -2.0f, +2.0f);
            plane            = Plane.GetFrustumFarPlane(projectionMatrix);
            Assert.AreEqual(Vertex3f.UnitZ, plane.Normal);
            Assert.AreEqual(-2.0f, plane.Distance);
        }
Ejemplo n.º 5
0
        public void MatrixMul2()
        {
            ProjectionMatrix proj  = new OrthoProjectionMatrix(-1.0f, 0.5f, 0.0f, 1.0f, 0.1f, 3.0f);
            ModelMatrix      model = new ModelMatrix();

            model.Translate(2.0f, -4.0f, 1.125f);
            model.RotateX(30.0f);

            Matrix r1 = proj * model;

            Matrix m = new Matrix(proj.ToArray(), 4, 4), n = new Matrix(model.ToArray(), 4, 4);

            Matrix r2 = m * n;
        }
Ejemplo n.º 6
0
        public void LocalProjection()
        {
            using (T transformState = Activator.CreateInstance <T>()) {
                IMatrix4x4 projectionMatrix;

                // By default, LocalProjection is null
                Assert.IsNull(transformState.LocalProjection);
                // Allow to set custom projections
                projectionMatrix = new OrthoProjectionMatrix();
                Assert.DoesNotThrow(delegate() { transformState.LocalProjection = (IProjectionMatrix)projectionMatrix; });
                // LocalProjection reference another clone
                Assert.IsNotNull(transformState.LocalProjection);
                Assert.AreNotSame(projectionMatrix, transformState.LocalProjection);
                // Allow to reset projection
                Assert.DoesNotThrow(delegate() { transformState.LocalProjection = null; });
                Assert.IsNull(transformState.LocalProjection);
            }
        }
Ejemplo n.º 7
0
        private void Es2_Render()
        {
            OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);

            Gl.UseProgram(_Es2_Program);

            using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition))
                using (MemoryLock arrayColor = new MemoryLock(_ArrayColor))
                {
                    Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address);
                    Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition);

                    Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address);
                    Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor);

                    Gl.UniformMatrix4(_Es2_Program_Location_uMVP, false, projectionMatrix.ToArray());

                    Gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
                }
        }
Ejemplo n.º 8
0
        private void DisplayShadowMaps(GraphicsContext ctx)
        {
            State.ViewportState   viewportState   = new State.ViewportState(ctx);
            OrthoProjectionMatrix orthoProjection = new OrthoProjectionMatrix(0.0f, viewportState.Width, 0.0f, viewportState.Height);
            ModelMatrix           model           = new ModelMatrix();

            // No depth test
            State.DepthTestState.DefaultState.Apply(ctx, null);

            ctx.Bind(_ShadowMapDebugProgram);

            for (int i = 0; i < _LightManager.ShadowLights.Count; i++)
            {
                SceneObjectLight     shadowLight     = _LightManager.ShadowLights[i];
                SceneObjectLightSpot shadowSpotLight = shadowLight as SceneObjectLightSpot;
                if (shadowSpotLight == null)
                {
                    continue;
                }

                Texture2d shadowTex = shadowSpotLight._ShadowMap;
                shadowTex.SamplerParams.CompareMode = false;

                ModelMatrix quadModel = new ModelMatrix(model);
                quadModel.Scale(shadowTex.Width / 4, shadowTex.Height / 4);

                _ShadowMapDebugProgram.SetUniform(ctx, "glo_ModelViewProjection", orthoProjection * quadModel);
                _ShadowMapDebugProgram.SetUniform(ctx, "glo_NearFar", new Vertex2f(0.1f, 100.0f));
                _ShadowMapDebugProgram.SetUniform(ctx, "glo_Texture", shadowTex);

                _ShadowMapQuad.Draw(ctx, _ShadowMapDebugProgram);

                shadowTex.SamplerParams.CompareMode = true;

                // Stride right
                model.Translate(shadowTex.Width, 0.0f);
            }
        }
Ejemplo n.º 9
0
        private static void Draw()
        {
            OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);

            Gl.Viewport(0, 0, 1920, 1080);
            Gl.Clear(ClearBufferMask.ColorBufferBit);

            Gl.UseProgram(_Es2_Program);

            using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition))
                using (MemoryLock arrayColor = new MemoryLock(_ArrayColor))
                {
                    Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address);
                    Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition);

                    Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address);
                    Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor);

                    Gl.UniformMatrix4(_Es2_Program_Location_uMVP, 1, false, projectionMatrix.ToArray());

                    Gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
                }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Update framebuffer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ObjectsControl_Render(object sender, GlControlEventArgs e)
        {
            GlControl senderControl     = (GlControl)sender;
            float     senderAspectRatio = (float)senderControl.Width / senderControl.Height;

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

            _CubeScene.CurrentView.LocalModel.SetIdentity();
            _CubeScene.CurrentView.LocalModel.Translate(_ViewStrideLat, _ViewStrideAlt, 0.0f);
            _CubeScene.CurrentView.LocalModel.RotateY(_ViewAzimuth);
            _CubeScene.CurrentView.LocalModel.RotateX(_ViewElevation);
            _CubeScene.CurrentView.LocalModel.Translate(0.0f, 0.0f, _ViewLever);

            _CubeScene.CurrentView.ProjectionMatrix = new PerspectiveProjectionMatrix(45.0f, senderAspectRatio, 0.5f, 10000.0f);

            _CubeScene.Draw(_Context);

            // Overlay
            ProjectionMatrix overlayProjection = new OrthoProjectionMatrix(0.0f, ClientSize.Width, 0.0f, ClientSize.Height);
            ModelMatrix      overlayModel      = new ModelMatrix();

            overlayModel.Translate(0.375f, 0.375f);

            Gl.Clear(ClearBufferMask.DepthBufferBit);

            ColorRGBAF fpsColor = ColorRGBAF.ColorGreen;
            int        fps      = senderControl.Fps;

            if (fps >= 59)
            {
                fpsColor = ColorRGBAF.ColorGreen;
            }
            else if (fps >= 29)
            {
                fpsColor = ColorRGBAF.ColorBlue;
            }
            else if (fps >= 24)
            {
                fpsColor = ColorRGBAF.ColorYellow;
            }
            else
            {
                fpsColor = ColorRGBAF.ColorRed;
            }

            fontPatch.DrawString(
                _Context, overlayProjection * overlayModel, fpsColor,
                String.Format("FPS: {0}", senderControl.Fps)
                );

            overlayModel.SetIdentity();
            overlayModel.Translate(0.375f, ClientSize.Height - 64 + 0.375f);
            fontTitle.DrawString(
                _Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen,
                "Hello Objects example ~(^.^)~"
                );

            overlayModel.SetIdentity();
            overlayModel.Translate(0.375f, ClientSize.Height - 128 + 0.375f);
            fontTitleV.DrawString(_Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen,
                                  "Hello Objects example ~(^.^)~"
                                  );
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Update framebuffer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ObjectsControl_Render(object sender, GlControlEventArgs e)
        {
            GlControl senderControl     = (GlControl)sender;
            float     senderAspectRatio = (float)senderControl.Width / senderControl.Height;

            KhronosApi.LogEnabled = false;
            KhronosApi.LogComment("--------------------------------------------------");
            KhronosApi.LogComment("*** Draw");

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

            _CubeScene.CurrentView.ProjectionMatrix = new PerspectiveProjectionMatrix(45.0f, senderAspectRatio, 0.1f, 100.0f);
            _CubeScene.CurrentView.LocalModel.SetIdentity();
            _CubeScene.CurrentView.LocalModel.Translate(_ViewStrideLat, _ViewStrideAlt, 0.0f);
            _CubeScene.CurrentView.LocalModel.RotateY(_ViewAzimuth);
            _CubeScene.CurrentView.LocalModel.RotateX(_ViewElevation);
            _CubeScene.CurrentView.LocalModel.Translate(0.0f, 0.0f, _ViewLever);
            _CubeScene.UpdateViewMatrix();

            _CubeScene.Draw(_Context);

            KhronosApi.LogEnabled = false;

            return;

            // Overlay
            ProjectionMatrix overlayProjection = new OrthoProjectionMatrix(0.0f, ClientSize.Width, 0.0f, ClientSize.Height);
            ModelMatrix      overlayModel      = new ModelMatrix();

            overlayModel.Translate(0.375f, 0.375f);

            Gl.Clear(ClearBufferMask.DepthBufferBit);

            ColorRGBAF fpsColor = ColorRGBAF.ColorGreen;
            int        fps      = (int)Math.Floor(1000.0 / senderControl.FrameSwapTime.TotalMilliseconds);

            if (fps >= 59)
            {
                fpsColor = ColorRGBAF.ColorGreen;
            }
            else if (fps >= 29)
            {
                fpsColor = ColorRGBAF.ColorBlue;
            }
            else if (fps >= 24)
            {
                fpsColor = ColorRGBAF.ColorYellow;
            }
            else
            {
                fpsColor = ColorRGBAF.ColorRed;
            }

            fontPatch.DrawString(
                _Context, overlayProjection * overlayModel, fpsColor,
                String.Format("FPS: {0}", fps)
                );

            overlayModel.SetIdentity();
            overlayModel.Translate(0.375f, ClientSize.Height - 64 + 0.375f);
            fontTitle.DrawString(
                _Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen,
                "Hello Objects example ~(^.^)~"
                );

            overlayModel.SetIdentity();
            overlayModel.Translate(0.375f, ClientSize.Height - 128 + 0.375f);
            fontTitleV.DrawString(_Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen,
                                  "Hello Objects example ~(^.^)~"
                                  );

            // Release resources
            _Context.DisposeResources();
        }
Ejemplo n.º 12
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
                ProjectionMatrix quadProj  = new OrthoProjectionMatrix(-1.0f, +1.0f, -1.0f, +1.0f);
                ModelMatrix      quadModel = new ModelMatrix();

                _Angle += 1.0f;

                quadModel.RotateZ(10.0f * Math.Cos(_Angle / 180.0 * Math.PI));

                _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", new OrthoProjectionMatrix(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", new OrthoProjectionMatrix(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);
        }