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); } }
private void RenderControl_Render_GLSL(object sender, GlControlEventArgs e) { Control control = (Control)sender; PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(45.0f, (float)control.Width / (float)control.Height, 0.1f, 100.0f); ModelMatrix viewMatrix = new ModelMatrix(); ModelMatrix modelMatrix = new ModelMatrix(); modelMatrix.Translate(new Vertex3f(modelPosition.X, modelPosition.Y, modelPosition.Z)); modelMatrix.Scale(new Vertex3f(0.2f, 0.2f, 0.2f)); Gl.Viewport(0, 0, control.Width, control.Height); Gl.ClearColor(0.05f, 0.05f, 0.05f, 1.0f); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); modelShader.Use(); modelMatrix.RotateX(modelAngle.X); modelMatrix.RotateY(modelAngle.Y); modelMatrix.RotateZ(modelAngle.Z); //viewMatrix.Translate(new Vertex3f(-2.0f * (float)Math.Sin(modelAngle.Y * PI_OVER_180), 0.0f, -2.0f*(float)Math.Cos(modelAngle.Y*PI_OVER_180))); Gl.UniformMatrix4(modelShader.uLocation_Projection, 1, false, projectionMatrix.ToArray()); Gl.UniformMatrix4(modelShader.uLocation_View, 1, false, viewMatrix.ToArray()); Gl.UniformMatrix4(modelShader.uLocation_Model, 1, false, modelMatrix.ToArray()); modelNanosuit.Draw(modelShader); }
private static void NativeWindow_Render(object sender, NativeWindowEventArgs e) { NativeWindow nativeWindow = (NativeWindow)sender; Gl.Viewport(0, 0, (int)nativeWindow.Width, (int)nativeWindow.Height); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.UseProgram(_CubeEdgeProgram); // Compute MVP PerspectiveProjectionMatrix proj = new PerspectiveProjectionMatrix(60.0f, (float)nativeWindow.Width / nativeWindow.Height, 0.5f, 1e6f); ModelMatrix view = new ModelMatrix(); ModelMatrix model = new ModelMatrix(); model.RotateY(_Angle); model.RotateZ(_Angle); view.LookAtTarget(Vertex3f.One * 7.0f, Vertex3f.Zero, Vertex3f.UnitY); Gl.BindVertexArray(_CubeVao); Gl.UniformMatrix4(_CubeEdgeProgram_Location_uMVP, false, (proj * view * model).ToArray()); foreach (float scale4d in new float[] { 64.0f, 32.0f, 16.0f, 8.0f, 4.0f, 2.0f, 1.0f, 0.5f, 0.25f, 0.125f }) { Gl.Uniform1(_CubeEdgeProgram_Location_uScale4D, scale4d * _Zooom); Gl.Uniform4(_CubeEdgeProgram_Location_uColor, 0.0f, 0.3f, 1.0f, Math.Min(1.0f, scale4d * _Zooom / 2.0f)); Gl.DrawElements(PrimitiveType.Lines, _CubeEdges.Length, DrawElementsType.UnsignedShort, IntPtr.Zero); } _Angle += 360.0f / (25.0f * 5); _Zooom -= 0.025f; if (_Zooom < 0.5f) { _Zooom = 1.0f; } // Save PNG frame if (_FrameNo < 125) { using (Bitmap bitmap = new Bitmap((int)nativeWindow.Width, (int)nativeWindow.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { BitmapData bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Gl.ReadPixels(0, 0, (int)nativeWindow.Width, (int)nativeWindow.Height, OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, bitmapData.Scan0); bitmap.Save(String.Format("Frame_{0:D3}.png", _FrameNo++)); } } }
private void RenderControl_Render_ES(object sender, GlControlEventArgs e) { Control control = (Control)sender; PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(45.0f, (float)control.Width / (float)control.Height, 0.1f, 100.0f); ModelMatrix viewMatrix = new ModelMatrix(); ModelMatrix modelMatrix = new ModelMatrix(); // Move camera viewMatrix.Translate(new Vertex3f(0.0f, 0.0f, -2.0f)); // Animate triangle /*modelMatrix.LookAtDirection( * new Vertex3f(0.0f, 0.0f, 0.0f), * new Vertex3f( * (float)Math.Sin(angle_rad), * 0.0f, * (float)Math.Cos(angle_rad) * ), * new Vertex3f(0.0f, 1.0f, 0.0f) * );*/ //Quaternion Q = new Quaternion(new Vertex3f(0.0f, 1.0f, 0.0f), angle); modelMatrix.RotateZ(angle); modelMatrix.RotateY(angle); //modelMatrix.Translate(Math.Cos(theta), Math.Sin(theta)); //modelMatrix.RotateY(theta); Gl.UseProgram(Program_Shader); Gl.Viewport(0, 0, control.Width, control.Height); Gl.Enable(EnableCap.DepthTest); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)Program_Location_aPosition, 3, Gl.FLOAT, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)Program_Location_aPosition); Gl.VertexAttribPointer((uint)Program_Location_aColor, 3, Gl.FLOAT, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)Program_Location_aColor); Gl.UniformMatrix4(Program_Location_uProjection, 1, false, projectionMatrix.ToArray()); Gl.UniformMatrix4(Program_Location_uView, 1, false, viewMatrix.ToArray()); Gl.UniformMatrix4(Program_Location_uModel, 1, false, modelMatrix.ToArray()); Gl.DrawArrays(PrimitiveType.Triangles, 0, 36); } }
static Matrix4x4Test() { Random random = new Random(); _MatArray = new Matrix4x4[MulArraySize]; for (int i = 0; i < _MatArray.Length; i++) { ModelMatrix modelMatrix = new ModelMatrix(); modelMatrix.RotateX(random.NextDouble() * 360.0); modelMatrix.RotateY(random.NextDouble() * 360.0); modelMatrix.RotateZ(random.NextDouble() * 360.0); modelMatrix.Translate(random.NextDouble(), random.NextDouble(), random.NextDouble()); _MatArray[i] = modelMatrix; } }
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); }