private void OnRender(object sender, OpenGL.GlControlEventArgs e) { GlControl gl_control = (GlControl)sender; if ((LastMotion == null) || (LastMotion.Finished == true)) { // モーションをランダムに再生する // 名前なしのモーショングループから0~9番のモーションを乱数で選ぶ var motion_group = Asset.MotionGroups[""]; int number = new Random().Next() % motion_group.Length; var motion = (CubismMotion)motion_group[number]; LastMotion = Asset.StartMotion(CubismAsset.MotionType.Base, motion, false); } // モデルを更新する var elapsed = Timer.Elapsed; Timer.Restart(); Asset.Update(elapsed.TotalSeconds); // モデルを描画する Gl.ClearColor(0.0f, 0.5f, 0.5f, 1.0f); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.Viewport(0, 0, gl_control.Width, gl_control.Height); Matrix mvp_matrix = DenseMatrix.CreateIdentity(4); mvp_matrix[0, 0] = 2.0f; mvp_matrix[1, 1] = 2.0f * gl_control.Width / gl_control.Height; RenderingManager.Draw(mvp_matrix); }
private void glControl_ContextCreated(object sender, OpenGL.GlControlEventArgs e) { GlControl glControl = (GlControl)sender; if (Gl.CurrentExtensions != null && Gl.CurrentExtensions.DebugOutput_ARB) { Gl.DebugMessageCallback(_debugProc, IntPtr.Zero); Gl.DebugMessageControl(Gl.DebugSource.DontCare, Gl.DebugType.DontCare, Gl.DebugSeverity.DontCare, 0, null, true); } _displayManager.createDisplay(Gl.CurrentVersion); if (Gl.CurrentVersion != null && Gl.CurrentVersion.Api == KhronosVersion.ApiGl && glControl.MultisampleBits > 0) { Gl.Enable(EnableCap.Multisample); } bool result = Soil.NET.WrapSOIL.Initialize(); if (result == false) { MessageBox.Show("SOIL: Not initialized: " + Soil.NET.WrapSOIL.GetSoilLastError()); return; } _model = _loader.loadToVAO(_vertices, _textureCoords, _indices); _texture = new ModelTexture(_loader.loadTexture("image")); _textureModel = new TextureModel(_model, _texture); _shader = new StaticShader(); }
private void GlControl1_ContextCreated(object sender, OpenGL.GlControlEventArgs e) { // Here you can allocate resources or initialize state Gl.MatrixMode(MatrixMode.Projection); Gl.LoadIdentity(); Gl.Ortho(0.0, 1.0f, 0.0, 1.0, 0.0, 1.0); Gl.MatrixMode(MatrixMode.Modelview); Gl.LoadIdentity(); Gl.Enable(EnableCap.Texture2d); // Shared resource _SharedTexture = Gl.GenTexture(); Gl.BindTexture(TextureTarget.Texture2d, _SharedTexture); Bitmap sharedTexture = new Bitmap(Properties.Resources.Logo); BitmapData iBitmapData = null; try { iBitmapData = sharedTexture.LockBits(new Rectangle(0, 0, sharedTexture.Width, sharedTexture.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, sharedTexture.PixelFormat); Gl.TexImage2D(TextureTarget.Texture2d, 0, Gl.RGBA, sharedTexture.Width, sharedTexture.Height, 0, OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, iBitmapData.Scan0); } finally { if (iBitmapData != null) { sharedTexture.UnlockBits(iBitmapData); } } Gl.GenerateMipmap(Gl.TEXTURE_2D); }
private void RenderControl_ContextDestroying_GLSL(object sender, OpenGL.GlControlEventArgs e) { if (modelShader.Program != 0) { Gl.DeleteProgram(modelShader.Program); } modelShader.Program = 0; if (modelNanosuit != null) { for (int i = 0; i < modelNanosuit.meshes.Count; i++) { Gl.DeleteVertexArrays(modelNanosuit.meshes[i].VAO); Gl.DeleteBuffers(modelNanosuit.meshes[i].VBO); Gl.DeleteBuffers(modelNanosuit.meshes[i].EBO); } } if (modelNanosuit != null) { for (int i = 0; i < modelNanosuit.textures_loaded.Count; i++) { Gl.DeleteTextures(modelNanosuit.textures_loaded[i].id); } } }
/// <summary> /// Raise the event <see cref="ContextCreated"/>. /// </summary> protected virtual void OnContextCreated() { if (_ContextCreated != null) { GlControlEventArgs glControlEventArgs = new GlControlEventArgs(_DeviceContext, _RenderContext); foreach (EventHandler <GlControlEventArgs> handler in _ContextCreated.GetInvocationList()) { try { handler(this, glControlEventArgs); } catch (Exception exception) { if (_ContextCreatedExceptions == null) { _ContextCreatedExceptions = new List <Exception>(); } _ContextCreatedExceptions.Add(exception); } } if (_ContextCreatedExceptions != null) { throw new InvalidOperationException(String.Format("exception ({0}) on ContextCreated", _ContextCreatedExceptions.Count), _ContextCreatedExceptions[0]); } } }
private void OnCreated(object sender, OpenGL.GlControlEventArgs e) { // モデルをリソースから読み込む // 第2引数にファイル名からリソースを読み込む関数を与える Asset = new CubismAsset(@"hiyori_free_t06.model3.json", (string file_path) => { // リソースは拡張子を持たず、他のピリオドもアンダーバーに変換されるのでそれに応じて文字列を加工する // 普通、画像ファイルはBitmapとして読み込まれるが、resxファイルを編集してあるのでbyte[]として読み込まれる string file_name = Path.GetFileNameWithoutExtension(file_path); string resource_name = file_name.Replace('.', '_'); byte[] byte_array = (byte[])Hiyori.ResourceManager.GetObject(resource_name); return(new MemoryStream(byte_array)); }); // 自動まばたきを設定する // 自動まばたきで設定するパラメータはmodel3.json中にパラメータグループ"EyeBlink"で指定されている var eye_blink_controller = new CubismEyeBlink(Asset.ParameterGroups["EyeBlink"]); Asset.StartMotion(CubismAsset.MotionType.Effect, eye_blink_controller); // OpenGL.Netを使ったレンダラーを作成する Renderer = new CubismOpenGlNetRenderer(); RenderingManager = new CubismRenderingManager(Renderer, Asset); Timer = Stopwatch.StartNew(); }
private void RenderControl_ContextDestroying_ES(object sender, OpenGL.GlControlEventArgs e) { if (Program_Shader != 0) { Gl.DeleteProgram(Program_Shader); } Program_Shader = 0; }
private void RenderControl_ContextDestroying(object sender, OpenGL.GlControlEventArgs e) { if (_Es2_Program != 0) { Gl.DeleteProgram(_Es2_Program); } _Es2_Program = 0; }
private void GlControl_ContextCreated(object sender, OpenGL.GlControlEventArgs e) { Gl.MatrixMode(MatrixMode.Projection); Gl.LoadIdentity(); Gl.Ortho(0.0, 1.0f, 0.0, 1.0, 0.0, 1.0); Gl.MatrixMode(MatrixMode.Modelview); Gl.LoadIdentity(); }
private void OnDestroying(object sender, OpenGL.GlControlEventArgs e) { RenderingManager.Dispose(); RenderingManager = null; Asset.Dispose(); Asset = null; Renderer.Dispose(); Renderer = null; }
/// <summary> /// Ensure <see cref="_RenderContext"/> is in sync with other <see cref="GlControl"/> owning the context. /// </summary> /// <param name="sender"> /// The <see cref="GlControl"/> raising the event. /// </param> /// <param name="e"> /// A <see cref="GlControlEventArgs"/> specifying the event arguments. /// </param> private void SharingControl_ContextCreated(object sender, GlControlEventArgs e) { Debug.Assert(ReferenceEquals(_SharingControl, sender)); Debug.Assert(_RenderContext == IntPtr.Zero); // Copy context reference _RenderContext = _SharingControl._RenderContext; // Emulates event OnContextCreated(); }
/// <summary> /// Ensure <see cref="_RenderContext"/> is in sync with other <see cref="GlControl"/> owning the context. /// </summary> /// <param name="sender"> /// The <see cref="GlControl"/> raising the event. /// </param> /// <param name="e"> /// A <see cref="GlControlEventArgs"/> specifying the event arguments. /// </param> private void SharingControl_ContextDestroying(object sender, GlControlEventArgs e) { Debug.Assert(ReferenceEquals(_SharingControl, sender)); Debug.Assert(_RenderContext != IntPtr.Zero); // Emulates event OnContextDestroying(); // Loose reference _RenderContext = IntPtr.Zero; }
// method called when Render is requested via Invalidate private void glControl1_Render(object sender, OpenGL.GlControlEventArgs e) { Control senderControl = (Control)sender; Gl.Viewport(0, 0, senderControl.ClientSize.Width, senderControl.ClientSize.Height); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GraphicsHandler.SetDrawingColor1(); GcodeHandler.RedrawFullPicture(); GraphicsHandler.SetDrawingColor2(); GcodeHandler.RedrawCompletedPicture(); }
private void glControl1_ContextCreated(object sender, OpenGL.GlControlEventArgs e) { Control senderControl = (Control)sender; isContextCreated = true; Scene.Instance.SceneRenderer = new Renderer(senderControl.ClientSize.Width, senderControl.ClientSize.Height); Scene.Instance.CreateMainCamera(); //Gl.Enable(EnableCap.CullFace); Gl.Enable(EnableCap.DepthTest); }
private void GlControl2_ContextCreated(object sender, OpenGL.GlControlEventArgs e) { // Here you can allocate resources or initialize state Gl.MatrixMode(MatrixMode.Projection); Gl.LoadIdentity(); Gl.Ortho(1.0, 0.0f, 1.0, 0.0, 0.0, 1.0); Gl.MatrixMode(MatrixMode.Modelview); Gl.LoadIdentity(); Gl.Enable(EnableCap.Texture2d); }
private void glControl_Render(object sender, OpenGL.GlControlEventArgs e) { Control senderControl = (Control)sender; Gl.Viewport(0, 0, senderControl.ClientSize.Width, senderControl.ClientSize.Height); _renderer.Prepare(); _shader.Start(); _renderer.Render(_textureModel); _shader.Stop(); _displayManager.updateDisplay(); }
/// <summary> /// Raise the event <see cref="ContextCreated"/>. /// </summary> protected virtual void OnContextCreated() { if (_ContextCreated != null) { GlControlEventArgs glControlEventArgs = new GlControlEventArgs(_DeviceContext, _RenderContext); foreach (EventHandler <GlControlEventArgs> handler in _ContextCreated.GetInvocationList()) { try { handler(this, glControlEventArgs); } catch (Exception exception) { Debug.Fail(String.Format("OnContextCreated: exception ({0})\n{1}", exception.Message, exception.ToString())); } } } }
/// <summary> /// Raise the event <see cref="ContextCreated"/>. /// </summary> protected virtual void OnContextCreated() { if (_ContextCreated != null) { GlControlEventArgs glControlEventArgs = new GlControlEventArgs(_DeviceContext, _RenderContext); foreach (EventHandler <GlControlEventArgs> handler in _ContextCreated.GetInvocationList()) { try { handler(this, glControlEventArgs); } catch (Exception) { // Fail-safe } } } }
private void GlControl_Render(object sender, OpenGL.GlControlEventArgs e) { Control senderControl = (Control)sender; // FIXME I wonder why the viewport is affected when the GlControl is hosted in WPF windows. int vpx = -senderControl.ClientSize.Width; int vpy = -senderControl.ClientSize.Height; int vpw = senderControl.ClientSize.Width * 2; int vph = senderControl.ClientSize.Height * 2; Gl.Viewport(vpx, vpy, vpw, vph); Gl.Clear(ClearBufferMask.ColorBufferBit); if (Gl.CurrentVersion >= Gl.Version_110) { // Old school OpenGL 1.1 // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexColorLock = new MemoryLock(_ArrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } } else { // Old school OpenGL Gl.Begin(PrimitiveType.Triangles); Gl.Color3(1.0f, 0.0f, 0.0f); Gl.Vertex2(0.0f, 0.0f); Gl.Color3(0.0f, 1.0f, 0.0f); Gl.Vertex2(0.5f, 1.0f); Gl.Color3(0.0f, 0.0f, 1.0f); Gl.Vertex2(1.0f, 0.0f); Gl.End(); } }
private void GlControl_Render(object sender, OpenGL.GlControlEventArgs e) { var senderControl = sender as GlControl; senderControl.Animation = true; senderControl.AnimationTime = 1; int vpx = 0; int vpy = 0; int vpw = senderControl.ClientSize.Width; int vph = senderControl.ClientSize.Height; Gl.Viewport(vpx, vpy, vpw, vph); Gl.ClearColor(0.39f, 0.58f, 0.92f, 1); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); cycle(); if (chip.draw) { Draw(chip.display); } if (Gl.CurrentVersion >= Gl.Version_110) { // Old school OpenGL 1.1 // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexColorLock = new MemoryLock(_ArrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, _ArrayPosition.Length / 2); } } }
private void glControl1_ContextCreated(object sender, OpenGL.GlControlEventArgs e) { GraphicsHandler.GraphicContextCreated(); }
private void VisionControl_ContextCreated(object sender, OpenGL.GlControlEventArgs e) { // Create GL context abstraction _GraphicsContext = new GraphicsContext(e.DeviceContext, e.RenderContext); // Create texture _FramebufferTexture = new Texture2d(1024, 1024, PixelLayout.RGB24); _FramebufferTexture.SamplerParams.MagFilter = TextureMagFilter.Linear; _FramebufferTexture.SamplerParams.MinFilter = TextureMinFilter.Linear; _FramebufferTexture.Create(_GraphicsContext); // Create framebuffer _Framebuffer = new Framebuffer(); _Framebuffer.AttachColor(0, _FramebufferTexture); _Framebuffer.Create(_GraphicsContext); // Create shader (standard) _ProgramStd = _GraphicsContext.CreateProgram("OpenGL.Standard"); _ProgramStd.Create(_GraphicsContext); // Create program (standard + texture) _ProgramStdTex = _GraphicsContext.CreateProgram("OpenGL.Standard+Texture"); _ProgramStdTex.Create(_GraphicsContext); // Create vertex arrays (square) ArrayBuffer <Vertex2f> quadBuffer = new ArrayBuffer <Vertex2f>(BufferUsage.StaticDraw); quadBuffer.Create(new Vertex2f[] { new Vertex2f(-0.5f, +0.5f), new Vertex2f(-0.5f, -0.5f), new Vertex2f(+0.5f, +0.5f), new Vertex2f(+0.5f, -0.5f), }); _ArraysQuad = new VertexArrays(); _ArraysQuad.SetArray(quadBuffer, VertexArraySemantic.Position); _ArraysQuad.SetElementArray(PrimitiveType.TriangleStrip); _ArraysQuad.Create(_GraphicsContext); // Create vertex arrays (square) ArrayBuffer <Vertex2f> postquadBuffer = new ArrayBuffer <Vertex2f>(BufferUsage.StaticDraw); postquadBuffer.Create(new Vertex2f[] { new Vertex2f(0.0f, 1.0f), new Vertex2f(0.0f, 0.0f), new Vertex2f(1.0f, 1.0f), new Vertex2f(1.0f, 0.0f), }); _ArraysPostQuad = new VertexArrays(); _ArraysPostQuad.SetArray(postquadBuffer, VertexArraySemantic.Position); _ArraysPostQuad.SetArray(postquadBuffer, VertexArraySemantic.TexCoord); _ArraysPostQuad.SetElementArray(PrimitiveType.TriangleStrip); _ArraysPostQuad.Create(_GraphicsContext); // Create vertex arrays (optical markers) _BufferOpticalMarkers = new ArrayBuffer <Vertex2f>(BufferUsage.DynamicDraw); _BufferOpticalMarkers.Create(10000 * 2); _ArraysOpticalMarkers = new VertexArrays(); _ArraysOpticalMarkers.SetArray(_BufferOpticalMarkers, VertexArraySemantic.Position); _ArraysOpticalMarkers.SetElementArray(PrimitiveType.Lines); _ArraysOpticalMarkers.Create(_GraphicsContext); }
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); }
private void OnUpdate(object sender, OpenGL.GlControlEventArgs e) { }