Commit 7126502d authored by Isaak Lim's avatar Isaak Lim
Browse files

- now mipmaps are generated for the font texture

- the font texture now has several rows and columns instead of just one row
- increased the default font-size in order to get a higher-resolution texture

refs #932

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@14763 383ad7c9-94d9-4d36-a494-682f7c89f535
parent a709b16f
...@@ -67,11 +67,12 @@ namespace SceneGraph { ...@@ -67,11 +67,12 @@ namespace SceneGraph {
//== IMPLEMENTATION ========================================================== //== IMPLEMENTATION ==========================================================
// static members // static members
QFont TextNode::qfont_ = QFont("Helvetica", 20); QFont TextNode::qfont_ = QFont("Helvetica", 30);
GLuint TextNode::texture_ = 0; GLuint TextNode::texture_ = 0;
int TextNode::imageWidth_ = 0; int TextNode::imageWidth_ = 0;
int TextNode::imageHeight_ = 0;
bool TextNode::initialised_ = false; bool TextNode::initialised_ = false;
std::map<char, unsigned int> TextNode::charToIndex_ = TextNode::createMap(); std::map< char, std::pair<unsigned int, unsigned int> > TextNode::charToIndex_ = TextNode::createMap();
QColor TextNode::color_ = QColor(255, 0, 0); QColor TextNode::color_ = QColor(255, 0, 0);
...@@ -202,13 +203,15 @@ setSize(unsigned int _size) { ...@@ -202,13 +203,15 @@ setSize(unsigned int _size) {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
std::map<char, unsigned int> std::map< char, std::pair<unsigned int, unsigned int> >
TextNode:: TextNode::
createMap() { createMap() {
std::map<char, unsigned int> m; std::map< char, std::pair<unsigned int, unsigned int> > m;
unsigned int i = 0; unsigned char c = ' ';
for (unsigned char c = ' '; c < '~'; ++c, ++i) { for (unsigned int i = 0; i < rows_; ++i) {
m[c] = i; for (unsigned int j = 0; j < columns_; ++j, ++c) {
m[c] = std::make_pair(j, i);
}
} }
return m; return m;
...@@ -365,9 +368,10 @@ updateFont() { ...@@ -365,9 +368,10 @@ updateFont() {
int width = metric.maxWidth(); int width = metric.maxWidth();
// ensure that the width of the texture is a power of 2 // ensure that the width of the texture is a power of 2
int widthPow2 = nearestPowerOfTwo(width); int widthPow2 = nearestPowerOfTwo(width);
imageWidth_ = widthPow2 * numberOfChars_; imageWidth_ = widthPow2 * columns_;
imageHeight_ = heightPow2 * rows_;
QImage finalImage(imageWidth_, heightPow2, QImage::Format_ARGB32); QImage finalImage(imageWidth_, imageHeight_, QImage::Format_ARGB32);
finalImage.fill(Qt::transparent); finalImage.fill(Qt::transparent);
QPainter painter; QPainter painter;
painter.begin(&finalImage); painter.begin(&finalImage);
...@@ -376,10 +380,10 @@ updateFont() { ...@@ -376,10 +380,10 @@ updateFont() {
painter.setFont(qfont_); painter.setFont(qfont_);
painter.setPen(color_); painter.setPen(color_);
// characters are drawn aligned to the left into the QImage finalImage next to each other // characters are drawn aligned to the left into the QImage finalImage
unsigned int i = 0; for (char c = ' '; c < '~'; ++c) {
for (char c = ' '; c < '~'; ++c, ++i) { std::pair<unsigned int, unsigned int> coords = charToIndex_[c];
painter.drawText(i*widthPow2, 0, widthPow2, heightPow2, Qt::AlignLeft | Qt::AlignBottom, QString(c)); painter.drawText(coords.first*widthPow2, imageHeight_ - (coords.second+1)*heightPow2, widthPow2, heightPow2, Qt::AlignLeft | Qt::AlignBottom, QString(c));
} }
painter.end(); painter.end();
...@@ -391,11 +395,12 @@ updateFont() { ...@@ -391,11 +395,12 @@ updateFont() {
glDeleteTextures(1, &texture_); glDeleteTextures(1, &texture_);
glGenTextures(1, &texture_); glGenTextures(1, &texture_);
ACG::GLState::bindTexture(GL_TEXTURE_2D, texture_); ACG::GLState::bindTexture(GL_TEXTURE_2D, texture_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, finalImage.width(), finalImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, finalImage.bits()); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, finalImage.width(), finalImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, finalImage.bits());
glGenerateMipmap(GL_TEXTURE_2D);
ACG::GLState::bindTexture(GL_TEXTURE_2D, 0); ACG::GLState::bindTexture(GL_TEXTURE_2D, 0);
initialised_ = true; initialised_ = true;
...@@ -421,6 +426,7 @@ updateVBO() { ...@@ -421,6 +426,7 @@ updateVBO() {
QFontMetrics metric(qfont_); QFontMetrics metric(qfont_);
int maxWidth = metric.maxWidth(); int maxWidth = metric.maxWidth();
int avgWidth = metric.averageCharWidth(); int avgWidth = metric.averageCharWidth();
int height = nearestPowerOfTwo(metric.height());
float lastCharRight = 0.0f; float lastCharRight = 0.0f;
for (unsigned int i = 0; i < text_.size(); ++i) { for (unsigned int i = 0; i < text_.size(); ++i) {
...@@ -453,10 +459,13 @@ updateVBO() { ...@@ -453,10 +459,13 @@ updateVBO() {
} }
float widthTx = (float) metricWidth / (float) imageWidth_; float widthTx = (float) metricWidth / (float) imageWidth_;
float heightTx = (float) height/ (float) imageHeight_;
// get the starting position of the character in the texture // get the starting position of the character in the texture
// note that the characters are drawn aligned to the bottom left in in the texture // note that the characters are drawn aligned to the bottom left in in the texture
float leftTx = ((float) charToIndex_[text_[i]] ) / (float) numberOfChars_; float leftTx = ((float) charToIndex_[text_[i]].first ) / (float) columns_;
float rightTx = leftTx + widthTx; float rightTx = leftTx + widthTx;
float bottomTx = charToIndex_[text_[i]].second / (float) rows_;
float topTx = bottomTx + heightTx;
// bottom left // bottom left
vertexBuffer_.push_back(left); vertexBuffer_.push_back(left);
...@@ -465,7 +474,7 @@ updateVBO() { ...@@ -465,7 +474,7 @@ updateVBO() {
// texture coordinates // texture coordinates
vertexBuffer_.push_back(leftTx); vertexBuffer_.push_back(leftTx);
vertexBuffer_.push_back(0.0f); vertexBuffer_.push_back(bottomTx);
// top left // top left
vertexBuffer_.push_back(left); vertexBuffer_.push_back(left);
...@@ -474,7 +483,7 @@ updateVBO() { ...@@ -474,7 +483,7 @@ updateVBO() {
// texture coordinates // texture coordinates
vertexBuffer_.push_back(leftTx); vertexBuffer_.push_back(leftTx);
vertexBuffer_.push_back(1.0f); vertexBuffer_.push_back(topTx);
// top right // top right
vertexBuffer_.push_back(right); vertexBuffer_.push_back(right);
...@@ -483,7 +492,7 @@ updateVBO() { ...@@ -483,7 +492,7 @@ updateVBO() {
// texture coordinates // texture coordinates
vertexBuffer_.push_back(rightTx); vertexBuffer_.push_back(rightTx);
vertexBuffer_.push_back(1.0f); vertexBuffer_.push_back(topTx);
// bottom right // bottom right
vertexBuffer_.push_back(right); vertexBuffer_.push_back(right);
...@@ -492,7 +501,7 @@ updateVBO() { ...@@ -492,7 +501,7 @@ updateVBO() {
// texture coordinates // texture coordinates
vertexBuffer_.push_back(rightTx); vertexBuffer_.push_back(rightTx);
vertexBuffer_.push_back(0.0f); vertexBuffer_.push_back(bottomTx);
} }
glDeleteBuffers(1, &vbo_); glDeleteBuffers(1, &vbo_);
......
...@@ -174,7 +174,7 @@ private: ...@@ -174,7 +174,7 @@ private:
* Creates a map #charToIndex_ from most characters to an incrementing set of indices. * Creates a map #charToIndex_ from most characters to an incrementing set of indices.
* These indices are used to create the texture coordinates in updateVBO(). * These indices are used to create the texture coordinates in updateVBO().
*/ */
static std::map<char, unsigned int> createMap(); static std::map< char, std::pair<unsigned int, unsigned int> > createMap();
private: private:
...@@ -218,7 +218,7 @@ private: ...@@ -218,7 +218,7 @@ private:
GLint blendDest_; GLint blendDest_;
/// maps most readable characters to indices for texture coordinate calculation in updateVBO() /// maps most readable characters to indices for texture coordinate calculation in updateVBO()
static std::map<char, unsigned int> charToIndex_; static std::map< char, std::pair<unsigned int, unsigned int> > charToIndex_;
/// font that is used to generate the texture in updateFont() /// font that is used to generate the texture in updateFont()
static QFont qfont_; static QFont qfont_;
...@@ -229,9 +229,18 @@ private: ...@@ -229,9 +229,18 @@ private:
/// width of the generated texture /// width of the generated texture
static int imageWidth_; static int imageWidth_;
/// height of the generated texture
static int imageHeight_;
/// number of characters that are drawn into the texture /// number of characters that are drawn into the texture
static const int numberOfChars_ = 94; static const int numberOfChars_ = 94;
/// number of rows of characters in the texture
static const unsigned int rows_ = 10;
/// number of columns of characters in the texture
static const unsigned int columns_ = 10;
/// this is used to ensure that the texture is only generated once when necessary /// this is used to ensure that the texture is only generated once when necessary
static bool initialised_; static bool initialised_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment