From 56f4bc467db1da5dc011c730f357966e9a2fc0ec Mon Sep 17 00:00:00 2001 From: omigamedev Date: Thu, 15 Aug 2019 19:39:10 +0200 Subject: [PATCH] use glReadPixels for compatibility with OpenGL ES --- src/texture.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/texture.cpp b/src/texture.cpp index 928ad15..1b66569 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -115,7 +115,29 @@ Image Texture2D::get_image() const noexcept App::I->render_task([&] { bind(); - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, ret.m_data.get()); + + GLuint fboID; + glGenFramebuffers(1, &fboID); + if (fboID == 0) + return; + + GLint oldFboID; + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldFboID); + + glBindFramebuffer(GL_FRAMEBUFFER, fboID); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_tex, 0); + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status == GL_FRAMEBUFFER_COMPLETE) + { + glReadPixels(0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, ret.m_data.get()); + } + else + { + LOG("Texture2D::get_image() failed because framebuffer status = %d", (int)status); + } + + glBindFramebuffer(GL_FRAMEBUFFER, oldFboID); + glDeleteFramebuffers(1, &fboID); }); return ret; }