text_3d_drawing.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - 3d drawing
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * NOTE: Draw a 2D text in 3D space, each letter is drawn in a quad (or 2 quads if backface is set)
  8. * where the texture coodinates of each quad map to the texture coordinates of the glyphs
  9. * inside the font texture
  10. *
  11. * A more efficient approach, i believe, would be to render the text in a render texture and
  12. * map that texture to a plane and render that, or maybe a shader but my method allows more
  13. * flexibility...for example to change position of each letter individually to make somethink
  14. * like a wavy text effect
  15. *
  16. * Special thanks to:
  17. * @Nighten for the DrawTextStyle() code https://github.com/NightenDushi/Raylib_DrawTextStyle
  18. * Chris Camacho (codifies - http://bedroomcoders.co.uk/) for the alpha discard shader
  19. *
  20. * Example originally created with raylib 3.5, last time updated with raylib 4.0
  21. *
  22. * Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
  23. *
  24. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  25. * BSD-like license that allows static linking with closed source software
  26. *
  27. * Copyright (c) 2021-2025 Vlad Adrian (@demizdor)
  28. *
  29. ********************************************************************************************/
  30. #include "raylib.h"
  31. #include "rlgl.h"
  32. #include <stddef.h> // Required for: NULL
  33. #include <math.h> // Required for: sinf()
  34. #if defined(PLATFORM_DESKTOP)
  35. #define GLSL_VERSION 330
  36. #else // PLATFORM_ANDROID, PLATFORM_WEB
  37. #define GLSL_VERSION 100
  38. #endif
  39. //--------------------------------------------------------------------------------------
  40. // Global variables
  41. //--------------------------------------------------------------------------------------
  42. #define LETTER_BOUNDRY_SIZE 0.25f
  43. #define TEXT_MAX_LAYERS 32
  44. #define LETTER_BOUNDRY_COLOR VIOLET
  45. bool SHOW_LETTER_BOUNDRY = false;
  46. bool SHOW_TEXT_BOUNDRY = false;
  47. //--------------------------------------------------------------------------------------
  48. // Types and Structures Definition
  49. //--------------------------------------------------------------------------------------
  50. // Configuration structure for waving the text
  51. typedef struct WaveTextConfig {
  52. Vector3 waveRange;
  53. Vector3 waveSpeed;
  54. Vector3 waveOffset;
  55. } WaveTextConfig;
  56. //--------------------------------------------------------------------------------------
  57. // Module Functions Declaration
  58. //--------------------------------------------------------------------------------------
  59. // Draw a codepoint in 3D space
  60. static void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontSize, bool backface, Color tint);
  61. // Draw a 2D text in 3D space
  62. static void DrawText3D(Font font, const char *text, Vector3 position, float fontSize, float fontSpacing, float lineSpacing, bool backface, Color tint);
  63. // Draw a 2D text in 3D space and wave the parts that start with '~~' and end with '~~'
  64. // This is a modified version of the original code by @Nighten found here https://github.com/NightenDushi/Raylib_DrawTextStyle
  65. static void DrawTextWave3D(Font font, const char *text, Vector3 position, float fontSize, float fontSpacing, float lineSpacing, bool backface, WaveTextConfig *config, float time, Color tint);
  66. // Measure a text in 3D ignoring the `~~` chars
  67. static Vector3 MeasureTextWave3D(Font font, const char *text, float fontSize, float fontSpacing, float lineSpacing);
  68. // Generates a nice color with a random hue
  69. static Color GenerateRandomColor(float s, float v);
  70. //------------------------------------------------------------------------------------
  71. // Program main entry point
  72. //------------------------------------------------------------------------------------
  73. int main(void)
  74. {
  75. // Initialization
  76. //--------------------------------------------------------------------------------------
  77. const int screenWidth = 800;
  78. const int screenHeight = 450;
  79. SetConfigFlags(FLAG_MSAA_4X_HINT|FLAG_VSYNC_HINT);
  80. InitWindow(screenWidth, screenHeight, "raylib [text] example - 3d drawing");
  81. bool spin = true; // Spin the camera?
  82. bool multicolor = false; // Multicolor mode
  83. // Define the camera to look into our 3d world
  84. Camera3D camera = { 0 };
  85. camera.position = (Vector3){ -10.0f, 15.0f, -10.0f }; // Camera position
  86. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  87. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  88. camera.fovy = 45.0f; // Camera field-of-view Y
  89. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  90. int camera_mode = CAMERA_ORBITAL;
  91. Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
  92. Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };
  93. // Use the default font
  94. Font font = GetFontDefault();
  95. float fontSize = 0.8f;
  96. float fontSpacing = 0.05f;
  97. float lineSpacing = -0.1f;
  98. // Set the text (using markdown!)
  99. char text[64] = "Hello ~~World~~ in 3D!";
  100. Vector3 tbox = {0};
  101. int layers = 1;
  102. int quads = 0;
  103. float layerDistance = 0.01f;
  104. WaveTextConfig wcfg;
  105. wcfg.waveSpeed.x = wcfg.waveSpeed.y = 3.0f; wcfg.waveSpeed.z = 0.5f;
  106. wcfg.waveOffset.x = wcfg.waveOffset.y = wcfg.waveOffset.z = 0.35f;
  107. wcfg.waveRange.x = wcfg.waveRange.y = wcfg.waveRange.z = 0.45f;
  108. float time = 0.0f;
  109. // Setup a light and dark color
  110. Color light = MAROON;
  111. Color dark = RED;
  112. // Load the alpha discard shader
  113. Shader alphaDiscard = LoadShader(NULL, TextFormat("resources/shaders/glsl%i/alpha_discard.fs", GLSL_VERSION));
  114. // Array filled with multiple random colors (when multicolor mode is set)
  115. Color multi[TEXT_MAX_LAYERS] = {0};
  116. DisableCursor(); // Limit cursor to relative movement inside the window
  117. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  118. //--------------------------------------------------------------------------------------
  119. // Main game loop
  120. while (!WindowShouldClose()) // Detect window close button or ESC key
  121. {
  122. // Update
  123. //----------------------------------------------------------------------------------
  124. UpdateCamera(&camera, camera_mode);
  125. // Handle font files dropped
  126. if (IsFileDropped())
  127. {
  128. FilePathList droppedFiles = LoadDroppedFiles();
  129. // NOTE: We only support first ttf file dropped
  130. if (IsFileExtension(droppedFiles.paths[0], ".ttf"))
  131. {
  132. UnloadFont(font);
  133. font = LoadFontEx(droppedFiles.paths[0], (int)fontSize, 0, 0);
  134. }
  135. else if (IsFileExtension(droppedFiles.paths[0], ".fnt"))
  136. {
  137. UnloadFont(font);
  138. font = LoadFont(droppedFiles.paths[0]);
  139. fontSize = (float)font.baseSize;
  140. }
  141. UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
  142. }
  143. // Handle Events
  144. if (IsKeyPressed(KEY_F1)) SHOW_LETTER_BOUNDRY = !SHOW_LETTER_BOUNDRY;
  145. if (IsKeyPressed(KEY_F2)) SHOW_TEXT_BOUNDRY = !SHOW_TEXT_BOUNDRY;
  146. if (IsKeyPressed(KEY_F3))
  147. {
  148. // Handle camera change
  149. spin = !spin;
  150. // we need to reset the camera when changing modes
  151. camera = (Camera3D){ 0 };
  152. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  153. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  154. camera.fovy = 45.0f; // Camera field-of-view Y
  155. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  156. if (spin)
  157. {
  158. camera.position = (Vector3){ -10.0f, 15.0f, -10.0f }; // Camera position
  159. camera_mode = CAMERA_ORBITAL;
  160. }
  161. else
  162. {
  163. camera.position = (Vector3){ 10.0f, 10.0f, -10.0f }; // Camera position
  164. camera_mode = CAMERA_FREE;
  165. }
  166. }
  167. // Handle clicking the cube
  168. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  169. {
  170. Ray ray = GetScreenToWorldRay(GetMousePosition(), camera);
  171. // Check collision between ray and box
  172. RayCollision collision = GetRayCollisionBox(ray,
  173. (BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 },
  174. (Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
  175. if (collision.hit)
  176. {
  177. // Generate new random colors
  178. light = GenerateRandomColor(0.5f, 0.78f);
  179. dark = GenerateRandomColor(0.4f, 0.58f);
  180. }
  181. }
  182. // Handle text layers changes
  183. if (IsKeyPressed(KEY_HOME)) { if (layers > 1) --layers; }
  184. else if (IsKeyPressed(KEY_END)) { if (layers < TEXT_MAX_LAYERS) ++layers; }
  185. // Handle text changes
  186. if (IsKeyPressed(KEY_LEFT)) fontSize -= 0.5f;
  187. else if (IsKeyPressed(KEY_RIGHT)) fontSize += 0.5f;
  188. else if (IsKeyPressed(KEY_UP)) fontSpacing -= 0.1f;
  189. else if (IsKeyPressed(KEY_DOWN)) fontSpacing += 0.1f;
  190. else if (IsKeyPressed(KEY_PAGE_UP)) lineSpacing -= 0.1f;
  191. else if (IsKeyPressed(KEY_PAGE_DOWN)) lineSpacing += 0.1f;
  192. else if (IsKeyDown(KEY_INSERT)) layerDistance -= 0.001f;
  193. else if (IsKeyDown(KEY_DELETE)) layerDistance += 0.001f;
  194. else if (IsKeyPressed(KEY_TAB))
  195. {
  196. multicolor = !multicolor; // Enable /disable multicolor mode
  197. if (multicolor)
  198. {
  199. // Fill color array with random colors
  200. for (int i = 0; i < TEXT_MAX_LAYERS; i++)
  201. {
  202. multi[i] = GenerateRandomColor(0.5f, 0.8f);
  203. multi[i].a = GetRandomValue(0, 255);
  204. }
  205. }
  206. }
  207. // Handle text input
  208. int ch = GetCharPressed();
  209. if (IsKeyPressed(KEY_BACKSPACE))
  210. {
  211. // Remove last char
  212. int len = TextLength(text);
  213. if (len > 0) text[len - 1] = '\0';
  214. }
  215. else if (IsKeyPressed(KEY_ENTER))
  216. {
  217. // handle newline
  218. int len = TextLength(text);
  219. if (len < sizeof(text) - 1)
  220. {
  221. text[len] = '\n';
  222. text[len+1] ='\0';
  223. }
  224. }
  225. else
  226. {
  227. // append only printable chars
  228. int len = TextLength(text);
  229. if (len < sizeof(text) - 1)
  230. {
  231. text[len] = ch;
  232. text[len+1] ='\0';
  233. }
  234. }
  235. // Measure 3D text so we can center it
  236. tbox = MeasureTextWave3D(font, text, fontSize, fontSpacing, lineSpacing);
  237. quads = 0; // Reset quad counter
  238. time += GetFrameTime(); // Update timer needed by `DrawTextWave3D()`
  239. //----------------------------------------------------------------------------------
  240. // Draw
  241. //----------------------------------------------------------------------------------
  242. BeginDrawing();
  243. ClearBackground(RAYWHITE);
  244. BeginMode3D(camera);
  245. DrawCubeV(cubePosition, cubeSize, dark);
  246. DrawCubeWires(cubePosition, 2.1f, 2.1f, 2.1f, light);
  247. DrawGrid(10, 2.0f);
  248. // Use a shader to handle the depth buffer issue with transparent textures
  249. // NOTE: more info at https://bedroomcoders.co.uk/posts/198
  250. BeginShaderMode(alphaDiscard);
  251. // Draw the 3D text above the red cube
  252. rlPushMatrix();
  253. rlRotatef(90.0f, 1.0f, 0.0f, 0.0f);
  254. rlRotatef(90.0f, 0.0f, 0.0f, -1.0f);
  255. for (int i = 0; i < layers; i++)
  256. {
  257. Color clr = light;
  258. if (multicolor) clr = multi[i];
  259. DrawTextWave3D(font, text, (Vector3){ -tbox.x/2.0f, layerDistance*i, -4.5f }, fontSize, fontSpacing, lineSpacing, true, &wcfg, time, clr);
  260. }
  261. // Draw the text boundry if set
  262. if (SHOW_TEXT_BOUNDRY) DrawCubeWiresV((Vector3){ 0.0f, 0.0f, -4.5f + tbox.z/2 }, tbox, dark);
  263. rlPopMatrix();
  264. // Don't draw the letter boundries for the 3D text below
  265. bool slb = SHOW_LETTER_BOUNDRY;
  266. SHOW_LETTER_BOUNDRY = false;
  267. // Draw 3D options (use default font)
  268. //-------------------------------------------------------------------------
  269. rlPushMatrix();
  270. rlRotatef(180.0f, 0.0f, 1.0f, 0.0f);
  271. char *opt = (char *)TextFormat("< SIZE: %2.1f >", fontSize);
  272. quads += TextLength(opt);
  273. Vector2 m = MeasureTextEx(GetFontDefault(), opt, 0.8f, 0.1f);
  274. Vector3 pos = { -m.x/2.0f, 0.01f, 2.0f};
  275. DrawText3D(GetFontDefault(), opt, pos, 0.8f, 0.1f, 0.0f, false, BLUE);
  276. pos.z += 0.5f + m.y;
  277. opt = (char *)TextFormat("< SPACING: %2.1f >", fontSpacing);
  278. quads += TextLength(opt);
  279. m = MeasureTextEx(GetFontDefault(), opt, 0.8f, 0.1f);
  280. pos.x = -m.x/2.0f;
  281. DrawText3D(GetFontDefault(), opt, pos, 0.8f, 0.1f, 0.0f, false, BLUE);
  282. pos.z += 0.5f + m.y;
  283. opt = (char *)TextFormat("< LINE: %2.1f >", lineSpacing);
  284. quads += TextLength(opt);
  285. m = MeasureTextEx(GetFontDefault(), opt, 0.8f, 0.1f);
  286. pos.x = -m.x/2.0f;
  287. DrawText3D(GetFontDefault(), opt, pos, 0.8f, 0.1f, 0.0f, false, BLUE);
  288. pos.z += 0.5f + m.y;
  289. opt = (char *)TextFormat("< LBOX: %3s >", slb? "ON" : "OFF");
  290. quads += TextLength(opt);
  291. m = MeasureTextEx(GetFontDefault(), opt, 0.8f, 0.1f);
  292. pos.x = -m.x/2.0f;
  293. DrawText3D(GetFontDefault(), opt, pos, 0.8f, 0.1f, 0.0f, false, RED);
  294. pos.z += 0.5f + m.y;
  295. opt = (char *)TextFormat("< TBOX: %3s >", SHOW_TEXT_BOUNDRY? "ON" : "OFF");
  296. quads += TextLength(opt);
  297. m = MeasureTextEx(GetFontDefault(), opt, 0.8f, 0.1f);
  298. pos.x = -m.x/2.0f;
  299. DrawText3D(GetFontDefault(), opt, pos, 0.8f, 0.1f, 0.0f, false, RED);
  300. pos.z += 0.5f + m.y;
  301. opt = (char *)TextFormat("< LAYER DISTANCE: %.3f >", layerDistance);
  302. quads += TextLength(opt);
  303. m = MeasureTextEx(GetFontDefault(), opt, 0.8f, 0.1f);
  304. pos.x = -m.x/2.0f;
  305. DrawText3D(GetFontDefault(), opt, pos, 0.8f, 0.1f, 0.0f, false, DARKPURPLE);
  306. rlPopMatrix();
  307. //-------------------------------------------------------------------------
  308. // Draw 3D info text (use default font)
  309. //-------------------------------------------------------------------------
  310. opt = "All the text displayed here is in 3D";
  311. quads += 36;
  312. m = MeasureTextEx(GetFontDefault(), opt, 1.0f, 0.05f);
  313. pos = (Vector3){-m.x/2.0f, 0.01f, 2.0f};
  314. DrawText3D(GetFontDefault(), opt, pos, 1.0f, 0.05f, 0.0f, false, DARKBLUE);
  315. pos.z += 1.5f + m.y;
  316. opt = "press [Left]/[Right] to change the font size";
  317. quads += 44;
  318. m = MeasureTextEx(GetFontDefault(), opt, 0.6f, 0.05f);
  319. pos.x = -m.x/2.0f;
  320. DrawText3D(GetFontDefault(), opt, pos, 0.6f, 0.05f, 0.0f, false, DARKBLUE);
  321. pos.z += 0.5f + m.y;
  322. opt = "press [Up]/[Down] to change the font spacing";
  323. quads += 44;
  324. m = MeasureTextEx(GetFontDefault(), opt, 0.6f, 0.05f);
  325. pos.x = -m.x/2.0f;
  326. DrawText3D(GetFontDefault(), opt, pos, 0.6f, 0.05f, 0.0f, false, DARKBLUE);
  327. pos.z += 0.5f + m.y;
  328. opt = "press [PgUp]/[PgDown] to change the line spacing";
  329. quads += 48;
  330. m = MeasureTextEx(GetFontDefault(), opt, 0.6f, 0.05f);
  331. pos.x = -m.x/2.0f;
  332. DrawText3D(GetFontDefault(), opt, pos, 0.6f, 0.05f, 0.0f, false, DARKBLUE);
  333. pos.z += 0.5f + m.y;
  334. opt = "press [F1] to toggle the letter boundry";
  335. quads += 39;
  336. m = MeasureTextEx(GetFontDefault(), opt, 0.6f, 0.05f);
  337. pos.x = -m.x/2.0f;
  338. DrawText3D(GetFontDefault(), opt, pos, 0.6f, 0.05f, 0.0f, false, DARKBLUE);
  339. pos.z += 0.5f + m.y;
  340. opt = "press [F2] to toggle the text boundry";
  341. quads += 37;
  342. m = MeasureTextEx(GetFontDefault(), opt, 0.6f, 0.05f);
  343. pos.x = -m.x/2.0f;
  344. DrawText3D(GetFontDefault(), opt, pos, 0.6f, 0.05f, 0.0f, false, DARKBLUE);
  345. //-------------------------------------------------------------------------
  346. SHOW_LETTER_BOUNDRY = slb;
  347. EndShaderMode();
  348. EndMode3D();
  349. // Draw 2D info text & stats
  350. //-------------------------------------------------------------------------
  351. DrawText("Drag & drop a font file to change the font!\nType something, see what happens!\n\n"
  352. "Press [F3] to toggle the camera", 10, 35, 10, BLACK);
  353. quads += TextLength(text)*2*layers;
  354. char *tmp = (char *)TextFormat("%2i layer(s) | %s camera | %4i quads (%4i verts)", layers, spin? "ORBITAL" : "FREE", quads, quads*4);
  355. int width = MeasureText(tmp, 10);
  356. DrawText(tmp, screenWidth - 20 - width, 10, 10, DARKGREEN);
  357. tmp = "[Home]/[End] to add/remove 3D text layers";
  358. width = MeasureText(tmp, 10);
  359. DrawText(tmp, screenWidth - 20 - width, 25, 10, DARKGRAY);
  360. tmp = "[Insert]/[Delete] to increase/decrease distance between layers";
  361. width = MeasureText(tmp, 10);
  362. DrawText(tmp, screenWidth - 20 - width, 40, 10, DARKGRAY);
  363. tmp = "click the [CUBE] for a random color";
  364. width = MeasureText(tmp, 10);
  365. DrawText(tmp, screenWidth - 20 - width, 55, 10, DARKGRAY);
  366. tmp = "[Tab] to toggle multicolor mode";
  367. width = MeasureText(tmp, 10);
  368. DrawText(tmp, screenWidth - 20 - width, 70, 10, DARKGRAY);
  369. //-------------------------------------------------------------------------
  370. DrawFPS(10, 10);
  371. EndDrawing();
  372. //----------------------------------------------------------------------------------
  373. }
  374. // De-Initialization
  375. //--------------------------------------------------------------------------------------
  376. UnloadFont(font);
  377. CloseWindow(); // Close window and OpenGL context
  378. //--------------------------------------------------------------------------------------
  379. return 0;
  380. }
  381. //--------------------------------------------------------------------------------------
  382. // Module Functions Definitions
  383. //--------------------------------------------------------------------------------------
  384. // Draw codepoint at specified position in 3D space
  385. static void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontSize, bool backface, Color tint)
  386. {
  387. // Character index position in sprite font
  388. // NOTE: In case a codepoint is not available in the font, index returned points to '?'
  389. int index = GetGlyphIndex(font, codepoint);
  390. float scale = fontSize/(float)font.baseSize;
  391. // Character destination rectangle on screen
  392. // NOTE: We consider charsPadding on drawing
  393. position.x += (float)(font.glyphs[index].offsetX - font.glyphPadding)*scale;
  394. position.z += (float)(font.glyphs[index].offsetY - font.glyphPadding)*scale;
  395. // Character source rectangle from font texture atlas
  396. // NOTE: We consider chars padding when drawing, it could be required for outline/glow shader effects
  397. Rectangle srcRec = { font.recs[index].x - (float)font.glyphPadding, font.recs[index].y - (float)font.glyphPadding,
  398. font.recs[index].width + 2.0f*font.glyphPadding, font.recs[index].height + 2.0f*font.glyphPadding };
  399. float width = (float)(font.recs[index].width + 2.0f*font.glyphPadding)*scale;
  400. float height = (float)(font.recs[index].height + 2.0f*font.glyphPadding)*scale;
  401. if (font.texture.id > 0)
  402. {
  403. const float x = 0.0f;
  404. const float y = 0.0f;
  405. const float z = 0.0f;
  406. // normalized texture coordinates of the glyph inside the font texture (0.0f -> 1.0f)
  407. const float tx = srcRec.x/font.texture.width;
  408. const float ty = srcRec.y/font.texture.height;
  409. const float tw = (srcRec.x+srcRec.width)/font.texture.width;
  410. const float th = (srcRec.y+srcRec.height)/font.texture.height;
  411. if (SHOW_LETTER_BOUNDRY) DrawCubeWiresV((Vector3){ position.x + width/2, position.y, position.z + height/2}, (Vector3){ width, LETTER_BOUNDRY_SIZE, height }, LETTER_BOUNDRY_COLOR);
  412. rlCheckRenderBatchLimit(4 + 4*backface);
  413. rlSetTexture(font.texture.id);
  414. rlPushMatrix();
  415. rlTranslatef(position.x, position.y, position.z);
  416. rlBegin(RL_QUADS);
  417. rlColor4ub(tint.r, tint.g, tint.b, tint.a);
  418. // Front Face
  419. rlNormal3f(0.0f, 1.0f, 0.0f); // Normal Pointing Up
  420. rlTexCoord2f(tx, ty); rlVertex3f(x, y, z); // Top Left Of The Texture and Quad
  421. rlTexCoord2f(tx, th); rlVertex3f(x, y, z + height); // Bottom Left Of The Texture and Quad
  422. rlTexCoord2f(tw, th); rlVertex3f(x + width, y, z + height); // Bottom Right Of The Texture and Quad
  423. rlTexCoord2f(tw, ty); rlVertex3f(x + width, y, z); // Top Right Of The Texture and Quad
  424. if (backface)
  425. {
  426. // Back Face
  427. rlNormal3f(0.0f, -1.0f, 0.0f); // Normal Pointing Down
  428. rlTexCoord2f(tx, ty); rlVertex3f(x, y, z); // Top Right Of The Texture and Quad
  429. rlTexCoord2f(tw, ty); rlVertex3f(x + width, y, z); // Top Left Of The Texture and Quad
  430. rlTexCoord2f(tw, th); rlVertex3f(x + width, y, z + height); // Bottom Left Of The Texture and Quad
  431. rlTexCoord2f(tx, th); rlVertex3f(x, y, z + height); // Bottom Right Of The Texture and Quad
  432. }
  433. rlEnd();
  434. rlPopMatrix();
  435. rlSetTexture(0);
  436. }
  437. }
  438. // Draw a 2D text in 3D space
  439. static void DrawText3D(Font font, const char *text, Vector3 position, float fontSize, float fontSpacing, float lineSpacing, bool backface, Color tint)
  440. {
  441. int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
  442. float textOffsetY = 0.0f; // Offset between lines (on line break '\n')
  443. float textOffsetX = 0.0f; // Offset X to next character to draw
  444. float scale = fontSize/(float)font.baseSize;
  445. for (int i = 0; i < length;)
  446. {
  447. // Get next codepoint from byte string and glyph index in font
  448. int codepointByteCount = 0;
  449. int codepoint = GetCodepoint(&text[i], &codepointByteCount);
  450. int index = GetGlyphIndex(font, codepoint);
  451. // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)
  452. // but we need to draw all of the bad bytes using the '?' symbol moving one byte
  453. if (codepoint == 0x3f) codepointByteCount = 1;
  454. if (codepoint == '\n')
  455. {
  456. // NOTE: Fixed line spacing of 1.5 line-height
  457. // TODO: Support custom line spacing defined by user
  458. textOffsetY += fontSize + lineSpacing;
  459. textOffsetX = 0.0f;
  460. }
  461. else
  462. {
  463. if ((codepoint != ' ') && (codepoint != '\t'))
  464. {
  465. DrawTextCodepoint3D(font, codepoint, (Vector3){ position.x + textOffsetX, position.y, position.z + textOffsetY }, fontSize, backface, tint);
  466. }
  467. if (font.glyphs[index].advanceX == 0) textOffsetX += (float)font.recs[index].width*scale + fontSpacing;
  468. else textOffsetX += (float)font.glyphs[index].advanceX*scale + fontSpacing;
  469. }
  470. i += codepointByteCount; // Move text bytes counter to next codepoint
  471. }
  472. }
  473. // Draw a 2D text in 3D space and wave the parts that start with `~~` and end with `~~`
  474. // This is a modified version of the original code by @Nighten found here https://github.com/NightenDushi/Raylib_DrawTextStyle
  475. static void DrawTextWave3D(Font font, const char *text, Vector3 position, float fontSize, float fontSpacing, float lineSpacing, bool backface, WaveTextConfig* config, float time, Color tint)
  476. {
  477. int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
  478. float textOffsetY = 0.0f; // Offset between lines (on line break '\n')
  479. float textOffsetX = 0.0f; // Offset X to next character to draw
  480. float scale = fontSize/(float)font.baseSize;
  481. bool wave = false;
  482. for (int i = 0, k = 0; i < length; ++k)
  483. {
  484. // Get next codepoint from byte string and glyph index in font
  485. int codepointByteCount = 0;
  486. int codepoint = GetCodepoint(&text[i], &codepointByteCount);
  487. int index = GetGlyphIndex(font, codepoint);
  488. // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)
  489. // but we need to draw all of the bad bytes using the '?' symbol moving one byte
  490. if (codepoint == 0x3f) codepointByteCount = 1;
  491. if (codepoint == '\n')
  492. {
  493. // NOTE: Fixed line spacing of 1.5 line-height
  494. // TODO: Support custom line spacing defined by user
  495. textOffsetY += fontSize + lineSpacing;
  496. textOffsetX = 0.0f;
  497. k = 0;
  498. }
  499. else if (codepoint == '~')
  500. {
  501. if (GetCodepoint(&text[i+1], &codepointByteCount) == '~')
  502. {
  503. codepointByteCount += 1;
  504. wave = !wave;
  505. }
  506. }
  507. else
  508. {
  509. if ((codepoint != ' ') && (codepoint != '\t'))
  510. {
  511. Vector3 pos = position;
  512. if (wave) // Apply the wave effect
  513. {
  514. pos.x += sinf(time*config->waveSpeed.x-k*config->waveOffset.x)*config->waveRange.x;
  515. pos.y += sinf(time*config->waveSpeed.y-k*config->waveOffset.y)*config->waveRange.y;
  516. pos.z += sinf(time*config->waveSpeed.z-k*config->waveOffset.z)*config->waveRange.z;
  517. }
  518. DrawTextCodepoint3D(font, codepoint, (Vector3){ pos.x + textOffsetX, pos.y, pos.z + textOffsetY }, fontSize, backface, tint);
  519. }
  520. if (font.glyphs[index].advanceX == 0) textOffsetX += (float)font.recs[index].width*scale + fontSpacing;
  521. else textOffsetX += (float)font.glyphs[index].advanceX*scale + fontSpacing;
  522. }
  523. i += codepointByteCount; // Move text bytes counter to next codepoint
  524. }
  525. }
  526. // Measure a text in 3D ignoring the `~~` chars
  527. static Vector3 MeasureTextWave3D(Font font, const char* text, float fontSize, float fontSpacing, float lineSpacing)
  528. {
  529. int len = TextLength(text);
  530. int tempLen = 0; // Used to count longer text line num chars
  531. int lenCounter = 0;
  532. float tempTextWidth = 0.0f; // Used to count longer text line width
  533. float scale = fontSize/(float)font.baseSize;
  534. float textHeight = scale;
  535. float textWidth = 0.0f;
  536. int letter = 0; // Current character
  537. int index = 0; // Index position in sprite font
  538. for (int i = 0; i < len; i++)
  539. {
  540. int next = 0;
  541. letter = GetCodepoint(&text[i], &next);
  542. index = GetGlyphIndex(font, letter);
  543. // NOTE: normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)
  544. // but we need to draw all of the bad bytes using the '?' symbol so to not skip any we set next = 1
  545. if (letter == 0x3f) next = 1;
  546. i += next - 1;
  547. if (letter != '\n')
  548. {
  549. if (letter == '~' && GetCodepoint(&text[i+1], &next) == '~')
  550. {
  551. i++;
  552. }
  553. else
  554. {
  555. lenCounter++;
  556. if (font.glyphs[index].advanceX != 0) textWidth += font.glyphs[index].advanceX*scale;
  557. else textWidth += (font.recs[index].width + font.glyphs[index].offsetX)*scale;
  558. }
  559. }
  560. else
  561. {
  562. if (tempTextWidth < textWidth) tempTextWidth = textWidth;
  563. lenCounter = 0;
  564. textWidth = 0.0f;
  565. textHeight += fontSize + lineSpacing;
  566. }
  567. if (tempLen < lenCounter) tempLen = lenCounter;
  568. }
  569. if (tempTextWidth < textWidth) tempTextWidth = textWidth;
  570. Vector3 vec = { 0 };
  571. vec.x = tempTextWidth + (float)((tempLen - 1)*fontSpacing); // Adds chars spacing to measure
  572. vec.y = 0.25f;
  573. vec.z = textHeight;
  574. return vec;
  575. }
  576. // Generates a nice color with a random hue
  577. static Color GenerateRandomColor(float s, float v)
  578. {
  579. const float Phi = 0.618033988749895f; // Golden ratio conjugate
  580. float h = (float)GetRandomValue(0, 360);
  581. h = fmodf((h + h*Phi), 360.0f);
  582. return ColorFromHSV(h, s, v);
  583. }