Textures and fonts

Started by
1 comment, last by eSCHEn 19 years, 2 months ago
I was learning to display fonts in my program, but when I tried it, it didn't work that easily. The problem is that when I load the textures, fonts don't appear on the screen, and when I dont load the textures, fonts work well. At first, I load some textures to memory at the beginning of the program, like this:

  data = (unsigned char*) malloc(128*128*3*sizeof(unsigned char));
  if(!data) return 0;

  glGenTextures(1, &tekstuuri);
  glBindTexture(GL_TEXTURE_2D, tekstuuri);
  
  // Load .raw texture
  tiedosto = fopen("tekstuuri.raw", "rb");
  if(!tiedosto) return 0;
  fread(data, 3, 128*128, tiedosto);
  fclose(tiedosto);
  glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

As you can see, I use .raw textures. And the code/function that makes the problem is glTexImage2D(....); I found that out by myself. The functions that deal with the fonts: (About the same code, as Nehe's tutorial 13)

void CreateFontti(void)
{
    HFONT font;
    HFONT oldfont;
    
    base = glGenLists(96);
    
    font = CreateFont(-24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, 
                      ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, 
                      ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH, "Courier New");
    
    oldfont = (HFONT)SelectObject(hdc, font);
    wglUseFontBitmaps(hdc, 32, 96, base);
    SelectObject(hdc, oldfont);
    DeleteObject(font);
}

void glPrint(char *text)
{     
    glPushAttrib(GL_LIST_BIT);
        glListBase(base - 32);
        glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
    glPopAttrib();
}

void KillFont(void)
{
    glDeleteLists(base, 96);
}    

The question: How to make this working, with .raw textures?
Tiritomba
Advertisement
Ok, I managed to find out what was the problem. I called glEnable(GL_TEXTURE_2D);
when i had created the texture. And if I want to display some text, like my way, I need to disable texturing. So I think the problem is fixed if I call glEnable(GL_TEXTURE_2D); when i want to texture an object and after that i disable it, so i can display some text. Am I right, or totally wrong?
Tiritomba
Yes you are right. OpenGL is a state machine and therefore if you set something it will stay in that state until you set it to something different. Calling glEnable(GL_TEXTURE_2D) before you want texturing and glDisable(GL_TEXTURE_2D) before you want no texturing is the right thing to do.

Your fonts were probably rendering fine when you had texturing on but they just had the default texture co-ordinates and subsequently ended up the same colour as your background. You can texture map your fonts, as seen in Lesson 15.

Incidently a .raw file is not necessarily a texture, in fact it could be anything: it's just raw data, in this case the raw pixel data in RGB triplets. A .raw file could be a list of birthdays or distances to stars - it really doesn't matter :)

When you get further on with texturing you may want to have a look at some image file formats that allow for compression, this will obviously save you disk space. A nice basic one to start with is a Targa (.tga) file. I find the best place for file format information is Wotsit.

Hope some of this helped and happy coding! :)
--
Cheers,
Darren Clark

This topic is closed to new replies.

Advertisement