Skip to content

Commit d847085

Browse files
author
Ralph Gauges
committed
Damn I was so sure I had found the problem.
I thought maybe the fact that the texture was first bound outside of the actual context of the QGLWidget could be the reason why it was sometimes invalid, but moving the code into the widget didn't fix the problem either. So now the code is where it belongs and I still have to do some cleanup, but the problem is still there.
1 parent dd7ff38 commit d847085

File tree

4 files changed

+109
-43
lines changed

4 files changed

+109
-43
lines changed

copasi/layout/CLFontRendererBase.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Begin CVS Header
22
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layout/CLFontRendererBase.h,v $
3-
// $Revision: 1.1 $
3+
// $Revision: 1.1.2.1 $
44
// $Name: $
55
// $Author: gauges $
6-
// $Date: 2010/03/10 12:26:12 $
6+
// $Date: 2010/10/06 04:31:17 $
77
// End CVS Header
88

99
// Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
@@ -17,6 +17,22 @@
1717
#include <string>
1818
#include <utility>
1919

20+
// opengl includes
21+
#ifdef WIN32
22+
# define WIN32_LEAN_AND_MEAN 1
23+
# include <windows.h>
24+
#endif // WIN32
25+
26+
#ifdef __APPLE__
27+
#include <OpenGL/gl.h>
28+
#include <OpenGL/glu.h>
29+
#else
30+
#include <GL/gl.h>
31+
#include <GL/glu.h>
32+
#include <copasi/GL/glext.h>
33+
#endif // __APPLE__
34+
35+
2036
#include <copasi/layout/CLText.h>
2137

2238
struct CLTextTextureSpec;
@@ -34,7 +50,7 @@ class CLFontRendererBase
3450
* The caller is responsible to free the memory of the TextureSpec object
3551
* and of the pData in the TextureSpec.
3652
*/
37-
virtual CLTextTextureSpec* operator()(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight = CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style = CLText::STYLE_NORMAL, double zoomFactor = 1.0) = 0;
53+
virtual std::pair<CLTextTextureSpec*, GLubyte*> operator()(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight = CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style = CLText::STYLE_NORMAL, double zoomFactor = 1.0) = 0;
3854

