models_billboard_rendering.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - billboard rendering
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 1.3, last time updated with raylib 3.5
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [models] example - billboard rendering");
  27. // Define the camera to look into our 3d world
  28. Camera camera = { 0 };
  29. camera.position = (Vector3){ 5.0f, 4.0f, 5.0f }; // Camera position
  30. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  31. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  32. camera.fovy = 45.0f; // Camera field-of-view Y
  33. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  34. Texture2D bill = LoadTexture("resources/billboard.png"); // Our billboard texture
  35. Vector3 billPositionStatic = { 0.0f, 2.0f, 0.0f }; // Position of static billboard
  36. Vector3 billPositionRotating = { 1.0f, 2.0f, 1.0f }; // Position of rotating billboard
  37. // Entire billboard texture, source is used to take a segment from a larger texture
  38. Rectangle source = { 0.0f, 0.0f, (float)bill.width, (float)bill.height };
  39. // NOTE: Billboard locked on axis-Y
  40. Vector3 billUp = { 0.0f, 1.0f, 0.0f };
  41. // Set the height of the rotating billboard to 1.0 with the aspect ratio fixed
  42. Vector2 size = { source.width/source.height, 1.0f };
  43. // Rotate around origin
  44. // Here we choose to rotate around the image center
  45. Vector2 origin = Vector2Scale(size, 0.5f);
  46. // Distance is needed for the correct billboard draw order
  47. // Larger distance (further away from the camera) should be drawn prior to smaller distance
  48. float distanceStatic;
  49. float distanceRotating;
  50. float rotation = 0.0f;
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. UpdateCamera(&camera, CAMERA_ORBITAL);
  59. rotation += 0.4f;
  60. distanceStatic = Vector3Distance(camera.position, billPositionStatic);
  61. distanceRotating = Vector3Distance(camera.position, billPositionRotating);
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. BeginMode3D(camera);
  68. DrawGrid(10, 1.0f); // Draw a grid
  69. // Draw order matters!
  70. if (distanceStatic > distanceRotating)
  71. {
  72. DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
  73. DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, WHITE);
  74. }
  75. else
  76. {
  77. DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, WHITE);
  78. DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
  79. }
  80. EndMode3D();
  81. DrawFPS(10, 10);
  82. EndDrawing();
  83. //----------------------------------------------------------------------------------
  84. }
  85. // De-Initialization
  86. //--------------------------------------------------------------------------------------
  87. UnloadTexture(bill); // Unload texture
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }