Skip to content

Commit

Permalink
Merge pull request #71 from luiscuenca/transparentBackground
Browse files Browse the repository at this point in the history
Configure the control to allow transparency
  • Loading branch information
NogginBops committed Jun 18, 2022
2 parents bf48285 + 4019968 commit 9ae5542
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
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 void Ready() {
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 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 @@ private void Control2_OnRender(TimeSpan delta) {
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 @@ public DxGLFramebuffer([NotNull] DxGlContext context, int width, int height, dou
context.DxDeviceHandle,
FramebufferWidth,
FramebufferHeight,
Format.X8R8G8B8,// this is like A8 R8 G8 B8, but avoids issues with Gamma correction being applied twice.
format,
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 @@ private void SetupRenderSize() {
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);
}

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
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 @@ internal GLWpfControlSettings Copy() {
MajorVersion = MajorVersion,
MinorVersion = MinorVersion,
RenderContinuously = RenderContinuously,
UseDeviceDpi = UseDeviceDpi
UseDeviceDpi = UseDeviceDpi,
TransparentBackground = TransparentBackground
};
return c;
}
Expand Down

0 comments on commit 9ae5542

Please sign in to comment.