OGLTextureCube.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "GraphicsEvents.h"
  27. #include "GraphicsImpl.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "Renderer.h"
  31. #include "ResourceCache.h"
  32. #include "TextureCube.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. namespace Urho3D
  39. {
  40. static const char* cubeMapLayoutNames[] = {
  41. "horizontal",
  42. "horizontalnvidia",
  43. "horizontalcross",
  44. "verticalcross",
  45. "blender",
  46. 0
  47. };
  48. static SharedPtr<Image> GetTileImage(Image* src, int tileX, int tileY, int tileWidth, int tileHeight)
  49. {
  50. return SharedPtr<Image>(src->GetSubimage(IntRect(tileX * tileWidth, tileY * tileHeight, (tileX + 1) * tileWidth, (tileY + 1) * tileHeight)));
  51. }
  52. TextureCube::TextureCube(Context* context) :
  53. Texture(context)
  54. {
  55. target_ = GL_TEXTURE_CUBE_MAP;
  56. // Default to clamp mode addressing
  57. addressMode_[COORD_U] = ADDRESS_CLAMP;
  58. addressMode_[COORD_V] = ADDRESS_CLAMP;
  59. addressMode_[COORD_W] = ADDRESS_CLAMP;
  60. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  61. faceMemoryUse_[i] = 0;
  62. }
  63. TextureCube::~TextureCube()
  64. {
  65. Release();
  66. }
  67. void TextureCube::RegisterObject(Context* context)
  68. {
  69. context->RegisterFactory<TextureCube>();
  70. }
  71. bool TextureCube::BeginLoad(Deserializer& source)
  72. {
  73. ResourceCache* cache = GetSubsystem<ResourceCache>();
  74. // In headless mode, do not actually load the texture, just return success
  75. if (!graphics_)
  76. return true;
  77. // If device is lost, retry later
  78. if (graphics_->IsDeviceLost())
  79. {
  80. LOGWARNING("Texture load while device is lost");
  81. dataPending_ = true;
  82. return true;
  83. }
  84. cache->ResetDependencies(this);
  85. String texPath, texName, texExt;
  86. SplitPath(GetName(), texPath, texName, texExt);
  87. loadParameters_ = (new XMLFile(context_));
  88. if (!loadParameters_->Load(source))
  89. {
  90. loadParameters_.Reset();
  91. return false;
  92. }
  93. loadImages_.Clear();
  94. XMLElement textureElem = loadParameters_->GetRoot();
  95. XMLElement imageElem = textureElem.GetChild("image");
  96. // Single image and multiple faces with layout
  97. if (imageElem)
  98. {
  99. String name = imageElem.GetAttribute("name");
  100. // If path is empty, add the XML file path
  101. if (GetPath(name).Empty())
  102. name = texPath + name;
  103. CubeMapLayout layout = (CubeMapLayout)GetStringListIndex(imageElem.GetAttribute("layout").CString(), cubeMapLayoutNames, CML_HORIZONTAL);
  104. SharedPtr<Image> image = cache->GetTempResource<Image>(name);
  105. if (!image)
  106. return false;
  107. int faceWidth, faceHeight;
  108. loadImages_.Resize(MAX_CUBEMAP_FACES);
  109. switch (layout)
  110. {
  111. case CML_HORIZONTAL:
  112. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  113. faceHeight = image->GetHeight();
  114. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  115. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  116. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  117. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 3, 0, faceWidth, faceHeight);
  118. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 4, 0, faceWidth, faceHeight);
  119. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 5, 0, faceWidth, faceHeight);
  120. break;
  121. case CML_HORIZONTALNVIDIA:
  122. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  123. faceHeight = image->GetHeight();
  124. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  125. loadImages_[i] = GetTileImage(image, i, 0, faceWidth, faceHeight);
  126. break;
  127. case CML_HORIZONTALCROSS:
  128. faceWidth = image->GetWidth() / 4;
  129. faceHeight = image->GetHeight() / 3;
  130. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  131. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  132. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  133. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  134. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 3, 1, faceWidth, faceHeight);
  135. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  136. break;
  137. case CML_VERTICALCROSS:
  138. faceWidth = image->GetWidth() / 3;
  139. faceHeight = image->GetHeight() / 4;
  140. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  141. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  142. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  143. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  144. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  145. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 3, faceWidth, faceHeight);
  146. if (loadImages_[FACE_NEGATIVE_Z])
  147. loadImages_[FACE_NEGATIVE_Z]->FlipVertical();
  148. break;
  149. case CML_BLENDER:
  150. faceWidth = image->GetWidth() / 3;
  151. faceHeight = image->GetHeight() / 2;
  152. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  153. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  154. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  155. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  156. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  157. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  158. break;
  159. }
  160. }
  161. // Face per image
  162. else
  163. {
  164. XMLElement faceElem = textureElem.GetChild("face");
  165. while (faceElem)
  166. {
  167. String name = faceElem.GetAttribute("name");
  168. // If path is empty, add the XML file path
  169. if (GetPath(name).Empty())
  170. name = texPath + name;
  171. loadImages_.Push(cache->GetTempResource<Image>(name));
  172. cache->StoreResourceDependency(this, name);
  173. faceElem = faceElem.GetNext("face");
  174. }
  175. }
  176. // Precalculate mip levels if async loading
  177. if (GetAsyncLoadState() == ASYNC_LOADING)
  178. {
  179. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  180. {
  181. if (loadImages_[i])
  182. loadImages_[i]->PrecalculateLevels();
  183. }
  184. }
  185. return true;
  186. }
  187. bool TextureCube::EndLoad()
  188. {
  189. // In headless mode, do not actually load the texture, just return success
  190. if (!graphics_ || graphics_->IsDeviceLost())
  191. return true;
  192. // If over the texture budget, see if materials can be freed to allow textures to be freed
  193. CheckTextureBudget(GetTypeStatic());
  194. SetParameters(loadParameters_);
  195. for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
  196. SetData((CubeMapFace)i, loadImages_[i]);
  197. loadImages_.Clear();
  198. loadParameters_.Reset();
  199. return true;
  200. }
  201. void TextureCube::OnDeviceLost()
  202. {
  203. GPUObject::OnDeviceLost();
  204. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  205. {
  206. if (renderSurfaces_[i])
  207. renderSurfaces_[i]->OnDeviceLost();
  208. }
  209. }
  210. void TextureCube::OnDeviceReset()
  211. {
  212. if (!object_ || dataPending_)
  213. {
  214. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  215. ResourceCache* cache = GetSubsystem<ResourceCache>();
  216. if (cache->Exists(GetName()))
  217. dataLost_ = !cache->ReloadResource(this);
  218. if (!object_)
  219. {
  220. Create();
  221. dataLost_ = true;
  222. }
  223. }
  224. dataPending_ = false;
  225. }
  226. void TextureCube::Release()
  227. {
  228. if (object_)
  229. {
  230. if (!graphics_)
  231. return;
  232. if (!graphics_->IsDeviceLost())
  233. {
  234. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  235. {
  236. if (graphics_->GetTexture(i) == this)
  237. graphics_->SetTexture(i, 0);
  238. }
  239. glDeleteTextures(1, &object_);
  240. }
  241. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  242. {
  243. if (renderSurfaces_[i])
  244. renderSurfaces_[i]->Release();
  245. }
  246. object_ = 0;
  247. }
  248. }
  249. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  250. {
  251. if (size <= 0)
  252. {
  253. LOGERROR("Zero or negative cube texture size");
  254. return false;
  255. }
  256. if (usage == TEXTURE_DEPTHSTENCIL)
  257. {
  258. LOGERROR("Depth-stencil usage not supported for cube maps");
  259. return false;
  260. }
  261. // Delete the old rendersurfaces if any
  262. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  263. {
  264. renderSurfaces_[i].Reset();
  265. faceMemoryUse_[i] = 0;
  266. }
  267. usage_ = usage;
  268. if (usage == TEXTURE_RENDERTARGET)
  269. {
  270. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  271. {
  272. renderSurfaces_[i] = new RenderSurface(this);
  273. renderSurfaces_[i]->SetTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
  274. }
  275. // Nearest filtering and mipmaps disabled by default
  276. filterMode_ = FILTER_NEAREST;
  277. requestedLevels_ = 1;
  278. }
  279. if (usage == TEXTURE_RENDERTARGET)
  280. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(TextureCube, HandleRenderSurfaceUpdate));
  281. else
  282. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  283. width_ = size;
  284. height_ = size;
  285. format_ = format;
  286. return Create();
  287. }
  288. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  289. {
  290. PROFILE(SetTextureData);
  291. if (!object_ || !graphics_)
  292. {
  293. LOGERROR("No texture created, can not set data");
  294. return false;
  295. }
  296. if (!data)
  297. {
  298. LOGERROR("Null source for setting data");
  299. return false;
  300. }
  301. if (level >= levels_)
  302. {
  303. LOGERROR("Illegal mip level for setting data");
  304. return false;
  305. }
  306. if (graphics_->IsDeviceLost())
  307. {
  308. LOGWARNING("Texture data assignment while device is lost");
  309. dataPending_ = true;
  310. return true;
  311. }
  312. if (IsCompressed())
  313. {
  314. x &= ~3;
  315. y &= ~3;
  316. }
  317. int levelWidth = GetLevelWidth(level);
  318. int levelHeight = GetLevelHeight(level);
  319. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  320. {
  321. LOGERROR("Illegal dimensions for setting data");
  322. return false;
  323. }
  324. graphics_->SetTextureForUpdate(this);
  325. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  326. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  327. if (!IsCompressed())
  328. {
  329. if (wholeLevel)
  330. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0, GetExternalFormat(format_),
  331. GetDataType(format_), data);
  332. else
  333. glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, GetExternalFormat(format_),
  334. GetDataType(format_), data);
  335. }
  336. else
  337. {
  338. if (wholeLevel)
  339. glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0,
  340. GetDataSize(width, height), data);
  341. else
  342. glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, format,
  343. GetDataSize(width, height), data);
  344. }
  345. graphics_->SetTexture(0, 0);
  346. return true;
  347. }
  348. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  349. {
  350. SharedPtr<Image> image(new Image(context_));
  351. if (!image->Load(source))
  352. return false;
  353. return SetData(face, image);
  354. }
  355. bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  356. {
  357. if (!image)
  358. {
  359. LOGERROR("Null image, can not set face data");
  360. return false;
  361. }
  362. unsigned memoryUse = 0;
  363. int quality = QUALITY_HIGH;
  364. Renderer* renderer = GetSubsystem<Renderer>();
  365. if (renderer)
  366. quality = renderer->GetTextureQuality();
  367. if (!image->IsCompressed())
  368. {
  369. unsigned char* levelData = image->GetData();
  370. int levelWidth = image->GetWidth();
  371. int levelHeight = image->GetHeight();
  372. unsigned components = image->GetComponents();
  373. unsigned format = 0;
  374. if (levelWidth != levelHeight)
  375. {
  376. LOGERROR("Cube texture width not equal to height");
  377. return false;
  378. }
  379. // Discard unnecessary mip levels
  380. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  381. {
  382. image = image->GetNextLevel();
  383. levelData = image->GetData();
  384. levelWidth = image->GetWidth();
  385. levelHeight = image->GetHeight();
  386. }
  387. switch (components)
  388. {
  389. case 1:
  390. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  391. break;
  392. case 2:
  393. format = Graphics::GetLuminanceAlphaFormat();
  394. break;
  395. case 3:
  396. format = Graphics::GetRGBFormat();
  397. break;
  398. case 4:
  399. format = Graphics::GetRGBAFormat();
  400. break;
  401. }
  402. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  403. if (!face)
  404. {
  405. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  406. if (IsCompressed() && requestedLevels_ > 1)
  407. requestedLevels_ = 0;
  408. SetSize(levelWidth, format);
  409. }
  410. else
  411. {
  412. if (!object_)
  413. {
  414. LOGERROR("Cube texture face 0 must be loaded first");
  415. return false;
  416. }
  417. if (levelWidth != width_ || format != format_)
  418. {
  419. LOGERROR("Cube texture face does not match size or format of face 0");
  420. return false;
  421. }
  422. }
  423. for (unsigned i = 0; i < levels_; ++i)
  424. {
  425. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  426. memoryUse += levelWidth * levelHeight * components;
  427. if (i < levels_ - 1)
  428. {
  429. image = image->GetNextLevel();
  430. levelData = image->GetData();
  431. levelWidth = image->GetWidth();
  432. levelHeight = image->GetHeight();
  433. }
  434. }
  435. }
  436. else
  437. {
  438. int width = image->GetWidth();
  439. int height = image->GetHeight();
  440. unsigned levels = image->GetNumCompressedLevels();
  441. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  442. bool needDecompress = false;
  443. if (width != height)
  444. {
  445. LOGERROR("Cube texture width not equal to height");
  446. return false;
  447. }
  448. if (!format)
  449. {
  450. format = Graphics::GetRGBAFormat();
  451. needDecompress = true;
  452. }
  453. unsigned mipsToSkip = mipsToSkip_[quality];
  454. if (mipsToSkip >= levels)
  455. mipsToSkip = levels - 1;
  456. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  457. --mipsToSkip;
  458. width /= (1 << mipsToSkip);
  459. height /= (1 << mipsToSkip);
  460. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  461. if (!face)
  462. {
  463. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  464. SetSize(width, format);
  465. }
  466. else
  467. {
  468. if (!object_)
  469. {
  470. LOGERROR("Cube texture face 0 must be loaded first");
  471. return false;
  472. }
  473. if (width != width_ || format != format_)
  474. {
  475. LOGERROR("Cube texture face does not match size or format of face 0");
  476. return false;
  477. }
  478. }
  479. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  480. {
  481. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  482. if (!needDecompress)
  483. {
  484. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  485. memoryUse += level.rows_ * level.rowSize_;
  486. }
  487. else
  488. {
  489. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  490. level.Decompress(rgbaData);
  491. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  492. memoryUse += level.width_ * level.height_ * 4;
  493. delete[] rgbaData;
  494. }
  495. }
  496. }
  497. faceMemoryUse_[face] = memoryUse;
  498. unsigned totalMemoryUse = sizeof(TextureCube);
  499. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  500. totalMemoryUse += faceMemoryUse_[i];
  501. SetMemoryUse(totalMemoryUse);
  502. return true;
  503. }
  504. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  505. {
  506. #ifndef GL_ES_VERSION_2_0
  507. if (!object_ || !graphics_)
  508. {
  509. LOGERROR("No texture created, can not get data");
  510. return false;
  511. }
  512. if (!dest)
  513. {
  514. LOGERROR("Null destination for getting data");
  515. return false;
  516. }
  517. if (level >= levels_)
  518. {
  519. LOGERROR("Illegal mip level for getting data");
  520. return false;
  521. }
  522. if (graphics_->IsDeviceLost())
  523. {
  524. LOGWARNING("Getting texture data while device is lost");
  525. return false;
  526. }
  527. graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
  528. if (!IsCompressed())
  529. glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, GetExternalFormat(format_), GetDataType(format_), dest);
  530. else
  531. glGetCompressedTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, dest);
  532. graphics_->SetTexture(0, 0);
  533. return true;
  534. #else
  535. LOGERROR("Getting texture data not supported");
  536. return false;
  537. #endif
  538. }
  539. bool TextureCube::Create()
  540. {
  541. Release();
  542. if (!graphics_ || !width_ || !height_)
  543. return false;
  544. if (graphics_->IsDeviceLost())
  545. {
  546. LOGWARNING("Texture creation while device is lost");
  547. return true;
  548. }
  549. glGenTextures(1, &object_);
  550. // Ensure that our texture is bound to OpenGL texture unit 0
  551. graphics_->SetTextureForUpdate(this);
  552. // If not compressed, create the initial level 0 texture with null data
  553. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  554. unsigned externalFormat = GetExternalFormat(format_);
  555. unsigned dataType = GetDataType(format_);
  556. bool success = true;
  557. if (!IsCompressed())
  558. {
  559. glGetError();
  560. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  561. {
  562. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, 0);
  563. if (glGetError())
  564. success = false;
  565. }
  566. }
  567. if (!success)
  568. LOGERROR("Failed to create texture");
  569. // Set mipmapping
  570. levels_ = requestedLevels_;
  571. if (!levels_)
  572. {
  573. unsigned maxSize = Max((int)width_, (int)height_);
  574. while (maxSize)
  575. {
  576. maxSize >>= 1;
  577. ++levels_;
  578. }
  579. }
  580. #ifndef GL_ES_VERSION_2_0
  581. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  582. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  583. #endif
  584. // Set initial parameters, then unbind the texture
  585. UpdateParameters();
  586. graphics_->SetTexture(0, 0);
  587. return success;
  588. }
  589. void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  590. {
  591. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  592. {
  593. if (renderSurfaces_[i] && renderSurfaces_[i]->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  594. renderSurfaces_[i]->QueueUpdate();
  595. }
  596. }
  597. }