0

I'm trying to render a string to screen and I can't get it to work

I use freeglut and visual studio 2013. This is my glutinit:

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA); // Double buffering, RGB format
glutInitWindowSize(width, heigth);
glutInitWindowPosition(0, 0);
glutCreateWindow("Project Demeter");
glClearColor(1.0, 1.0, 1.0, 1.0);

glColor3f(1.0, 1.0, 1.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, heigth, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_BLEND);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

and my text drawing:

void text::draw()
{
    glColor3ub(_color[0], _color[1], _color[2]);
    glRasterPos2f(_x, _y);
    glDisable(GL_TEXTURE);
    glDisable(GL_TEXTURE_2D);
    for (char& c : _str)
    {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c);
    }
}

however nothing is drawn on the screen when i create a text as such:

float clr[3] = { 0, 0, 0 };
text* txt = new text(&DrawList, "test1234", clr, 600, 600);

does anyone know what im doing wrong and how i can fix this?

EDIT: by request the constructor:

text::text(std::list<item*>* BaseList, const string str, const float color[3], int x, int y)
:item(BaseList)
{   
    if (str == "")
    {
        assert(0);
    }

    _str = str;

    _x = x;
    _y = y;

    if (!color) {      // When a null pointer is passed to this function
        _color[0] = 0;// black is used
        _color[1] = 0;
        _color[2] = 0;

    }
    else { //else copy it over
        copy_array(color, _color);
    }
}

EDIT2: so i tested it on a windows 8 machine and it worked there? wtf? im using w7 normally. this is very strange.

WillieWonka
  • 115
  • 1
  • 11
  • 1
    Post your text ctor code. – Anonymous Dec 30 '14 at 22:26
  • Playing Captain Obvious here, but are you calling `glutSwapBuffers()` at the end of drawing the frame? Is the background color showing up? – Reto Koradi Dec 31 '14 at 02:49
  • @RetoKoradi yes i use it but is not shown here and yes everything else is showing up (lines and pixels) – WillieWonka Dec 31 '14 at 09:18
  • @Anonymous i edited the OP to include it – WillieWonka Dec 31 '14 at 09:20
  • Ok, let's dig a bit deeper. Why do you use glColor3ub on float? It may be minor thing as your color is black. Now regarding w8/w7 difference it sounds like uninitialized variables arę somewhere used. They can have different (random values). Did you debug step by step rendering so you can be sure that your text::draw is at least executed? – Anonymous Dec 31 '14 at 14:06

1 Answers1

0

Use a c++ string and iterate through the string to draw each letter instead. If this hasn't helped then try disabling the blend or the lighting before rendering the text. Did you manage to display text using the glutBitmapCharacter() function before putting it in your 'text' class? If so you probably have a problem with how the data is held in the text class. Again, I recommend using a string object (not char*) within the text class and looping through the string using a string::iterator.


I'm not by my computer at the moment but I will post some code later on as I am working on a similar project and have text etc up and running.

I noticed you are using glortho2d and you use 'raster2d' but you haven't disabled depth testing before drawing the text. I use 'raster3f' in a glortho environment and it allows for layering text and objects etc and there's no need to disable depth testing etc.

Also, in your text constructor you pass in a pointer to a "std::list*" but aren't STL containers passed by reference automagically?

I will be more helpful later when I'm at my computer later on

user2796283
  • 273
  • 4
  • 8
  • maybe i should have specified this but im already doing a c++ string method. that for loop is a c++11 way to iterate through a c++ string – WillieWonka Dec 31 '14 at 09:17