From 3bbc453237d8c303daeb5b255756f29edd01010d Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Wed, 10 Apr 2019 17:02:04 +0200 Subject: Save and restore GL parameters as appropriate According to the docs "glPixelStorei sets pixel storage modes that affect the operation of subsequent glReadPixels as well as the unpacking of texture patterns (see glTexImage2D and glTexSubImage2D)." Hence we (a) must be aware that current values of these modes can be anything set by user or FLTK code before (b) should be polite and restore these modes to previous values See discussion in fltk.coredev, thread "gl_draw problem" started on Apr 05, 2019. --- src/gl_draw.cxx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/gl_draw.cxx b/src/gl_draw.cxx index ed35d8d62..e45c870f3 100644 --- a/src/gl_draw.cxx +++ b/src/gl_draw.cxx @@ -217,9 +217,12 @@ void gl_rect(int x, int y, int w, int h) { void gl_draw_image(const uchar* b, int x, int y, int w, int h, int d, int ld) { if (!ld) ld = w*d; + GLint row_length; + glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length); // get current row length glPixelStorei(GL_UNPACK_ROW_LENGTH, ld/d); glRasterPos2i(x,y); glDrawPixels(w,h,d<4?GL_RGB:GL_RGBA,GL_UNSIGNED_BYTE,(const ulong*)b); + glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length); // restore row length } @@ -404,18 +407,24 @@ int gl_texture_fifo::compute_texture(const char* str, int n) int w, h; w = fl_width(fifo[current].utf8, n) * gl_scale; // Hack - make w be aligned - w = (w + 3) & 0xFFFFFFC; + w = (w + 3) & (~3); h = fl_height() * gl_scale; - + fifo[current].scale = gl_scale; fifo[current].fdesc = gl_fontsize; char *txt_buf = Fl_Gl_Window_Driver::global()->alpha_mask_for_string(str, n, w, h); - + + // save GL parameters GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT + GLint row_length, alignment; + glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length); + glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); + // put the bitmap in an alpha-component-only texture glPushAttrib(GL_TEXTURE_BIT); glBindTexture (GL_TEXTURE_RECTANGLE_ARB, fifo[current].texName); glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - //glPixelStorei(GL_UNPACK_ROW_LENGTH, w); + glPixelStorei(GL_UNPACK_ROW_LENGTH, w); + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // GL_ALPHA8 is defined in GL/gl.h of X11 and of MinGW32 and of MinGW64 and of OpenGL.framework for MacOS glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_ALPHA8, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, txt_buf); /* For the record: texture construction if an alpha-only-texture is not possible @@ -424,6 +433,9 @@ int gl_texture_fifo::compute_texture(const char* str, int n) */ delete[] txt_buf; // free the buffer now we have copied it into the Gl texture glPopAttrib(); + // restore saved GL parameters + glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length); + glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); return current; } -- cgit v1.2.3