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.

113 lines
4.7 KiB

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