c++ - Drawing bitmap fonts with OpenGL, what does glRasterPos2i() do? -


this 1 of "i have blank screen, please me fix it" moments.

this example opengl programming guide, version 2.1, page 311-312.

the example supposed draw 2 lines of text on screen.

past of problem think don't understand how glrasterpos2i() works. it: a:) set position of bitmaps drawn in 3d world in homogeneous / "opengl coordinates" b:) set position of bitmaps drawn on screen in pixel coordinates

here code have far: can pretty ignore first big lump defines bitmaps are.

#include <gl/glut.h> #include <cstdlib> #include <iostream> #include <cstring>   // first bit kind of irreverent, sets fonts in memory bitmaps glubyte space[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };  glubyte letters[][13] = {                         { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0xc3, 0x18 },                         { 0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },                         { 0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e },                         { 0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc },                         { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff },                         { 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff },                         { 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e },                         { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },                         { 0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e },                         { 0x00, 0x00, 0x7c, 0xee, 0xc6, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06 },                         { 0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xe0, 0xf0, 0xd8, 0xcc, 0xc6, 0xc3 },                         { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0 },                         { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xdb, 0xff, 0xff, 0xe7, 0xc3 },                         { 0x00, 0x00, 0xc7, 0xc7, 0xcf, 0xcf, 0xdf, 0xdb, 0xfb, 0xf3, 0xf3, 0xe3, 0xe3 },                         { 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x7e },                         { 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc0, 0xf3, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },                         { 0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c },                         { 0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },                         { 0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e },                         { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff },                         { 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },                         { 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },                         { 0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },                         { 0x00, 0x00, 0xc3, 0x66, 0x66, 0xc3, 0xc3, 0x18, 0xc3, 0xc3, 0x66, 0x66, 0xc3 },                         { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xc3, 0xc3, 0x66, 0x66, 0xc3 },                         { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff }                         };   // copying book gluint fontoffset;  void makerasterfont() {     gluint i, j;     glpixelstorei(gl_unpack_alignment, 1);     fontoffset = glgenlists(128);     for(i = 0, j = 'a'; < 26; ++, j ++)     {         glnewlist(fontoffset + ' ', gl_compile);             glbitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]);         glendlist();     }     glnewlist(fontoffset + ' ', gl_compile);         glbitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);     glendlist(); }   void init() {     glshademodel(gl_flat);     makerasterfont(); }   void printstring(char* s) {     glpushattrib(gl_list_bit);         gllistbase(fontoffset);         glcalllists(std::strlen(s), gl_unsigned_byte, (glubyte*)s);     glpopattrib(); }   void display() {     glfloat white[3] = {1.0, 1.0, 1.0 };      glclear(gl_color_buffer_bit | gl_depth_buffer_bit);      glcolor3fv(white);      // print text on screen @ (20,60) , (20,40)     glrasterpos2i(20, 60);     printstring("the quick brown fox jumps");     glrasterpos2i(20, 40);     printstring("over lazy dog");      glflush(); }   void reshape(int w, int h) {     // set viewport     glviewport(0, 0, (glsizei)w, (glsizei)h);      // set viewing mode     glmatrixmode(gl_projection);     glloadidentity();     gluperspective(45.0, (glfloat)w / (glfloat)h, 0.01, 100.0);     glmatrixmode(gl_modelview); }    int main(int argc, char** argv) {      /* init glut single buffer display mode,      * window size, position , title */     glutinit(&argc, argv);     glutinitdisplaymode(glut_single | glut_rgb | glut_depth);     glutinitwindowsize(500, 500);     glutinitwindowposition(100, 100);     glutcreatewindow(argv[0]);      // call init routine set opengl specific initialization values     init();      // set callback function     glutdisplayfunc(display);     glutreshapefunc(reshape);      // enter main loop     glutmainloop();      return exit_success; } 

sorry type of question - hate asking "please fix code", because should able fix myself. on occasion find myself, stuck, basically. time , help.

solution:

for interested, "get work", changes made were:

1: change gluperspective gluortho2d(0, width, 0, height).

2: change glnewlist(fontoffset + ' ', gl_compile) glnewlist(fontoffset + j, gl_compile) - not both, first 1 in loop.

3: set glrasterpos2i anywhere within region specified glortho2d. width , height both 500, used coordinates (20, 60) , (20, 40).

you have left gluperspective, , used coordinates (0,0) without specifying transformations. however, since bitmap 2d think less intuitive.

as rendering problem, hint, don't use j...

in loop:

glnewlist(fontoffset + ' ', gl_compile); 

replace space letter want.


Comments