2007-07-23 00:15:18 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* Contributions from Erik Ylvisaker
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
using OpenTK.OpenGL;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
|
{
|
|
|
|
|
public sealed class WinGLContext : OpenTK.Platform.IGLContext, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private IntPtr deviceContext;
|
|
|
|
|
private IntPtr renderContext;
|
|
|
|
|
static private IntPtr opengl32Handle;
|
|
|
|
|
static private readonly string opengl32Name = "OPENGL32.DLL";
|
|
|
|
|
private IntPtr windowHandle;
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private DisplayMode mode;
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
private bool disposed;
|
|
|
|
|
|
|
|
|
|
#region --- Contructors ---
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
public WinGLContext()
|
2007-09-02 00:16:22 +00:00
|
|
|
|
: this(new DisplayMode(640, 480, new ColorMode(32), 16, 0, 0, 2, false, false, false, 0.0f))
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
public WinGLContext(DisplayMode mode)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
Trace.WriteLine(String.Format("Creating opengl context (driver: {0})", this.ToString()));
|
2007-08-20 12:25:48 +00:00
|
|
|
|
this.mode = mode;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
#region public void PrepareContext(IntPtr handle)
|
|
|
|
|
|
|
|
|
|
public void PrepareContext(IntPtr handle)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
this.windowHandle = handle;
|
|
|
|
|
Debug.WriteLine(String.Format("OpenGL context is bound to handle: {0}", windowHandle));
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
// Dynamically load the OpenGL32.dll in order to use the extension loading capabilities of Wgl.
|
|
|
|
|
if (opengl32Handle == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
opengl32Handle = API.LoadLibrary(opengl32Name);
|
|
|
|
|
if (opengl32Handle == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
//System.Diagnostics.Debug.WriteLine("LoadLibrary({0}) set error code: {1}. Will not load extensions.", _dll_name, error_code);
|
|
|
|
|
throw new ApplicationException(
|
|
|
|
|
String.Format(
|
|
|
|
|
"LoadLibrary(\"{0}\") call failed with code {1}",
|
|
|
|
|
opengl32Name,
|
|
|
|
|
Marshal.GetLastWin32Error()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Debug.WriteLine(String.Format("Loaded opengl32.dll: {0}", opengl32Handle));
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deviceContext = API.GetDC(windowHandle);
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Debug.WriteLine(String.Format("Device context: {0}", deviceContext));
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Debug.Write("Setting pixel format... ");
|
2007-09-02 00:16:22 +00:00
|
|
|
|
PixelFormatDescriptor pixelFormat = new PixelFormatDescriptor();
|
2007-08-20 12:25:48 +00:00
|
|
|
|
pixelFormat.Size = API.PixelFormatDescriptorSize;
|
|
|
|
|
pixelFormat.Version = API.PixelFormatDescriptorVersion;
|
|
|
|
|
pixelFormat.Flags =
|
2007-09-02 00:16:22 +00:00
|
|
|
|
PixelFormatDescriptorFlags.SUPPORT_OPENGL |
|
|
|
|
|
PixelFormatDescriptorFlags.DRAW_TO_WINDOW;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
pixelFormat.ColorBits = (byte)(mode.Color.Red + mode.Color.Green + mode.Color.Blue);
|
2007-08-20 12:25:48 +00:00
|
|
|
|
if (mode.Color.IsIndexed)
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
pixelFormat.PixelType = PixelType.INDEXED;
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
pixelFormat.PixelType = PixelType.RGBA;
|
2007-08-20 12:25:48 +00:00
|
|
|
|
pixelFormat.RedBits = (byte)mode.Color.Red;
|
|
|
|
|
pixelFormat.GreenBits = (byte)mode.Color.Green;
|
|
|
|
|
pixelFormat.BlueBits = (byte)mode.Color.Blue;
|
|
|
|
|
pixelFormat.AlphaBits = (byte)mode.Color.Alpha;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
/*
|
|
|
|
|
if (accum != null)
|
|
|
|
|
{
|
|
|
|
|
pixelFormat.AccumBits = (byte)(accum.Red + accum.Green + accum.Blue);
|
|
|
|
|
pixelFormat.AccumRedBits = (byte)accum.Red;
|
|
|
|
|
pixelFormat.AccumGreenBits = (byte)accum.Green;
|
|
|
|
|
pixelFormat.AccumBlueBits = (byte)accum.Blue;
|
|
|
|
|
pixelFormat.AccumAlphaBits = (byte)accum.Alpha;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
pixelFormat.DepthBits = (byte)mode.DepthBits;
|
|
|
|
|
pixelFormat.StencilBits = (byte)mode.StencilBits;
|
|
|
|
|
|
|
|
|
|
if (mode.DepthBits <= 0)
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
pixelFormat.Flags |= PixelFormatDescriptorFlags.DEPTH_DONTCARE;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode.Stereo)
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
pixelFormat.Flags |= PixelFormatDescriptorFlags.STEREO;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode.Buffers > 1)
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
pixelFormat.Flags |= PixelFormatDescriptorFlags.DOUBLEBUFFER;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: More elaborate mode setting, using DescribePixelFormat.
|
2007-08-20 12:25:48 +00:00
|
|
|
|
/*
|
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
int pixel = Wgl.Imports.ChoosePixelFormat(deviceContext, &pixelFormat);
|
|
|
|
|
|
|
|
|
|
if (pixel == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("The requested pixel format is not supported by the hardware configuration.");
|
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Wgl.Imports.SetPixelFormat(deviceContext, pixel, &pixelFormat);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Debug.WriteLine(String.Format("done! (format: {0})", pixel));
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
int pixel = API.ChoosePixelFormat(deviceContext, ref pixelFormat);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
if (pixel == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("The requested pixel format is not supported by the hardware configuration.");
|
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
API.SetPixelFormat(deviceContext, pixel, ref pixelFormat);
|
|
|
|
|
|
|
|
|
|
Debug.Print("done! (format: {0})", pixel);
|
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
#endregion
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
#region public void CreateContext()
|
|
|
|
|
|
|
|
|
|
public void CreateContext()
|
|
|
|
|
{
|
|
|
|
|
Debug.Write("Creating render context... ");
|
|
|
|
|
// Do not rely on OpenTK.Platform.Windows.Wgl - the context is not ready yet,
|
|
|
|
|
// and Wgl extensions will fail to load.
|
|
|
|
|
renderContext = Wgl.Imports.CreateContext(deviceContext);
|
|
|
|
|
if (renderContext != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine(String.Format("done! (id: {0})", renderContext));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("Could not create opengl Rendering Context.");
|
|
|
|
|
}
|
|
|
|
|
Wgl.Imports.MakeCurrent(deviceContext, renderContext);
|
|
|
|
|
Wgl.LoadAll();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#region --- IGLContext Members ---
|
|
|
|
|
|
|
|
|
|
#region public void SwapBuffers()
|
|
|
|
|
|
|
|
|
|
public void SwapBuffers()
|
|
|
|
|
{
|
|
|
|
|
API.SwapBuffers(deviceContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public IntPtr GetAddress(string function_string)
|
|
|
|
|
|
|
|
|
|
public IntPtr GetAddress(string function_string)
|
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
return Wgl.Imports.GetProcAddress(function_string);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void MakeCurrent()
|
|
|
|
|
|
|
|
|
|
public void MakeCurrent()
|
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Wgl.Imports.MakeCurrent(deviceContext, renderContext);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public DisplayMode[] GetDisplayModes()
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DisplayMode> GetDisplayModes()
|
|
|
|
|
{
|
|
|
|
|
List<DisplayMode> modes = new List<DisplayMode>();
|
|
|
|
|
bool done = false;
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
DeviceMode currentMode = new DeviceMode();
|
|
|
|
|
IntPtr handle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DeviceMode)));
|
2007-07-23 00:15:18 +00:00
|
|
|
|
Marshal.StructureToPtr(currentMode, handle, true);
|
|
|
|
|
|
|
|
|
|
done = (API.EnumDisplaySettings(null, index++, handle) != 0) ? false : true;
|
|
|
|
|
int error = Marshal.GetLastWin32Error();
|
|
|
|
|
|
|
|
|
|
Marshal.PtrToStructure(handle, currentMode);
|
|
|
|
|
Marshal.FreeHGlobal(handle);
|
|
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Error: {0}", error);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (done)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
//DisplayMode mode = new DisplayMode(currentMode.PelsWidth, currentMode.PelsHeight);
|
|
|
|
|
DisplayMode mode = new DisplayMode(
|
|
|
|
|
currentMode.PelsWidth,
|
|
|
|
|
currentMode.PelsHeight,
|
2007-09-02 00:16:22 +00:00
|
|
|
|
new ColorMode(currentMode.BitsPerPel),
|
2007-07-23 00:15:18 +00:00
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
currentMode.DisplayFrequency
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
modes.Add(mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return modes.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IDisposable Members ---
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
//Debug.Print("Manually disposing WinGLContext {0}.", this.renderContext);
|
2007-08-10 16:55:24 +00:00
|
|
|
|
Dispose(true);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Dispose(bool calledManually)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
// Clean unmanaged resources here
|
|
|
|
|
// The following call uses the Debug and Wgl classes, making it unsafe?
|
2007-08-20 12:25:48 +00:00
|
|
|
|
ReleaseResources();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
if (calledManually)
|
|
|
|
|
{
|
|
|
|
|
// Safe to clean managed resources
|
|
|
|
|
}
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~WinGLContext()
|
|
|
|
|
{
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-22 00:30:16 +00:00
|
|
|
|
#region private void ReleaseResources()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
private void ReleaseResources()
|
|
|
|
|
{
|
|
|
|
|
if (renderContext != IntPtr.Zero)
|
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
|
|
|
|
|
if (!Wgl.Imports.DeleteContext(renderContext))
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
//throw new ApplicationException("Could not destroy the OpenGL render context. Error: " + Marshal.GetLastWin32Error());
|
|
|
|
|
//Debug.Print("Could not destroy the OpenGL render context. Error: {0}", Marshal.GetLastWin32Error());
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
renderContext = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
if (deviceContext != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
if (!API.ReleaseDC(windowHandle, deviceContext))
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
//throw new ApplicationException("Could not release device context. Error: " + Marshal.GetLastWin32Error());
|
|
|
|
|
//Debug.Print("Could not destroy the device context. Error: {0}", Marshal.GetLastWin32Error());
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
if (opengl32Handle != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
if (!API.FreeLibrary(opengl32Handle))
|
|
|
|
|
{
|
2007-09-02 00:16:22 +00:00
|
|
|
|
//throw new ApplicationException("FreeLibray call failed ('opengl32.dll'), Error: " + Marshal.GetLastWin32Error());
|
|
|
|
|
//Debug.Print("Could not release {0}. Error: {1}", opengl32Name, Marshal.GetLastWin32Error());
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
opengl32Handle = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|