查看完整版本: [-- QGLText 核心绘制函数(用于Qt3D中文字显示) --]

QTCN开发网 -> Qt代码秀 -> QGLText 核心绘制函数(用于Qt3D中文字显示) [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

xlttap 2012-10-10 14:37

QGLText 核心绘制函数(用于Qt3D中文字显示)

  1. /*-- 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();
    }



XChinux 2012-10-10 21:43
顶楼主

kimtaikee 2012-10-11 00:35
老大都顶了,那小的们都得膜拜了 哈哈

toby520 2012-10-17 11:46
强势围观,mark

shugangwang 2012-11-22 15:52
请问下载地址在哪,请给个链接,谢谢!

acuke 2014-07-08 08:27
也不说咋用,有屁用。

weinkym 2014-07-08 20:26
少了个效果图


查看完整版本: [-- QGLText 核心绘制函数(用于Qt3D中文字显示) --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled