directional.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "./common.h"
  2. /* === Resources === */
  3. static R3D_Mesh plane = { 0 };
  4. static R3D_Mesh sphere = { 0 };
  5. static R3D_Material material = { 0 };
  6. static Camera3D camera = { 0 };
  7. static Matrix* transforms = NULL;
  8. /* === Examples === */
  9. const char* Init(void)
  10. {
  11. /* --- Initialize R3D with its internal resolution --- */
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. SetTargetFPS(60);
  14. /* --- Generates a plane and sphere meshes and a default material to render them --- */
  15. plane = R3D_GenMeshPlane(1000, 1000, 1, 1);
  16. sphere = R3D_GenMeshSphere(0.35f, 16, 16);
  17. material = R3D_GetDefaultMaterial();
  18. /* --- Calculation of transformation matrices for all spheres instances --- */
  19. transforms = RL_MALLOC(100 * 100 * sizeof(Matrix));
  20. if (transforms == NULL) {
  21. TraceLog(LOG_FATAL, "EXAMPLE: Failed to allocate transforms buffer");
  22. exit(-1);
  23. }
  24. for (int x = -50; x < 50; x++) {
  25. for (int z = -50; z < 50; z++) {
  26. int index = (z + 50) * 100 + (x + 50);
  27. transforms[index] = MatrixTranslate((float)x * 2, 0, (float)z * 2);
  28. }
  29. }
  30. /* --- Setup the scene lighting --- */
  31. R3D_SetAmbientColor((Color) { 10, 10, 10, 255 });
  32. R3D_SetSceneBounds(
  33. (BoundingBox) {
  34. .min = { -100, -1, -100 },
  35. .max = { +100, +1, +100 }
  36. }
  37. );
  38. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  39. {
  40. R3D_SetLightDirection(light, (Vector3) { 0, -1, -1 });
  41. R3D_SetShadowUpdateMode(light, R3D_SHADOW_UPDATE_MANUAL);
  42. R3D_SetLightActive(light, true);
  43. R3D_EnableShadow(light, 4096);
  44. R3D_SetShadowDepthBias(light, 0.01f);
  45. R3D_SetShadowSoftness(light, 2.0f);
  46. }
  47. /* --- Setup the camera --- */
  48. camera = (Camera3D){
  49. .position = (Vector3) { 0, 2, 2 },
  50. .target = (Vector3) { 0, 0, 0 },
  51. .up = (Vector3) { 0, 1, 0 },
  52. .fovy = 60,
  53. };
  54. /* --- Capture the mouse and play! --- */
  55. DisableCursor();
  56. return "[r3d] - Directional light example";
  57. }
  58. void Update(float delta)
  59. {
  60. UpdateCamera(&camera, CAMERA_FREE);
  61. }
  62. void Draw(void)
  63. {
  64. R3D_Begin(camera);
  65. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  66. R3D_DrawMeshInstanced(&sphere, &material, transforms, 100 * 100);
  67. R3D_End();
  68. DrawFPS(10, 10);
  69. }
  70. void Close(void)
  71. {
  72. R3D_UnloadMesh(&plane);
  73. R3D_UnloadMesh(&sphere);
  74. R3D_UnloadMaterial(&material);
  75. R3D_Close();
  76. }