PageRenderTime 104ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 1ms

/Graphics/OpenTK/OpenTKBinding.cs

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