Skip to content

Commit

Permalink
Damn I was so sure I had found the problem.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ralph Gauges committed Oct 6, 2010
1 parent dd7ff38 commit d847085
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 43 deletions.
22 changes: 19 additions & 3 deletions copasi/layout/CLFontRendererBase.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Begin CVS Header
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layout/CLFontRendererBase.h,v $
// $Revision: 1.1 $
// $Revision: 1.1.2.1 $
// $Name: $
// $Author: gauges $
// $Date: 2010/03/10 12:26:12 $
// $Date: 2010/10/06 04:31:17 $
// End CVS Header

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

// opengl includes
#ifdef WIN32
# define WIN32_LEAN_AND_MEAN 1
# include <windows.h>
#endif // WIN32

#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <copasi/GL/glext.h>
#endif // __APPLE__


#include <copasi/layout/CLText.h>

struct CLTextTextureSpec;
Expand All @@ -34,7 +50,7 @@ class CLFontRendererBase
* The caller is responsible to free the memory of the TextureSpec object
* and of the pData in the TextureSpec.
*/
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;
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;

/**
* Returns the size for a font given a font, a text and a zoom factor.
Expand Down
90 changes: 75 additions & 15 deletions copasi/layout/CLLayoutRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Begin CVS Header
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layout/CLLayoutRenderer.cpp,v $
// $Revision: 1.5.2.4 $
// $Revision: 1.5.2.5 $
// $Name: $
// $Author: gauges $
// $Date: 2010/10/05 18:58:45 $
// $Date: 2010/10/06 04:31:25 $
// End CVS Header

// Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
Expand Down Expand Up @@ -1377,8 +1377,8 @@ void CLLayoutRenderer::draw_text(const CLTextTextureSpec* pTexture, double x, do
// we draw a rectangle in the current stroke color. At places where the texture is black, the underlying color should be seen.
// load the texture
// enable 2D texturing
glBindTexture(GL_TEXTURE_2D, pTexture->mTextureName);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, pTexture->mTextureName);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslated(xOffset, yOffset, zOffset);
Expand Down Expand Up @@ -3205,11 +3205,26 @@ void CLLayoutRenderer::update_textures_and_colors()
{
// add the texture although it might be NULL
//std::cout << "Creating new texture for text glyph: " << pTG->getId() << std::endl;
CLTextTextureSpec* pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);

//std::cout << "Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
pos->second[text] = pTexture;
this->mTextGlyphMap[pTG] = pTexture;
if (texture.first != NULL && texture.second != NULL)
{
glGenTextures(1, &texture.first->mTextureName);
assert(texture.first->mTextureName != 0);
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
delete[] texture.second;
}

pos->second[text] = texture.first;
this->mTextGlyphMap[pTG] = texture.first;
}
else
{
Expand Down Expand Up @@ -3237,13 +3252,28 @@ void CLLayoutRenderer::update_textures_and_colors()
newScale = this->mZoomFactor;
}

pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);

// check if the texture has a size that is supported
//std::cout << "Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
pos2->second = pTexture;
if (texture.first != NULL && texture.second != NULL)
{
glGenTextures(1, &texture.first->mTextureName);
assert(texture.first->mTextureName != 0);
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
delete[] texture.second;
}

pos2->second = texture.first;
//std::cout << "rescaled texture id: " << pTexture->mTextureName << std::endl;
this->mTextGlyphMap[pTG] = pTexture;
this->mTextGlyphMap[pTG] = texture.first;
}
else
{
Expand Down Expand Up @@ -3640,11 +3670,26 @@ void CLLayoutRenderer::update_textures_and_colors(const CLGroup* pGroup, double
if (pos2 == pos->second.end())
{
// add the texture although it might be NULL
CLTextTextureSpec* pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, this->mZoomFactor);

//std::cout << "No texture found. Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
pos->second[text] = pTexture;
this->mTextMap[pText] = pTexture;
if (texture.first != NULL && texture.second != NULL)
{
glGenTextures(1, &texture.first->mTextureName);
assert(texture.first->mTextureName != 0);
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
delete[] texture.second;
}

pos->second[text] = texture.first;
this->mTextMap[pText] = texture.first;
}
else
{
Expand Down Expand Up @@ -3673,11 +3718,26 @@ void CLLayoutRenderer::update_textures_and_colors(const CLGroup* pGroup, double
newScale = this->mZoomFactor;
}

pTexture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);
std::pair<CLTextTextureSpec*, GLubyte*> texture = (*this->mpFontRenderer)(fontSpec.mFamily, fontSpec.mSize, text, fontSpec.mWeight, fontSpec.mStyle, newScale);

//std::cout << "Created texture at " << pTexture << " for text \"" << text << "\"" << std::endl;
//std::cout << "texture id: " << pTexture->mTextureName << std::endl;
pos2->second = pTexture;
this->mTextMap[pText] = pTexture;
if (texture.first != NULL && texture.second != NULL)
{
glGenTextures(1, &texture.first->mTextureName);
assert(texture.first->mTextureName != 0);
glBindTexture(GL_TEXTURE_2D, texture.first->mTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.first->mTextureWidth, texture.first->mTextureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.second);
delete[] texture.second;
}

pos2->second = texture.first;
this->mTextMap[pText] = texture.first;
}
else
{
Expand Down
30 changes: 10 additions & 20 deletions copasi/layoutUI/CQFontRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Begin CVS Header
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layoutUI/CQFontRenderer.cpp,v $
// $Revision: 1.2.2.2 $
// $Revision: 1.2.2.3 $
// $Name: $
// $Author: gauges $
// $Date: 2010/10/03 15:31:45 $
// $Date: 2010/10/06 04:31:31 $
// End CVS Header

// Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
Expand Down Expand Up @@ -71,16 +71,15 @@ CQFontRenderer::~CQFontRenderer()
* The caller is responsible to free the memory of the TextureSpec object
* and of the pData in the TextureSpec.
*/
CLTextTextureSpec* CQFontRenderer::operator()(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight, CLText::FONT_STYLE style, double zoomFactor)
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)
{
CLFontSpec spec;
spec.mFamily = family;
spec.mSize = fontSize * zoomFactor;
spec.mWeight = weight;
spec.mStyle = style;
QFont font = this->getFont(spec);
CLTextTextureSpec* pSpec = getTexture(font, text, zoomFactor);
return pSpec;
return getTexture(font, text, zoomFactor);
}

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

