directional.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "./common.h"
  2. /* === Resources === */
  3. static Model plane = { 0 };
  4. static Mesh sphere = { 0 };
  5. static Material material = { 0 };
  6. static Camera3D camera = { 0 };
  7. static Matrix* transforms = NULL;
  8. /* === Examples === */
  9. const char* Init(void)
  10. {
  11. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  12. SetTargetFPS(60);
  13. plane = LoadModelFromMesh(GenMeshPlane(1000, 1000, 1, 1));
  14. plane.materials[0].maps[MATERIAL_MAP_OCCLUSION].value = 1;
  15. plane.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 1;
  16. plane.materials[0].maps[MATERIAL_MAP_METALNESS].value = 0;
  17. sphere = GenMeshSphere(0.35f, 16, 16);
  18. material = LoadMaterialDefault();
  19. material.maps[MATERIAL_MAP_OCCLUSION].value = 1;
  20. material.maps[MATERIAL_MAP_ROUGHNESS].value = 0.25;
  21. material.maps[MATERIAL_MAP_METALNESS].value = 0.75;
  22. camera = (Camera3D){
  23. .position = (Vector3) { 0, 2, 2 },
  24. .target = (Vector3) { 0, 0, 0 },
  25. .up = (Vector3) { 0, 1, 0 },
  26. .fovy = 60,
  27. };
  28. transforms = RL_MALLOC(100 * 100 * sizeof(Matrix));
  29. for (int x = -50; x < 50; x++) {
  30. for (int z = -50; z < 50; z++) {
  31. int index = (z + 50) * 100 + (x + 50);
  32. transforms[index] = MatrixTranslate(x * 2, 0, z * 2);
  33. }
  34. }
  35. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  36. {
  37. R3D_SetLightDirection(light, (Vector3) { 0, -1, -1 });
  38. R3D_SetShadowUpdateMode(light, R3D_SHADOW_UPDATE_MANUAL);
  39. R3D_SetShadowBias(light, 0.005f);
  40. R3D_EnableShadow(light, 4096);
  41. R3D_SetLightActive(light, true);
  42. }
  43. DisableCursor();
  44. return "[r3d] - directional example";
  45. }
  46. void Update(float delta)
  47. {
  48. UpdateCamera(&camera, CAMERA_FREE);
  49. }
  50. void Draw(void)
  51. {
  52. R3D_Begin(camera);
  53. R3D_DrawModel(plane, (Vector3) { 0, -0.5f, 0 }, 1.0f);
  54. R3D_DrawMeshInstanced(sphere, material, transforms, 100 * 100);
  55. R3D_End();
  56. DrawFPS(10, 10);
  57. }
  58. void Close(void)
  59. {
  60. UnloadModel(plane);
  61. UnloadMesh(sphere);
  62. UnloadMaterial(material);
  63. R3D_Close();
  64. }