CmGLTexture.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "CmGLTexture.h"
  25. #include "CmGLSupport.h"
  26. #include "CmGLPixelFormat.h"
  27. #include "CmGLHardwarePixelBuffer.h"
  28. //#include "CmTextureManager.h"
  29. #include "CmCamera.h"
  30. #include "CmException.h"
  31. #include "CmBitwise.h"
  32. #include "CmTextureManager.h"
  33. #include "CmRenderSystemManager.h"
  34. #include "CmGLFBORenderTexture.h"
  35. #if CM_PLATFORM == CM_PLATFORM_WIN32
  36. # define WIN32_LEAN_AND_MEAN
  37. # if !defined(NOMINMAX) && defined(_MSC_VER)
  38. # define NOMINMAX // required to stop windows.h messing up std::min
  39. # endif
  40. # include <windows.h>
  41. # include <wingdi.h>
  42. #endif
  43. namespace CamelotEngine {
  44. GLTexture::GLTexture(GLSupport& support)
  45. : Texture(),
  46. mTextureID(0), mGLSupport(support)
  47. {
  48. }
  49. GLTexture::~GLTexture()
  50. {
  51. freeInternalResources();
  52. }
  53. GLenum GLTexture::getGLTextureTarget(void) const
  54. {
  55. switch(mTextureType)
  56. {
  57. case TEX_TYPE_1D:
  58. return GL_TEXTURE_1D;
  59. case TEX_TYPE_2D:
  60. return GL_TEXTURE_2D;
  61. case TEX_TYPE_3D:
  62. return GL_TEXTURE_3D;
  63. case TEX_TYPE_CUBE_MAP:
  64. return GL_TEXTURE_CUBE_MAP;
  65. default:
  66. return 0;
  67. };
  68. }
  69. //* Creation / loading methods ********************************************
  70. void GLTexture::createInternalResourcesImpl(void)
  71. {
  72. if (!GLEW_VERSION_1_2 && mTextureType == TEX_TYPE_3D)
  73. CM_EXCEPT(NotImplementedException,
  74. "3D Textures not supported before OpenGL 1.2");
  75. // Convert to nearest power-of-two size if required
  76. mWidth = GLPixelUtil::optionalPO2(mWidth);
  77. mHeight = GLPixelUtil::optionalPO2(mHeight);
  78. mDepth = GLPixelUtil::optionalPO2(mDepth);
  79. // Adjust format if required
  80. mFormat = TextureManager::instance().getNativeFormat(mTextureType, mFormat, mUsage);
  81. // Check requested number of mipmaps
  82. size_t maxMips = GLPixelUtil::getMaxMipmaps(mWidth, mHeight, mDepth, mFormat);
  83. if(mNumMipmaps>maxMips)
  84. mNumMipmaps = maxMips;
  85. // Generate texture name
  86. glGenTextures( 1, &mTextureID );
  87. // Set texture type
  88. glBindTexture( getGLTextureTarget(), mTextureID );
  89. // This needs to be set otherwise the texture doesn't get rendered
  90. if (GLEW_VERSION_1_2)
  91. glTexParameteri( getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, mNumMipmaps );
  92. // Set some misc default parameters so NVidia won't complain, these can of course be changed later
  93. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  94. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  95. if (GLEW_VERSION_1_2)
  96. {
  97. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  98. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  99. }
  100. // Allocate internal buffer so that glTexSubImageXD can be used
  101. // Internal format
  102. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat, mHwGamma);
  103. size_t width = mWidth;
  104. size_t height = mHeight;
  105. size_t depth = mDepth;
  106. if(PixelUtil::isCompressed(mFormat))
  107. {
  108. // Compressed formats
  109. size_t size = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  110. // Provide temporary buffer filled with zeroes as glCompressedTexImageXD does not
  111. // accept a 0 pointer like normal glTexImageXD
  112. // Run through this process for every mipmap to pregenerate mipmap piramid
  113. UINT8 *tmpdata = new UINT8[size];
  114. memset(tmpdata, 0, size);
  115. for(size_t mip=0; mip<=mNumMipmaps; mip++)
  116. {
  117. size = PixelUtil::getMemorySize(width, height, depth, mFormat);
  118. switch(mTextureType)
  119. {
  120. case TEX_TYPE_1D:
  121. glCompressedTexImage1DARB(GL_TEXTURE_1D, mip, format,
  122. width, 0,
  123. size, tmpdata);
  124. break;
  125. case TEX_TYPE_2D:
  126. glCompressedTexImage2DARB(GL_TEXTURE_2D, mip, format,
  127. width, height, 0,
  128. size, tmpdata);
  129. break;
  130. case TEX_TYPE_3D:
  131. glCompressedTexImage3DARB(GL_TEXTURE_3D, mip, format,
  132. width, height, depth, 0,
  133. size, tmpdata);
  134. break;
  135. case TEX_TYPE_CUBE_MAP:
  136. for(int face=0; face<6; face++) {
  137. glCompressedTexImage2DARB(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  138. width, height, 0,
  139. size, tmpdata);
  140. }
  141. break;
  142. };
  143. if(width>1) width = width/2;
  144. if(height>1) height = height/2;
  145. if(depth>1) depth = depth/2;
  146. }
  147. delete [] tmpdata;
  148. }
  149. else
  150. {
  151. // Run through this process to pregenerate mipmap piramid
  152. for(size_t mip=0; mip<=mNumMipmaps; mip++)
  153. {
  154. // Normal formats
  155. switch(mTextureType)
  156. {
  157. case TEX_TYPE_1D:
  158. glTexImage1D(GL_TEXTURE_1D, mip, format,
  159. width, 0,
  160. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  161. break;
  162. case TEX_TYPE_2D:
  163. glTexImage2D(GL_TEXTURE_2D, mip, format,
  164. width, height, 0,
  165. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  166. break;
  167. case TEX_TYPE_3D:
  168. glTexImage3D(GL_TEXTURE_3D, mip, format,
  169. width, height, depth, 0,
  170. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  171. break;
  172. case TEX_TYPE_CUBE_MAP:
  173. for(int face=0; face<6; face++) {
  174. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  175. width, height, 0,
  176. GL_RGBA, GL_UNSIGNED_BYTE, 0);
  177. }
  178. break;
  179. };
  180. if(width>1) width = width/2;
  181. if(height>1) height = height/2;
  182. if(depth>1) depth = depth/2;
  183. }
  184. }
  185. _createSurfaceList();
  186. // Get final internal format
  187. mFormat = getBuffer(0,0)->getFormat();
  188. }
  189. void GLTexture::createRenderTexture(void)
  190. {
  191. // Create the GL texture
  192. // This already does everything neccessary
  193. createInternalResources();
  194. }
  195. void GLTexture::initImpl()
  196. {
  197. if( mUsage & TU_RENDERTARGET )
  198. {
  199. createRenderTexture();
  200. return;
  201. }
  202. initializeFromTextureData();
  203. mTextureData.clear();
  204. }
  205. //*************************************************************************
  206. void GLTexture::freeInternalResourcesImpl()
  207. {
  208. mSurfaceList.clear();
  209. glDeleteTextures( 1, &mTextureID );
  210. }
  211. //---------------------------------------------------------------------------------------------
  212. void GLTexture::_createSurfaceList()
  213. {
  214. mSurfaceList.clear();
  215. for(size_t face=0; face<getNumFaces(); face++)
  216. {
  217. for(size_t mip=0; mip<=getNumMipmaps(); mip++)
  218. {
  219. GLHardwarePixelBuffer *buf = new GLTextureBuffer("", getGLTextureTarget(), mTextureID, face, mip,
  220. static_cast<HardwareBuffer::Usage>(mUsage), false, mHwGamma, mFSAA);
  221. mSurfaceList.push_back(HardwarePixelBufferPtr(buf));
  222. /// Check for error
  223. if(buf->getWidth()==0 || buf->getHeight()==0 || buf->getDepth()==0)
  224. {
  225. CM_EXCEPT(RenderingAPIException,
  226. "Zero sized texture surface on texture face "
  227. + toString(face)
  228. + " mipmap "+toString(mip)
  229. + ". Probably, the GL driver refused to create the texture.");
  230. }
  231. }
  232. }
  233. }
  234. //---------------------------------------------------------------------------------------------
  235. HardwarePixelBufferPtr GLTexture::getBuffer(size_t face, size_t mipmap)
  236. {
  237. if(face >= getNumFaces())
  238. CM_EXCEPT(InvalidParametersException, "Face index out of range");
  239. if(mipmap > mNumMipmaps)
  240. CM_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  241. unsigned int idx = face*(mNumMipmaps+1) + mipmap;
  242. assert(idx < mSurfaceList.size());
  243. return mSurfaceList[idx];
  244. }
  245. //---------------------------------------------------------------------------------------------
  246. void GLTexture::getCustomAttribute(const String& name, void* pData)
  247. {
  248. if (name == "GLID")
  249. *static_cast<GLuint*>(pData) = mTextureID;
  250. }
  251. }