billboards.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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());
  13. R3D_SetTextureFilter(TEXTURE_FILTER_POINT);
  14. // Set background/ambient color
  15. R3D_ENVIRONMENT_SET(background.color, (Color){102, 191, 255, 255});
  16. R3D_ENVIRONMENT_SET(ambient.color, (Color){10, 19, 25, 255});
  17. R3D_ENVIRONMENT_SET(tonemap.mode, R3D_TONEMAP_FILMIC);
  18. // Create ground mesh and material
  19. R3D_Mesh meshGround = R3D_GenMeshPlane(200, 200, 1, 1);
  20. R3D_Material matGround = R3D_GetDefaultMaterial();
  21. matGround.albedo.color = GREEN;
  22. // Create billboard mesh and material
  23. R3D_Mesh meshBillboard = R3D_GenMeshQuad(1.0f, 1.0f, 1, 1, (Vector3){0.0f, 0.0f, 1.0f});
  24. meshBillboard.shadowCastMode = R3D_SHADOW_CAST_ON_DOUBLE_SIDED;
  25. R3D_Material matBillboard = R3D_GetDefaultMaterial();
  26. matBillboard.albedo = R3D_LoadAlbedoMap(RESOURCES_PATH "images/tree.png", WHITE);
  27. matBillboard.billboardMode = R3D_BILLBOARD_Y_AXIS;
  28. // Create transforms for instanced billboards
  29. R3D_InstanceBuffer instances = R3D_LoadInstanceBuffer(64, R3D_INSTANCE_POSITION | R3D_INSTANCE_SCALE);
  30. Vector3* positions = R3D_MapInstances(instances, R3D_INSTANCE_POSITION);
  31. Vector3* scales = R3D_MapInstances(instances, R3D_INSTANCE_SCALE);
  32. for (int i = 0; i < 64; i++) {
  33. float scaleFactor = GetRandomValue(25, 50) / 10.0f;
  34. scales[i] = (Vector3) {scaleFactor, scaleFactor, 1.0f};
  35. positions[i] = (Vector3) {
  36. (float)GetRandomValue(-100, 100),
  37. scaleFactor * 0.5f,
  38. (float)GetRandomValue(-100, 100)
  39. };
  40. }
  41. R3D_UnmapInstances(instances, R3D_INSTANCE_POSITION | R3D_INSTANCE_SCALE);
  42. // Setup directional light with shadows
  43. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  44. R3D_SetLightDirection(light, (Vector3){-1, -1, -1});
  45. R3D_SetShadowDepthBias(light, 0.01f);
  46. R3D_EnableShadow(light);
  47. R3D_SetLightActive(light, true);
  48. R3D_SetLightRange(light, 32.0f);
  49. // Setup camera
  50. Camera3D camera = {
  51. .position = {0, 5, 0},
  52. .target = {0, 5, -1},
  53. .up = {0, 1, 0},
  54. .fovy = 60
  55. };
  56. // Capture mouse
  57. DisableCursor();
  58. // Main loop
  59. while (!WindowShouldClose())
  60. {
  61. UpdateCamera(&camera, CAMERA_FREE);
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. R3D_Begin(camera);
  65. R3D_DrawMesh(meshGround, matGround, Vector3Zero(), 1.0f);
  66. R3D_DrawMeshInstanced(meshBillboard, matBillboard, instances, 64);
  67. R3D_End();
  68. EndDrawing();
  69. }
  70. // Cleanup
  71. R3D_UnloadMaterial(matBillboard);
  72. R3D_UnloadMesh(meshBillboard);
  73. R3D_UnloadMesh(meshGround);
  74. R3D_Close();
  75. CloseWindow();
  76. return 0;
  77. }