Skip to content

Commit

Permalink
Updated Sample and addressed #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomCrypto committed Nov 28, 2014
1 parent 58af2fc commit 3f8df7a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 21 deletions.
31 changes: 31 additions & 0 deletions Library/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public sealed class Context : IEnumerable<Bar>, IDisposable
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Int32 identifier;

/// <summary>
/// Gets this context's last set window size. This is used for release/reset.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Size size;

/// <summary>
/// Checks whether a graphics API requires a device pointer.
/// </summary>
Expand Down Expand Up @@ -67,6 +73,25 @@ public void Draw()
Tw.Draw(); // Applies to all bars
}

/// <summary>
/// Releases all graphics resources used by this context. They can be reset with <see cref="AntTweakBar.Context.ResetResources"/>.
/// </summary>
/// <remarks>
/// This does not affect the bars and variables in this context in any way.
/// </remarks>
public void ReleaseResources()
{
HandleResize(Size.Empty);
}

/// <summary>
/// Resets all graphics resources used by this context.
/// </summary>
public void ResetResources()
{
HandleResize(size);
}

#region Event Handlers

/// <summary>
Expand All @@ -77,6 +102,10 @@ public void HandleResize(Size size)
{
Tw.SetCurrentWindow(Identifier);
Tw.WindowSize(size.Width, size.Height);

if (size != Size.Empty) {
this.size = size;
}
}

/// <summary>
Expand Down Expand Up @@ -233,6 +262,8 @@ private void Dispose(bool disposing)
bars.First().Dispose();
}

ReleaseResources();

