PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Graphics/OpenTK/OpenTKBinding.cs

#
C# | 263 lines | 154 code | 20 blank | 89 comment | 7 complexity | bb30576cbcb81df5aa5425d7c2e96950 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Same as DISABLE_SWAP_BUFFERS_FOR_PERFORMANCE_TESTING, allow to disable
  2. // clearing to CPU test performance more closely.
  3. //#define DISABLE_CLEAR_FOR_PERFORMANCE_TESTING
  4. using Delta.Graphics.BaseOpenGL;
  5. using Delta.Utilities.Graphics;
  6. using OpenTK.Graphics.OpenGL;
  7. using System;
  8. namespace Delta.Graphics.OpenTK
  9. {
  10. /// <summary>
  11. /// Open TK binding
  12. /// </summary>
  13. internal class OpenTKBinding : BaseOpenGLBinding
  14. {
  15. #region Constructors
  16. /// <summary>
  17. /// Create OpenTK binding.
  18. /// </summary>
  19. public OpenTKBinding()
  20. : base()
  21. {
  22. }
  23. #endregion
  24. #region SetDepthTest (Public)
  25. /// <summary>
  26. /// Implementation of SetDepthTest.
  27. /// </summary>
  28. /// <param name="enable">
  29. /// True to enable false otherwisse.
  30. /// </param>
  31. public override void SetDepthTest(bool enable)
  32. {
  33. if (enable)
  34. {
  35. GL.Enable(EnableCap.DepthTest);
  36. }
  37. else
  38. {
  39. GL.Disable(EnableCap.DepthTest);
  40. }
  41. }
  42. #endregion
  43. #region Viewport (Public)
  44. /// <summary>
  45. /// Set viewport size (pixel width and height from the window). Called in
  46. /// the constructor and every time when the device is reseted or scaled.
  47. /// </summary>
  48. /// <param name="x">X position for the viewport, usually 0</param>
  49. /// <param name="y">Y position for the viewport, usually 0</param>
  50. /// <param name="width">The width for the viewport.</param>
  51. /// <param name="height">The height for the viewport.</param>
  52. public override void Viewport(int x, int y, int width, int height)
  53. {
  54. GL.Viewport(x, y, width, height);
  55. }
  56. #endregion
  57. #region SetClearColor (Public)
  58. /// <summary>
  59. /// Clear color
  60. /// </summary>
  61. /// <param name="r">The r.</param>
  62. /// <param name="g">The g.</param>
  63. /// <param name="b">The b.</param>
  64. /// <param name="a">A.</param>
  65. public override void SetClearColor(float r, float g, float b, float a)
  66. {
  67. GL.ClearColor(r, g, b, a);
  68. }
  69. #endregion
  70. #region Clear (Public)
  71. /// <summary>
  72. /// Clear
  73. /// </summary>
  74. /// <param name="clearColorBuffer">if true then clear color buffer.</param>
  75. /// <param name="clearDepthBuffer">if true then clear depth buffer.</param>
  76. public override void Clear(bool clearColorBuffer, bool clearDepthBuffer)
  77. {
  78. GL.Clear(
  79. (clearColorBuffer
  80. ? ClearBufferMask.ColorBufferBit
  81. : 0) |
  82. (clearDepthBuffer
  83. ? ClearBufferMask.DepthBufferBit
  84. : 0));
  85. }
  86. #endregion
  87. #region TextureFilteringMode (Public)
  88. /// <summary>
  89. /// Texture Filtering Mode
  90. /// </summary>
  91. /// <param name="mode">The mode.</param>
  92. public override void TextureFilteringMode(TextureFilterMode mode)
  93. {
  94. int filterMode = (int)
  95. (mode == TextureFilterMode.Linear
  96. ? All.Linear
  97. : All.Nearest);
  98. GL.TexParameter(TextureTarget.Texture2D,
  99. TextureParameterName.TextureMinFilter, filterMode);
  100. GL.TexParameter(TextureTarget.Texture2D,
  101. TextureParameterName.TextureMagFilter, filterMode);
  102. }
  103. #endregion
  104. #region GetErrorString (Public)
  105. /// <summary>
  106. /// Get an error string if any native error occurred.
  107. /// </summary>
  108. public override string GetErrorString()
  109. {
  110. ErrorCode error = GL.GetError();
  111. if (error != ErrorCode.NoError)
  112. {
  113. string result = error.ToString();
  114. // Check if we got more than this one..
  115. while ((error = GL.GetError()) != ErrorCode.NoError)
  116. {
  117. // Then simply add it to the one before.
  118. result += ", " + error.ToString();
  119. }
  120. return result;
  121. }
  122. return "";
  123. }
  124. #endregion
  125. #region BindTexture (override)
  126. /// <summary>
  127. /// Binds texture unit with given OpenGL handle (texture id).
  128. /// </summary>
  129. /// <param name="textureHandle">The openGL texture id.</param>
  130. public override void BindTexture(int textureHandle)
  131. {
  132. GL.BindTexture(TextureTarget.Texture2D, textureHandle);
  133. }
  134. #endregion
  135. #region SetDepthMask (Public)
  136. /// <summary>
  137. /// Switch depth write on or off.
  138. /// </summary>
  139. /// <param name="enable">if set to <c>true</c> [enable].</param>
  140. public override void SetDepthMask(bool enable)
  141. {
  142. GL.DepthMask(enable);
  143. }
  144. #endregion
  145. #region GetString (Public)
  146. /// <summary>
  147. /// Get the OpenGL system string.
  148. /// </summary>
  149. /// <param name="stringName">The name of the string.</param>
  150. public override string GetString(int stringName)
  151. {
  152. return GL.GetString((StringName)stringName);
  153. }
  154. #endregion
  155. #region DeleteBuffer (Public)
  156. /// <summary>
  157. /// Delete the OpenGL vertex or index buffer object.
  158. /// </summary>
  159. /// <param name="bufferHandle">The openGL buffer id.</param>
  160. public override void DeleteBuffer(int bufferHandle)
  161. {
  162. }
  163. #endregion
  164. #region DeleteVertexArray (Public)
  165. /// <summary>
  166. /// Delete the OpenGL vertex array object.
  167. /// </summary>
  168. /// <param name="vertexArrayHandle">The openGL vertex array id.</param>
  169. public override void DeleteVertexArray(int vertexArrayHandle)
  170. {
  171. }
  172. #endregion
  173. #region DeleteTexture (Public)
  174. /// <summary>
  175. /// Delete the OpenGL texture object.
  176. /// </summary>
  177. /// <param name="textureHandle">The openGL texture id.</param>
  178. public override void DeleteTexture(int textureHandle)
  179. {
  180. GL.DeleteTextures(1, ref textureHandle);
  181. }
  182. #endregion
  183. #region DisableClientState (Public)
  184. /// <summary>
  185. /// Disable the client state which is used after rendering FFP geometry.
  186. /// </summary>
  187. /// <param name="stateType">Type of which state to disable.</param>
  188. public override void DisableClientState(int stateType)
  189. {
  190. GL.DisableClientState((ArrayCap)stateType);
  191. }
  192. #endregion
  193. #region SetupVertexAttribute (Public)
  194. /// <summary>
  195. /// Setup a vertex attribute.
  196. /// </summary>
  197. /// <param name="vertexAttributeId"></param>
  198. /// <param name="type"></param>
  199. /// <param name="size"></param>
  200. /// <param name="normalized"></param>
  201. /// <param name="stride"></param>
  202. /// <param name="offset"></param>
  203. public override void SetupVertexAttribute(int vertexAttributeId,
  204. int type, int size, bool normalized, int stride, int offset)
  205. {
  206. GL.EnableVertexAttribArray(vertexAttributeId);
  207. GL.VertexAttribPointer(vertexAttributeId, size,
  208. (VertexAttribPointerType)type, normalized, stride, offset);
  209. }
  210. #endregion
  211. #region SetupFFPVertexAttribute (Public)
  212. /// <summary>
  213. /// Setup a fixed function pipeline vertex attribute.
  214. /// </summary>
  215. /// <param name="clientStateType"></param>
  216. /// <param name="type"></param>
  217. /// <param name="size"></param>
  218. /// <param name="stride"></param>
  219. /// <param name="offset"></param>
  220. public override void SetupFFPVertexAttribute(int clientStateType,
  221. int type, int size, int stride, int offset)
  222. {
  223. ArrayCap cap = (ArrayCap)clientStateType;
  224. GL.EnableClientState(cap);
  225. switch (cap)
  226. {
  227. case ArrayCap.VertexArray:
  228. GL.VertexPointer(size, (VertexPointerType)type, stride,
  229. (IntPtr)offset);
  230. break;
  231. case ArrayCap.TextureCoordArray:
  232. GL.TexCoordPointer(size, (TexCoordPointerType)type, stride,
  233. (IntPtr)offset);
  234. break;
  235. case ArrayCap.ColorArray:
  236. GL.ColorPointer(size, (ColorPointerType)type, stride,
  237. (IntPtr)offset);
  238. break;
  239. }
  240. }
  241. #endregion
  242. }
  243. }