Ensure EtcTexture always provides a valid textureId.
authorMichael Brasser <michael.brasser@live.com>
Fri, 15 Feb 2013 03:18:59 +0000 (21:18 -0600)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 15 Feb 2013 13:38:00 +0000 (14:38 +0100)
Change-Id: I1e7e8095593838f0fc1d78d1cb5a146787f748a7
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>

examples/quick/textureprovider/etcprovider.cpp
examples/quick/textureprovider/etcprovider.h

index 0bb072f..507965c 100644 (file)
@@ -86,7 +86,7 @@ unsigned short getPaddedHeight(ETCHeader *pHeader)
 }
 
 EtcTexture::EtcTexture()
-    : m_texture_id(0)
+    : m_texture_id(0), m_uploaded(false)
 {
 
 }
@@ -97,14 +97,22 @@ EtcTexture::~EtcTexture()
         glDeleteTextures(1, &m_texture_id);
 }
 
+int EtcTexture::textureId() const
+{
+    if (m_texture_id == 0)
+        glGenTextures(1, &const_cast<EtcTexture *>(this)->m_texture_id);
+    return m_texture_id;
+}
+
 void EtcTexture::bind()
 {
-    if (m_texture_id) {
+    if (m_uploaded && m_texture_id) {
         glBindTexture(GL_TEXTURE_2D, m_texture_id);
         return;
     }
 
-    glGenTextures(1, &m_texture_id);
+    if (m_texture_id == 0)
+        glGenTextures(1, &m_texture_id);
     glBindTexture(GL_TEXTURE_2D, m_texture_id);
 
 #ifdef ETC_DEBUG
@@ -129,6 +137,7 @@ void EtcTexture::bind()
         return;
     }
 
+    m_uploaded = true;
     updateBindOptions(true);
 }
 
index 08418f8..24570e9 100644 (file)
@@ -67,7 +67,7 @@ public:
     void bind();
 
     QSize textureSize() const { return m_size; }
-    int textureId() const { return m_texture_id; }
+    int textureId() const;
 
     bool hasAlphaChannel() const { return false; }
     bool hasMipmaps() const { return false; }
@@ -76,6 +76,7 @@ public:
     QSize m_size;
     QSize m_paddedSize;
     GLuint m_texture_id;
+    bool m_uploaded;
 };
 
 #endif // ETCPROVIDER_H