sprite_instanced.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "./common.h"
  2. #include "r3d.h"
  3. #include "raylib.h"
  4. #include "raymath.h"
  5. /* === Resources === */
  6. static Camera3D camera = { 0 };
  7. static R3D_Mesh plane = { 0 };
  8. static R3D_Material material = { 0 };
  9. static Texture2D texture = { 0 };
  10. static R3D_Sprite sprite = { 0 };
  11. static Matrix transforms[64] = { 0 };
  12. /* === Examples === */
  13. const char* Init(void)
  14. {
  15. /* --- Initialize R3D with its internal resolution --- */
  16. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  17. SetTargetFPS(60);
  18. /* --- Set the background color --- */
  19. R3D_SetBackgroundColor(SKYBLUE);
  20. /* --- Generate a large plane to act as the ground --- */
  21. plane = R3D_GenMeshPlane(200, 200, 1, 1, true);
  22. material = R3D_GetDefaultMaterial();
  23. material.albedo.color = GREEN;
  24. /* --- Load a texture and create a sprite --- */
  25. texture = LoadTexture(RESOURCES_PATH "tree.png");
  26. sprite = R3D_LoadSprite(texture, 1, 1);
  27. sprite.material.shadowCastMode = R3D_SHADOW_CAST_ALL_FACES;
  28. /* --- Create multiple transforms for instanced sprites with random positions and scales --- */
  29. for (int i = 0; i < sizeof(transforms) / sizeof(*transforms); i++) {
  30. float scaleFactor = GetRandomValue(25, 50) / 10.0f;
  31. Matrix scale = MatrixScale(scaleFactor, scaleFactor, 1.0f);
  32. Matrix translate = MatrixTranslate(GetRandomValue(-100, 100), scaleFactor, GetRandomValue(-100, 100));
  33. transforms[i] = MatrixMultiply(scale, translate);
  34. }
  35. /* --- Setup the scene lighting --- */
  36. R3D_SetSceneBounds(
  37. (BoundingBox) {
  38. .min = { -100, -10, -100 },
  39. .max = { +100, +10, +100 }
  40. }
  41. );
  42. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  43. {
  44. R3D_SetLightDirection(light, (Vector3) { -1, -1, -1 });
  45. R3D_EnableShadow(light, 4096);
  46. R3D_SetShadowBias(light, 0.0025f);
  47. R3D_SetLightActive(light, true);
  48. }
  49. /* --- Setup the camera --- */
  50. camera = (Camera3D){
  51. .position = (Vector3) { 0, 5, 0 },
  52. .target = (Vector3) { 0, 5, -1 },
  53. .up = (Vector3) { 0, 1, 0 },
  54. .fovy = 60,
  55. };
  56. /* --- Capture the mouse and let's go! --- */
  57. DisableCursor();
  58. return "[r3d] - Instanced sprites example";
  59. }
  60. void Update(float delta)
  61. {
  62. UpdateCamera(&camera, CAMERA_FREE);
  63. }
  64. void Draw(void)
  65. {
  66. R3D_Begin(camera);
  67. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, 0, 0));
  68. R3D_DrawSpriteInstanced(&sprite, transforms, sizeof(transforms) / sizeof(*transforms));
  69. R3D_End();
  70. }
  71. void Close(void)
  72. {
  73. R3D_UnloadSprite(&sprite);
  74. R3D_UnloadMesh(&plane);
  75. UnloadTexture(texture);
  76. R3D_Close();
  77. }