directional.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /* --- Initialize R3D with its internal resolution --- */
  13. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  14. SetTargetFPS(60);
  15. /* --- Generates a plane and sphere meshes and a default material to render them --- */
  16. plane = R3D_GenMeshPlane(1000, 1000, 1, 1, true);
  17. sphere = R3D_GenMeshSphere(0.35f, 16, 16, true);
  18. material = R3D_GetDefaultMaterial();
  19. /* --- Calculation of transformation matrices for all spheres instances --- */
  20. transforms = RL_MALLOC(100 * 100 * sizeof(Matrix));
  21. for (int x = -50; x < 50; x++) {
  22. for (int z = -50; z < 50; z++) {
  23. int index = (z + 50) * 100 + (x + 50);
  24. transforms[index] = MatrixTranslate(x * 2, 0, z * 2);
  25. }
  26. }
  27. /* --- Setup the scene lighting --- */
  28. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  29. {
  30. R3D_SetLightDirection(light, (Vector3) { 0, -1, -1 });
  31. R3D_SetShadowUpdateMode(light, R3D_SHADOW_UPDATE_MANUAL);
  32. R3D_SetShadowBias(light, 0.005f);
  33. R3D_EnableShadow(light, 4096);
  34. R3D_SetLightActive(light, true);
  35. }
  36. /* --- Setup the camera --- */
  37. camera = (Camera3D){
  38. .position = (Vector3) { 0, 2, 2 },
  39. .target = (Vector3) { 0, 0, 0 },
  40. .up = (Vector3) { 0, 1, 0 },
  41. .fovy = 60,
  42. };
  43. /* --- Capture the mouse and play! --- */
  44. DisableCursor();
  45. return "[r3d] - Directional light example";
  46. }
  47. void Update(float delta)
  48. {
  49. UpdateCamera(&camera, CAMERA_FREE);
  50. }
  51. void Draw(void)
  52. {
  53. R3D_Begin(camera);
  54. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  55. R3D_DrawMeshInstanced(&sphere, &material, transforms, 100 * 100);
  56. R3D_End();
  57. DrawFPS(10, 10);
  58. }
  59. void Close(void)
  60. {
  61. R3D_UnloadMesh(&plane);
  62. R3D_UnloadMesh(&sphere);
  63. R3D_UnloadMaterial(&material);
  64. R3D_Close();
  65. }