简体   繁体   English

使用 OpenGLES2 在 3D 场景上绘制 2D 文本?

[英]Drawing 2D text over 3D scene with OpenGLES2?

I'm trying to render 2D text over a 3D scene.我正在尝试在 3D 场景上渲染 2D 文本。 The 2D text is loaded using freetype from a TTF font and uses an orthographic projection to render and the scene uses a perspective projection using my camera. 2D 文本使用 TTF 字体中的 freetype 加载,并使用正交投影进行渲染,场景使用我的相机使用透视投影。 I have modified the code from this Learn OpenGL tutorial for text rendering .我已经修改了学习 OpenGL 教程中用于文本渲染的代码。 I can render the text by itself and the 3D scene separately however the 2D text does not appear when drawing them together.我可以单独渲染文本和 3D 场景,但是将它们绘制在一起时不会出现 2D 文本。

My render function:我的渲染功能:

void Engine::render()
{
    std::string fpsStr = std::to_string(fps).substr(0, std::to_string(fps).find(".") + 3);
    glViewport(0, 0, surface_width, surface_height);
    glClearColor(0.53f, 0.8f, 0.92f, 1.0f);
    glEnable(GL_DEPTH_TEST);
    glFrontFace(GL_CCW);
    glCullFace(GL_BACK);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // 3D scene gets rendered here
    scene->render(display, surface, deltaTime);
    //
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // Text gets rendered here
    debugText.renderText(fpsStr,25.0f, 25.0f, 1.0f, glm::vec3(0.0, 0.0, 0.0));
    //
    glDisable(GL_BLEND);
    eglSwapBuffers(display, surface);
}

The text projection is a member variable ( glm::mat4 ) that is initialized during creation of the text rendering class like so:文本投影是一个成员变量 ( glm::mat4 ),它在创建文本渲染类的过程中被初始化,如下所示:

...
projection = glm::ortho(0.0f, static_cast<float>(screenWidth), 0.0f, static_cast<float>(screenHeight));
...

My render text function:我的渲染文本功能:

void Font::renderText(std::string text, float x, float y, float scale, glm::vec3 colour)
{
    // activate corresponding render state
    textShader.use();
    textShader.setMat4("projection", projection);
    textShader.setVec3("textColor", colour);
    glActiveTexture(GL_TEXTURE0);

    // iterate through all characters
    std::string::const_iterator c;
    for (c = text.begin(); c != text.end(); c++)
    {
        Character ch = characters[*c];

        float xpos = x + ch.bearing.x * scale;
        float ypos = y - (ch.size.y - ch.bearing.y) * scale;

        float w = ch.size.x * scale;
        float h = ch.size.y * scale;
        // update VBO for each character
        float vertices[6][4] = {
                { xpos,     ypos + h,   0.0f, 0.0f },
                { xpos,     ypos,       0.0f, 1.0f },
                { xpos + w, ypos,       1.0f, 1.0f },

                { xpos,     ypos + h,   0.0f, 0.0f },
                { xpos + w, ypos,       1.0f, 1.0f },
                { xpos + w, ypos + h,   1.0f, 0.0f }
        };
        // render glyph texture over quad
        glBindTexture(GL_TEXTURE_2D, ch.textureID);
        // update content of VBO memory
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        // render quad
        glDrawArrays(GL_TRIANGLES, 0, 6);
        // now advance cursors for next glyph (note that advance is number of 1/64 pixels)
        x += (ch.advance >> 6) * scale; // bitshift by 6 to get value in pixels (2^6 = 64)
    }
    glBindTexture(GL_TEXTURE_2D, 0);
}

Here are two images, in this one I'm only rendering the text and in this one I've enabled both the 3D scene and the text, however only the 3D scene is displayed .这是两张图片,在这张图片中,我只渲染了文本,在这张图片中,我启用了 3D 场景和文本,但是只显示了 3D 场景

How can I overlay this 2D perspective over the 3D scene so they both get rendered?我怎样才能在 3D 场景上覆盖这个 2D 透视图,以便它们都得到渲染?

You have said that rendering them (the 2D quad and 3D scene) separately works fine but rendering them together works causes the 2D quad not to render.您已经说过单独渲染它们(2D 四边形和 3D 场景)可以正常工作,但是将它们一起渲染会导致 2D 四边形无法渲染。 Hmmm, try checking your rendering order of the objects;嗯,尝试检查对象的渲染顺序; make sure you are binding and unbinding your shaders correctly.确保正确绑定和解除绑定着色器。 Is there a particular reason you have disabled depth testing for the text (try enabling it and see if that fixes the problem) ?您是否有特殊原因禁用了文本的深度测试(尝试启用它,看看是否可以解决问题)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM