You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

750 lines
33 KiB

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