lock (lk)
{
if (--contextCounter == 0) {
Expand Down
4 changes: 2 additions & 2 deletions Library/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ public static void Draw()
/// <param name="height">Height of the graphics window.</param>
public static void WindowSize(int width, int height)
{
if (width <= 0) {
if ((width == 0) && (height > 0)) {
throw new ArgumentOutOfRangeException("width");
} else if (height <= 0) {
} else if ((height == 0) && (width > 0)) {
throw new ArgumentOutOfRangeException("height");
}

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ Any issues or pull requests are welcome, I especially need help with verifying m
Changelog
---------

Next release:

- changed `Tw.WindowSize` to accept sizes of (0, 0) to allow AntTweakBar resource cleanup (see [#3](https://github.com/TomCrypto/AntTweakBar.NET/issues/3))
- added `ReleaseResources` and `ResetResources` methods to the `Context` class (see [#3](https://github.com/TomCrypto/AntTweakBar.NET/issues/3))
- changed Sample to use GLSL version 120, fixed shader saving overwrite bug and close on Esc.

28 November 2014 (v0.4.4)

- fixed an interop bug for 32-bit Windows (see [#1](https://github.com/TomCrypto/AntTweakBar.NET/issues/1))
Expand Down
2 changes: 1 addition & 1 deletion Sample/Fractal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Size Dimensions
public int Iterations
{
get { return iterations; }
set { GL.Uniform1(GL.GetUniformLocation(shHandle, "iters"), (uint)(iterations = value)); }
set { GL.Uniform1(GL.GetUniformLocation(shHandle, "iters"), iterations = value); }
}

private bool hardcodePolynomial;
Expand Down
6 changes: 5 additions & 1 deletion Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ protected override void OnLoad(EventArgs _)

KeyPress += (sender, e) => context.HandleKeyPress(e.KeyChar);
Resize += (sender, e) => context.HandleResize(ClientSize);
KeyDown += (sender, e) => HandleKeyPress(context, e);
KeyDown += (sender, e) => {
if (!HandleKeyPress(context, e) && (e.Key == Key.Escape)) {
Close(); // Close program on Esc when appropriate
}
};

Mouse.WheelChanged += (sender, e) => fractal.ZoomIn(e.DeltaPrecise);
Mouse.Move += (sender, e) => context.HandleMouseMove(e.Position);
Expand Down
31 changes: 14 additions & 17 deletions Sample/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public static String VertShader()
{
var str = new StringBuilder();

str.AppendLine("#version 130");
str.AppendLine("#version 120");
str.AppendLine();
str.AppendLine("/* DO NOT EDIT - AUTOGENERATED */");
str.AppendLine();
str.AppendLine("in vec3 vertexPosition;");
str.AppendLine("out vec2 uv;");
str.AppendLine("attribute vec3 vertexPosition;");
str.AppendLine("varying vec2 uv;");
str.AppendLine();
str.AppendLine("void main(void)");
str.AppendLine("{");
Expand All @@ -41,7 +41,7 @@ public static String FragShader(Polynomial poly, ShadingType type, AAQuality aa,
{
var shader = String.Join("\n", new[]
{
"#version 130\n",
"#version 120\n",
"/* DO NOT EDIT - AUTOGENERATED */\n",
FragArithmetic(),
FragPolyRoots(poly, "poly", hardcodePoly),
Expand All @@ -59,7 +59,7 @@ public static String FragShader(Polynomial poly, ShadingType type, AAQuality aa,

private static void WriteToFile(String path, String shader)
{
var stream = new StreamWriter(File.OpenWrite(path));
var stream = new StreamWriter(File.Open(path, FileMode.Create));
stream.Write(shader);
stream.Close();
}
Expand Down Expand Up @@ -142,13 +142,14 @@ private static String FragIterate()

str.AppendLine("uniform vec2 aCoeff;");
str.AppendLine("uniform vec2 kCoeff;");
str.AppendLine("uniform uint iters;");
str.AppendLine("uniform int iters;");
str.AppendLine();
str.AppendLine("vec2 iterate(vec2 z, out float speed, out uint t)");
str.AppendLine("vec4 iterate(vec2 z)");
str.AppendLine("{");
str.AppendLine(" speed = 0;");
str.AppendLine(" float speed;");
str.AppendLine(" int t;");
str.AppendLine();
str.AppendLine(" for (t = uint(0); t < iters; ++t)");
str.AppendLine(" for (t = int(0); t < iters; ++t)");
str.AppendLine(" {");
str.AppendLine(" vec2 r = z;");
str.AppendLine(" z -= cmul(cdiv(poly(z), derv(z)), aCoeff) + kCoeff;");
Expand All @@ -157,7 +158,7 @@ private static String FragIterate()
str.AppendLine(" if (l < 1e-8) break;");
str.AppendLine(" }");
str.AppendLine();
str.AppendLine(" return z;");
str.AppendLine(" return vec4(z, speed, float(t));");
str.AppendLine("}");

return str.ToString();
Expand All @@ -175,14 +176,12 @@ private static String FragColorize(ShadingType type)
switch (type)
{
case ShadingType.Standard:
str.AppendLine(" if (isnan(speed)) return vec3(1);");
str.AppendLine(" speed *= speed * 0.05;");
str.AppendLine(" vec3 retval = (sin(vec3(r.x) * palette.xyz) + sin(vec3(r.y) * palette.xyz) + 2) * speed;");
str.AppendLine(" return retval / (retval + vec3(1));");
break;
case ShadingType.Negative:
str.AppendLine(" if (speed == 0) return vec3(1);");
str.AppendLine(" if (isnan(speed)) return vec3(0);");
str.AppendLine(" vec3 retval = (sin(vec3(r.x) * palette.xyz) + sin(vec3(r.y) * palette.xyz) + 2) / speed;");
str.AppendLine(" return retval / (retval + vec3(1));");
break;
Expand All @@ -207,10 +206,8 @@ private static String FragShade()
str.AppendLine();
str.AppendLine("vec3 shade(vec2 z)");
str.AppendLine("{");
str.AppendLine(" uint t;");
str.AppendLine(" float speed;");
str.AppendLine(" vec2 r = iterate(z, speed, t);");
str.AppendLine(" return colorize(z, r, pow(speed, intensity), t * intensity / iters);");
str.AppendLine(" vec4 r = iterate(z);");
str.AppendLine(" return colorize(z, r.xy, pow(r.z, intensity), r.w * r.z / iters);");
str.AppendLine("}");

return str.ToString();
Expand All @@ -223,7 +220,7 @@ private static String FragMainSampler(AAQuality aa)
str.AppendLine("uniform vec2 offset;");
str.AppendLine("uniform float zoom;");
str.AppendLine("uniform vec2 dims;");
str.AppendLine("in vec2 uv;");
str.AppendLine("varying vec2 uv;");
str.AppendLine();
str.AppendLine("vec3 plot_fractal(vec2 z)");
str.AppendLine("{");
Expand Down

0 comments on commit 3f8df7a

Please sign in to comment.