xlttap |
2012-10-10 14:37 |
QGLText 核心绘制函数(用于Qt3D中文字显示)
- /*-- Author: Xiao Long Teng , shared on 2012-10-10 --*/
void Geotech::QGLText::draw(QGLPainter *painter) { if (_str.isEmpty()) return; QFontMetrics fm(_font); QRect rect = fm.boundingRect(_str); // text bounding box rect.adjust(0, 0, 1, 1); QImage image(rect.size(), QImage::Format_ARGB32); image.fill(0); // set to transparent // draw the text on an image QPainter p2d(&image); p2d.setFont(_font); p2d.setPen(QColor(_red * 255, _green * 255, _blue * 255, _alpha * 255)); p2d.drawText(0, 0, rect.width(), rect.height(), Qt::AlignCenter, _str); p2d.end(); // convert the object coordinate to screen coordinate GLdouble winx, winy, winz; // the screen coordinate of the object QMatrix4x4 model = painter->modelViewMatrix().top(); QMatrix4x4 proj = painter->projectionMatrix().top(); QGLUtils::objectToWindowCoord(_x, _y, _z, model.data(), proj.data(), &winx, &winy, &winz); if (_alignW) winx -= rect.width()/2.0; // align center of width if (_alignH) winy -= rect.height()/2.0; // align center of height // define the font rectangle which is // (x, y) (x + rect.width(), y) // ------------------------------------ // | | // | | // ------------------------------------ // (x, y + rect.heigth()) (x + rect.width(), y + rect.heigth()) int x = (int)winx, y = (int)winy; QVector2DArray vertices; vertices.append(x, y + rect.height()); vertices.append(x, y); vertices.append(x + rect.width(), y); vertices.append(x + rect.width(), y + rect.height()); // texture coordinates QVector2DArray texCoord; texCoord.append(0.0f, 0.0f); texCoord.append(0.0f, 1.0f); texCoord.append(1.0f, 1.0f); texCoord.append(1.0f, 0.0f); // map the image to texture QGLTexture2D texture; texture.setImage(image); // get viewport GLint view[4]; glGetIntegerv(GL_VIEWPORT, &view[0]); // use ortho projection to draw the text, because it // is easy to find the position of the text under ortho projection. // set projection matrix stack painter->modelViewMatrix().push(); painter->modelViewMatrix().setToIdentity(); QMatrix4x4 projm; // projm.ortho(QRect(view[0], view[1], view[2], view[3])); // Znear = -1.0, Zfar = 1.0 /** * Description of glOrtho(left, right, bottom, top, nearVal, farVal) : * (left, bottom, -nearVal) and (right, top, -nearVal) specify the points * on the near clipping plane that are mapped to the lower left and upper * right corners of the window. * * Note: * -nearVal : specifies the location of the near clipping plane * -farVal : specifies the location of the far clipping plane */ projm.ortho(view[0], view[2], view[3], view[1], 0, 1); painter->projectionMatrix().push(); painter->projectionMatrix() = projm; // move to the actual position from the screen origin painter->modelViewMatrix().translate(0, 0, -winz); // enable blend to make the background transaprecy of the text glEnable(GL_BLEND); // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); painter->clearAttributes(); painter->setStandardEffect(QGL::FlatReplaceTexture2D); texture.bind(); painter->setVertexAttribute(QGL::Position, vertices); painter->setVertexAttribute(QGL::TextureCoord0, texCoord); painter->draw(QGL::TriangleFan, 4); painter->setStandardEffect(QGL::FlatColor); glBindTexture(GL_TEXTURE_2D, 0); glDisable(GL_BLEND); // restore the matrix stack painter->projectionMatrix().pop(); painter->modelViewMatrix().pop(); }
|
|