glVertex3f缩放问题

时间:2015-12-13 07:23:05

标签: c++ opengl

OpenGL新手,请放轻松。我的任务是使用GLFont在我的学校项目中渲染文本,我在下面粘贴了渲染方法。我已使用glOrtho将原点设置到窗口的左上角,但这会导致GLFont渲染字符上下颠倒。我需要做的是在y缩放-1我相信,但我无法弄清楚如何更改下面的渲染器来做到这一点。有什么想法吗?

void GLFont::RenderText (const char* String, float x, float y, float z, float size)
{
    //Return if we don't have a valid glFont
    if (!ok)
    {
        throw GLFontError::InvalidFont();
    }

    //Get length of string
    int Length = strlen(String);

    //Begin rendering quads
    glBegin(GL_TRIANGLE_STRIP);

    //Loop through characters
    for (int i = 0; i < Length; i++)
    {
        //Get pointer to glFont character
        GLFONTCHAR *Char = &Font.Char[(int)String[i] - Font.IntStart];

        float dx = Char->dx*size;
        float dy = Char->dy*size;

        //Specify vertices and texture coordinates
        glTexCoord2f(Char->tx1, Char->ty2);
        glVertex3f(x, y - dy, z);
        glTexCoord2f(Char->tx1, Char->ty1);
        glVertex3f(x, y, z);
        glTexCoord2f(Char->tx2, Char->ty2);
        glVertex3f(x + dx, y - dy, z);
        glTexCoord2f(Char->tx2, Char->ty1);
        glVertex3f(x + dx, y, z);

        //Move to next character
        x += dx;
    }

    //Stop rendering quads
    glEnd();
}

2 个答案:

答案 0 :(得分:1)

我建议反转&#char;&gt; ty1&#39;而不是缩放。和&#39; char-&gt; ty2&#39;在你的代码中:

//Specify vertices and texture coordinates
glTexCoord2f(Char->tx1, Char->ty1);
glVertex3f(x, y - dy, z);
glTexCoord2f(Char->tx1, Char->ty2);
glVertex3f(x, y, z);
glTexCoord2f(Char->tx2, Char->ty1);
glVertex3f(x + dx, y - dy, z);
glTexCoord2f(Char->tx2, Char->ty2);
glVertex3f(x + dx, y, z);

它将导致通过y轴的反转纹理映射。

希望它有所帮助!

答案 1 :(得分:1)

只需翻转投影,即将您的值交换为top的{​​{1}}和bottom参数。如果你已经启用了剔除,你还必须将剔除绕组设置为相反的方向。