OGLTextureCube.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. {
  148. loadImages_[FACE_NEGATIVE_Z]->FlipVertical();
  149. loadImages_[FACE_NEGATIVE_Z]->FlipHorizontal();
  150. }
  151. break;
  152. case CML_BLENDER:
  153. faceWidth = image->GetWidth() / 3;
  154. faceHeight = image->GetHeight() / 2;
  155. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  156. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  157. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  158. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  159. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  160. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  161. break;
  162. }
  163. }
  164. // Face per image
  165. else
  166. {
  167. XMLElement faceElem = textureElem.GetChild("face");
  168. while (faceElem)
  169. {
  170. String name = faceElem.GetAttribute("name");
  171. // If path is empty, add the XML file path
  172. if (GetPath(name).Empty())
  173. name = texPath + name;
  174. loadImages_.Push(cache->GetTempResource<Image>(name));
  175. cache->StoreResourceDependency(this, name);
  176. faceElem = faceElem.GetNext("face");
  177. }
  178. }
  179. // Precalculate mip levels if async loading
  180. if (GetAsyncLoadState() == ASYNC_LOADING)
  181. {
  182. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  183. {
  184. if (loadImages_[i])
  185. loadImages_[i]->PrecalculateLevels();
  186. }
  187. }
  188. return true;
  189. }
  190. bool TextureCube::EndLoad()
  191. {
  192. // In headless mode, do not actually load the texture, just return success
  193. if (!graphics_ || graphics_->IsDeviceLost())
  194. return true;
  195. // If over the texture budget, see if materials can be freed to allow textures to be freed
  196. CheckTextureBudget(GetTypeStatic());
  197. SetParameters(loadParameters_);
  198. for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
  199. SetData((CubeMapFace)i, loadImages_[i]);
  200. loadImages_.Clear();
  201. loadParameters_.Reset();
  202. return true;
  203. }
  204. void TextureCube::OnDeviceLost()
  205. {
  206. GPUObject::OnDeviceLost();
  207. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  208. {
  209. if (renderSurfaces_[i])
  210. renderSurfaces_[i]->OnDeviceLost();
  211. }
  212. }
  213. void TextureCube::OnDeviceReset()
  214. {
  215. if (!object_ || dataPending_)
  216. {
  217. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  218. ResourceCache* cache = GetSubsystem<ResourceCache>();
  219. if (cache->Exists(GetName()))
  220. dataLost_ = !cache->ReloadResource(this);
  221. if (!object_)
  222. {
  223. Create();
  224. dataLost_ = true;
  225. }
  226. }
  227. dataPending_ = false;
  228. }
  229. void TextureCube::Release()
  230. {
  231. if (object_)
  232. {
  233. if (!graphics_)
  234. return;
  235. if (!graphics_->IsDeviceLost())
  236. {
  237. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  238. {
  239. if (graphics_->GetTexture(i) == this)
  240. graphics_->SetTexture(i, 0);
  241. }
  242. glDeleteTextures(1, &object_);
  243. }
  244. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  245. {
  246. if (renderSurfaces_[i])
  247. renderSurfaces_[i]->Release();
  248. }
  249. object_ = 0;
  250. }
  251. }
  252. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  253. {
  254. if (size <= 0)
  255. {
  256. LOGERROR("Zero or negative cube texture size");
  257. return false;
  258. }
  259. if (usage == TEXTURE_DEPTHSTENCIL)
  260. {
  261. LOGERROR("Depth-stencil usage not supported for cube maps");
  262. return false;
  263. }
  264. // Delete the old rendersurfaces if any
  265. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  266. {
  267. renderSurfaces_[i].Reset();
  268. faceMemoryUse_[i] = 0;
  269. }
  270. usage_ = usage;
  271. if (usage == TEXTURE_RENDERTARGET)
  272. {
  273. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  274. {
  275. renderSurfaces_[i] = new RenderSurface(this);
  276. renderSurfaces_[i]->SetTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
  277. }
  278. // Nearest filtering and mipmaps disabled by default
  279. filterMode_ = FILTER_NEAREST;
  280. requestedLevels_ = 1;
  281. }
  282. if (usage == TEXTURE_RENDERTARGET)
  283. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(TextureCube, HandleRenderSurfaceUpdate));
  284. else
  285. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  286. width_ = size;
  287. height_ = size;
  288. format_ = format;
  289. return Create();
  290. }
  291. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  292. {
  293. PROFILE(SetTextureData);
  294. if (!object_ || !graphics_)
  295. {
  296. LOGERROR("No texture created, can not set data");
  297. return false;
  298. }
  299. if (!data)
  300. {
  301. LOGERROR("Null source for setting data");
  302. return false;
  303. }
  304. if (level >= levels_)
  305. {
  306. LOGERROR("Illegal mip level for setting data");
  307. return false;
  308. }
  309. if (graphics_->IsDeviceLost())
  310. {
  311. LOGWARNING("Texture data assignment while device is lost");
  312. dataPending_ = true;
  313. return true;
  314. }
  315. if (IsCompressed())
  316. {
  317. x &= ~3;
  318. y &= ~3;
  319. }
  320. int levelWidth = GetLevelWidth(level);
  321. int levelHeight = GetLevelHeight(level);
  322. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  323. {
  324. LOGERROR("Illegal dimensions for setting data");
  325. return false;
  326. }
  327. graphics_->SetTextureForUpdate(this);
  328. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  329. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  330. if (!IsCompressed())
  331. {
  332. if (wholeLevel)
  333. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0, GetExternalFormat(format_),
  334. GetDataType(format_), data);
  335. else
  336. glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, GetExternalFormat(format_),
  337. GetDataType(format_), data);
  338. }
  339. else
  340. {
  341. if (wholeLevel)
  342. glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0,
  343. GetDataSize(width, height), data);
  344. else
  345. glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, format,
  346. GetDataSize(width, height), data);
  347. }
  348. graphics_->SetTexture(0, 0);
  349. return true;
  350. }
  351. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  352. {
  353. SharedPtr<Image> image(new Image(context_));
  354. if (!image->Load(source))
  355. return false;
  356. return SetData(face, image);
  357. }
  358. bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  359. {
  360. if (!image)
  361. {
  362. LOGERROR("Null image, can not set face data");
  363. return false;
  364. }
  365. unsigned memoryUse = 0;
  366. int quality = QUALITY_HIGH;
  367. Renderer* renderer = GetSubsystem<Renderer>();
  368. if (renderer)
  369. quality = renderer->GetTextureQuality();
  370. if (!image->IsCompressed())
  371. {
  372. unsigned char* levelData = image->GetData();
  373. int levelWidth = image->GetWidth();
  374. int levelHeight = image->GetHeight();
  375. unsigned components = image->GetComponents();
  376. unsigned format = 0;
  377. if (levelWidth != levelHeight)
  378. {
  379. LOGERROR("Cube texture width not equal to height");
  380. return false;
  381. }
  382. // Discard unnecessary mip levels
  383. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  384. {
  385. image = image->GetNextLevel();
  386. levelData = image->GetData();
  387. levelWidth = image->GetWidth();
  388. levelHeight = image->GetHeight();
  389. }
  390. switch (components)
  391. {
  392. case 1:
  393. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  394. break;
  395. case 2:
  396. format = Graphics::GetLuminanceAlphaFormat();
  397. break;
  398. case 3:
  399. format = Graphics::GetRGBFormat();
  400. break;
  401. case 4:
  402. format = Graphics::GetRGBAFormat();
  403. break;
  404. }
  405. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  406. if (!face)
  407. {
  408. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  409. if (IsCompressed() && requestedLevels_ > 1)
  410. requestedLevels_ = 0;
  411. SetSize(levelWidth, format);
  412. }
  413. else
  414. {
  415. if (!object_)
  416. {
  417. LOGERROR("Cube texture face 0 must be loaded first");
  418. return false;
  419. }
  420. if (levelWidth != width_ || format != format_)
  421. {
  422. LOGERROR("Cube texture face does not match size or format of face 0");
  423. return false;
  424. }
  425. }
  426. for (unsigned i = 0; i < levels_; ++i)
  427. {
  428. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  429. memoryUse += levelWidth * levelHeight * components;
  430. if (i < levels_ - 1)
  431. {
  432. image = image->GetNextLevel();
  433. levelData = image->GetData();
  434. levelWidth = image->GetWidth();
  435. levelHeight = image->GetHeight();
  436. }
  437. }
  438. }
  439. else
  440. {
  441. int width = image->GetWidth();
  442. int height = image->GetHeight();
  443. unsigned levels = image->GetNumCompressedLevels();
  444. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  445. bool needDecompress = false;
  446. if (width != height)
  447. {
  448. LOGERROR("Cube texture width not equal to height");
  449. return false;
  450. }
  451. if (!format)
  452. {
  453. format = Graphics::GetRGBAFormat();
  454. needDecompress = true;
  455. }
  456. unsigned mipsToSkip = mipsToSkip_[quality];
  457. if (mipsToSkip >= levels)
  458. mipsToSkip = levels - 1;
  459. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  460. --mipsToSkip;
  461. width /= (1 << mipsToSkip);
  462. height /= (1 << mipsToSkip);
  463. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  464. if (!face)
  465. {
  466. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  467. SetSize(width, format);
  468. }
  469. else
  470. {
  471. if (!object_)
  472. {
  473. LOGERROR("Cube texture face 0 must be loaded first");
  474. return false;
  475. }
  476. if (width != width_ || format != format_)
  477. {
  478. LOGERROR("Cube texture face does not match size or format of face 0");
  479. return false;
  480. }
  481. }
  482. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  483. {
  484. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  485. if (!needDecompress)
  486. {
  487. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  488. memoryUse += level.rows_ * level.rowSize_;
  489. }
  490. else
  491. {
  492. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  493. level.Decompress(rgbaData);
  494. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  495. memoryUse += level.width_ * level.height_ * 4;
  496. delete[] rgbaData;
  497. }
  498. }
  499. }
  500. faceMemoryUse_[face] = memoryUse;
  501. unsigned totalMemoryUse = sizeof(TextureCube);
  502. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  503. totalMemoryUse += faceMemoryUse_[i];
  504. SetMemoryUse(totalMemoryUse);
  505. return true;
  506. }
  507. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  508. {
  509. #ifndef GL_ES_VERSION_2_0
  510. if (!object_ || !graphics_)
  511. {
  512. LOGERROR("No texture created, can not get data");
  513. return false;
  514. }
  515. if (!dest)
  516. {
  517. LOGERROR("Null destination for getting data");
  518. return false;
  519. }
  520. if (level >= levels_)
  521. {
  522. LOGERROR("Illegal mip level for getting data");
  523. return false;
  524. }
  525. if (graphics_->IsDeviceLost())
  526. {
  527. LOGWARNING("Getting texture data while device is lost");
  528. return false;
  529. }
  530. graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
  531. if (!IsCompressed())
  532. glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, GetExternalFormat(format_), GetDataType(format_), dest);
  533. else
  534. glGetCompressedTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, dest);
  535. graphics_->SetTexture(0, 0);
  536. return true;
  537. #else
  538. LOGERROR("Getting texture data not supported");
  539. return false;
  540. #endif
  541. }
  542. bool TextureCube::Create()
  543. {
  544. Release();
  545. if (!graphics_ || !width_ || !height_)
  546. return false;
  547. if (graphics_->IsDeviceLost())
  548. {
  549. LOGWARNING("Texture creation while device is lost");
  550. return true;
  551. }
  552. glGenTextures(1, &object_);
  553. // Ensure that our texture is bound to OpenGL texture unit 0
  554. graphics_->SetTextureForUpdate(this);
  555. // If not compressed, create the initial level 0 texture with null data
  556. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  557. unsigned externalFormat = GetExternalFormat(format_);
  558. unsigned dataType = GetDataType(format_);
  559. bool success = true;
  560. if (!IsCompressed())
  561. {
  562. glGetError();
  563. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  564. {
  565. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, 0);
  566. if (glGetError())
  567. success = false;
  568. }
  569. }
  570. if (!success)
  571. LOGERROR("Failed to create texture");
  572. // Set mipmapping
  573. levels_ = requestedLevels_;
  574. if (!levels_)
  575. {
  576. unsigned maxSize = Max((int)width_, (int)height_);
  577. while (maxSize)
  578. {
  579. maxSize >>= 1;
  580. ++levels_;
  581. }
  582. }
  583. #ifndef GL_ES_VERSION_2_0
  584. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  585. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  586. #endif
  587. // Set initial parameters, then unbind the texture
  588. UpdateParameters();
  589. graphics_->SetTexture(0, 0);
  590. return success;
  591. }
  592. void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  593. {
  594. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  595. {
  596. if (renderSurfaces_[i] && renderSurfaces_[i]->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  597. renderSurfaces_[i]->QueueUpdate();
  598. }
  599. }
  600. }