Opengl fix: bitmaps having a width not multiple of 4 were incorrectly drawn.

This fix draws a "sub bitmap" having a width multiple of 4.
(The loss of 1 to 3 pixels in horizontal size should be not really noticeable)

(cherry picked from commit 3a02e54411)
This commit is contained in:
jean-pierre charras 2018-07-30 16:18:53 +02:00 committed by Seth Hillbrand
parent a43ec3e3f0
commit 99f25b0895
1 changed files with 19 additions and 12 deletions

View File

@ -65,7 +65,7 @@ bool OPENGL_GAL::isBitmapFontLoaded = false;
SHADER* OPENGL_GAL::shader = NULL; SHADER* OPENGL_GAL::shader = NULL;
namespace KIGFX { namespace KIGFX {
class GL_BITMAP_CACHE class GL_BITMAP_CACHE
{ {
public: public:
GL_BITMAP_CACHE() GL_BITMAP_CACHE()
@ -73,11 +73,11 @@ public:
} }
~GL_BITMAP_CACHE(); ~GL_BITMAP_CACHE();
GLuint RequestBitmap( const BITMAP_BASE* aBitmap ); GLuint RequestBitmap( const BITMAP_BASE* aBitmap );
private: private:
struct CACHED_BITMAP struct CACHED_BITMAP
{ {
GLuint id; GLuint id;
@ -85,8 +85,8 @@ private:
}; };
GLuint cacheBitmap( const BITMAP_BASE* aBitmap ); GLuint cacheBitmap( const BITMAP_BASE* aBitmap );
std::map<const BITMAP_BASE*, CACHED_BITMAP> m_bitmaps; std::map<const BITMAP_BASE*, CACHED_BITMAP> m_bitmaps;
}; };
}; };
@ -121,21 +121,28 @@ GLuint GL_BITMAP_CACHE::cacheBitmap( const BITMAP_BASE* aBitmap )
bmp.w = aBitmap->GetSizePixels().x; bmp.w = aBitmap->GetSizePixels().x;
bmp.h = aBitmap->GetSizePixels().y; bmp.h = aBitmap->GetSizePixels().y;
// There are draw issues (incorrect rendering) with some w values.
// It happens when the w value is not a multiple of 4
// so we use only a sub image with a modified width
bmp.w -= bmp.w % 4;
GLuint textureID; GLuint textureID;
glGenTextures(1, &textureID); glGenTextures(1, &textureID);
uint8_t *buf = new uint8_t [ bmp.w * bmp.h * 3]; uint8_t *buf = new uint8_t [ bmp.w * bmp.h * 3];
auto imgData = const_cast<BITMAP_BASE*>( aBitmap )->GetImageData(); auto imgData = const_cast<BITMAP_BASE*>( aBitmap )->GetImageData();
for( int y=0; y < bmp.h; y++ ) for( int y = 0; y < bmp.h; y++ )
for( int x = 0; x < bmp.w;x++) {
for( int x = 0; x < bmp.w; x++ )
{ {
auto *p = buf + ( bmp.w * y + x ) * 3; uint8_t *p = buf + ( bmp.w * y + x ) * 3;
p[0] = imgData->GetRed( x, y ); p[0] = imgData->GetRed( x, y );
p[1] = imgData->GetGreen( x, y ); p[1] = imgData->GetGreen( x, y );
p[2] = imgData->GetBlue( x, y ); p[2] = imgData->GetBlue( x, y );
} }
}
glBindTexture( GL_TEXTURE_2D, textureID ); glBindTexture( GL_TEXTURE_2D, textureID );
@ -998,7 +1005,7 @@ void OPENGL_GAL::DrawBitmap( const BITMAP_BASE& aBitmap )
double w = aBitmap.GetSizePixels().x * pix_size_iu; double w = aBitmap.GetSizePixels().x * pix_size_iu;
double h = aBitmap.GetSizePixels().y * pix_size_iu; double h = aBitmap.GetSizePixels().y * pix_size_iu;
auto xform = currentManager->GetTransformation(); auto xform = currentManager->GetTransformation();
glm::vec4 v0 = xform * glm::vec4( -w/2, -h/2, 0.0, 0.0 ); glm::vec4 v0 = xform * glm::vec4( -w/2, -h/2, 0.0, 0.0 );
@ -1016,7 +1023,7 @@ void OPENGL_GAL::DrawBitmap( const BITMAP_BASE& aBitmap )
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glActiveTexture( GL_TEXTURE0 ); glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, id ); glBindTexture( GL_TEXTURE_2D, id );
glBegin( GL_QUADS ); glBegin( GL_QUADS );
glColor4f(1.0, 1.0, 1.0, 1.0); glColor4f(1.0, 1.0, 1.0, 1.0);
glTexCoord2f(0.0, 0.0); glTexCoord2f(0.0, 0.0);
@ -1034,7 +1041,7 @@ void OPENGL_GAL::DrawBitmap( const BITMAP_BASE& aBitmap )
SetTarget( oldTarget ); SetTarget( oldTarget );
glBindTexture( GL_TEXTURE_2D, 0 ); glBindTexture( GL_TEXTURE_2D, 0 );
glPopMatrix(); glPopMatrix();
} }
@ -1457,7 +1464,7 @@ void OPENGL_GAL::DeleteGroup( int aGroupNumber )
void OPENGL_GAL::ClearCache() void OPENGL_GAL::ClearCache()
{ {
bitmapCache.reset( new GL_BITMAP_CACHE ); bitmapCache.reset( new GL_BITMAP_CACHE );
groups.clear(); groups.clear();
if( isInitialized ) if( isInitialized )