• 9908阅读
  • 6回复

QGLText 核心绘制函数(用于Qt3D中文字显示) [复制链接]

上一主题 下一主题
离线xlttap
 

只看楼主 倒序阅读 楼主  发表于: 2012-10-10
  1. /*-- Author: Xiao Long Teng , shared on 2012-10-10 --*/
  2. void Geotech::QGLText::draw(QGLPainter *painter)
  3. {
  4.     if (_str.isEmpty()) return;
  5.     QFontMetrics fm(_font);
  6.     QRect rect = fm.boundingRect(_str);  // text bounding box
  7.     rect.adjust(0, 0, 1, 1);
  8.     QImage image(rect.size(), QImage::Format_ARGB32);
  9.     image.fill(0);  // set to transparent
  10.     // draw the text on an image
  11.     QPainter p2d(&image);
  12.     p2d.setFont(_font);
  13.     p2d.setPen(QColor(_red * 255, _green * 255, _blue * 255, _alpha * 255));
  14.     p2d.drawText(0, 0, rect.width(), rect.height(), Qt::AlignCenter, _str);
  15.     p2d.end();
  16.     // convert the object coordinate to screen coordinate
  17.     GLdouble winx, winy, winz; // the screen coordinate of the object
  18.     QMatrix4x4 model = painter->modelViewMatrix().top();
  19.     QMatrix4x4 proj = painter->projectionMatrix().top();
  20.     QGLUtils::objectToWindowCoord(_x, _y, _z, model.data(),
  21.                                   proj.data(), &winx, &winy, &winz);
  22.     if (_alignW) winx -= rect.width()/2.0;  // align center of width
  23.     if (_alignH) winy -= rect.height()/2.0;  // align center of height
  24.     // define the font rectangle which is
  25.     // (x, y)                        (x + rect.width(), y)
  26.     //    ------------------------------------
  27.     //    |                                  |
  28.     //    |                                  |
  29.     //    ------------------------------------
  30.     // (x, y + rect.heigth())        (x + rect.width(), y + rect.heigth())
  31.     int x = (int)winx, y = (int)winy;
  32.     QVector2DArray vertices;
  33.     vertices.append(x, y + rect.height());
  34.     vertices.append(x, y);
  35.     vertices.append(x + rect.width(), y);
  36.     vertices.append(x + rect.width(), y + rect.height());
  37.     // texture coordinates
  38.     QVector2DArray texCoord;
  39.     texCoord.append(0.0f, 0.0f);
  40.     texCoord.append(0.0f, 1.0f);
  41.     texCoord.append(1.0f, 1.0f);
  42.     texCoord.append(1.0f, 0.0f);
  43.     // map the image to texture
  44.     QGLTexture2D texture;
  45.     texture.setImage(image);
  46.     // get viewport
  47.     GLint view[4];
  48.     glGetIntegerv(GL_VIEWPORT, &view[0]);
  49.     // use ortho projection to draw the text, because it
  50.     // is easy to find the position of the text under ortho projection.
  51.     // set projection matrix stack
  52.     painter->modelViewMatrix().push();
  53.     painter->modelViewMatrix().setToIdentity();
  54.     QMatrix4x4 projm;
  55. //    projm.ortho(QRect(view[0], view[1], view[2], view[3])); // Znear = -1.0, Zfar = 1.0
  56.     /**
  57.      * Description of glOrtho(left, right, bottom, top, nearVal, farVal) :
  58.      * (left, bottom, -nearVal) and (right, top, -nearVal) specify the points
  59.      * on the near clipping plane that are mapped to the lower left and upper
  60.      * right corners of the window.
  61.      *
  62.      * Note:
  63.      * -nearVal : specifies the location of the near clipping plane
  64.      * -farVal  : specifies the location of the far clipping plane
  65.      */
  66.     projm.ortho(view[0], view[2], view[3], view[1], 0, 1);
  67.     painter->projectionMatrix().push();
  68.     painter->projectionMatrix() = projm;
  69.     // move to the actual position from the screen origin
  70.     painter->modelViewMatrix().translate(0, 0, -winz);
  71.     // enable blend to make the background transaprecy of the text
  72.     glEnable(GL_BLEND);
  73. //    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  74.     glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  75.     painter->clearAttributes();
  76.     painter->setStandardEffect(QGL::FlatReplaceTexture2D);
  77.     texture.bind();
  78.     painter->setVertexAttribute(QGL::Position, vertices);
  79.     painter->setVertexAttribute(QGL::TextureCoord0, texCoord);
  80.     painter->draw(QGL::TriangleFan, 4);
  81.     painter->setStandardEffect(QGL::FlatColor);
  82.     glBindTexture(GL_TEXTURE_2D, 0);
  83.     glDisable(GL_BLEND);
  84.     // restore the matrix stack
  85.     painter->projectionMatrix().pop();
  86.     painter->modelViewMatrix().pop();
  87. }


我简单我快乐
离线XChinux

只看该作者 1楼 发表于: 2012-10-10
顶楼主
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线kimtaikee

只看该作者 2楼 发表于: 2012-10-11
老大都顶了,那小的们都得膜拜了 哈哈

离线toby520

只看该作者 3楼 发表于: 2012-10-17
强势围观,mark
QtQML多多指教开发社区 http://qtclub.heilqt.com
将QtCoding进行到底
关注移动互联网,关注金融
开发跨平台客户端,服务于金融行业
专业定制界面
群号:312125701   373955953(qml控件定做)
离线shugangwang
只看该作者 4楼 发表于: 2012-11-22
请问下载地址在哪,请给个链接,谢谢!
离线acuke

只看该作者 5楼 发表于: 2014-07-08
也不说咋用,有屁用。
离线weinkym

只看该作者 6楼 发表于: 2014-07-08
少了个效果图
快速回复
限100 字节
 
上一个 下一个