From d01867b37441f5b3441d1740b2bc5ede7279e63b Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Fri, 28 Nov 2025 08:26:07 +0100 Subject: [PATCH] PckStudio(SceneViewport) - Add 'Initialize' function --- PCK-Studio/Rendering/ModelRenderer.cs | 17 +++++------ PCK-Studio/Rendering/SceneViewport.cs | 43 ++++++++++++++++++++------- PCK-Studio/Rendering/SkinRenderer.cs | 10 +------ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/PCK-Studio/Rendering/ModelRenderer.cs b/PCK-Studio/Rendering/ModelRenderer.cs index 8bfdc817..cc1e05dd 100644 --- a/PCK-Studio/Rendering/ModelRenderer.cs +++ b/PCK-Studio/Rendering/ModelRenderer.cs @@ -58,6 +58,10 @@ namespace PckStudio.Rendering private Image _modelTexture; private Texture2D _modelRenderTexture; private List> _rootCollection; + private HighlightInfo _highlightingInfo; + + private readonly Vector3 modelOffset = Vector3.UnitY * 24f; + private struct HighlightInfo { public static readonly HighlightInfo Empty = new HighlightInfo(Vector3.Zero, Vector3.Zero, BoundingBox.Empty); @@ -78,24 +82,16 @@ namespace PckStudio.Rendering BoundingBox = boundingBox; } } - private HighlightInfo _highlightingInfo; - - private readonly Vector3 modelOffset = Vector3.UnitY * 24f; public ModelRenderer() : base(fov: 60f) { InitializeComponent(); _rootCollection = new List>(5); - if (!DesignMode) - { - InitializeShaders(); - } + } - private void InitializeShaders() + protected override void Initialize() { - Debug.Assert(Context.IsCurrent); - // render texture { _modelRenderTexture = new Texture2D(); @@ -172,6 +168,7 @@ namespace PckStudio.Rendering GL.Disable(EnableCap.Blend); GL.BlendFunc(BlendingFactor.One, BlendingFactor.Zero); } + ResetCamera(); _currentModelName = model.Name; } diff --git a/PCK-Studio/Rendering/SceneViewport.cs b/PCK-Studio/Rendering/SceneViewport.cs index 5b105c96..ae7cca25 100644 --- a/PCK-Studio/Rendering/SceneViewport.cs +++ b/PCK-Studio/Rendering/SceneViewport.cs @@ -66,10 +66,12 @@ namespace PckStudio.Rendering set { base.BackColor = value; - if (!DesignMode) + if (!DesignMode && Context.IsCurrent) Renderer.SetClearColor(value); - } } + } + + public bool IsInitialized => _initialized; protected new bool DesignMode => base.DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime; @@ -147,7 +149,13 @@ namespace PckStudio.Rendering Camera = new PerspectiveCamera(fov, camareaPosition); _shaderLibrary = new ShaderLibrary(); + } + protected virtual void Initialize() { } + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); if (!DesignMode) { _timer = new Timer(); @@ -168,6 +176,12 @@ namespace PckStudio.Rendering _timer.Start(); } + protected override void OnGotFocus(EventArgs e) + { + GetContext(); + base.OnGotFocus(e); + } + private void InitializeInternal() { if (_initialized) @@ -175,7 +189,7 @@ namespace PckStudio.Rendering Debug.Fail("Already Initialized."); return; } - MakeCurrent(); + GetContext(); Trace.TraceInformation(GL.GetString(StringName.Version)); GL.DebugMessageCallback(DebugProc, this.Handle); AddShader("Internal_colorShader", Resources.plainColorVertexShader, Resources.plainColorFragmentShader); @@ -206,6 +220,7 @@ namespace PckStudio.Rendering #endif InitializeDebugComponents(); InitializeDebugShaders(); + Initialize(); _initialized = true; } @@ -280,6 +295,7 @@ namespace PckStudio.Rendering protected void DrawBoundingBox(Matrix4 transform, Core.BoundingBox boundingBox, Color color) { + GetContext(); ShaderProgram colorShader = _shaderLibrary.GetShader("Internal_colorShader"); colorShader.Bind(); Matrix4 viewProjection = Camera.GetViewProjection(); @@ -304,6 +320,7 @@ namespace PckStudio.Rendering { string dbgMessage = Marshal.PtrToStringAnsi(message, length); Debug.WriteLine($"{source}:{id} {type} {severity}: {dbgMessage}"); + Debugger.Break(); } [Conditional("DEBUG")] @@ -432,6 +449,7 @@ namespace PckStudio.Rendering base.OnPaint(e); if (DesignMode) return; + GetContext(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.DepthTest); // Enable correct Z Drawings } @@ -441,13 +459,17 @@ namespace PckStudio.Rendering base.OnSizeChanged(e); if (DesignMode) return; - if (Camera is not null) - { - Camera.ViewportSize = ClientSize; - } + GetContext(); + Camera?.ViewportSize = ClientSize; Renderer.SetViewportSize(Camera.ViewportSize); } + private void GetContext() + { + if (!Context.IsCurrent) + MakeCurrent(); + } + protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); @@ -469,7 +491,7 @@ namespace PckStudio.Rendering goto default; case MouseButtons.Right: if (AllowCameraMovement) - Camera.Pan(deltaX, deltaY); + Camera.Pan(deltaX, deltaY); goto default; default: if (LockMousePosition) @@ -549,6 +571,7 @@ namespace PckStudio.Rendering { #if DEBUG d_debugLabel.Text = Camera.ToString(); + GetContext(); GL.Disable(EnableCap.Blend); GL.DepthMask(false); GL.DepthFunc(DepthFunction.Always); @@ -601,7 +624,7 @@ namespace PckStudio.Rendering // // contextMenuStrip1 // - debugContextMenuStrip1.Items.AddRange(new ToolStripItem[] {}); + debugContextMenuStrip1.Items.AddRange(new ToolStripItem[] { }); debugContextMenuStrip1.Name = "contextMenuStrip1"; debugContextMenuStrip1.Size = new Size(159, 48); // @@ -648,4 +671,4 @@ namespace PckStudio.Rendering #endif } -} +} diff --git a/PCK-Studio/Rendering/SkinRenderer.cs b/PCK-Studio/Rendering/SkinRenderer.cs index 95370103..c90dafa9 100644 --- a/PCK-Studio/Rendering/SkinRenderer.cs +++ b/PCK-Studio/Rendering/SkinRenderer.cs @@ -87,8 +87,6 @@ namespace PckStudio.Rendering [Category("Appearance")] public Color HighlightlingColor { get; set; } = Color.Aqua; - public bool IsInitialized => initialized; - public int SelectedIndex { get => selectedIndices.Length > 0 ? selectedIndices[0] : -1; @@ -262,8 +260,6 @@ namespace PckStudio.Rendering new Vector3(-1.0f, 1.0f, -1.0f) }; - private bool initialized = false; - public SkinRenderer() : base(fov: 60f) { InitializeSkinData(); @@ -294,15 +290,11 @@ namespace PckStudio.Rendering ModelData.CollectionChanged += ModelData_CollectionChanged; } - public void Initialize(bool inflateOverlayParts) + protected override void Initialize() { - if (initialized) - Debug.Fail("Already Initialized!"); - autoInflateOverlayParts = inflateOverlayParts; InitializeShaders(); Renderer.SetClearColor(BackColor); GLErrorCheck(); - initialized = true; } private const float DefaultCameraDistance = 64f;