CmGLTextureManager.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLTextureManager.h"
  25. #include "CmRenderSystem.h"
  26. #include "CmGLRenderTexture.h"
  27. #include "CmGLMultiRenderTexture.h"
  28. namespace CamelotEngine {
  29. //-----------------------------------------------------------------------------
  30. GLTextureManager::GLTextureManager(GLSupport& support)
  31. :TextureManager(), mGLSupport(support), mWarningTextureID(0)
  32. {
  33. createWarningTexture();
  34. }
  35. //-----------------------------------------------------------------------------
  36. GLTextureManager::~GLTextureManager()
  37. {
  38. // Delete warning texture
  39. glDeleteTextures(1, &mWarningTextureID);
  40. }
  41. //-----------------------------------------------------------------------------
  42. Texture* GLTextureManager::createTextureImpl()
  43. {
  44. return new GLTexture(mGLSupport);
  45. }
  46. //-----------------------------------------------------------------------------
  47. RenderTexture* GLTextureManager::createRenderTextureImpl()
  48. {
  49. return new GLRenderTexture();
  50. }
  51. //----------------------------------------------------------------------------
  52. MultiRenderTexture* GLTextureManager::createMultiRenderTextureImpl()
  53. {
  54. return new GLMultiRenderTexture();
  55. }
  56. //-----------------------------------------------------------------------------
  57. void GLTextureManager::createWarningTexture()
  58. {
  59. // Generate warning texture
  60. UINT32 width = 8;
  61. UINT32 height = 8;
  62. UINT32 *data = new UINT32[width*height]; // 0xXXRRGGBB
  63. // Yellow/black stripes
  64. for(UINT32 y=0; y<height; ++y)
  65. {
  66. for(UINT32 x=0; x<width; ++x)
  67. {
  68. data[y*width+x] = (((x+y)%8)<4)?0x000000:0xFFFF00;
  69. }
  70. }
  71. // Create GL resource
  72. glGenTextures(1, &mWarningTextureID);
  73. glBindTexture(GL_TEXTURE_2D, mWarningTextureID);
  74. if (GLEW_VERSION_1_2)
  75. {
  76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  77. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, (void*)data);
  78. }
  79. else
  80. {
  81. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT, (void*)data);
  82. }
  83. // Free memory
  84. delete [] data;
  85. }
  86. //-----------------------------------------------------------------------------
  87. PixelFormat GLTextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage)
  88. {
  89. // Adjust requested parameters to capabilities
  90. const RenderSystemCapabilities *caps = CamelotEngine::RenderSystem::instancePtr()->getCapabilities();
  91. // Check compressed texture support
  92. // if a compressed format not supported, revert to PF_A8R8G8B8
  93. if(PixelUtil::isCompressed(format) &&
  94. !caps->hasCapability( RSC_TEXTURE_COMPRESSION_DXT ))
  95. {
  96. return PF_A8R8G8B8;
  97. }
  98. // if floating point textures not supported, revert to PF_A8R8G8B8
  99. if(PixelUtil::isFloatingPoint(format) &&
  100. !caps->hasCapability( RSC_TEXTURE_FLOAT ))
  101. {
  102. return PF_A8R8G8B8;
  103. }
  104. // Check if this is a valid rendertarget format
  105. if( usage & TU_RENDERTARGET )
  106. {
  107. /// Get closest supported alternative
  108. /// If mFormat is supported it's returned
  109. return GLRTTManager::instance().getSupportedAlternative(format);
  110. }
  111. // Supported
  112. return format;
  113. }
  114. }