Browse Source

Use glReadPixels() workaround to get texture data from rendertargets on GLES. For regular textures this cannot be used.

Lasse Öörni 9 years ago
parent
commit
6538f3bb4a

+ 11 - 1
Source/Urho3D/Graphics/OpenGL/OGLTexture2D.cpp

@@ -402,13 +402,13 @@ bool Texture2D::SetData(SharedPtr<Image> image, bool useAlpha)
 
 
 bool Texture2D::GetData(unsigned level, void* dest) const
 bool Texture2D::GetData(unsigned level, void* dest) const
 {
 {
-#ifndef GL_ES_VERSION_2_0
     if (!object_ || !graphics_)
     if (!object_ || !graphics_)
     {
     {
         URHO3D_LOGERROR("No texture created, can not get data");
         URHO3D_LOGERROR("No texture created, can not get data");
         return false;
         return false;
     }
     }
 
 
+#ifndef GL_ES_VERSION_2_0
     if (!dest)
     if (!dest)
     {
     {
         URHO3D_LOGERROR("Null destination for getting data");
         URHO3D_LOGERROR("Null destination for getting data");
@@ -437,6 +437,16 @@ bool Texture2D::GetData(unsigned level, void* dest) const
     graphics_->SetTexture(0, 0);
     graphics_->SetTexture(0, 0);
     return true;
     return true;
 #else
 #else
+    // Special case on GLES: if the texture is a rendertarget, can make it current and use glReadPixels()
+    if (usage_ == TEXTURE_RENDERTARGET)
+    {
+        graphics_->SetRenderTarget(0, const_cast<Texture2D*>(this));
+        // Ensure the FBO is current; this viewport is actually never rendered to
+        graphics_->SetViewport(IntRect(0, 0, width_, height_));
+        glReadPixels(0, 0, width_, height_, GetExternalFormat(format_), GetDataType(format_), dest);
+        return true;
+    }
+
     URHO3D_LOGERROR("Getting texture data not supported");
     URHO3D_LOGERROR("Getting texture data not supported");
     return false;
     return false;
 #endif
 #endif

+ 11 - 1
Source/Urho3D/Graphics/OpenGL/OGLTextureCube.cpp

@@ -624,13 +624,13 @@ bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlph
 
 
 bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
 bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
 {
 {
-#ifndef GL_ES_VERSION_2_0
     if (!object_ || !graphics_)
     if (!object_ || !graphics_)
     {
     {
         URHO3D_LOGERROR("No texture created, can not get data");
         URHO3D_LOGERROR("No texture created, can not get data");
         return false;
         return false;
     }
     }
 
 
+#ifndef GL_ES_VERSION_2_0
     if (!dest)
     if (!dest)
     {
     {
         URHO3D_LOGERROR("Null destination for getting data");
         URHO3D_LOGERROR("Null destination for getting data");
@@ -659,6 +659,16 @@ bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
     graphics_->SetTexture(0, 0);
     graphics_->SetTexture(0, 0);
     return true;
     return true;
 #else
 #else
+    // Special case on GLES: if the texture is a rendertarget, can make it current and use glReadPixels()
+    if (usage_ == TEXTURE_RENDERTARGET)
+    {
+        graphics_->SetRenderTarget(0, renderSurfaces_[face]);
+        // Ensure the FBO is current; this viewport is actually never rendered to
+        graphics_->SetViewport(IntRect(0, 0, width_, height_));
+        glReadPixels(0, 0, width_, height_, GetExternalFormat(format_), GetDataType(format_), dest);
+        return true;
+    }
+
     URHO3D_LOGERROR("Getting texture data not supported");
     URHO3D_LOGERROR("Getting texture data not supported");
     return false;
     return false;
 #endif
 #endif