/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
- // Same as DISABLE_SWAP_BUFFERS_FOR_PERFORMANCE_TESTING, allow to disable
- // clearing to CPU test performance more closely.
- //#define DISABLE_CLEAR_FOR_PERFORMANCE_TESTING
-
- using Delta.Graphics.BaseOpenGL;
- using Delta.Utilities.Graphics;
- using OpenTK.Graphics.OpenGL;
- using System;
-
- namespace Delta.Graphics.OpenTK
- {
- /// <summary>
- /// Open TK binding
- /// </summary>
- internal class OpenTKBinding : BaseOpenGLBinding
- {
- #region Constructors
- /// <summary>
- /// Create OpenTK binding.
- /// </summary>
- public OpenTKBinding()
- : base()
- {
- }
- #endregion
-
- #region SetDepthTest (Public)
- /// <summary>
- /// Implementation of SetDepthTest.
- /// </summary>
- /// <param name="enable">
- /// True to enable false otherwisse.
- /// </param>
- public override void SetDepthTest(bool enable)
- {
- if (enable)
- {
- GL.Enable(EnableCap.DepthTest);
- }
- else
- {
- GL.Disable(EnableCap.DepthTest);
- }
- }
- #endregion
-
- #region Viewport (Public)
- /// <summary>
- /// Set viewport size (pixel width and height from the window). Called in
- /// the constructor and every time when the device is reseted or scaled.
- /// </summary>
- /// <param name="x">X position for the viewport, usually 0</param>
- /// <param name="y">Y position for the viewport, usually 0</param>
- /// <param name="width">The width for the viewport.</param>
- /// <param name="height">The height for the viewport.</param>
- public override void Viewport(int x, int y, int width, int height)
- {
- GL.Viewport(x, y, width, height);
- }
- #endregion
-
- #region SetClearColor (Public)
- /// <summary>
- /// Clear color
- /// </summary>
- /// <param name="r">The r.</param>
- /// <param name="g">The g.</param>
- /// <param name="b">The b.</param>
- /// <param name="a">A.</param>
- public override void SetClearColor(float r, float g, float b, float a)
- {
- GL.ClearColor(r, g, b, a);
- }
- #endregion
-
- #region Clear (Public)
- /// <summary>
- /// Clear
- /// </summary>
- /// <param name="clearColorBuffer">if true then clear color buffer.</param>
- /// <param name="clearDepthBuffer">if true then clear depth buffer.</param>
- public override void Clear(bool clearColorBuffer, bool clearDepthBuffer)
- {
- GL.Clear(
- (clearColorBuffer
- ? ClearBufferMask.ColorBufferBit
- : 0) |
- (clearDepthBuffer
- ? ClearBufferMask.DepthBufferBit
- : 0));
- }
- #endregion
-
- #region TextureFilteringMode (Public)
- /// <summary>
- /// Texture Filtering Mode
- /// </summary>
- /// <param name="mode">The mode.</param>
- public override void TextureFilteringMode(TextureFilterMode mode)
- {
- int filterMode = (int)
- (mode == TextureFilterMode.Linear
- ? All.Linear
- : All.Nearest);
- GL.TexParameter(TextureTarget.Texture2D,
- TextureParameterName.TextureMinFilter, filterMode);
- GL.TexParameter(TextureTarget.Texture2D,
- TextureParameterName.TextureMagFilter, filterMode);
- }
- #endregion
-
- #region GetErrorString (Public)
- /// <summary>
- /// Get an error string if any native error occurred.
- /// </summary>
- public override string GetErrorString()
- {
- ErrorCode error = GL.GetError();
- if (error != ErrorCode.NoError)
- {
- string result = error.ToString();
- // Check if we got more than this one..
- while ((error = GL.GetError()) != ErrorCode.NoError)
- {
- // Then simply add it to the one before.
- result += ", " + error.ToString();
- }
- return result;
- }
-
- return "";
- }
- #endregion
-
- #region BindTexture (override)
- /// <summary>
- /// Binds texture unit with given OpenGL handle (texture id).
- /// </summary>
- /// <param name="textureHandle">The openGL texture id.</param>
- public override void BindTexture(int textureHandle)
- {
- GL.BindTexture(TextureTarget.Texture2D, textureHandle);
- }
- #endregion
-
- #region SetDepthMask (Public)
- /// <summary>
- /// Switch depth write on or off.
- /// </summary>
- /// <param name="enable">if set to <c>true</c> [enable].</param>
- public override void SetDepthMask(bool enable)
- {
- GL.DepthMask(enable);
- }
- #endregion
-
- #region GetString (Public)
- /// <summary>
- /// Get the OpenGL system string.
- /// </summary>
- /// <param name="stringName">The name of the string.</param>
- public override string GetString(int stringName)
- {
- return GL.GetString((StringName)stringName);
- }
- #endregion
-
- #region DeleteBuffer (Public)
- /// <summary>
- /// Delete the OpenGL vertex or index buffer object.
- /// </summary>
- /// <param name="bufferHandle">The openGL buffer id.</param>
- public override void DeleteBuffer(int bufferHandle)
- {
- }
- #endregion
-
- #region DeleteVertexArray (Public)
- /// <summary>
- /// Delete the OpenGL vertex array object.
- /// </summary>
- /// <param name="vertexArrayHandle">The openGL vertex array id.</param>
- public override void DeleteVertexArray(int vertexArrayHandle)
- {
- }
- #endregion
-
- #region DeleteTexture (Public)
- /// <summary>
- /// Delete the OpenGL texture object.
- /// </summary>
- /// <param name="textureHandle">The openGL texture id.</param>
- public override void DeleteTexture(int textureHandle)
- {
- GL.DeleteTextures(1, ref textureHandle);
- }
- #endregion
-
- #region DisableClientState (Public)
- /// <summary>
- /// Disable the client state which is used after rendering FFP geometry.
- /// </summary>
- /// <param name="stateType">Type of which state to disable.</param>
- public override void DisableClientState(int stateType)
- {
- GL.DisableClientState((ArrayCap)stateType);
- }
- #endregion
-
- #region SetupVertexAttribute (Public)
- /// <summary>
- /// Setup a vertex attribute.
- /// </summary>
- /// <param name="vertexAttributeId"></param>
- /// <param name="type"></param>
- /// <param name="size"></param>
- /// <param name="normalized"></param>
- /// <param name="stride"></param>
- /// <param name="offset"></param>
- public override void SetupVertexAttribute(int vertexAttributeId,
- int type, int size, bool normalized, int stride, int offset)
- {
- GL.EnableVertexAttribArray(vertexAttributeId);
- GL.VertexAttribPointer(vertexAttributeId, size,
- (VertexAttribPointerType)type, normalized, stride, offset);
- }
- #endregion
-
- #region SetupFFPVertexAttribute (Public)
- /// <summary>
- /// Setup a fixed function pipeline vertex attribute.
- /// </summary>
- /// <param name="clientStateType"></param>
- /// <param name="type"></param>
- /// <param name="size"></param>
- /// <param name="stride"></param>
- /// <param name="offset"></param>
- public override void SetupFFPVertexAttribute(int clientStateType,
- int type, int size, int stride, int offset)
- {
- ArrayCap cap = (ArrayCap)clientStateType;
- GL.EnableClientState(cap);
- switch (cap)
- {
- case ArrayCap.VertexArray:
- GL.VertexPointer(size, (VertexPointerType)type, stride,
- (IntPtr)offset);
- break;
-
- case ArrayCap.TextureCoordArray:
- GL.TexCoordPointer(size, (TexCoordPointerType)type, stride,
- (IntPtr)offset);
- break;
-
- case ArrayCap.ColorArray:
- GL.ColorPointer(size, (ColorPointerType)type, stride,
- (IntPtr)offset);
- break;
- }
- }
- #endregion
- }
- }