简体   繁体   English

如何将SDL TTF表面转换为具有透明背景的GL纹理

[英]How to transform SDL TTF Surface into GL Texture with Transparent background

I would like to draw text over content drawn in GL. 我想在GL中绘制的内容上绘制文本。 I want the symbols themselves to be opaque, whilst the rest transparent, allowing the drawn content to be seen. 我希望符号本身是不透明的,而其余符号是透明的,从而可以看到绘制的内容。 The following code yields a text, which is the correct one, yet with a perfectly white background. 下面的代码产生一个文本,它是正确的文本,但是具有完美的白色背景。 My drawn content is completely absent. 我绘制的内容完全不存在。

How am I to solve this? 我该如何解决? I am using SDL 2.0, VSC 我正在使用SDL 2.0,VSC

   glPushMatrix();

   /*Content drawn in GL*/

   GLuint TextureID = 0;
   SDL_Color Color = {30, 30, 30, 0};
   TTF_Font * Font = TTF_OpenFont("Times.ttf", 30);
   SDL_Surface * Message = TTF_RenderText_Blended(Font, "ASDASDASD", Color);

   glGenTextures(1, &TextureID);
   glBindTexture(GL_TEXTURE_2D, TextureID);

   glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, Message->w, Message->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, Message->pixels);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   glBindTexture(GL_TEXTURE_2D, TextureID);

   glBegin(GL_QUADS);
   {
        glColor4f(.5, .5, .5, 1);
        glTexCoord2f(0,0); glVertex2f(MouseX/10, MouseY/10);
        glTexCoord2f(1,0); glVertex2f((MouseX/10) + (Message->w / 10), MouseY/10);
        glTexCoord2f(1,1); glVertex2f((MouseX/10) + (Message->w / 10), (MouseY/10) + (Message->h / 10));
        glTexCoord2f(0,1); glVertex2f(MouseX/10, (MouseY/10) + (Message->h / 10));
   }
   glEnd();

   glPopMatrix();

   SDL_FreeSurface(Message);
   SDL_GL_SwapWindow(GameWindow);

Think of it this way: When you create the texture above, you're drawing dark grey fully transparent (30,30,30,0) text against a fully transparent background (0,0,0,0 due to TTF_RenderText_Blended ). 这样想:在创建上面的纹理时,您是在fully transparent background绘制dark grey fully transparent (30,30,30,0)文本(由于TTF_RenderText_Blended导致为0,0,0,0)。 You're then using immediate mode color blending to raster a quad of the same size (HINT: with no blend func and no mention of z buffer disable!). 然后,您将使用即时模式颜色混合来光栅化具有相同大小的四边形(提示:没有混合功能,也没有提到禁用z缓冲区!)。

PS you're leaking like crazy in the example: PS,在示例中您像疯了似的泄漏:

  • glGenTexture every frame without a glDeleteTextures (you only need a new one if it changes) glGenTexture每帧都没有glDeleteTextures(更改后只需要一个新帧)

  • TTF_OpenFont without a TTF_CloseFont in every frame (keep it only until you do not require rendering of that font) 在每个帧中都没有TTF_CloseFont的TTF_OpenFont(仅保留它,直到不需要渲染该字体为止)

  • TTF_RenderText_Blended surface isn't leaked but you could free it right after glTexImage2D TTF_RenderText_Blended表面没有泄漏,但是您可以在glTexImage2D之后立即释放它

I assume you're meaning to use the font as a mask to subtract from a solid color background? 我假设您是要使用字体作为蒙版从纯色背景中减去的意思? I generally create font textures white (full alpha) then apply color or masking in the shader. 我通常将字体纹理创建为白色(全alpha),然后在着色器中应用颜色或遮罩。 To accomplish a similar effect in immediate mode you could: 要在即时模式下实现类似效果,您可以:

// startup
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);

SDL_Color Color = {255, 255, 255, 255};
TTF_Font * Font = TTF_OpenFont("Times.ttf", 30);
SDL_Surface * Message = TTF_RenderText_Blended(Font, "ASDASDASD", Color);
TTF_CloseFont(Font);

glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);

glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, Message->w, Message->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, Message->pixels);
SDL_FreeSurface(Message);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



// frame render
glClear(GL_COLOR_BUFFER_BIT);

// you'll see this in many (immediate mode) 2D rendering examples
// without the blend eq below, this would render the white text on top of the quad's background color
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// use text as a mask to cut away from what is below
glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);

glBindTexture(GL_TEXTURE_2D, TextureID);


glBegin(GL_QUADS);
{
    glColor4f(.5, .5, .5, 1);
    glTexCoord2f(0,0); glVertex2f(MouseX/10, MouseY/10);
    glTexCoord2f(1,0); glVertex2f((MouseX/10) + (Message->w / 10), MouseY/10);
    glTexCoord2f(1,1); glVertex2f((MouseX/10) + (Message->w / 10), (MouseY/10) + (Message->h / 10));
    glTexCoord2f(0,1); glVertex2f(MouseX/10, (MouseY/10) + (Message->h / 10));
}
glEnd();

// if done every frame, make sure to: glDeleteTextures(1, &TextureID);

SDL_GL_SwapWindow(GameWindow);

Very handy site here: http://www.andersriggelsen.dk/glblendfunc.php 非常方便的网站在这里: http : //www.andersriggelsen.dk/glblendfunc.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM