Esempio n. 1
0
//------------------------------------------------------------------------------
void CModels::createRect()
{
  rect.vertices.resize(NHudRect::VERTICES_COUNT);
  rect.vertices[0].position = QVector3D( 0.5,  0.5, 0.0);
  rect.vertices[1].position = QVector3D(-0.5,  0.5, 0.0);
  rect.vertices[2].position = QVector3D(-0.5, -0.5, 0.0);
  rect.vertices[3].position = QVector3D( 0.5, -0.5, 0.0);
  rect.vertices[0].texCoord = QVector2D(1.0, 0.0);
  rect.vertices[1].texCoord = QVector2D(0.0, 0.0);
  rect.vertices[2].texCoord = QVector2D(0.0, 1.0);
  rect.vertices[3].texCoord = QVector2D(1.0, 1.0);

  rect.faces.push_back(SFaces());
  SFaces *faces = &rect.faces.back();
  faces->faces.resize(NHudRect::FACE_BLOCK_SIZE);
  faces->faces[0] = SFace(1, 0, 2);
  faces->faces[1] = SFace(3, 2, 0);
  createVbo(&rect);
}
Esempio n. 2
0
bool Magic3D::TextData::update(Object* object)
{
    if (object)
    {

    }
    Renderer* renderer = Renderer::getInstance();
    Font* font = getFont();
    std::vector<float> lines;

    if (!font)
    {
        return false;
    }

    float fsize = (textSize / 512.0f) / font->getLineHeightInPixels();
    float lineHeight = fsize * (font->getLineHeightInPixels() + 2.0f);

    int quadCount = getVerticesCount() / 4;

    if (quadCount < (int)text.size() - lastReturns)
    {
        lastReturns = 0;
        size_t tsize = text.size();
        for (size_t i = 0; i < tsize; i++)
        {
            if (text.at(i) == '\n')
            {
                lastReturns++;
                continue;
            }
            else if (renderer->hasMapBuffer() || ((int)i >= quadCount))
            {
                addQuad(0.0f, 0.0f, 1.0f, 1.0f, eAXIS_Z, false);
            }
        }

        createVbo();
        quadCount = getVerticesCount() / 4;
    }

    width = 0.0f;
    height = lineHeight;

    if (getVerticesCount() > 0)
    {        
        int quad = -1;
        float startX = 0.0f;
        float startY = 0.0f;
        float* buffer = mapBuffer();

        int lastIndex = 0;
        size_t tsize = text.size();
        for (size_t i = 0; i < tsize; i++)
        {
            int index = text.at(i);

            if (index < 0)
            {
                index = 256 + index;
            }

            FontChar* fchar = font->getChar(index);            

            if (text.at(i) == '\n')
            {
                lines.push_back(startX);
                if (width < startX)
                {
                    width = startX;
                }
                startX = 0.0f;
                if (invert)
                {
                    startY -= lineHeight;
                }
                else
                {
                    startY += lineHeight;
                }
                height += lineHeight;
                continue;
            }
            else
            {
                quad++;
            }

            float texX = fchar->x;
            float texY = 1.0f - fchar->y;
            float texW = fchar->width;
            float texH = fchar->height;

            float cwidth = fchar->width * font->getTextureWidth() * fsize;
            float cheight = fchar->height * font->getTextureHeight() * fsize;

            float coffsetX = fchar->offsetX * font->getTextureWidth() * fsize;
            float coffsetY = fchar->offsetY * font->getTextureHeight() * fsize;
            float cadvanceX = fchar->advanceX * font->getTextureWidth() * fsize;

            float ckernel = 0.0f;
            std::map<int, float>::const_iterator k = fchar->kernel.find(lastIndex);
            if (k != fchar->kernel.end())
            {
                ckernel = (*k).second * fsize;
            }

            setQuad(buffer, quad, startX + coffsetX + ckernel, startY + coffsetY * 0.5f, cwidth, cheight);
            if (invert)
            {
                setQuadTexture(buffer, quad, texX, texY - texH, texW, -texH);
            }
            else
            {
                setQuadTexture(buffer, quad, texX, texY, texW, texH);
            }


            startX += cadvanceX;
            lastIndex = index;
        }
        quad++;
        for (int i = quad; i < quadCount; i++)
        {
            setQuad(buffer, i, startX, startY, 0.0f, 0.0f);
        }

        lines.push_back(startX);
        if (width < startX)
        {
            width = startX;
        }

        width += (2.0f * fsize);

        quad = 0;
        int line = 0;
        tsize = text.size();
        for (size_t i = 0; i < tsize; i++)
        {
            if (text.at(i) != '\n')
            {
                float w = width * -0.5f;
                float diff = line < (int)lines.size() ? lines.at(line) : width;
                switch (textAlignment)
                {
                    case eHORIZONTAL_ALIGN_CENTER: w += (width - diff) * 0.5f; break;
                    case eHORIZONTAL_ALIGN_RIGHT:  w += (width - diff); break;
                    default: break;
                }

                if (invert)
                {
                    moveQuad(buffer, quad, w, height * 0.5f - lineHeight, 0.0f);
                }
                else
                {
                    moveQuad(buffer, quad, w, height * -0.5f, 0.0f);
                }
                quad++;
            }
            else
            {
                line++;
            }
        }

        unmapBuffer();
    }

    changed = false;

    box.corners[0] = Vector3(-width * 0.5f, -height * 0.5f, 0.0f);
    box.corners[1] = Vector3(width * 0.5f, height * 0.5f, 0.0f);
    return true;
}