lights.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "./common.h"
  2. #include "r3d.h"
  3. #include "raylib.h"
  4. #include "raymath.h"
  5. /* === Resources === */
  6. static R3D_Mesh plane = { 0 };
  7. static R3D_Mesh sphere = { 0 };
  8. static R3D_Material material = { 0 };
  9. static Camera3D camera = { 0 };
  10. static Matrix* transforms = NULL;
  11. static R3D_Light lights[100] = { 0 };
  12. /* === Example === */
  13. const char* Init(void)
  14. {
  15. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  16. SetTargetFPS(60);
  17. plane = R3D_GenMeshPlane(1000, 1000, 1, 1, true);
  18. sphere = R3D_GenMeshSphere(0.35f, 16, 16, true);
  19. material = R3D_GetDefaultMaterial();
  20. camera = (Camera3D) {
  21. .position = (Vector3) { 0, 2, 2 },
  22. .target = (Vector3) { 0, 0, 0 },
  23. .up = (Vector3) { 0, 1, 0 },
  24. .fovy = 60,
  25. };
  26. transforms = RL_MALLOC(100 * 100 * sizeof(Matrix));
  27. for (int x = -50; x < 50; x++) {
  28. for (int z = -50; z < 50; z++) {
  29. int index = (z + 50) * 100 + (x + 50);
  30. transforms[index] = MatrixTranslate(x, 0, z);
  31. }
  32. }
  33. for (int x = -5; x < 5; x++) {
  34. for (int z = -5; z < 5; z++) {
  35. int index = (z + 5) * 10 + (x + 5);
  36. lights[index] = R3D_CreateLight(R3D_LIGHT_OMNI);
  37. R3D_SetLightPosition(lights[index], (Vector3) { x * 10, 10, z * 10 });
  38. R3D_SetLightColor(lights[index], ColorFromHSV((float)index / 100 * 360, 1.0f, 1.0f));
  39. R3D_SetLightRange(lights[index], 20.0f);
  40. R3D_SetLightActive(lights[index], true);
  41. }
  42. }
  43. return "[r3d] - Many lights example";
  44. }
  45. void Update(float delta)
  46. {
  47. UpdateCamera(&camera, CAMERA_ORBITAL);
  48. }
  49. void Draw(void)
  50. {
  51. R3D_Begin(camera);
  52. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  53. R3D_DrawMeshInstanced(&sphere, &material, transforms, 100 * 100);
  54. R3D_End();
  55. if (IsKeyDown(KEY_SPACE)) {
  56. BeginMode3D(camera);
  57. for (int i = 0; i < 100; i++) {
  58. R3D_DrawLightShape(lights[i]);
  59. }
  60. EndMode3D();
  61. }
  62. DrawFPS(10, 10);
  63. DrawText("Press SPACE to show the lights", 10, GetScreenHeight() - 34, 24, BLACK);
  64. }
  65. void Close(void)
  66. {
  67. R3D_UnloadMesh(&plane);
  68. R3D_UnloadMesh(&sphere);
  69. R3D_Close();
  70. }