3955
/**
4056
* Returns the size for a font given a font, a text and a zoom factor.

copasi/layout/CLLayoutRenderer.cpp

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Begin CVS Header
22
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layout/CLLayoutRenderer.cpp,v $
3-
// $Revision: 1.5.2.4 $
3+
// $Revision: 1.5.2.5 $
44
// $Name: $
55
// $Author: gauges $
6-
// $Date: 2010/10/05 18:58:45 $
6+
// $Date: 2010/10/06 04:31:25 $
77
// End CVS Header
88

99
// Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
@@ -1377,8 +1377,8 @@ void CLLayoutRenderer::draw_text(const CLTextTextureSpec* pTexture, double x, do
13771377
// we draw a rectangle in the current stroke color. At places where the texture is black, the underlying color should be seen.
13781378
// load the texture
13791379
// enable 2D texturing
1380-
glBindTexture(GL_TEXTURE_2D, pTexture->mTextureName);
13811380
glEnable(GL_TEXTURE_2D);
1381+
glBindTexture(GL_TEXTURE_2D, pTexture->mTextureName);
13821382
glMatrixMode(GL_MODELVIEW);
13831383
glPushMatrix();
13841384
glTranslated(xOffset, yOffset, zOffset);
@@ -3205,11 +3205,26 @@ void CLLayoutRenderer::update_textures_and_colors()
32053205
{
32063206
// add the texture although it might be NULL
32073207
//std::cout << "Creating new texture for text glyph: " << pTG->getId() << std::endl;
3208-
CLTextTextureSpec* pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);
3208+
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);
3209+
32093210
//std::cout << "Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
32103211
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
3211-
pos->second[text] = pTexture;
3212-
this->mTextGlyphMap[pTG] = pTexture;
3212+
if (texture.first != NULL && texture.second != NULL)
3213+
{
3214+
glGenTextures(1, &texture.first->mTextureName);
3215+
assert(texture.first->mTextureName != 0);
3216+
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
3217+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3218+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3219+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3220+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3221+
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3222+
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
3223+
delete[] texture.second;
3224+
}
3225+
3226+
pos->second[text] = texture.first;
3227+
this->mTextGlyphMap[pTG] = texture.first;
32133228
}
32143229
else
32153230
{
@@ -3237,13 +3252,28 @@ void CLLayoutRenderer::update_textures_and_colors()
32373252
newScale = this->mZoomFactor;
32383253
}
32393254

3240-
pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);
3255+
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);
3256+
32413257
// check if the texture has a size that is supported
32423258
//std::cout << "Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
32433259
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
3244-
pos2->second = pTexture;
3260+
if (texture.first != NULL && texture.second != NULL)
3261+
{
3262+
glGenTextures(1, &texture.first->mTextureName);
3263+
assert(texture.first->mTextureName != 0);
3264+
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
3265+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3266+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3267+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3268+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3269+
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3270+
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
3271+
delete[] texture.second;
3272+
}
3273+
3274+
pos2->second = texture.first;
32453275
//std::cout << "rescaled texture id: " << pTexture->mTextureName << std::endl;
3246-
this->mTextGlyphMap[pTG] = pTexture;
3276+
this->mTextGlyphMap[pTG] = texture.first;
32473277
}
32483278
else
32493279
{
@@ -3640,11 +3670,26 @@ void CLLayoutRenderer::update_textures_and_colors(const CLGroup* pGroup, double
36403670
if (pos2 == pos->second.end())
36413671
{
36423672
// add the texture although it might be NULL
3643-
CLTextTextureSpec* pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);
3673+
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);
3674+
36443675
//std::cout << "No texture found. Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
36453676
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
3646-
pos->second[text] = pTexture;
3647-
this->mTextMap[pText] = pTexture;
3677+
if (texture.first != NULL && texture.second != NULL)
3678+
{
3679+
glGenTextures(1, &texture.first->mTextureName);
3680+
assert(texture.first->mTextureName != 0);
3681+
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
3682+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3683+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3684+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3685+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3686+
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3687+
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
3688+
delete[] texture.second;
3689+
}
3690+
3691+
pos->second[text] = texture.first;
3692+
this->mTextMap[pText] = texture.first;
36483693
}
36493694
else
36503695
{
@@ -3673,11 +3718,26 @@ void CLLayoutRenderer::update_textures_and_colors(const CLGroup* pGroup, double
36733718
newScale = this->mZoomFactor;
36743719
}
36753720

3676-
pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);
3721+
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);
3722+
36773723
//std::cout << "Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
36783724
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
3679-
pos2->second = pTexture;
3680-
this->mTextMap[pText] = pTexture;
3725+
if (texture.first != NULL && texture.second != NULL)
3726+
{
3727+
glGenTextures(1, &texture.first->mTextureName);
3728+
assert(texture.first->mTextureName != 0);
3729+
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
3730+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3731+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3732+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3733+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3734+
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3735+
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
3736+
delete[] texture.second;
3737+
}
3738+
3739+
pos2->second = texture.first;
3740+
this->mTextMap[pText] = texture.first;
36813741
}
36823742
else
36833743
{

copasi/layoutUI/CQFontRenderer.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Begin CVS Header
22
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layoutUI/CQFontRenderer.cpp,v $
3-
// $Revision: 1.2.2.2 $
3+
// $Revision: 1.2.2.3 $
44
// $Name: $
55
// $Author: gauges $
6-
// $Date: 2010/10/03 15:31:45 $
6+
// $Date: 2010/10/06 04:31:31 $
77
// End CVS Header
88

99
// Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
@@ -71,16 +71,15 @@ CQFontRenderer::~CQFontRenderer()
7171
* The caller is responsible to free the memory of the TextureSpec object
7272
* and of the pData in the TextureSpec.
7373
*/
74-
CLTextTextureSpec* CQFontRenderer::operator()(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight, CLText::FONT_STYLE style, double zoomFactor)
74+
std::pair<CLTextTextureSpec*, GLubyte*> CQFontRenderer::operator()(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight, CLText::FONT_STYLE style, double zoomFactor)
7575
{
7676
CLFontSpec spec;
7777
spec.mFamily = family;
7878
spec.mSize = fontSize * zoomFactor;
7979
spec.mWeight = weight;
8080
spec.mStyle = style;
8181
QFont font = this->getFont(spec);
82-
CLTextTextureSpec* pSpec = getTexture(font, text, zoomFactor);
83-
return pSpec;
82+
return getTexture(font, text, zoomFactor);
8483
}
8584

