opengl Visual Studio Express 2017输出未显示笔画文字函式

hivapdat  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(105)

我一直在尝试在Visual Studio Express 2017中运行这个程序。使用opengl。我在一个pdf文件中找到了渲染代码和笔画代码,并尝试了一下,但首先它显示了许多错误,一旦处理好我编译了程序。虽然运行没有任何错误,但输出屏幕仍然是空白的。


# include "stdafx.h"

# include <windows.h>

# include <gl/GL.h>

# include <glut.h>

# include <gl/GLU.h>

void myInit(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glColor3f(0.0f, 0.0f, 0.0f);
    glMatrixMode(GL_PROJECTION);
    glLineWidth(6.0);
    glLoadIdentity();
    gluOrtho2D(0.0, 700, 0.0, 700);
}

void drawStrokeText(const char *string, int x, int y, int z)
{
    const char *c;
    glPushMatrix();
    glTranslatef(x, y + 8, z);
    glScalef(0.09f, -0.08f, z);
    for (c = string; *c != '\0'; c++)
    {
        glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
    }
    glPopMatrix();
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glColor3ub(255, 50, 255);
    drawStrokeText("Hello", 300, 400, 0);
    glutSwapBuffers();
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    render();
    glFlush();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(700, 700);
    glutInitWindowPosition(100, 150);
    glutCreateWindow("My First Program");
    glutDisplayFunc(myDisplay);
    myInit();
    glutMainLoop();
}
2w3rbyxf

2w3rbyxf1#

myInit中,矩阵模式被切换到GL_PROJECTION,但再也没有切换回来。因此,render中的glLoadIdentity()指令将覆盖投影矩阵。您必须在glLoadIdentity()之前将矩阵模式切换到GL_MODELVIEW

void render()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);     // <--
    glLoadIdentity();
    glColor3ub(255, 50, 255);
    drawStrokeText("Hello", 300, 400, 0);
    glutSwapBuffers();
}

相关问题