Texture.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using OpenGl;
  18. namespace Drawing
  19. {
  20. public class Texture : IDisposable
  21. {
  22. private uint handle;
  23. public uint Handle
  24. {
  25. get
  26. {
  27. if (handle == 0) {
  28. Create();
  29. }
  30. return handle;
  31. }
  32. }
  33. protected uint width;
  34. public uint Width
  35. {
  36. get
  37. {
  38. return width;
  39. }
  40. }
  41. protected uint height;
  42. public uint Height
  43. {
  44. get
  45. {
  46. return height;
  47. }
  48. }
  49. //cached value for use in Create()
  50. private uint glformat;
  51. protected Texture()
  52. {
  53. handle = 0;
  54. }
  55. public Texture(uint width, uint height, uint glformat)
  56. {
  57. this.width = width;
  58. this.height = height;
  59. this.glformat = glformat;
  60. }
  61. protected virtual void Create()
  62. {
  63. LogManager.Log(LogLevel.Debug, "Texture.Create()");
  64. // Not needed on newer OpenGL
  65. if (!GlHelper.HasExtension("GL_ARB_texture_non_power_of_two"))
  66. {
  67. if(!IsPowerOf2(width) || !IsPowerOf2(height))
  68. throw new Exception("Texture size must be power of 2");
  69. }
  70. GlUtil.Assert("before creating texture");
  71. CreateTexture();
  72. try {
  73. gl.BindTexture(gl.TEXTURE_2D, Handle);
  74. gl.TexImage2D(gl.TEXTURE_2D, 0, (int) glformat,
  75. (int) width, (int) height, 0,
  76. gl.RGBA, gl.UNSIGNED_BYTE, IntPtr.Zero);
  77. GlUtil.Assert("creating texture (too big?)");
  78. SetTextureParams();
  79. } catch(Exception) {
  80. uint[] handles = { Handle };
  81. gl.DeleteTextures(1, handles);
  82. throw;
  83. }
  84. }
  85. public unsafe void Dispose()
  86. {
  87. if(handle == 0)
  88. return;
  89. uint[] handles = { Handle };
  90. gl.DeleteTextures(1, handles);
  91. this.handle = 0;
  92. }
  93. /// <summary>
  94. /// Helper method: set common texture parameters.
  95. /// </summary>
  96. protected static void SetTextureParams()
  97. {
  98. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  99. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  100. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP);
  101. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP);
  102. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP);
  103. GlUtil.Assert("setting texture parameters");
  104. }
  105. /// <summary>
  106. /// Helper method: creates a texture using glGenTexture and saves the handle.
  107. /// </summary>
  108. protected unsafe void CreateTexture()
  109. {
  110. if(!GlUtil.ContextValid) {
  111. LogManager.Log(LogLevel.Warning, "No opengl context active when creating textures");
  112. }
  113. uint[] handles = new uint[1];
  114. gl.GenTextures(1, handles);
  115. // 0 is used as special value to mark invalid textures here
  116. if(handles[0] == 0) {
  117. gl.GenTextures(1, handles);
  118. }
  119. this.handle = handles[0];
  120. }
  121. protected static bool IsPowerOf2(uint val)
  122. {
  123. return (val & (val - 1)) == 0;
  124. }
  125. }
  126. }
  127. /* EOF */