directional.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <r3d/r3d.h>
  2. #include <raymath.h>
  3. #include <stdlib.h>
  4. int main(void)
  5. {
  6. // Initialize window
  7. InitWindow(800, 450, "[r3d] - Directional light example");
  8. SetTargetFPS(60);
  9. // Initialize R3D
  10. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  11. // Create meshes and material
  12. R3D_Mesh plane = R3D_GenMeshPlane(1000, 1000, 1, 1);
  13. R3D_Mesh sphere = R3D_GenMeshSphere(0.35f, 24, 16);
  14. R3D_Material material = R3D_GetDefaultMaterial();
  15. // Create transforms for instanced spheres
  16. int count = 100;
  17. Matrix* transforms = RL_MALLOC(count * count * sizeof(Matrix));
  18. if (!transforms) exit(-1);
  19. for (int x = -50; x < 50; x++) {
  20. for (int z = -50; z < 50; z++) {
  21. transforms[(z+50)*count + (x+50)] = MatrixTranslate((float)x*2, 0, (float)z*2);
  22. }
  23. }
  24. // Setup environment
  25. R3D_ENVIRONMENT_SET(ambient.color, (Color){10, 10, 10, 255});
  26. // Create directional light with shadows
  27. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  28. R3D_SetLightDirection(light, (Vector3){0, -1, -1});
  29. R3D_SetLightActive(light, true);
  30. R3D_SetLightRange(light, 16.0f);
  31. R3D_EnableShadow(light, 4096);
  32. R3D_SetShadowDepthBias(light, 0.01f);
  33. R3D_SetShadowSoftness(light, 2.0f);
  34. // Setup camera
  35. Camera3D camera = {
  36. .position = {0, 2, 2},
  37. .target = {0, 0, 0},
  38. .up = {0, 1, 0},
  39. .fovy = 60
  40. };
  41. // Capture mouse
  42. DisableCursor();
  43. // Main loop
  44. while (!WindowShouldClose())
  45. {
  46. UpdateCamera(&camera, CAMERA_FREE);
  47. BeginDrawing();
  48. ClearBackground(RAYWHITE);
  49. R3D_Begin(camera);
  50. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  51. R3D_DrawMeshInstanced(&sphere, &material, transforms, count*count);
  52. R3D_End();
  53. DrawFPS(10, 10);
  54. EndDrawing();
  55. }
  56. // Cleanup
  57. RL_FREE(transforms);
  58. R3D_UnloadMesh(&plane);
  59. R3D_UnloadMesh(&sphere);
  60. R3D_UnloadMaterial(&material);
  61. R3D_Close();
  62. CloseWindow();
  63. return 0;
  64. }