directional.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "./common.h"
  2. #include "r3d.h"
  3. /* === Resources === */
  4. static R3D_Mesh plane = { 0 };
  5. static R3D_Mesh sphere = { 0 };
  6. static R3D_Material material = { 0 };
  7. static Camera3D camera = { 0 };
  8. static Matrix* transforms = NULL;
  9. /* === Examples === */
  10. const char* Init(void)
  11. {
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. SetTargetFPS(60);
  14. plane = R3D_GenMeshPlane(1000, 1000, 1, 1, true);
  15. sphere = R3D_GenMeshSphere(0.35f, 16, 16, true);
  16. material = R3D_GetDefaultMaterial();
  17. camera = (Camera3D){
  18. .position = (Vector3) { 0, 2, 2 },
  19. .target = (Vector3) { 0, 0, 0 },
  20. .up = (Vector3) { 0, 1, 0 },
  21. .fovy = 60,
  22. };
  23. transforms = RL_MALLOC(100 * 100 * sizeof(Matrix));
  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(x * 2, 0, z * 2);
  28. }
  29. }
  30. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  31. {
  32. R3D_SetLightDirection(light, (Vector3) { 0, -1, -1 });
  33. R3D_SetShadowUpdateMode(light, R3D_SHADOW_UPDATE_MANUAL);
  34. R3D_SetShadowBias(light, 0.005f);
  35. R3D_EnableShadow(light, 4096);
  36. R3D_SetLightActive(light, true);
  37. }
  38. DisableCursor();
  39. return "[r3d] - Directional light example";
  40. }
  41. void Update(float delta)
  42. {
  43. UpdateCamera(&camera, CAMERA_FREE);
  44. }
  45. void Draw(void)
  46. {
  47. R3D_Begin(camera);
  48. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  49. R3D_DrawMeshInstanced(&sphere, &material, transforms, 100 * 100);
  50. R3D_End();
  51. DrawFPS(10, 10);
  52. }
  53. void Close(void)
  54. {
  55. R3D_UnloadMesh(&plane);
  56. R3D_UnloadMesh(&sphere);
  57. R3D_UnloadMaterial(&material);
  58. R3D_Close();
  59. }