使用 GLC 库在 GLFW 中呈现字体的问题

Issues in rendering the font in GLFW using GLC library

我正在使用 glc 库在 glfw window 的上下文中呈现字体。但是,我无法为字体设置坐标。它总是在 (0,0) 位置(即左下角)渲染字体。

下面是使用 GLC 库呈现示例字符串的代码,

int main(){
    GLint ctx, myFont;

    if (!glfwInit())
        exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    GLFWwindow* window = glfwCreateWindow(500, 500, "font rendering", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    glfwSwapInterval(1);
    if ( GLEW_OK != glewInit( ) )
        {
        std::cout << "Failed to initialize GLEW" << std::endl;

        }


 ctx = glcGenContext();
 glcContext(ctx);
 myFont = glcGenFontID();
 glcNewFontFromFamily(myFont, "Palatino");
 glcFontFace(myFont, "Normal");
 glcFont(myFont);

 glViewport(0, 0, 500, 500);
 glMatrixMode(GL_PROJECTION); // make the projection matrix the current matrix
 glLoadIdentity();            // init the projection matrix by the identity matrix
 glOrtho(0.0, 500.0, 500.0, 0.0, -1.0, 1.0); // top-left (0, 0); bottom-right (500, 500)
  glcScale(100,100);
  glcRenderString("Sample Text");
 glMatrixMode(GL_MODELVIEW);  // make the modelview matrix the current matrix
 glLoadIdentity();            // init the modelview matrix by the identity matrix


glfwSwapBuffers(window);

while(!glfwWindowShouldClose(window)){

}

在这里,glTranslatef(100,100, 0); 不起作用,如果我使用 glRasterPos3f(x,y,z);,则不会在屏幕上呈现任何内容。

我是不是漏掉了什么?

投影矩阵描述了对象如何投影到视口上。如果没有投影矩阵,则必须在标准化设备 space 中设置对象,其中所有坐标都在 [-1.0, 1.0].

范围内

由于您想在二维中绘制,我建议设置正交投影,将视图space 1:1 映射到 window 坐标。
使用glOrtho for this and use glMatrixMode在投影矩阵栈和模型视图矩阵栈之间切换:

glViewport(0, 0, 500, 500);

glMatrixMode(GL_PROJECTION); // make the projection matrix the current matrix
glLoadIdentity();            // init the projection matrix by the identity matrix
glOrtho(0.0, 500.0, 500.0, 0.0, -1.0, 1.0); // top-left (0, 0); bottom-right (500, 500)

glMatrixMode(GL_MODELVIEW);  // make the modelview matrix the current matrix
glLoadIdentity();            // init the modelview matrix by the identity matrix

glcScale(100,100);

.....

glcRenderString renders a string by using glBitmap respectively glDrawPixels. Becaus of that the positon has to be set by glRasterPos.

如果要将字符串呈现为红色,则必须跳过红色和绿色分量。这可以通过 glPixelTransfer.

来完成

例如

glPixelTransferf(GL_RED_SCALE, 1.0f);
glPixelTransferf(GL_GREE_SCALE, 0.0f); 
glPixelTransferf(GL_BLUE_SCALE, 0.0f);