[rtext] Blurry text when drawing custom font to RenderTexture
Please, before submitting a new issue verify and check:
- [x] I tested it on latest raylib version from master branch
- [x] I checked there is no similar issue already reported
- [x] I checked the documentation on the wiki
- [x] My code has no errors or misuse of raylib
Issue description
Problem: When I attempt to draw text to a render texture, it consistently appears blurry, regardless of the various methods I have tried to resolve this issue.
Tests Conducted:
-
Drawing Directly: I tried drawing text just using DrawTextEx(...) which resulted in sharp text, indicating no issues.
-
Drawing to an Image: I tried drawing the text to an image, then converting this image to a normal Texture2D. This method resulted in clear and sharp text, indicating no issues.
-
Drawing to a Render Texture: The blurriness issue specifically arises when rendering text onto a RenderTexture.
Vendor: Apple Renderer: Apple M2 Version: 4.1 Metal - 88.1 GLSL: 4.10
Issue Screenshot
Code Example
#include "raylib.h"
// Using a custom font
const char* font_path =
"/opt/homebrew/Caskroom/font-mononoki-nerd-font/3.2.1/"
"MononokiNerdFont-Regular.ttf";
int main(int argc, char** argv) {
InitWindow(800, 600, "Blurry Render Texture Text");
SetTargetFPS(60);
Font font = LoadFontEx(font_path, 18, NULL, 0);
float spacing = (float)font.baseSize / 10;
RenderTexture rt = LoadRenderTexture(800, 600);
// tried most variations of filters but they don't seem to make a difference on the render texture text
SetTextureFilter(font.texture, TEXTURE_FILTER_POINT);
SetTextureFilter(rt.texture, TEXTURE_FILTER_POINT);
while (!WindowShouldClose()) {
BeginTextureMode(rt);
ClearBackground(BLANK);
const char* rt_text = "Hello, World - Render Texture";
DrawTextEx(font, rt_text, (Vector2){0}, 18, spacing, WHITE);
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
Rectangle src = {0, 0, rt.texture.width, -rt.texture.height};
// The text on this render texture is blurry
DrawTextureRec(rt.texture, src, (Vector2){0}, WHITE);
// This text draws nicely
const char* dir_text = "Hello, World - Direct";
DrawTextEx(font, dir_text, (Vector2){0, 20}, 18, spacing, WHITE);
EndDrawing();
}
UnloadRenderTexture(rt);
UnloadFont(font);
CloseWindow();
}
Update:
I found this thread here #1121 that mentioned changing the pixel format of the FBO.
Since when drawing to the main FBO texts renders nicely, and here Ray mentions the main FBO is RGB, I tried changing it and now text renders nicely!
So seems there is something odd when the FBO is RGBA, I also noticed the quality of the text would change drastically depending on which ClearBackground(COLOR) I had, setting color to BLANK left the text unreadable, where as if I used BLACK it was somewhat readable.
@richard-egeli Mmmh... issue seems related to blending... It can be configured with BeginBlendMode() and EndBlendMode() but, in any case, it should be the user that takes care of that. raylib setups a default-standard blending mode to cover most use-cases.
After trying a few different blend modes it seems you're right, it now draws text with no problems so I'm gonna close this issue.
For anyone else finding this. The following should work well with text by taking into account alpha.
BeginBlendMode(BLEND_ALPHA_PREMULTIPLY);
DrawTextureRec(to->texture.texture, src, (Vector2){x, y}, WHITE);
EndBlendMode();