您能帮我在ftgles渲染函数中找到错误的点吗?

时间:2018-08-12 09:00:41

标签: android opengl-es android-ndk rendering ftgl

我正在尝试在Android上使用ftgles。

我成功在Android上加载了ftgles库。

但是我无法渲染文本。

我认为ftgles的render函数有问题。

template <typename T>inline FTPoint FTBufferFontImpl::RenderI(const T* string, const int len,
                                     FTPoint position, FTPoint spacing,
                                     int renderMode)
{
const float padding = 3.0f;
int width, height, texWidth, texHeight;
int cacheIndex = -1;
bool inCache = false;

// Protect blending functions, GL_BLEND and GL_TEXTURE_2D
// glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);

// Protect glPixelStorei() calls
// glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);

glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE

// Search whether the string is already in a texture we uploaded
for(int n = 0; n < BUFFER_CACHE_SIZE; n++)
{
    int i = (lastString + n + BUFFER_CACHE_SIZE) % BUFFER_CACHE_SIZE;

    if(stringCache[i] && !StringCompare(stringCache[i], string, len))
    {
        cacheIndex = i;
        inCache = true;
        break;
    }
}

// If the string was not found, we need to put it in the cache and compute
// its new bounding box.
if(!inCache)
{
    // FIXME: this cache is not very efficient. We should first expire
    // strings that are not used very often.
    cacheIndex = lastString;
    lastString = (lastString + 1) % BUFFER_CACHE_SIZE;

    if(stringCache[cacheIndex])
    {
        free(stringCache[cacheIndex]);
    }
    // FIXME: only the first N bytes are copied; we want the first N chars.
    stringCache[cacheIndex] = StringCopy(string, len);
    bboxCache[cacheIndex] = BBox(string, len, FTPoint(), spacing);
}

FTBBox bbox = bboxCache[cacheIndex];

width = static_cast<int>(bbox.Upper().X() - bbox.Lower().X()
                          + padding + padding + 0.5);
height = static_cast<int>(bbox.Upper().Y() - bbox.Lower().Y()
                           + padding + padding + 0.5);

texWidth = NextPowerOf2(width);
texHeight = NextPowerOf2(height);

glBindTexture(GL_TEXTURE_2D, idCache[cacheIndex]);

// If the string was not found, we need to render the text in a new
// texture buffer, then upload it to the OpenGL layer.
if(!inCache)
{
    buffer->Size(texWidth, texHeight);
    buffer->Pos(FTPoint(padding, padding) - bbox.Lower());

    advanceCache[cacheIndex] =
          FTFontImpl::Render(string, len, FTPoint(), spacing, renderMode);

    glBindTexture(GL_TEXTURE_2D, idCache[cacheIndex]);

    //glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
    //glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    /* TODO: use glTexSubImage2D later? */
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texWidth, texHeight, 0,
                 GL_ALPHA, GL_UNSIGNED_BYTE, (GLvoid *)buffer->Pixels());

    buffer->Size(0, 0);
}

FTPoint low = position + bbox.Lower();
FTPoint up = position + bbox.Upper();

ftglBegin(GL_QUADS);
    glNormal3f(0.0f, 0.0f, 1.0f);
    ftglTexCoord2f(padding / texWidth,
                 (texHeight - height + padding) / texHeight);
    ftglVertex2f(low.Xf(), up.Yf());
    ftglTexCoord2f(padding / texWidth,
                 (texHeight - padding) / texHeight);
    ftglVertex2f(low.Xf(), low.Yf());
    ftglTexCoord2f((width - padding) / texWidth,
                 (texHeight - padding) / texHeight);
    ftglVertex2f(up.Xf(), low.Yf());
    ftglTexCoord2f((width - padding) / texWidth,
                 (texHeight - height + padding) / texHeight);
    ftglVertex2f(up.Xf(), up.Yf());
ftglEnd();

//glPopClientAttrib();
// glPopAttrib();

return position + advanceCache[cacheIndex];
}

这是渲染功能。

您能帮我发现问题吗?

还有一个问题。

Android OpenGLES和IOS OpenGLES之间有区别吗?

我使用iOS的库来构建它。

这可能是个问题吗?

0 个答案:

没有答案