2007-07-23 02:15:18 +02:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
2007-10-05 08:52:40 +02:00
|
|
|
|
using System.Data;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
using OpenTK.Platform;
|
|
|
|
|
|
|
|
|
|
namespace OpenTK
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
/// Defines a UserControl with OpenGL rendering capabilities.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// </summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public partial class GLControl : UserControl
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
IGLContext context;
|
|
|
|
|
IPlatformIdle idle;
|
2007-10-15 13:15:34 +02:00
|
|
|
|
DisplayMode display_mode;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region --- Constructor ---
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-11-11 20:28:43 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new GLControl.
|
|
|
|
|
/// </summary>
|
2007-07-23 02:15:18 +02:00
|
|
|
|
public GLControl()
|
2007-10-15 13:15:34 +02:00
|
|
|
|
: this(null)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-11 20:28:43 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new GLControl with the specified DisplayMode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="mode"></param>
|
2007-10-15 13:15:34 +02:00
|
|
|
|
public GLControl(DisplayMode mode)
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2007-08-20 12:46:37 +02:00
|
|
|
|
|
2007-11-12 09:33:24 +01:00
|
|
|
|
this.SetStyle(ControlStyles.Opaque, true);
|
2007-08-20 12:46:37 +02:00
|
|
|
|
this.SetStyle(ControlStyles.UserPaint, true);
|
|
|
|
|
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
2007-10-15 13:15:34 +02:00
|
|
|
|
|
|
|
|
|
this.display_mode = mode;
|
2007-10-24 18:41:40 +02:00
|
|
|
|
|
|
|
|
|
this.CreateControl();
|
2007-09-02 02:19:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:46:37 +02:00
|
|
|
|
#endregion
|
2007-09-02 10:07:09 +02:00
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region --- Context Setup ---
|
2007-08-20 12:46:37 +02:00
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
protected override void OnHandleCreated(EventArgs e)
|
2007-08-20 12:46:37 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
base.OnHandleCreated(e);
|
2007-10-15 13:15:34 +02:00
|
|
|
|
this.CreateContext();
|
2007-10-05 08:52:40 +02:00
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
protected override void OnHandleDestroyed(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnHandleDestroyed(e);
|
2007-10-15 13:15:34 +02:00
|
|
|
|
this.DestroyContext();
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 10:07:09 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region --- Public Properties ---
|
|
|
|
|
|
|
|
|
|
#region public bool IsIdle
|
2007-09-02 10:07:09 +02:00
|
|
|
|
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// <summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
/// Gets a value indicating whether the current thread contains pending system messages.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// </summary>
|
2007-10-15 13:15:34 +02:00
|
|
|
|
[Browsable(false)]
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public bool IsIdle
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
get { return idle.IsIdle; }
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 10:07:09 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region public IGLContext Context
|
2007-09-02 10:07:09 +02:00
|
|
|
|
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// <summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
/// Gets an interface to the underlying GLContext used by this GLControl.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// </summary>
|
2007-10-15 13:15:34 +02:00
|
|
|
|
[Browsable(false)]
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public IGLContext Context
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
get { return context; }
|
2007-10-15 13:15:34 +02:00
|
|
|
|
private set { context = value; }
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region public float AspectRatio
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
/// Gets the aspect ratio of this GLControl.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// </summary>
|
2007-10-15 13:15:34 +02:00
|
|
|
|
[Description("The aspect ratio of the client area of this GLControl.")]
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public float AspectRatio
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
return this.ClientSize.Width / (float)this.ClientSize.Height;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public bool VSync
|
|
|
|
|
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// <summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
/// Gets or sets a value indicating whether vsync is active for this GLControl.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// </summary>
|
2007-10-15 13:15:34 +02:00
|
|
|
|
[Description("Indicates whether GLControl updates are synced to the monitor's refresh.")]
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public bool VSync
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
if (Context != null)
|
|
|
|
|
return Context.VSync;
|
2007-08-05 15:51:07 +02:00
|
|
|
|
return false;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
if (Context != null)
|
|
|
|
|
Context.VSync = value;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-15 13:15:34 +02:00
|
|
|
|
#region public DisplayMode Mode
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the DisplayMode of the GLContext attached to this GLControl.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// You cannot change the DisplayMode of an existing GLContext.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public DisplayMode Mode
|
|
|
|
|
{
|
|
|
|
|
get { return Context.Mode; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#endregion
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region --- Public Methods ---
|
|
|
|
|
|
|
|
|
|
#region public void SwapBuffers()
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
/// Swaps the front and back buffers, presenting the rendered scene to the screen.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// </summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public void SwapBuffers()
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
Context.SwapBuffers();
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region public void MakeCurrent()
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
/// Makes the underlying this GLControl current in the calling thread.
|
|
|
|
|
/// All OpenGL commands issued are hereafter interpreted by this GLControl.
|
2007-07-23 02:15:18 +02:00
|
|
|
|
/// </summary>
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public void MakeCurrent()
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
Context.MakeCurrent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-15 13:15:34 +02:00
|
|
|
|
#region public void CreateContext()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a GLContext and attaches it to this GLControl.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void CreateContext()
|
|
|
|
|
{
|
|
|
|
|
if (display_mode == null)
|
|
|
|
|
display_mode = new DisplayMode();
|
|
|
|
|
WindowInfo info = new WindowInfo(this);
|
|
|
|
|
|
|
|
|
|
if (!this.DesignMode)
|
|
|
|
|
{
|
|
|
|
|
// Mono's implementation of Windows.Forms on X11 does not allow the context to
|
|
|
|
|
// have a different colordepth from the parent. To combat this, we do not set a
|
|
|
|
|
// specific depth for the DisplayMode - we let the driver select one instead.
|
|
|
|
|
display_mode.Color = new ColorMode(0);
|
|
|
|
|
context = new GLContext(display_mode, info);
|
|
|
|
|
context.CreateContext();
|
|
|
|
|
idle = new PlatformIdle(info);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
context = new DummyGLContext(display_mode);
|
|
|
|
|
idle = new DummyPlatformIdle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void DestroyContext()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Destroys the GLContext attached to this GLControl.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <exception cref="NullReferenceException">Occurs when no GLContext is attached.</exception>
|
|
|
|
|
public void DestroyContext()
|
|
|
|
|
{
|
|
|
|
|
Context.Dispose();
|
|
|
|
|
Context = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region internal interface IPlatformIdle
|
|
|
|
|
|
|
|
|
|
internal interface IPlatformIdle
|
|
|
|
|
{
|
|
|
|
|
bool IsIdle { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class WinPlatformIdle : IPlatformIdle
|
|
|
|
|
{
|
|
|
|
|
OpenTK.Platform.Windows.MSG msg = new OpenTK.Platform.Windows.MSG();
|
|
|
|
|
object get_lock = new object();
|
2007-11-11 20:28:43 +01:00
|
|
|
|
//IntPtr handle;
|
2007-10-05 08:52:40 +02:00
|
|
|
|
|
2007-11-11 20:28:43 +01:00
|
|
|
|
public WinPlatformIdle(WindowInfo info)
|
2007-10-05 08:52:40 +02:00
|
|
|
|
{
|
2007-11-11 20:28:43 +01:00
|
|
|
|
//handle = info.Handle;
|
2007-10-05 08:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IPlatformIdle Members
|
|
|
|
|
|
|
|
|
|
public bool IsIdle
|
|
|
|
|
{
|
|
|
|
|
get
|
2007-08-20 12:46:37 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
lock (get_lock)
|
|
|
|
|
{
|
|
|
|
|
return !OpenTK.Platform.Windows.Functions.PeekMessage(ref msg, IntPtr.Zero, 0, 0, 0);
|
|
|
|
|
}
|
2007-08-20 12:46:37 +02:00
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-10-05 08:52:40 +02:00
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
internal class X11PlatformIdle : IPlatformIdle
|
|
|
|
|
{
|
|
|
|
|
object get_lock = new object();
|
|
|
|
|
IntPtr display;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
2007-11-11 20:28:43 +01:00
|
|
|
|
public X11PlatformIdle(WindowInfo info)
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-11-11 20:28:43 +01:00
|
|
|
|
display = ((OpenTK.Platform.X11.WindowInfo)info).Display;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region IPlatformIdle Members
|
|
|
|
|
|
|
|
|
|
public bool IsIdle
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (get_lock)
|
|
|
|
|
{
|
|
|
|
|
return OpenTK.Platform.X11.Functions.XPending(display) == 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class PlatformIdle : IPlatformIdle
|
|
|
|
|
{
|
|
|
|
|
IPlatformIdle implementation;
|
|
|
|
|
|
2007-11-11 20:28:43 +01:00
|
|
|
|
public PlatformIdle(WindowInfo info)
|
2007-07-23 02:15:18 +02:00
|
|
|
|
{
|
2007-10-05 08:52:40 +02:00
|
|
|
|
switch (System.Environment.OSVersion.Platform)
|
|
|
|
|
{
|
|
|
|
|
case PlatformID.Unix:
|
|
|
|
|
case (PlatformID)128:
|
|
|
|
|
implementation = new X11PlatformIdle(info);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
case PlatformID.Win32S:
|
|
|
|
|
case PlatformID.Win32Windows:
|
|
|
|
|
case PlatformID.WinCE:
|
|
|
|
|
implementation = new WinPlatformIdle(info);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
#region IPlatformIdle Members
|
|
|
|
|
|
|
|
|
|
public bool IsIdle
|
|
|
|
|
{
|
|
|
|
|
get { return implementation.IsIdle; }
|
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
2007-10-05 08:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class DummyPlatformIdle : IPlatformIdle
|
|
|
|
|
{
|
|
|
|
|
#region IPlatformIdle Members
|
2007-09-02 02:19:01 +02:00
|
|
|
|
|
2007-10-05 08:52:40 +02:00
|
|
|
|
public bool IsIdle
|
|
|
|
|
{
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|
2007-10-05 08:52:40 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-23 02:15:18 +02:00
|
|
|
|
}
|