OpenGL 1.1 + SDL2 + SDL_TTF文本渲染透明度

时间:2017-03-29 16:13:47

标签: c++ opengl sdl sdl-2 sdl-ttf

我正在尝试使用opengl + sdl2在c ++中编写简单的gui,但现在我在文本渲染方面遇到了问题。

这是我在旧笔记本电脑上使用opengl 1.1(注意白色矩形)的原因:

screenshot_opengl1.1

这在我的电脑上:

screenshot_working

这是我的文字渲染功能:

void drawText(std::string text,TTF_Font * font,GLubyte R,GLubyte G,GLubyte B,GLubyte A,float X,float Y,float Z=0){
  SDL_Color color={R,G,B,A};
  SDL_Surface *msg = TTF_RenderUTF8_Blended(font,text.c_str(),color);
  GLuint texture=0;
  glGenTextures(1,&texture);
  glBindTexture(GL_TEXTURE_2D,texture);
  GLuint texture_format;
  int wW,wH;
  SDL_GetWindowSize(window,&wW,&wH);
  int colors = msg->format->BytesPerPixel;
    if (colors == 4) {   // alpha
      if (msg->format->Rmask == 0x000000ff)
        texture_format = GL_RGBA;
      else
        texture_format = GL_BGRA_EXT;
    } else {             // no alpha
      if (msg->format->Rmask == 0x000000ff)
        texture_format = GL_RGB;
      else
        texture_format = GL_BGR_EXT;
    }
  float MsgWP=msg->w*1.0/wW;
  float MsgHP=msg->h*1.0/wH;
  int dX=-1;
  int dY=1;
  X*=2;
  Y*=2;
  glTexImage2D(GL_TEXTURE_2D, 0, colors,msg->w,msg->h,0,texture_format,GL_UNSIGNED_BYTE,msg->pixels);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  glBegin(GL_QUADS);
    glColor3f(1,1,1);
    glTexCoord2f(0,0); glVertex2f(dX+X+0     ,  dY-Y+0);
    glTexCoord2f(1,0); glVertex2f(dX+X+MsgWP ,  dY-Y+0);
    glTexCoord2f(1,1); glVertex2f(dX+X+MsgWP ,  dY-Y-MsgHP);
    glTexCoord2f(0,1); glVertex2f(dX+X+0     ,  dY-Y-MsgHP);
  glEnd();
  glDisable(GL_TEXTURE_2D);
  glDeleteTextures(1,&texture);
  SDL_FreeSurface(msg);
}

现在我有一个问题:为什么较低版本的opengl文本无法正确呈现?

0 个答案:

没有答案