|
| 1 | +/* |
| 2 | +# glutBitmapCharacter |
| 3 | +
|
| 4 | +http://stackoverflow.com/questions/8847899/opengl-how-to-draw-text-using-only-opengl-methods |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#include <GL/gl.h> |
| 11 | +#include <GL/glu.h> |
| 12 | +#include <GL/glut.h> |
| 13 | + |
| 14 | +static void init() { |
| 15 | + glEnable(GL_DEPTH_TEST); |
| 16 | + glClearColor(0.0, 0.0, 0.0, 0.0); |
| 17 | + glShadeModel(GL_FLAT); |
| 18 | +} |
| 19 | + |
| 20 | +static void drawString (void *font, char *s, GLfloat x, GLfloat y, GLfloat z){ |
| 21 | + unsigned int i; |
| 22 | + /* Must come before glRasterPos. */ |
| 23 | + glColor3f(0.0f, 0.0f, 1.0f); |
| 24 | + glRasterPos3f(x, y, z); |
| 25 | + for (i = 0; i < strlen(s); i++) |
| 26 | + glutBitmapCharacter(font, s[i]); |
| 27 | +} |
| 28 | + |
| 29 | +static void display(void) { |
| 30 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 31 | + glLoadIdentity(); |
| 32 | + gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); |
| 33 | + glBegin(GL_TRIANGLES); |
| 34 | + glColor3f(1.0f, 0.0f, 0.0f); |
| 35 | + glVertex3f( 0.0f, 1.0f, 0.0f); |
| 36 | + glVertex3f(-1.0f, -1.0f, 0.0f); |
| 37 | + glVertex3f( 1.0f, -1.0f, 0.0f); |
| 38 | + glEnd(); |
| 39 | + |
| 40 | + /* This is drawn as close as possible to the camera. */ |
| 41 | + drawString(GLUT_BITMAP_HELVETICA_18, "bottom", -1.0, -1.0, 3.5); |
| 42 | + |
| 43 | + /* Depth is considered. Things that come behind are hidden. */ |
| 44 | + drawString(GLUT_BITMAP_HELVETICA_18, "behind", 0.0, 0.0, -1.0); |
| 45 | + drawString(GLUT_BITMAP_HELVETICA_18, "front", 0.0, 0.5, 1.0); |
| 46 | + |
| 47 | + /* The font size is fixed no matter how far back we are.*/ |
| 48 | + drawString(GLUT_BITMAP_HELVETICA_18, "far", 200.0, 200.0, -990.0); |
| 49 | + |
| 50 | + glFlush(); |
| 51 | +} |
| 52 | + |
| 53 | +static void reshape(int w, int h) { |
| 54 | + glViewport(0, 0, w, h); |
| 55 | + glMatrixMode(GL_PROJECTION); |
| 56 | + glLoadIdentity(); |
| 57 | + glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 1000.0); |
| 58 | + glMatrixMode(GL_MODELVIEW); |
| 59 | +} |
| 60 | + |
| 61 | +int main(int argc, char** argv) { |
| 62 | + glutInit(&argc, argv); |
| 63 | + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); |
| 64 | + glutInitWindowSize(500, 500); |
| 65 | + glutInitWindowPosition(100, 100); |
| 66 | + glutCreateWindow(argv[0]); |
| 67 | + init(); |
| 68 | + glutDisplayFunc(display); |
| 69 | + glutReshapeFunc(reshape); |
| 70 | + glutMainLoop(); |
| 71 | + return EXIT_SUCCESS; |
| 72 | +} |
0 commit comments