models_directional_billboard.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - directional billboard
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6
  8. *
  9. * Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Robin (@RobinsAviary)
  15. * Killbot art by patvanmackelberg https://opengameart.org/content/killbot-8-directional under CC0
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #include "raymath.h"
  20. #include <stdlib.h>
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [models] example - directional billboard");
  31. // Set up the camera
  32. Camera camera = { 0 };
  33. camera.position = (Vector3){ 2.0f, 1.0f, 2.0f }; // Starting position
  34. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Target position
  35. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Up vector
  36. camera.fovy = 45.0f; // FOV
  37. camera.projection = CAMERA_PERSPECTIVE; // Projection type (Standard 3D perspective)
  38. // Load billboard texture
  39. Texture skillbot = LoadTexture("resources/skillbot.png");
  40. // Timer to update animation
  41. float anim_timer = 0.0f;
  42. // Animation frame
  43. unsigned int anim = 0;
  44. SetTargetFPS(60);
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. UpdateCamera(&camera, CAMERA_ORBITAL);
  52. // Update timer with delta time
  53. anim_timer += GetFrameTime();
  54. // Update frame index after a certain amount of time (half a second)
  55. if (anim_timer > 0.5f)
  56. {
  57. anim_timer = 0.0f;
  58. anim += 1;
  59. }
  60. // Reset frame index to zero on overflow
  61. if (anim >= 4) anim = 0;
  62. // Find the current direction frame based on the camera position to the billboard object
  63. float dir = (float)floor(((Vector2Angle((Vector2){ 2.0f, 0.0f }, (Vector2){ camera.position.x, camera.position.z })/PI)*4.0f) + 0.25f);
  64. // Correct frame index if angle is negative
  65. if (dir < 0.0f)
  66. {
  67. dir = 8.0f - (float)abs((int)dir);
  68. }
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. BeginMode3D(camera);
  75. DrawGrid(10, 1.0f);
  76. // Draw billboard pointing straight up to the sky, rotated relative to the camera and offset from the bottom
  77. DrawBillboardPro(camera, skillbot, (Rectangle){ 0.0f + (anim*24.0f), 0.0f + (dir*24.0f), 24.0f, 24.0f }, Vector3Zero(), (Vector3){ 0.0f, 1.0f, 0.0f }, Vector2One(), (Vector2){ 0.5f, 0.0f }, 0, WHITE);
  78. EndMode3D();
  79. // Render various variables for reference
  80. DrawText(TextFormat("animation: %d", anim), 10, 10, 20, DARKGRAY);
  81. DrawText(TextFormat("direction frame: %.0f", dir), 10, 40, 20, DARKGRAY);
  82. EndDrawing();
  83. //----------------------------------------------------------------------------------
  84. }
  85. // De-Initialization
  86. //--------------------------------------------------------------------------------------
  87. // Unload billboard texture
  88. UnloadTexture(skillbot);
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }