c++ - Visual Studio Express 2017 笔划文本功能的输出不显示

标签 c++ opengl glut

我一直在尝试在 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();
}

最佳答案

矩阵模式在 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();
}

关于c++ - Visual Studio Express 2017 笔划文本功能的输出不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71974453/

相关文章:

c++ - X 请求失败错误 : GLXBadFBConfig

c++ - 将变量放在标题中的什么位置?

c++ - 是否可以将整数指针转换为位于该内存位置的实际整数?

c++ - 指向第二个基础对象的指针

C++和多指针

c++ - 使用 openGL、C++ 绘制/渲染的替代方法

java - 加载纹理 LWJGL

python - PyOpenGL - 获取绘制图像的深度图

c++ - 在 Windows 上为 glut 3.7 构建静态库

c++ - GL 或 GLUT 有问题