PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ThirdParty/OpenTK.1.0/Source/Compatibility/Graphics/GL/GLHelper.cs

http://github.com/virtualglobebook/OpenGlobe
C# | 1322 lines | 911 code | 240 blank | 171 comment | 77 complexity | ee8572e68cc89663f7efd64439793ce2 MD5 | raw file
  1. #region --- License ---
  2. /* Copyright (c) 2006-2008 the OpenTK team.
  3. * See license.txt for license info
  4. *
  5. * Contributions by Andy Gill.
  6. */
  7. #endregion
  8. #region --- Using Directives ---
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.Runtime.InteropServices;
  13. using System.Reflection;
  14. using System.Diagnostics;
  15. using System.Reflection.Emit;
  16. #endregion
  17. namespace OpenTK.Graphics
  18. {
  19. /// <summary>
  20. /// OpenGL bindings for .NET, implementing OpenGL 3.1, plus extensions.
  21. /// </summary>
  22. /// <remarks>
  23. /// <para>
  24. /// This class contains all OpenGL enums and functions defined in the 3.1 specification.
  25. /// The official .spec files can be found at: http://opengl.org/registry/.
  26. /// </para>
  27. /// <para> A valid OpenGL context must be created before calling any OpenGL function.</para>
  28. /// <para>
  29. /// Use the GL.Load and GL.LoadAll methods to prepare function entry points prior to use. To maintain
  30. /// cross-platform compatibility, this must be done for both core and extension functions. The GameWindow
  31. /// and the GLControl class will take care of this automatically.
  32. /// </para>
  33. /// <para>
  34. /// You can use the GL.SupportsExtension method to check whether any given category of extension functions
  35. /// exists in the current OpenGL context. Keep in mind that different OpenGL contexts may support different
  36. /// extensions, and under different entry points. Always check if all required extensions are still supported
  37. /// when changing visuals or pixel formats.
  38. /// </para>
  39. /// <para>
  40. /// You may retrieve the entry point for an OpenGL function using the GL.GetDelegate method.
  41. /// </para>
  42. /// </remarks>
  43. /// <see href="http://opengl.org/registry/"/>
  44. /// <seealso cref="GL.SupportsExtension"/>
  45. /// <seealso cref="GL.GetDelegate(string)"/>
  46. /// <seealso cref="GL.LoadAll"/>
  47. /// <seealso cref="GL.Load"/>
  48. [Obsolete("Use OpenTK.Graphics.OpenGL or one of the specific profiles instead.")]
  49. public static partial class GL
  50. {
  51. delegate void VoidGLDelegate(object @class, object[] parameters);
  52. delegate object ObjectGLDelegate(object @class, object[] parameters);
  53. #region --- Fields ---
  54. internal const string Library = "opengl32.dll";
  55. static StringBuilder sb = new StringBuilder();
  56. static object gl_lock = new object();
  57. private static SortedList<string, bool> AvailableExtensions = new SortedList<string, bool>();
  58. private static bool rebuildExtensionList;
  59. private static Type glClass;
  60. private static Type delegatesClass;
  61. private static Type importsClass;
  62. #endregion
  63. #region --- Constructor ---
  64. static GL()
  65. {
  66. glClass = typeof(GL);
  67. delegatesClass = glClass.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic);
  68. importsClass = glClass.GetNestedType("Imports", BindingFlags.Static | BindingFlags.NonPublic);
  69. LoadAll(); // Ensure this class is loaded, since it is no longer visible to the GraphicsContext.LoadAll() method.
  70. }
  71. #endregion
  72. #region --- Imports ---
  73. internal static partial class Imports
  74. {
  75. internal static SortedList<string, MethodInfo> FunctionMap; // This is faster than either Dictionary or SortedDictionary
  76. static Imports()
  77. {
  78. MethodInfo[] methods = importsClass.GetMethods(BindingFlags.Static | BindingFlags.NonPublic);
  79. FunctionMap = new SortedList<string, MethodInfo>(methods.Length);
  80. foreach (MethodInfo m in methods)
  81. {
  82. FunctionMap.Add(m.Name, m);
  83. }
  84. }
  85. }
  86. #endregion
  87. #region --- Public Members ---
  88. #region public static bool SupportsExtension(string name)
  89. /// <summary>
  90. /// Determines whether the specified OpenGL extension category is available in
  91. /// the current OpenGL context. Equivalent to IsExtensionSupported(name, true)
  92. /// </summary>
  93. /// <param name="name">The string for the OpenGL extension category (eg. "GL_ARB_multitexture")</param>
  94. /// <returns>True if the specified extension is available, false otherwise.</returns>
  95. public static bool SupportsExtension(string name)
  96. {
  97. if (rebuildExtensionList)
  98. BuildExtensionList();
  99. lock (gl_lock)
  100. {
  101. sb.Remove(0, sb.Length);
  102. if (!name.StartsWith("GL_"))
  103. sb.Append("gl_");
  104. sb.Append(name.ToLower());
  105. sb.Replace("_", String.Empty);
  106. // Search the cache for the string.
  107. return AvailableExtensions.ContainsKey(sb.ToString());
  108. }
  109. }
  110. #endregion
  111. #region public static Delegate GetDelegate(string name)
  112. /// <summary>
  113. /// Returns a System.Delegate wrapping the specified OpenGL function. You must use the
  114. /// base OpenGL name of the function (e.g. "glVertex3fv" instead of "Vertex3").
  115. /// </summary>
  116. /// <param name="name">The name of the OpenGL function (eg. "glNewList")</param>
  117. /// <returns>
  118. /// A System.Delegate that can be used to call this OpenGL function or null, if the specified
  119. /// function name does not correspond to an OpenGL function or if the function is not
  120. /// supported by the video drivers.
  121. /// </returns>
  122. public static Delegate GetDelegate(string name)
  123. {
  124. FieldInfo info = typeof(Delegates).GetField(name, BindingFlags.Static | BindingFlags.NonPublic);
  125. if (info == null)
  126. return null;
  127. return (Delegate)info.GetValue(null);
  128. }
  129. #endregion
  130. #region public static Delegate GetDelegate(string name, Type signature)
  131. /// <summary>
  132. /// Returns a System.Delegate wrapping an OpenGL function.
  133. /// </summary>
  134. /// <param name="name">The name of the OpenGL function (eg. "glNewList")</param>
  135. /// <param name="signature">The signature of the OpenGL function.</param>
  136. /// <returns>
  137. /// A System.Delegate that can be used to call this OpenGL function, or null if the specified
  138. /// function name did not correspond to an OpenGL function.
  139. /// </returns>
  140. [Obsolete("Use GetDelegate(string name) instead.")]
  141. public static Delegate GetDelegate(string name, Type signature)
  142. {
  143. return LoadDelegate(name, signature);
  144. }
  145. #endregion
  146. #region public static void LoadAll()
  147. /// <summary>
  148. /// Loads all OpenGL functions (core and extensions).
  149. /// </summary>
  150. /// <remarks>
  151. /// <para>
  152. /// This function will be automatically called the first time you use any opengl function. There is
  153. /// </para>
  154. /// <para>
  155. /// Call this function manually whenever you need to update OpenGL entry points.
  156. /// This need may arise if you change the pixelformat/visual, or in case you cannot
  157. /// (or do not want) to use the automatic initialization of the GL class.
  158. /// </para>
  159. /// </remarks>
  160. public static void LoadAll()
  161. {
  162. //TODO: Route GameWindow context creation through GraphicsContext.
  163. //if (GraphicsContext.CurrentContext == null)
  164. // throw new InvalidOperationException("You must create an OpenGL context before using the GL class.");
  165. if (GraphicsContext.CurrentContext == null)
  166. {
  167. throw new InvalidOperationException("No GraphicsContext available in the calling thread.");
  168. }
  169. else
  170. {
  171. int supported = 0;
  172. Type extensions_class = typeof(GL).GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
  173. if (extensions_class == null)
  174. throw new InvalidOperationException("The specified type does not have any loadable extensions.");
  175. FieldInfo[] delegates = extensions_class.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
  176. if (delegates == null)
  177. throw new InvalidOperationException("The specified type does not have any loadable extensions.");
  178. foreach (FieldInfo f in delegates)
  179. {
  180. Delegate d = LoadDelegate(f.Name, f.FieldType);
  181. if (d != null)
  182. ++supported;
  183. f.SetValue(null, d);
  184. }
  185. rebuildExtensionList = true;
  186. }
  187. }
  188. #endregion
  189. #region public static bool Load(string function)
  190. /// <summary>
  191. /// Tries to reload the given OpenGL function (core or extension).
  192. /// </summary>
  193. /// <param name="function">The name of the OpenGL function (i.e. glShaderSource)</param>
  194. /// <returns>True if the function was found and reloaded, false otherwise.</returns>
  195. /// <remarks>
  196. /// <para>
  197. /// Use this function if you require greater granularity when loading OpenGL entry points.
  198. /// </para>
  199. /// <para>
  200. /// While the automatic initialisation will load all OpenGL entry points, in some cases
  201. /// the initialisation can take place before an OpenGL Context has been established.
  202. /// In this case, use this function to load the entry points for the OpenGL functions
  203. /// you will need, or use ReloadFunctions() to load all available entry points.
  204. /// </para>
  205. /// <para>
  206. /// This function returns true if the given OpenGL function is supported, false otherwise.
  207. /// </para>
  208. /// <para>
  209. /// To query for supported extensions use the IsExtensionSupported() function instead.
  210. /// </para>
  211. /// </remarks>
  212. public static bool Load(string function)
  213. {
  214. FieldInfo f = delegatesClass.GetField(function, BindingFlags.Static | BindingFlags.NonPublic);
  215. if (f == null)
  216. return false;
  217. Delegate old = f.GetValue(null) as Delegate;
  218. Delegate @new = LoadDelegate(f.Name, f.FieldType);
  219. if (old.Target != @new.Target)
  220. {
  221. f.SetValue(null, @new);
  222. rebuildExtensionList = true;
  223. }
  224. return @new != null;
  225. }
  226. #endregion
  227. #region static Delegate LoadDelegate(string name, Type signature)
  228. /// <private />
  229. /// <summary>
  230. /// Loads an OpenGL function into a type-safe System.Delegate.
  231. /// </summary>
  232. /// <param name="name">The name of the OpenGL function (eg. "glNewList")</param>
  233. /// <param name="signature">The signature of the OpenGL function.</param>
  234. /// <returns>
  235. /// A System.Delegate that can be used to call this OpenGL function, or null if the specified
  236. /// function name did not correspond to an OpenGL function.
  237. /// </returns>
  238. static Delegate LoadDelegate(string name, Type signature)
  239. {
  240. MethodInfo m;
  241. return
  242. GetExtensionDelegate(name, signature) ??
  243. (Imports.FunctionMap.TryGetValue((name.Substring(2)), out m) ?
  244. Delegate.CreateDelegate(signature, m) : null);
  245. }
  246. #endregion
  247. #region public static bool SupportsFunction(string function)
  248. /// <summary>
  249. /// Checks if a given OpenGL function is supported by the current context
  250. /// </summary>
  251. /// <param name="function">The name of the OpenGL function (i.e. glShaderSource)</param>
  252. /// <returns>True if the function is supported, false otherwise</returns>
  253. public static bool SupportsFunction(string function)
  254. {
  255. lock (gl_lock)
  256. {
  257. if (function == null)
  258. throw new ArgumentNullException("function");
  259. sb.Remove(0, sb.Length);
  260. if (!function.StartsWith("gl"))
  261. sb.Append("gl");
  262. sb.Append(function);
  263. FieldInfo f = delegatesClass.GetField(sb.ToString(), BindingFlags.Static | BindingFlags.NonPublic);
  264. if (f == null)
  265. return false;
  266. return f.GetValue(null) != null;
  267. }
  268. }
  269. #endregion
  270. #region public static bool SupportsFunction(string function, string extension)
  271. /// <summary>
  272. /// Checks if a given OpenGL function is supported by the current context
  273. /// </summary>
  274. /// <param name="function">The name of the OpenGL function (e.g. glShaderSource)</param>
  275. /// <param name="extension">The name of the extension catagory (e.g. ARB, EXT, ATI, ...)</param>
  276. /// <returns>True if the function is supported, false otherwise</returns>
  277. public static bool SupportsFunction(string function, string extension)
  278. {
  279. lock (gl_lock)
  280. {
  281. if (function == null)
  282. throw new ArgumentNullException("function");
  283. if (extension == null)
  284. throw new ArgumentNullException("extension");
  285. sb.Remove(0, sb.Length);
  286. if (!function.StartsWith("gl"))
  287. sb.Append("gl");
  288. sb.Append(function);
  289. if (!function.EndsWith(extension))
  290. sb.Append(extension);
  291. FieldInfo f = delegatesClass.GetField(sb.ToString(), BindingFlags.Static | BindingFlags.NonPublic);
  292. if (f == null)
  293. return false;
  294. return f.GetValue(null) != null;
  295. }
  296. }
  297. #endregion
  298. #region static bool SupportsFunction(MethodInfo function)
  299. /// <summary>
  300. /// Checks if a given OpenGL function is supported by the current context.
  301. /// </summary>
  302. /// <param name="function">The System.Reflection.MethodInfo for the OpenGL function.</param>
  303. /// <returns>True if the function is supported, false otherwise.</returns>
  304. static bool SupportsFunction(MethodInfo function)
  305. {
  306. if (function == null)
  307. throw new ArgumentNullException("function");
  308. AutoGeneratedAttribute[] attr = (AutoGeneratedAttribute[])
  309. function.GetCustomAttributes(typeof(AutoGeneratedAttribute), false);
  310. if (attr.Length == 0)
  311. return false;
  312. return SupportsFunction(attr[0].EntryPoint);
  313. }
  314. #endregion
  315. #region private static void BuildExtensionList()
  316. /// <summary>
  317. /// Builds a cache of the supported extensions to speed up searches.
  318. /// </summary>
  319. private static void BuildExtensionList()
  320. {
  321. // Assumes there is an opengl context current.
  322. AvailableExtensions.Clear();
  323. string version_string = GL.GetString(StringName.Version);
  324. if (String.IsNullOrEmpty(version_string))
  325. {
  326. throw new ApplicationException("Failed to build extension list. Is there an opengl context current?");
  327. }
  328. string version;
  329. // Most drivers return the version in the 3 first characters of the version string,
  330. // (e.g. on Ati X1950 with Catalyst 7.10 -> "2.0.6956 Release"). However, Mesa seems
  331. // to do something strange: "1.4 (2.1 Mesa 7.0.1).".
  332. // Update: this seems to occur with indirect rendering. E.g. Ati 8.2: 1.4 (2.1.7281 ...)
  333. // We'll do some trickery to get the second number (2.1), but this may break on
  334. // some implementations...
  335. //if (version_string.ToLower().Contains("mesa"))
  336. {
  337. int index = version_string.IndexOf('(');
  338. if (index != -1)
  339. version = version_string.Substring(index + 1, 3);
  340. else
  341. version = version_string.TrimStart(' ');
  342. }
  343. //else
  344. // version = version_string.TrimStart(' ');
  345. if (version.StartsWith("1.1"))
  346. {
  347. AvailableExtensions.Add("glversion11", true);
  348. }
  349. else if (version.StartsWith("1.2"))
  350. {
  351. AvailableExtensions.Add("glversion11", true);
  352. AvailableExtensions.Add("glversion12", true);
  353. }
  354. else if (version.StartsWith("1.3"))
  355. {
  356. AvailableExtensions.Add("glversion11", true);
  357. AvailableExtensions.Add("glversion12", true);
  358. AvailableExtensions.Add("glversion13", true);
  359. }
  360. else if (version.StartsWith("1.4"))
  361. {
  362. AvailableExtensions.Add("glversion11", true);
  363. AvailableExtensions.Add("glversion12", true);
  364. AvailableExtensions.Add("glversion13", true);
  365. AvailableExtensions.Add("glversion14", true);
  366. }
  367. else if (version.StartsWith("1.5"))
  368. {
  369. AvailableExtensions.Add("glversion11", true);
  370. AvailableExtensions.Add("glversion12", true);
  371. AvailableExtensions.Add("glversion13", true);
  372. AvailableExtensions.Add("glversion14", true);
  373. AvailableExtensions.Add("glversion15", true);
  374. }
  375. else if (version.StartsWith("2.0"))
  376. {
  377. AvailableExtensions.Add("glversion11", true);
  378. AvailableExtensions.Add("glversion12", true);
  379. AvailableExtensions.Add("glversion13", true);
  380. AvailableExtensions.Add("glversion14", true);
  381. AvailableExtensions.Add("glversion15", true);
  382. AvailableExtensions.Add("glversion20", true);
  383. }
  384. else if (version.StartsWith("2.1"))
  385. {
  386. AvailableExtensions.Add("glversion11", true);
  387. AvailableExtensions.Add("glversion12", true);
  388. AvailableExtensions.Add("glversion13", true);
  389. AvailableExtensions.Add("glversion14", true);
  390. AvailableExtensions.Add("glversion15", true);
  391. AvailableExtensions.Add("glversion20", true);
  392. AvailableExtensions.Add("glversion21", true);
  393. }
  394. else if (version.StartsWith("3.0"))
  395. {
  396. AvailableExtensions.Add("glversion11", true);
  397. AvailableExtensions.Add("glversion12", true);
  398. AvailableExtensions.Add("glversion13", true);
  399. AvailableExtensions.Add("glversion14", true);
  400. AvailableExtensions.Add("glversion15", true);
  401. AvailableExtensions.Add("glversion20", true);
  402. AvailableExtensions.Add("glversion21", true);
  403. AvailableExtensions.Add("glversion30", true);
  404. }
  405. else if (version.StartsWith("3.1"))
  406. {
  407. AvailableExtensions.Add("glversion11", true);
  408. AvailableExtensions.Add("glversion12", true);
  409. AvailableExtensions.Add("glversion13", true);
  410. AvailableExtensions.Add("glversion14", true);
  411. AvailableExtensions.Add("glversion15", true);
  412. AvailableExtensions.Add("glversion20", true);
  413. AvailableExtensions.Add("glversion21", true);
  414. AvailableExtensions.Add("glversion30", true);
  415. AvailableExtensions.Add("glversion31", true);
  416. }
  417. string extension_string = GL.GetString(StringName.Extensions);
  418. if (String.IsNullOrEmpty(extension_string))
  419. return; // no extensions are available
  420. string[] extensions = extension_string.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  421. foreach (string ext in extensions)
  422. AvailableExtensions.Add(ext.Replace("_", String.Empty).ToLower(), true);
  423. rebuildExtensionList = false;
  424. }
  425. #endregion
  426. #endregion
  427. #region --- GetProcAddress ---
  428. private static IGetProcAddress getProcAddress;
  429. internal interface IGetProcAddress
  430. {
  431. IntPtr GetProcAddress(string function);
  432. }
  433. internal class GetProcAddressWindows : IGetProcAddress
  434. {
  435. [System.Runtime.InteropServices.DllImport(Library, EntryPoint = "wglGetProcAddress", ExactSpelling = true)]
  436. private static extern IntPtr wglGetProcAddress(String lpszProc);
  437. public IntPtr GetProcAddress(string function)
  438. {
  439. return wglGetProcAddress(function);
  440. }
  441. }
  442. internal class GetProcAddressX11 : IGetProcAddress
  443. {
  444. [DllImport(Library, EntryPoint = "glXGetProcAddress")]
  445. private static extern IntPtr glxGetProcAddress([MarshalAs(UnmanagedType.LPTStr)] string procName);
  446. public IntPtr GetProcAddress(string function)
  447. {
  448. return glxGetProcAddress(function);
  449. }
  450. }
  451. internal class GetProcAddressOSX : IGetProcAddress
  452. {
  453. private const string Library = "libdl.dylib";
  454. [DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")]
  455. private static extern bool NSIsSymbolNameDefined(string s);
  456. [DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")]
  457. private static extern IntPtr NSLookupAndBindSymbol(string s);
  458. [DllImport(Library, EntryPoint = "NSAddressOfSymbol")]
  459. private static extern IntPtr NSAddressOfSymbol(IntPtr symbol);
  460. public IntPtr GetProcAddress(string function)
  461. {
  462. string fname = "_" + function;
  463. if (!NSIsSymbolNameDefined(fname))
  464. return IntPtr.Zero;
  465. IntPtr symbol = NSLookupAndBindSymbol(fname);
  466. if (symbol != IntPtr.Zero)
  467. symbol = NSAddressOfSymbol(symbol);
  468. return symbol;
  469. }
  470. }
  471. #region private static IntPtr GetAddress(string function)
  472. /// <summary>
  473. /// Retrieves the entry point for a dynamically exported OpenGL function.
  474. /// </summary>
  475. /// <param name="function">The function string for the OpenGL function (eg. "glNewList")</param>
  476. /// <returns>
  477. /// An IntPtr contaning the address for the entry point, or IntPtr.Zero if the specified
  478. /// OpenGL function is not dynamically exported.
  479. /// </returns>
  480. /// <remarks>
  481. /// <para>
  482. /// The Marshal.GetDelegateForFunctionPointer method can be used to turn the return value
  483. /// into a call-able delegate.
  484. /// </para>
  485. /// <para>
  486. /// This function is cross-platform. It determines the underlying platform and uses the
  487. /// correct wgl, glx or agl GetAddress function to retrieve the function pointer.
  488. /// </para>
  489. /// </remarks>
  490. private static IntPtr GetAddress(string function)
  491. {
  492. if (getProcAddress == null)
  493. {
  494. if (Configuration.RunningOnWindows)
  495. {
  496. getProcAddress = new GetProcAddressWindows();
  497. }
  498. else if (Configuration.RunningOnMacOS)
  499. {
  500. getProcAddress = new GetProcAddressOSX();
  501. }
  502. else if (Configuration.RunningOnX11)
  503. {
  504. getProcAddress = new GetProcAddressX11();
  505. }
  506. else
  507. {
  508. throw new PlatformNotSupportedException(
  509. "Extension loading is only supported under Mac OS X, X11 and Windows. We are sorry for the inconvience.");
  510. }
  511. }
  512. return getProcAddress.GetProcAddress(function);
  513. }
  514. #endregion
  515. #region internal static Delegate GetExtensionDelegate(string name, Type signature)
  516. /// <summary>
  517. /// Creates a System.Delegate that can be used to call a dynamically exported OpenGL function.
  518. /// </summary>
  519. /// <param name="name">The name of the OpenGL function (eg. "glNewList")</param>
  520. /// <param name="signature">The signature of the OpenGL function.</param>
  521. /// <returns>
  522. /// A System.Delegate that can be used to call this OpenGL function or null
  523. /// if the function is not available in the current OpenGL context.
  524. /// </returns>
  525. internal static Delegate GetExtensionDelegate(string name, Type signature)
  526. {
  527. IntPtr address = GetAddress(name);
  528. if (address == IntPtr.Zero ||
  529. address == new IntPtr(1) || // Workaround for buggy nvidia drivers which return
  530. address == new IntPtr(2)) // 1 or 2 instead of IntPtr.Zero for some extensions.
  531. {
  532. return null;
  533. }
  534. else
  535. {
  536. return Marshal.GetDelegateForFunctionPointer(address, signature);
  537. }
  538. }
  539. #endregion
  540. #endregion
  541. #region --- GL Overloads ---
  542. #pragma warning disable 3019
  543. #pragma warning disable 1591
  544. #pragma warning disable 1572
  545. #pragma warning disable 1573
  546. #region public static void Color[34]() overloads
  547. public static void Color3(System.Drawing.Color color)
  548. {
  549. GL.Color3(color.R, color.G, color.B);
  550. }
  551. public static void Color4(System.Drawing.Color color)
  552. {
  553. GL.Color4(color.R, color.G, color.B, color.A);
  554. }
  555. public static void Color3(Vector3 color)
  556. {
  557. GL.Color3(color.X, color.Y, color.Z);
  558. }
  559. public static void Color4(Vector4 color)
  560. {
  561. GL.Color4(color.X, color.Y, color.Z, color.W);
  562. }
  563. #endregion
  564. #region public static void ClearColor() overloads
  565. public static void ClearColor(System.Drawing.Color color)
  566. {
  567. GL.ClearColor(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
  568. }
  569. #endregion
  570. #region public static void BlendColor() overloads
  571. public static void BlendColor(System.Drawing.Color color)
  572. {
  573. GL.BlendColor(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
  574. }
  575. #endregion
  576. #region public static void Material() overloads
  577. public static void Material(MaterialFace face, MaterialParameter pname, Vector4 @params)
  578. {
  579. unsafe { Material(face, pname, (float*)&@params.X); }
  580. }
  581. public static void Material(MaterialFace face, MaterialParameter pname, Color4 @params)
  582. {
  583. unsafe { GL.Material(face, pname, (float*)&@params); }
  584. }
  585. #endregion
  586. #region public static void Light() overloads
  587. public static void Light(LightName name, LightParameter pname, Vector4 @params)
  588. {
  589. unsafe { GL.Light(name, pname, (float*)&@params.X); }
  590. }
  591. public static void Light(LightName name, LightParameter pname, Color4 @params)
  592. {
  593. unsafe { GL.Light(name, pname, (float*)&@params); }
  594. }
  595. #endregion
  596. #region --- Overloads for OpenTK.Math ---
  597. public static void Normal3(Vector3 normal)
  598. {
  599. Delegates.glNormal3f(normal.X, normal.Y, normal.Z);
  600. }
  601. public static void RasterPos2(Vector2 pos)
  602. {
  603. Delegates.glRasterPos2f(pos.X, pos.Y);
  604. }
  605. public static void RasterPos3(Vector3 pos)
  606. {
  607. Delegates.glRasterPos3f(pos.X, pos.Y, pos.Z);
  608. }
  609. public static void RasterPos4(Vector4 pos)
  610. {
  611. Delegates.glRasterPos4f(pos.X, pos.Y, pos.Z, pos.W);
  612. }
  613. public static void Vertex2(Vector2 v)
  614. {
  615. Delegates.glVertex2f(v.X, v.Y);
  616. }
  617. public static void Vertex3(Vector3 v)
  618. {
  619. Delegates.glVertex3f(v.X, v.Y, v.Z);
  620. }
  621. public static void Vertex4(Vector4 v)
  622. {
  623. Delegates.glVertex4f(v.X, v.Y, v.Z, v.W);
  624. }
  625. public static void TexCoord2(Vector2 v)
  626. {
  627. Delegates.glTexCoord2f(v.X, v.Y);
  628. }
  629. public static void TexCoord3(Vector3 v)
  630. {
  631. Delegates.glTexCoord3f(v.X, v.Y, v.Z);
  632. }
  633. public static void TexCoord4(Vector4 v)
  634. {
  635. Delegates.glTexCoord4f(v.X, v.Y, v.Z, v.W);
  636. }
  637. public static void Rotate(Single angle, Vector3 axis)
  638. {
  639. Delegates.glRotatef((Single)angle, axis.X, axis.Y, axis.Z);
  640. }
  641. public static void Scale(Vector3 scale)
  642. {
  643. Delegates.glScalef(scale.X, scale.Y, scale.Z);
  644. }
  645. public static void Translate(Vector3 trans)
  646. {
  647. Delegates.glTranslatef(trans.X, trans.Y, trans.Z);
  648. }
  649. public static void MultMatrix(ref Matrix4 mat)
  650. {
  651. unsafe
  652. {
  653. fixed (Single* m_ptr = &mat.Row0.X)
  654. {
  655. Delegates.glMultMatrixf((Single*)m_ptr);
  656. }
  657. }
  658. }
  659. public static void LoadMatrix(ref Matrix4 mat)
  660. {
  661. unsafe
  662. {
  663. fixed (Single* m_ptr = &mat.Row0.X)
  664. {
  665. Delegates.glLoadMatrixf((Single*)m_ptr);
  666. }
  667. }
  668. }
  669. public static void LoadTransposeMatrix(ref Matrix4 mat)
  670. {
  671. unsafe
  672. {
  673. fixed (Single* m_ptr = &mat.Row0.X)
  674. {
  675. Delegates.glLoadTransposeMatrixf((Single*)m_ptr);
  676. }
  677. }
  678. }
  679. public static void MultTransposeMatrix(ref Matrix4 mat)
  680. {
  681. unsafe
  682. {
  683. fixed (Single* m_ptr = &mat.Row0.X)
  684. {
  685. Delegates.glMultTransposeMatrixf((Single*)m_ptr);
  686. }
  687. }
  688. }
  689. public static void MultMatrix(ref Matrix4d mat)
  690. {
  691. unsafe
  692. {
  693. fixed (Double* m_ptr = &mat.Row0.X)
  694. {
  695. Delegates.glMultMatrixd((Double*)m_ptr);
  696. }
  697. }
  698. }
  699. public static void LoadMatrix(ref Matrix4d mat)
  700. {
  701. unsafe
  702. {
  703. fixed (Double* m_ptr = &mat.Row0.X)
  704. {
  705. Delegates.glLoadMatrixd((Double*)m_ptr);
  706. }
  707. }
  708. }
  709. public static void LoadTransposeMatrix(ref Matrix4d mat)
  710. {
  711. unsafe
  712. {
  713. fixed (Double* m_ptr = &mat.Row0.X)
  714. {
  715. Delegates.glLoadTransposeMatrixd((Double*)m_ptr);
  716. }
  717. }
  718. }
  719. public static void MultTransposeMatrix(ref Matrix4d mat)
  720. {
  721. unsafe
  722. {
  723. fixed (Double* m_ptr = &mat.Row0.X)
  724. {
  725. Delegates.glMultTransposeMatrixd((Double*)m_ptr);
  726. }
  727. }
  728. }
  729. public static void UniformMatrix4(int location, bool transpose, ref Matrix4 matrix)
  730. {
  731. unsafe
  732. {
  733. fixed (float* matrix_ptr = &matrix.Row0.X)
  734. {
  735. GL.UniformMatrix4(location, 1, transpose, matrix_ptr);
  736. }
  737. }
  738. }
  739. #region Uniform
  740. [CLSCompliant(false)]
  741. public static void Uniform2(int location, ref Vector2 vector)
  742. {
  743. GL.Uniform2(location, vector.X, vector.Y);
  744. }
  745. [CLSCompliant(false)]
  746. public static void Uniform3(int location, ref Vector3 vector)
  747. {
  748. GL.Uniform3(location, vector.X, vector.Y, vector.Z);
  749. }
  750. [CLSCompliant(false)]
  751. public static void Uniform4(int location, ref Vector4 vector)
  752. {
  753. GL.Uniform4(location, vector.X, vector.Y, vector.Z, vector.W);
  754. }
  755. public static void Uniform2(int location, Vector2 vector)
  756. {
  757. GL.Uniform2(location, vector.X, vector.Y);
  758. }
  759. public static void Uniform3(int location, Vector3 vector)
  760. {
  761. GL.Uniform3(location, vector.X, vector.Y, vector.Z);
  762. }
  763. public static void Uniform4(int location, Vector4 vector)
  764. {
  765. GL.Uniform4(location, vector.X, vector.Y, vector.Z, vector.W);
  766. }
  767. public static void Uniform4(int location, Color4 color)
  768. {
  769. GL.Uniform4(location, color.R, color.G, color.B, color.A);
  770. }
  771. public static void Uniform4(int location, Quaternion quaternion)
  772. {
  773. GL.Uniform4(location, quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
  774. }
  775. #endregion
  776. #endregion
  777. #region public static void ShaderSource(Int32 shader, System.String @string)
  778. public static void ShaderSource(Int32 shader, System.String @string)
  779. {
  780. unsafe
  781. {
  782. int length = @string.Length;
  783. Delegates.glShaderSource((UInt32)shader, 1, new string[] { @string }, &length);
  784. }
  785. }
  786. #endregion
  787. #region public static string GetShaderInfoLog(Int32 shader)
  788. public static string GetShaderInfoLog(Int32 shader)
  789. {
  790. string info;
  791. GetShaderInfoLog(shader, out info);
  792. return info;
  793. }
  794. #endregion
  795. #region public static void GetShaderInfoLog(Int32 shader, out string info)
  796. public static void GetShaderInfoLog(Int32 shader, out string info)
  797. {
  798. unsafe
  799. {
  800. int length;
  801. GL.GetShader(shader, ShaderParameter.InfoLogLength, out length);
  802. if (length == 0)
  803. {
  804. info = String.Empty;
  805. return;
  806. }
  807. StringBuilder sb = new StringBuilder(length);
  808. Delegates.glGetShaderInfoLog((UInt32)shader, sb.Capacity, &length, sb);
  809. info = sb.ToString();
  810. }
  811. }
  812. #endregion
  813. #region public static string GetProgramInfoLog(Int32 program)
  814. public static string GetProgramInfoLog(Int32 program)
  815. {
  816. string info;
  817. GetProgramInfoLog(program, out info);
  818. return info;
  819. }
  820. #endregion
  821. #region public static void GetProgramInfoLog(Int32 program, out string info)
  822. public static void GetProgramInfoLog(Int32 program, out string info)
  823. {
  824. unsafe
  825. {
  826. int length;
  827. GL.GetProgram(program, OpenTK.Graphics.ProgramParameter.InfoLogLength, out length);
  828. if (length == 0)
  829. {
  830. info = String.Empty;
  831. return;
  832. }
  833. StringBuilder sb = new StringBuilder(length);
  834. Delegates.glGetProgramInfoLog((UInt32)program, sb.Capacity, &length, sb);
  835. info = sb.ToString();
  836. }
  837. }
  838. #endregion
  839. #region public static void PointParameter(PointSpriteCoordOriginParameter param)
  840. /// <summary>
  841. /// Helper function that defines the coordinate origin of the Point Sprite.
  842. /// </summary>
  843. /// <param name="param">
  844. /// A OpenTK.Graphics.OpenGL.GL.PointSpriteCoordOriginParameter token,
  845. /// denoting the origin of the Point Sprite.
  846. /// </param>
  847. public static void PointParameter(PointSpriteCoordOriginParameter param)
  848. {
  849. GL.PointParameter(PointParameterName.PointSpriteCoordOrigin, (int)param);
  850. }
  851. #endregion
  852. #region public static void VertexAttrib2(Int32 index, ref Vector2 v)
  853. [CLSCompliant(false)]
  854. public static void VertexAttrib2(Int32 index, ref Vector2 v)
  855. {
  856. GL.VertexAttrib2(index, v.X, v.Y);
  857. }
  858. #endregion
  859. #region public static void VertexAttrib3(Int32 index, ref Vector3 v)
  860. [CLSCompliant(false)]
  861. public static void VertexAttrib3(Int32 index, ref Vector3 v)
  862. {
  863. GL.VertexAttrib3(index, v.X, v.Y, v.Z);
  864. }
  865. #endregion
  866. #region public static void VertexAttrib4(Int32 index, ref Vector4 v)
  867. [CLSCompliant(false)]
  868. public static void VertexAttrib4(Int32 index, ref Vector4 v)
  869. {
  870. GL.VertexAttrib4(index, v.X, v.Y, v.Z, v.W);
  871. }
  872. #endregion
  873. #region public static void VertexAttrib2(Int32 index, Vector2 v)
  874. public static void VertexAttrib2(Int32 index, Vector2 v)
  875. {
  876. GL.VertexAttrib2(index, v.X, v.Y);
  877. }
  878. #endregion
  879. #region public static void VertexAttrib3(Int32 index, Vector3 v)
  880. public static void VertexAttrib3(Int32 index, Vector3 v)
  881. {
  882. GL.VertexAttrib3(index, v.X, v.Y, v.Z);
  883. }
  884. #endregion
  885. #region public static void VertexAttrib4(Int32 index, Vector4 v)
  886. public static void VertexAttrib4(Int32 index, Vector4 v)
  887. {
  888. GL.VertexAttrib4(index, v.X, v.Y, v.Z, v.W);
  889. }
  890. #endregion
  891. #region public static void MultiTexCoord2(TextureUnit target, ref Vector2 v)
  892. public static void MultiTexCoord2(TextureUnit target, ref Vector2 v)
  893. {
  894. GL.MultiTexCoord2(target, v.X, v.Y);
  895. }
  896. #endregion
  897. #region public static void MultiTexCoord3(TextureUnit target, ref Vector3 v)
  898. public static void MultiTexCoord3(TextureUnit target, ref Vector3 v)
  899. {
  900. GL.MultiTexCoord3(target, v.X, v.Y, v.Z);
  901. }
  902. #endregion
  903. #region public static void MultiTexCoord4(TextureUnit target, ref Vector4 v)
  904. public static void MultiTexCoord4(TextureUnit target, ref Vector4 v)
  905. {
  906. GL.MultiTexCoord4(target, v.X, v.Y, v.Z, v.W);
  907. }
  908. #endregion
  909. #region public static void Rect(System.Drawing.RectangleF rect)
  910. public static void Rect(System.Drawing.RectangleF rect)
  911. {
  912. GL.Rect(rect.Left, rect.Top, rect.Right, rect.Bottom);
  913. }
  914. #endregion
  915. #region public static void Rect(ref System.Drawing.RectangleF rect)
  916. [CLSCompliant(false)]
  917. public static void Rect(ref System.Drawing.RectangleF rect)
  918. {
  919. GL.Rect(rect.Left, rect.Top, rect.Right, rect.Bottom);
  920. }
  921. #endregion
  922. #region public static void Rect(System.Drawing.Rectangle rect)
  923. public static void Rect(System.Drawing.Rectangle rect)
  924. {
  925. GL.Rect(rect.Left, rect.Top, rect.Right, rect.Bottom);
  926. }
  927. #endregion
  928. #region public static void Rect(ref System.Drawing.Rectangle rect)
  929. [CLSCompliant(false)]
  930. public static void Rect(ref System.Drawing.Rectangle rect)
  931. {
  932. GL.Rect(rect.Left, rect.Top, rect.Right, rect.Bottom);
  933. }
  934. #endregion
  935. #region public static int GenTexture()
  936. public static int GenTexture()
  937. {
  938. int id;
  939. GenTextures(1, out id);
  940. return id;
  941. }
  942. #endregion
  943. #region public static void DeleteTexture(int id)
  944. public static void DeleteTexture(int id)
  945. {
  946. DeleteTextures(1, ref id);
  947. }
  948. #endregion
  949. #region [Vertex|Normal|Index|Color|FogCoord|VertexAttrib]Pointer
  950. public static void VertexPointer(int size, VertexPointerType type, int stride, int pointer)
  951. {
  952. VertexPointer(size, type, stride, (IntPtr)pointer);
  953. }
  954. public static void NormalPointer(int size, NormalPointerType type, int stride, int pointer)
  955. {
  956. NormalPointer(type, stride, (IntPtr)pointer);
  957. }
  958. public static void IndexPointer(IndexPointerType type, int stride, int pointer)
  959. {
  960. IndexPointer(type, stride, (IntPtr)pointer);
  961. }
  962. public static void ColorPointer(int size, ColorPointerType type, int stride, int pointer)
  963. {
  964. ColorPointer(size, type, stride, (IntPtr)pointer);
  965. }
  966. public static void FogCoordPointer(int size, FogPointerType type, int stride, int pointer)
  967. {
  968. FogCoordPointer(type, stride, (IntPtr)pointer);
  969. }
  970. public static void EdgeFlagPointer(int stride, int pointer)
  971. {
  972. EdgeFlagPointer(stride, (IntPtr)pointer);
  973. }
  974. public static void VertexAttribPointer(int index, int size, VertexAttribPointerType type, bool normalized, int stride, int pointer)
  975. {
  976. VertexAttribPointer(index, size, type, normalized, stride, (IntPtr)pointer);
  977. }
  978. #endregion
  979. #region Get[Float|Double]
  980. public static void GetFloat(GetPName pname, out Vector2 vector)
  981. {
  982. unsafe
  983. {
  984. fixed (Vector2* ptr = &vector)
  985. GetFloat(pname, (float*)ptr);
  986. }
  987. }
  988. public static void GetFloat(GetPName pname, out Vector3 vector)
  989. {
  990. unsafe
  991. {
  992. fixed (Vector3* ptr = &vector)
  993. GetFloat(pname, (float*)ptr);
  994. }
  995. }
  996. public static void GetFloat(GetPName pname, out Vector4 vector)
  997. {
  998. unsafe
  999. {
  1000. fixed (Vector4* ptr = &vector)
  1001. GetFloat(pname, (float*)ptr);
  1002. }
  1003. }
  1004. public static void GetFloat(GetPName pname, out Matrix4 matrix)
  1005. {
  1006. unsafe
  1007. {
  1008. fixed (Matrix4* ptr = &matrix)
  1009. GetFloat(pname, (float*)ptr);
  1010. }
  1011. }
  1012. public static void GetDouble(GetPName pname, out Vector2d vector)
  1013. {
  1014. unsafe
  1015. {
  1016. fixed (Vector2d* ptr = &vector)
  1017. GetFloat(pname, (float*)ptr);
  1018. }
  1019. }
  1020. public static void GetDouble(GetPName pname, out Vector3d vector)
  1021. {
  1022. unsafe
  1023. {
  1024. fixed (Vector3d* ptr = &vector)
  1025. GetFloat(pname, (float*)ptr);
  1026. }
  1027. }
  1028. public static void GetDouble(GetPName pname, out Vector4d vector)
  1029. {
  1030. unsafe
  1031. {
  1032. fixed (Vector4d* ptr = &vector)
  1033. GetFloat(pname, (float*)ptr);
  1034. }
  1035. }
  1036. public static void GetDouble(GetPName pname, out Matrix4d matrix)
  1037. {
  1038. unsafe
  1039. {
  1040. fixed (Matrix4d* ptr = &matrix)
  1041. GetFloat(pname, (float*)ptr);
  1042. }
  1043. }
  1044. #endregion
  1045. #region Viewport
  1046. public static void Viewport(System.Drawing.Size size)
  1047. {
  1048. GL.Viewport(0, 0, size.Width, size.Height);
  1049. }
  1050. public static void Viewport(System.Drawing.Point location, System.Drawing.Size size)
  1051. {
  1052. GL.Viewport(location.X, location.Y, size.Width, size.Height);
  1053. }
  1054. public static void Viewport(System.Drawing.Rectangle rectangle)
  1055. {
  1056. GL.Viewport(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
  1057. }
  1058. #endregion
  1059. #region TexEnv
  1060. public static void TexEnv(TextureEnvTarget target, TextureEnvParameter pname, System.Drawing.Color color)
  1061. {
  1062. Color4 c = new Color4(color);
  1063. unsafe
  1064. {
  1065. TexEnv(target, pname, &c.R);
  1066. }
  1067. }
  1068. public static void TexEnv(TextureEnvTarget target, TextureEnvParameter pname, Color4 color)
  1069. {
  1070. unsafe
  1071. {
  1072. TexEnv(target, pname, &color.R);
  1073. }
  1074. }
  1075. #endregion
  1076. #pragma warning restore 3019
  1077. #pragma warning restore 1591
  1078. #pragma warning restore 1572
  1079. #pragma warning restore 1573
  1080. #endregion
  1081. }
  1082. }