GLubyte* textureData = NULL;

if (w != 0)
{
if (w != pSpec->mTextureWidth)
Expand All @@ -385,28 +386,17 @@ CLTextTextureSpec* CQFontRenderer::getTexture(QFont& font, const std::string& te
pSpec->mTextureHeight = height;
}

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

for (i = 0; i < iMax; ++i)
{
textureData[i] = image.bits()[4*i];
}

glGenTextures(1, &pSpec->mTextureName);
assert(pSpec->mTextureName != 0);
glBindTexture(GL_TEXTURE_2D, pSpec->mTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, format, GL_UNSIGNED_BYTE, textureData);
delete[] textureData;
}

return pSpec;
return std::pair<CLTextTextureSpec*, GLubyte*>(pSpec, textureData);
}

/**
Expand Down Expand Up @@ -471,7 +461,7 @@ void CQFontRenderer::getFamilyList(const std::string& family, std::list<std::str
}
}

CLTextTextureSpec* CQFontRenderer::createTexture(const std::string& family, double fontSize, const std::string& text, CLText::FONT_WEIGHT weight, CLText::FONT_STYLE style, double zoomFactor)
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)
{
return FONT_RENDERER(family, fontSize, text, weight, style, zoomFactor);
}
10 changes: 5 additions & 5 deletions copasi/layoutUI/CQFontRenderer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Begin CVS Header
// $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layoutUI/CQFontRenderer.h,v $
// $Revision: 1.1 $
// $Revision: 1.1.2.1 $
// $Name: $
// $Author: gauges $
// $Date: 2010/03/10 12:33:51 $
// $Date: 2010/10/06 04:31:36 $
// End CVS Header

// Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
Expand Down Expand Up @@ -44,14 +44,14 @@ class CQFontRenderer: public CLFontRendererBase
* The caller is responsible to free the memory of the TextureSpec object
* and of the pData in the TextureSpec.
*/
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);
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);

/**
* static method that creates a texture with a static FontRenderer
* object.
* This method can be used as a callback.
*/
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);
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);

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

/**
* Finds the font families that fit the given family name.
Expand Down

0 comments on commit d847085

Please sign in to comment.