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.

110 lines
4.6 KiB

пре 2 година
пре 2 година
пре 5 година
пре 5 година
пре 2 година
пре 5 година
пре 2 година
пре 5 година
пре 2 година
пре 2 година
пре 5 година
пре 9 година
пре 5 година
пре 9 година
  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-2023 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. // Rotate around origin
  40. // Here we choose to rotate around the image center
  41. // NOTE: (-1, 1) is the range where origin.x, origin.y is inside the texture
  42. Vector2 rotateOrigin = { 0.0f };
  43. // Distance is needed for the correct billboard draw order
  44. // Larger distance (further away from the camera) should be drawn prior to smaller distance.
  45. float distanceStatic;
  46. float distanceRotating;
  47. float rotation = 0.0f;
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. UpdateCamera(&camera, CAMERA_ORBITAL);
  56. rotation += 0.4f;
  57. distanceStatic = Vector3Distance(camera.position, billPositionStatic);
  58. distanceRotating = Vector3Distance(camera.position, billPositionRotating);
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. BeginMode3D(camera);
  65. DrawGrid(10, 1.0f); // Draw a grid
  66. // Draw order matters!
  67. if (distanceStatic > distanceRotating)
  68. {
  69. DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
  70. DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
  71. }
  72. else
  73. {
  74. DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
  75. DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
  76. }
  77. EndMode3D();
  78. DrawFPS(10, 10);
  79. EndDrawing();
  80. //----------------------------------------------------------------------------------
  81. }
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. UnloadTexture(bill); // Unload texture
  85. CloseWindow(); // Close window and OpenGL context
  86. //--------------------------------------------------------------------------------------
  87. return 0;
  88. }