I'm using a open source project for drawing text with OpenGL called fontstash. It can be found here on github: https://github.com/memononen/fontstash
That works fine for desktop but when I tried to build it for android and it failed. This since it is using glPixelStorei with the three paramter named GL_UNPACK_ROW_LENGTH, GL_UNPACK_SKIP_PIXELS, GL_UNPACK_SKIP_ROWS. These are not available in OpenGL ES 1.1 or 2.0 (I think they have been added in 3.0 though). But I found this stackoverflow answer stating that one can do this by yourself by using som pointer math: openGL SubTexturing
But I'm not getting it to work.
This is the original code:
static void glfons__renderUpdate(void* userPtr, int* rect, const unsigned char* data)
{
struct GLFONScontext* gl = (struct GLFONScontext*)userPtr;
int w = rect[2] - rect[0];
int h = rect[3] - rect[1];
if (gl->tex == 0) return;
glBindTexture(GL_TEXTURE_2D, gl->tex);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, gl->width);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect[0]);
glPixelStorei(GL_UNPACK_SKIP_ROWS, rect[1]);
glTexSubImage2D(GL_TEXTURE_2D, 0, rect[0], rect[1], w, h, GL_ALPHA,GL_UNSIGNED_BYTE, data);
}
And this is what I have come up with so far (not working):
static void glfons__renderUpdate(void* userPtr, int* rect, unsigned char* data)
{
struct GLFONScontext* gl = (struct GLFONScontext*)userPtr;
int w = rect[2] - rect[0];
int h = rect[3] - rect[1];
if (gl->tex == 0) return;
glBindTexture(GL_TEXTURE_2D, gl->tex);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexSubImage2D(GL_TEXTURE_2D, 0, rect[0], rect[1], w, h, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
int y = 0;
for(y; y < h; y++)
{
char *row = data + ((y + rect[1])*gl->width + rect[0]) * 4;
glTexSubImage2D(GL_TEXTURE_2D, 0, rect[0], rect[1] + y, w, 1, GL_ALPHA, GL_UNSIGNED_BYTE, row);
}
}