Bonjour,

J'ai essayé SDL_ttf pour écrire du texte dans une fenêtre OpenGL (sous Windows).
J'ai utilisé la fonction ci-dessous et pour 6 écritures de moins de 10 caractères, ça me coûte à peut près 10 millisecondes sur un Athlon 1.8 GHz et une carte graphique passive qui supporte OpenGl (soit 5 fois plus que tout le reste de l'affichage (qui est bien plus important)).
Je trouve cela exhorbitant ! Surtout que j'essaye de faire une application temps réel à 75 images par seconde...

Mais effectivement, si on regarde la fonction ci-dessous, elle génère une texture à chaque fois !
A part SDL_ttf et FreeType, je n'ai pas vu d'autres librairie...
N'existe-t-il pas un librairie qui charge une texture une fois pour toutes à l'initialisation et qui s'en sert pour écrire le texte avec des GL_QUADS ?

Si ça n'existe pas, je crois que je vais me l'écrire... Ca ne doit pas être très long...




Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
void ma_calsse::SDL_GL_RenderText(const char *text,
                                                  TTF_Font *font,
                                                  SDL_Color color,
                                                  SDL_Rect & rectPos) {
  SDL_Surface *initial;
  SDL_Surface *intermediary;
  SDL_Rect rect;
  int w,h;
  GLuint texture;
 
  /* Use SDL_TTF to render our text */
  //initial = TTF_RenderText_Blended(font, text, color);
  initial = TTF_RenderText_Solid(font, text, color);
 
  /* Convert the rendered text to a known format */
  w = nextPowerOfTwo(initial->w);
  h = nextPowerOfTwo(initial->h);
 
  intermediary = SDL_CreateRGBSurface(0, w, h, 32,
      0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
 
  SDL_BlitSurface(initial, 0, intermediary, 0);
 
  /* Tell GL about our new texture */
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
                  GL_UNSIGNED_BYTE, intermediary->pixels );
 
 
  /* GL_NEAREST looks horrible, if scaled... */
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
  /* prepare to render our texture */
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, texture);
  //glColor3f(1.0f, 1.0f, 1.0f);
  //glColor3f(0.0f, 0.0f, 0.0f);// couleur de fond du texte
 
  /* Draw a quad at location */
  glBegin(GL_QUADS);
    /* Recall that the origin is in the lower-left corner
       That is why the TexCoords specify different corners
       than the Vertex coors seem to. */
    glTexCoord2f(0.0f, 1.0f);
      glVertex2f(rectPos.x    , rectPos.y);
    glTexCoord2f(1.0f, 1.0f);
      glVertex2f(rectPos.x + w, rectPos.y);
    glTexCoord2f(1.0f, 0.0f);
      glVertex2f(rectPos.x + w, rectPos.y + h);
    glTexCoord2f(0.0f, 0.0f);
      glVertex2f(rectPos.x    , rectPos.y + h);
  glEnd();
 
  /* Bad things happen if we delete the texture before it finishes */
  glFinish();
 
  /* return the deltas in the unused w,h part of the rect */
  rectPos.w = initial->w;
  rectPos.h = initial->h;
 
  // Nettoyage
  SDL_FreeSurface(initial);
  SDL_FreeSurface(intermediary);
  glDeleteTextures(1, &texture);
}// SDL_GL_RenderText