8685
/**
@@ -323,7 +322,7 @@ std::pair<double, double> CQFontRenderer::getTextureSize(const CLFontSpec& spec,
323322
* The caller has to free the memory for the TextureSpec object and the
324323
* pData in the TextureSpec object.
325324
*/
326-
CLTextTextureSpec* CQFontRenderer::getTexture(QFont& font, const std::string& text, double zoomFactor)
325+
std::pair<CLTextTextureSpec*, GLubyte*> CQFontRenderer::getTexture(QFont& font, const std::string& text, double zoomFactor)
327326
{
328327
CLTextTextureSpec* pSpec = NULL;
329328
// find out what size the text will have
@@ -377,6 +376,8 @@ CLTextTextureSpec* CQFontRenderer::getTexture(QFont& font, const std::string& te
377376
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
378377
}
379378

379+
GLubyte* textureData = NULL;
380+
380381
if (w != 0)
381382
{
382383
if (w != pSpec->mTextureWidth)
@@ -385,28 +386,17 @@ CLTextTextureSpec* CQFontRenderer::getTexture(QFont& font, const std::string& te
385386
pSpec->mTextureHeight = height;
386387
}
387388

388-
GLubyte* textureData = new GLubyte[width*height];
389+
textureData = new GLubyte[width*height];
389390
pSpec->mAscent = (double)fontMetrics.ascent();
390391
unsigned int i, iMax = width * height;
391392

392393
for (i = 0; i < iMax; ++i)
393394
{
394395
textureData[i] = image.bits()[4*i];
395396
}
396-
397-
glGenTextures(1, &pSpec->mTextureName);
398-
assert(pSpec->mTextureName != 0);
399-
glBindTexture(GL_TEXTURE_2D, pSpec->mTextureName);
400-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
401-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
402-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
403-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
404-
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
405-
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, format, GL_UNSIGNED_BYTE, textureData);
406-
delete[] textureData;
407397
}
408398

409-
return pSpec;
399+
return std::pair<CLTextTextureSpec*, GLubyte*>(pSpec, textureData);
410400
}
411401

412402
/**
@@ -471,7 +461,7 @@ void CQFontRenderer::getFamilyList(const std::string& family, std::list<std::str
471461
}
472462
}
473463

474-
CLTextTextureSpec* CQFontRenderer::createTexture(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight, CLText::FONT_STYLE style, double zoomFactor)
464+
std::pair<CLTextTextureSpec*, GLubyte*> CQFontRenderer::createTexture(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight, CLText::FONT_STYLE style, double zoomFactor)
475465
{
476466
return FONT_RENDERER(family, fontSize, text, weight, style, zoomFactor);
477467
}

copasi/layoutUI/CQFontRenderer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Begin CVS Header
22
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layoutUI/CQFontRenderer.h,v $
3-
// $Revision: 1.1 $
3+
// $Revision: 1.1.2.1 $
44
// $Name: $
55
// $Author: gauges $
6-
// $Date: 2010/03/10 12:33:51 $
6+
// $Date: 2010/10/06 04:31:36 $
77
// End CVS Header
88

99
// Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
@@ -44,14 +44,14 @@ class CQFontRenderer: public CLFontRendererBase
4444
* The caller is responsible to free the memory of the TextureSpec object
4545
* and of the pData in the TextureSpec.
4646
*/
47-
virtual CLTextTextureSpec* operator()(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight = CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style = CLText::STYLE_NORMAL, double zoomFactor = 1.0);
47+
virtual std::pair<CLTextTextureSpec*, GLubyte*> operator()(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight = CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style = CLText::STYLE_NORMAL, double zoomFactor = 1.0);
4848

4949
/**
5050
* static method that creates a texture with a static FontRenderer
5151
* object.
5252
* This method can be used as a callback.
5353
*/
54-
static CLTextTextureSpec* createTexture(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight = CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style = CLText::STYLE_NORMAL, double zoomFactor = 1.0);
54+
static std::pair<CLTextTextureSpec*, GLubyte*> createTexture(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight = CLText::WEIGHT_NORMAL, CLText::FONT_STYLE style = CLText::STYLE_NORMAL, double zoomFactor = 1.0);
5555

5656
/**
5757
* Returns the size for a font given a font, a text and a zoom factor.
@@ -74,7 +74,7 @@ class CQFontRenderer: public CLFontRendererBase
7474
* The caller has to free the memory for the TextureSpec object and the
7575
* pData in the TextureSpec object.
7676
*/
77-
CLTextTextureSpec* getTexture(QFont& font, const std::string& text, double zoomFactor);
77+
std::pair<CLTextTextureSpec*, GLubyte*> getTexture(QFont& font, const std::string& text, double zoomFactor);
7878

7979
/**
8080
* Finds the font families that fit the given family name.

0 commit comments

Comments
 (0)