Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure the control to allow transparency #71

Merged
merged 2 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Example/ExampleScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static class ExampleScene {
GL.Enable(EnableCap.ScissorTest);
}

public static void Render() {
public static void Render(float alpha = 1.0f) {
var hue = (float) _stopwatch.Elapsed.TotalSeconds * 0.15f % 1;
var c = Color4.FromHsv(new Vector4(hue, 0.75f, 0.75f, 1));
var c = Color4.FromHsv(new Vector4(alpha * hue, alpha * 0.75f, alpha * 0.75f, alpha));
GL.ClearColor(c);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.LoadIdentity();
Expand Down
16 changes: 14 additions & 2 deletions src/Example/TabbedMainWindowTest.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@
x:Name="Control2"
Render="Control2_OnRender" />
</TabItem>
<TabItem Header="Blank 3">
</TabItem>
<TabItem Header="Control 3">
<Grid>
<Label
Background="#FFE6931C"
Content="BEHIND CONTROL"
FontSize="50px"
HorizontalContentAlignment="Center"
Padding="45px 180px 0px 0px" />

<glWpfControl:GLWpfControl
x:Name="Control3"
Render="Control3_OnRender" />
</Grid>
</TabItem>
<TabItem Header="Blank 4">
</TabItem>
</glWpfControl:NonReloadingTabControl>
Expand Down
7 changes: 7 additions & 0 deletions src/Example/TabbedMainWindowTest.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public sealed partial class TabbedMainWindowTest
Control1.Start(mainSettings);
var insetSettings = new GLWpfControlSettings {MajorVersion = 4, MinorVersion = 5, GraphicsProfile = ContextProfile.Compatability, GraphicsContextFlags = ContextFlags.Debug};
Control2.Start(insetSettings);
var transparentSettings = new GLWpfControlSettings { MajorVersion = 4, MinorVersion = 5, GraphicsProfile = ContextProfile.Compatability, GraphicsContextFlags = ContextFlags.Debug, TransparentBackground = true};
Control3.Start(transparentSettings);
}

private void OpenTkControl_OnRender(TimeSpan delta) {
Expand All @@ -28,5 +30,10 @@ public sealed partial class TabbedMainWindowTest
private void Control1_OnRender(TimeSpan delta) {
ExampleScene.Render();
}

private void Control3_OnRender(TimeSpan delta)
{
ExampleScene.Render(0.0f);
}
}
}
4 changes: 2 additions & 2 deletions src/GLWpfControl/DxGLFramebuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal sealed class DxGLFramebuffer : IDisposable {
public ScaleTransform FlipYTransform { get; }


public DxGLFramebuffer([NotNull] DxGlContext context, int width, int height, double dpiScaleX, double dpiScaleY) {
public DxGLFramebuffer([NotNull] DxGlContext context, int width, int height, double dpiScaleX, double dpiScaleY, Format format) {
DxGlContext = context;
Width = width;
Height = height;
Expand All @@ -65,7 +65,7 @@ internal sealed class DxGLFramebuffer : IDisposable {
context.DxDeviceHandle,
FramebufferWidth,
FramebufferHeight,
Format.X8R8G8B8,// this is like A8 R8 G8 B8, but avoids issues with Gamma correction being applied twice.
format,// this is like A8 R8 G8 B8, but avoids issues with Gamma correction being applied twice.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this comment hold up? Is gamma correction applied twice with the A8R8G8B8 format?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what that comment is referring to. Enabling automatic gamma correction by including GL.Enable(EnableCap.FramebufferSrgb); on the example, does not have any effect on final render. But just in case, this is reasoning behind making this format configurable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this comment on my last commit. If there is a specific example where gamma correction is a problem, I could take a look.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@varon do you remember what this is about?
The comment was added in this commit: 4bb4064

MultisampleType.None,
0,
false,
Expand Down
6 changes: 4 additions & 2 deletions src/GLWpfControl/GLWpfControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows;
using System.Windows.Media;
using JetBrains.Annotations;
using OpenTK.Wpf.Interop;

namespace OpenTK.Wpf
{
Expand Down Expand Up @@ -125,12 +126,13 @@ public void Start(GLWpfControlSettings settings)
dpiScaleY = transformToDevice.M22;
}
}
_renderer?.SetSize((int) RenderSize.Width, (int) RenderSize.Height, dpiScaleX, dpiScaleY);
var format = _settings.TransparentBackground ? Format.A8R8G8B8 : Format.X8R8G8B8;
_renderer?.SetSize((int) RenderSize.Width, (int) RenderSize.Height, dpiScaleX, dpiScaleY, format);
}

private void OnUnloaded()
{
_renderer?.SetSize(0,0, 1, 1);
_renderer?.SetSize(0,0, 1, 1, Format.X8R8G8B8);
NogginBops marked this conversation as resolved.
Show resolved Hide resolved
}

private void OnCompTargetRender(object sender, EventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions src/GLWpfControl/GLWpfControlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public GLWpfControlRenderer(GLWpfControlSettings settings)
}


public void SetSize(int width, int height, double dpiScaleX, double dpiScaleY) {
public void SetSize(int width, int height, double dpiScaleX, double dpiScaleY, Format format) {
if (_framebuffer == null || _framebuffer.Width != width || _framebuffer.Height != height) {
_framebuffer?.Dispose();
_framebuffer = null;
if (width > 0 && height > 0) {
_framebuffer = new DxGLFramebuffer(_context, width, height, dpiScaleX, dpiScaleY);
_framebuffer = new DxGLFramebuffer(_context, width, height, dpiScaleX, dpiScaleY, format);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/GLWpfControl/GLWpfControlSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public sealed class GLWpfControlSettings {
/// This setting may be useful to get extra performance on mobile platforms.
public bool UseDeviceDpi { get; set; } = true;

// If this parameter is set to true, the alpha channel of the color passed to the function GL.ClearColor
// will determine the level of transparency of this control
NogginBops marked this conversation as resolved.
Show resolved Hide resolved
public bool TransparentBackground { get; set; } = false;

/// May be null. If defined, an external context will be used, of which the caller is responsible
/// for managing the lifetime and disposal of.
public IGraphicsContext ContextToUse { get; set; }
Expand All @@ -39,7 +43,8 @@ public sealed class GLWpfControlSettings {
MajorVersion = MajorVersion,
MinorVersion = MinorVersion,
RenderContinuously = RenderContinuously,
UseDeviceDpi = UseDeviceDpi
UseDeviceDpi = UseDeviceDpi,
TransparentBackground = TransparentBackground
};
return c;
}
Expand Down