GLUtils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <GL/glew.h>
  25. #include "Texture.h"
  26. #include "Material.h"
  27. #include "PixelFormat.h"
  28. namespace crown
  29. {
  30. /// OpenGL utilities for converting from wrapped names to GL names and vice-versa.
  31. class GL
  32. {
  33. public:
  34. static GLenum compare_function(CompareFunction function);
  35. static GLenum blend_function(BlendFunction function);
  36. static GLenum blend_equation(BlendEquation equation);
  37. static GLenum texture_wrap(TextureWrap wrap);
  38. static void texture_filter(TextureFilter filter, GLint& minFilter, GLint& magFilter);
  39. static GLenum polygon_mode(PolygonMode mode);
  40. static GLenum pixel_format(PixelFormat format);
  41. static PixelFormat pixel_format_from_gl_format(GLenum format);
  42. private:
  43. static const GLenum COMPARE_FUNCTION_TABLE[CF_COUNT];
  44. static const GLenum BLEND_FUNCTION_TABLE[BF_COUNT];
  45. static const GLenum BLEND_EQUATION_TABLE[BE_COUNT];
  46. static const GLenum TEXTURE_WRAP_TABLE[TW_COUNT];
  47. static const GLenum TEXTURE_MIN_FILTER_TABLE[TF_COUNT];
  48. static const GLenum TEXTURE_MAG_FILTER_TABLE[TF_COUNT];
  49. static const GLenum POLYGON_MODE_TABLE[PM_COUNT];
  50. // Disable construction
  51. GL();
  52. };
  53. //-----------------------------------------------------------------------------
  54. inline GLenum GL::compare_function(CompareFunction function)
  55. {
  56. return COMPARE_FUNCTION_TABLE[function];
  57. }
  58. //-----------------------------------------------------------------------------
  59. inline GLenum GL::blend_function(BlendFunction function)
  60. {
  61. return BLEND_FUNCTION_TABLE[function];
  62. }
  63. //-----------------------------------------------------------------------------
  64. inline GLenum GL::blend_equation(BlendEquation equation)
  65. {
  66. return BLEND_EQUATION_TABLE[equation];
  67. }
  68. //-----------------------------------------------------------------------------
  69. inline GLenum GL::texture_wrap(TextureWrap wrap)
  70. {
  71. return TEXTURE_WRAP_TABLE[wrap];
  72. }
  73. //-----------------------------------------------------------------------------
  74. inline void GL::texture_filter(TextureFilter filter, GLint& minFilter, GLint& magFilter)
  75. {
  76. minFilter = TEXTURE_MIN_FILTER_TABLE[filter];
  77. magFilter = TEXTURE_MAG_FILTER_TABLE[filter];
  78. }
  79. //-----------------------------------------------------------------------------
  80. inline GLenum GL::polygon_mode(PolygonMode mode)
  81. {
  82. return POLYGON_MODE_TABLE[mode];
  83. }
  84. //-----------------------------------------------------------------------------
  85. inline GLenum GL::pixel_format(PixelFormat format)
  86. {
  87. switch (format)
  88. {
  89. case PF_RGBA_8:
  90. return GL_RGBA;
  91. case PF_RGB_8:
  92. return GL_RGB;
  93. case PF_LA_8:
  94. return GL_LUMINANCE_ALPHA;
  95. case PF_L_8:
  96. return GL_LUMINANCE;
  97. default:
  98. return GL_RGB;
  99. }
  100. }
  101. //-----------------------------------------------------------------------------
  102. inline PixelFormat GL::pixel_format_from_gl_format(GLenum format)
  103. {
  104. switch (format)
  105. {
  106. case GL_RGBA:
  107. return PF_RGBA_8;
  108. case GL_RGB:
  109. return PF_RGB_8;
  110. case GL_LUMINANCE_ALPHA:
  111. return PF_LA_8;
  112. case GL_LUMINANCE:
  113. return PF_L_8;
  114. default:
  115. return PF_RGB_8;
  116. }
  117. }
  118. } // namespace crown