billboards.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <r3d/r3d.h>
  2. #include <raymath.h>
  3. #ifndef RESOURCES_PATH
  4. # define RESOURCES_PATH "./"
  5. #endif
  6. int main(void)
  7. {
  8. // Initialize window
  9. InitWindow(800, 450, "[r3d] - Billboards example");
  10. SetTargetFPS(60);
  11. // Initialize R3D
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. // Set background/ambient color
  14. R3D_ENVIRONMENT_SET(background.color, (Color){102, 191, 255, 255});
  15. R3D_ENVIRONMENT_SET(ambient.color, (Color){10, 19, 25, 255});
  16. // Create ground mesh and material
  17. R3D_Mesh meshGround = R3D_GenMeshPlane(200, 200, 1, 1);
  18. R3D_Material matGround = R3D_GetDefaultMaterial();
  19. matGround.albedo.color = GREEN;
  20. // Create billboard mesh and material
  21. R3D_Mesh meshBillboard = R3D_GenMeshQuad(1.0f, 1.0f, 1, 1, (Vector3){0.0f, 0.0f, 1.0f});
  22. meshBillboard.shadowCastMode = R3D_SHADOW_CAST_ON_DOUBLE_SIDED;
  23. R3D_Material matBillboard = R3D_GetDefaultMaterial();
  24. matBillboard.billboardMode = R3D_BILLBOARD_Y_AXIS;
  25. matBillboard.albedo.texture = LoadTexture(RESOURCES_PATH "tree.png");
  26. // Create transforms for instanced billboards
  27. Matrix transformsBillboards[64];
  28. for (int i = 0; i < 64; i++)
  29. {
  30. float scaleFactor = GetRandomValue(25, 50) / 10.0f;
  31. Matrix scale = MatrixScale(scaleFactor, scaleFactor, 1.0f);
  32. Matrix translate = MatrixTranslate(
  33. (float)GetRandomValue(-100, 100),
  34. scaleFactor * 0.5f,
  35. (float)GetRandomValue(-100, 100)
  36. );
  37. transformsBillboards[i] = MatrixMultiply(scale, translate);
  38. }
  39. // Setup directional light with shadows
  40. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  41. R3D_SetLightDirection(light, (Vector3){-1, -1, -1});
  42. R3D_SetShadowDepthBias(light, 0.01f);
  43. R3D_EnableShadow(light, 4096);
  44. R3D_SetLightActive(light, true);
  45. R3D_SetLightRange(light, 32.0f);
  46. // Setup camera
  47. Camera3D camera = {
  48. .position = {0, 5, 0},
  49. .target = {0, 5, -1},
  50. .up = {0, 1, 0},
  51. .fovy = 60
  52. };
  53. // Capture mouse
  54. DisableCursor();
  55. // Main loop
  56. while (!WindowShouldClose())
  57. {
  58. UpdateCamera(&camera, CAMERA_FREE);
  59. BeginDrawing();
  60. ClearBackground(RAYWHITE);
  61. R3D_Begin(camera);
  62. R3D_DrawMesh(&meshGround, &matGround, MatrixIdentity());
  63. R3D_DrawMeshInstanced(&meshBillboard, &matBillboard, transformsBillboards, 64);
  64. R3D_End();
  65. EndDrawing();
  66. }
  67. // Cleanup
  68. R3D_UnloadMaterial(&matBillboard);
  69. R3D_UnloadMesh(&meshBillboard);
  70. R3D_UnloadMesh(&meshGround);
  71. R3D_Close();
  72. CloseWindow();
  73. return 0;
  74. }