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

Toon shader sample #60

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Version 2.5 (In Progress)

* All imported APIs now use 'SetLastError = true' to aid in analysing issues (thanks [robinsedlaczek](https://github.com/robinsedlaczek).
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ All documentation is available on [the Wiki](https://github.com/dwmkerr/sharpgl/
Credits, Sponsorship & Thanks
-----------------------------

SharpGL is written and maintained by me.
SharpGL is written and maintained by me. Special thanks go to the following contributors:

* [robinsedlaczek](https://github.com/robinsedlaczek) - Code and documentation updates, tireless patience
while I get through a backlog of work!

### NDepend ###

Expand Down
19 changes: 9 additions & 10 deletions source/SharpGL/Core/SharpGL.SceneGraph/Primitives/Teapot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,20 @@ public Material Material
/// Extensions for Array type.
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// Flattens the specified array.
/// </summary>
/// <typeparam name="T">The array type.</typeparam>
/// <param name="array">The array.</param>
/// <returns>The flattened array.</returns>
public static T[] Flatten<T>(this T[,,] array)
where T : struct
{
/// <summary>
/// Flattens the specified array.
/// </summary>
/// <typeparam name="T">The array type.</typeparam>
/// <param name="array">The array.</param>
/// <returns>The flattened array.</returns>
public static T[] Flatten<T>(this T[,,] array) where T : struct
{
int size = Marshal.SizeOf(array[0, 0, 0]);
int totalSize = Buffer.ByteLength(array);
T[] result = new T[totalSize / size];
Buffer.BlockCopy(array, 0, result, 0, totalSize);
return result;
}
}
}
}
14 changes: 9 additions & 5 deletions source/SharpGL/Core/SharpGL.WPF/BitmapConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ public static BitmapSource HBitmapToBitmapSource(IntPtr hBitmap)

try
{
bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
if (hBitmap != IntPtr.Zero)
{

bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
}
catch (Win32Exception)
{
Expand Down
64 changes: 40 additions & 24 deletions source/SharpGL/Core/SharpGL.WPF/OpenGLControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,16 @@ void timer_Tick(object sender, EventArgs e)
{
case RenderContextType.DIBSection:
{
RenderContextProviders.DIBSectionRenderContextProvider provider = gl.RenderContextProvider as RenderContextProviders.DIBSectionRenderContextProvider;

// TODO: We have to remove the alpha channel - for some reason it comes out as 0.0
// meaning the drawing comes out transparent.
FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.DIBSection.HBitmap);
newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24;
newFormatedBitmapSource.EndInit();

// Copy the pixels over.
image.Source = newFormatedBitmapSource;
var provider = gl.RenderContextProvider as RenderContextProviders.DIBSectionRenderContextProvider;
var hBitmap = provider.DIBSection.HBitmap;

if (hBitmap != IntPtr.Zero)
{
var newFormatedBitmapSource = GetFormatedBitmapSource(hBitmap);

// Copy the pixels over.
image.Source = newFormatedBitmapSource;
}
}
break;
case RenderContextType.NativeWindow:
Expand All @@ -169,18 +167,16 @@ void timer_Tick(object sender, EventArgs e)
break;
case RenderContextType.FBO:
{
RenderContextProviders.FBORenderContextProvider provider = gl.RenderContextProvider as RenderContextProviders.FBORenderContextProvider;

// TODO: We have to remove the alpha channel - for some reason it comes out as 0.0
// meaning the drawing comes out transparent.
FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap);
newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24;
newFormatedBitmapSource.EndInit();

// Copy the pixels over.
image.Source = newFormatedBitmapSource;
var provider = gl.RenderContextProvider as RenderContextProviders.FBORenderContextProvider;
var hBitmap = provider.InternalDIBSection.HBitmap;

if (hBitmap != IntPtr.Zero)
{
var newFormatedBitmapSource = GetFormatedBitmapSource(hBitmap);

// Copy the pixels over.
image.Source = newFormatedBitmapSource;
}
}
break;
default:
Expand All @@ -195,6 +191,26 @@ void timer_Tick(object sender, EventArgs e)
}
}

/// <summary>
/// This method converts the output from the OpenGL render context provider to a
/// FormatConvertedBitmap in order to show it in the image.
/// </summary>
/// <param name="hBitmap">The handle of the bitmap from the OpenGL render context.</param>
/// <returns>Returns the new format converted bitmap.</returns>
private static FormatConvertedBitmap GetFormatedBitmapSource(IntPtr hBitmap)
{
// TODO: We have to remove the alpha channel - for some reason it comes out as 0.0
// meaning the drawing comes out transparent.

FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(hBitmap);
newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24;
newFormatedBitmapSource.EndInit();

return newFormatedBitmapSource;
}

/// <summary>
/// Called when the frame rate is changed.
/// </summary>
Expand Down
5 changes: 1 addition & 4 deletions source/SharpGL/Core/SharpGL.WinForms/IdleTimeManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace SharpGL.WinForms
Expand Down Expand Up @@ -33,7 +30,7 @@ static void Application_Idle(object sender, EventArgs e)
public static event EventHandler OnIdle;

[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool PeekMessage(out IntPtr msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
}
}
Loading