diff --git a/gfx/gl.c b/gfx/gl.c index eb3ab282a7..3d6bd32ca9 100644 --- a/gfx/gl.c +++ b/gfx/gl.c @@ -1485,11 +1485,53 @@ static bool gl_read_viewport(void *data, uint8_t *buffer) glGetIntegerv(GL_VIEWPORT, vp); glPixelStorei(GL_PACK_ALIGNMENT, get_alignment(vp[2])); + +#ifdef HAVE_OPENGLES + glReadPixels(vp[0], vp[1], + vp[2], vp[3], + GL_RGB, GL_UNSIGNED_BYTE, buffer); + + unsigned numbytes = vp[2] * vp[3]; + unsigned count = numbytes / 4; + unsigned remainder = numbytes % 4; + uint32_t *pixels = (uint32_t *)buffer; + // convert RGB to BGR + for (unsigned i = 0; i <= count; pixels += 3, i++) + { + uint8_t p[12]; + memcpy(p, pixels, 12); + pixels[0] = p[2] | p[1] << 8 | p[0] << 16 | p[5] << 24; + pixels[1] = p[4] | p[3] << 8 | p[8] << 16 | p[7] << 24; + pixels[2] = p[6] | p[11] << 8 | p[10] << 16 | p[9] << 24; + } + + uint8_t *rem = (uint8_t *)pixels; + uint8_t tmp; + switch (remainder) + { + case 3: + tmp = rem[8]; + rem[6] = rem[8]; + rem[8] = tmp; + case 2: + tmp = rem[5]; + rem[3] = rem[5]; + rem[5] = tmp; + case 1: + tmp = rem[2]; + rem[2] = rem[0]; + rem[0] = tmp; + break; + default: + break; + } +#else glPixelStorei(GL_PACK_ROW_LENGTH, vp[2]); glReadPixels(vp[0], vp[1], vp[2], vp[3], GL_BGR, GL_UNSIGNED_BYTE, buffer); +#endif return true; } diff --git a/gfx/gl_common.h b/gfx/gl_common.h index 165bafac86..cb90dcec3c 100644 --- a/gfx/gl_common.h +++ b/gfx/gl_common.h @@ -274,7 +274,7 @@ extern PFNGLACTIVETEXTUREPROC pglActiveTexture; #endif // Platform specific workarounds/hacks. -#if defined(__CELLOS_LV2__) || defined(HAVE_OPENGLES) +#if defined(__CELLOS_LV2__) #define NO_GL_READ_VIEWPORT #endif