Search code examples
c++openglglut

glutBitmapCharacter() text not appearing on screen?


#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; 
}

I have this problem where I can't make the text appear on the screen, however when I change the gluOrtho2D code to gluOrtho2D(-200.0, 800, -200.0, 800); it starts to work, but I want to keep the gluOrtho2D code the same as above, and I don't want to change it.


Solution

  • y = 600 is just a bit above your gluOrtho2D() clip volume. Drop the string down a bit (passing 580 into the y argument of renderSpacedBitmapString() works) or increase the window height (620 works).