glutBitmapCharacter()文本没有出现在屏幕上?

时间:2019-01-07 17:15:58

标签: c++ opengl glut

#include "stdafx.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "glut.h"

int winWidth = 700;
int winHeight = 600;

void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);   
    glLoadIdentity();
    gluOrtho2D(0.0, winWidth, 0.0, winHeight);
}

void renderSpacedBitmapString(float x, float y, void *font, char *string) {
    char *c;
    int x1 = x;
    for (c = string; *c != '\0'; c++) {
        glRasterPos2f(x1, y);
        glutBitmapCharacter(font, *c);
        x1 = x1 + glutBitmapWidth(font, *c);
    }
}

void draw_text() {
    glColor3f(255.0, 0, 0.0); /* red color */
    char buf[100] = { 0 };
    sprintf_s(buf, "DO YOU WANT TO GO TO SPACE?");
    renderSpacedBitmapString(15, 600, GLUT_BITMAP_HELVETICA_18, buf);
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    draw_text();
    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(winWidth, winHeight);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Drawn Text");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0; 
}

我有一个问题,我无法使文本显示在屏幕上,但是,当我将gluOrtho2D代码更改为gluOrtho2D(-200.0, 800, -200.0, 800);时,它开始起作用,但是我想保留{{ 1}}的代码与上面相同,并且我不想更改它。

1 个答案:

答案 0 :(得分:2)

y = 600仅比您的gluOrtho2D()剪辑音量高一点。将字符串放下一点(将580传递到y的{​​{1}}参数中)或增加窗口高度(620作品)。