lights.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <r3d/r3d.h>
  2. #include <raymath.h>
  3. #include <stdlib.h>
  4. #define GRID_SIZE 100
  5. #define LIGHT_GRID 10
  6. int main(void)
  7. {
  8. // Initialize window
  9. InitWindow(800, 450, "[r3d] - Many lights example");
  10. SetTargetFPS(60);
  11. // Initialize R3D
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. // Set ambient light
  14. R3D_ENVIRONMENT_SET(ambient.color, (Color){10, 10, 10, 255});
  15. // Create plane and sphere meshes
  16. R3D_Mesh plane = R3D_GenMeshPlane(1000, 1000, 1, 1);
  17. R3D_Mesh sphere = R3D_GenMeshSphere(0.35f, 16, 16);
  18. R3D_Material material = R3D_GetDefaultMaterial();
  19. // Allocate transforms for all spheres
  20. Matrix* transforms = (Matrix*)RL_MALLOC(GRID_SIZE * GRID_SIZE * sizeof(Matrix));
  21. if (!transforms) exit(-1);
  22. for (int x = -50; x < 50; x++) {
  23. for (int z = -50; z < 50; z++) {
  24. transforms[(z+50)*GRID_SIZE + (x+50)] = MatrixTranslate((float)x, 0, (float)z);
  25. }
  26. }
  27. // Create lights
  28. R3D_Light lights[LIGHT_GRID * LIGHT_GRID];
  29. for (int x = -5; x < 5; x++) {
  30. for (int z = -5; z < 5; z++) {
  31. int index = (z+5)*LIGHT_GRID + (x+5);
  32. lights[index] = R3D_CreateLight(R3D_LIGHT_OMNI);
  33. R3D_SetLightPosition(lights[index], (Vector3){(float)x*10, 10, (float)z*10});
  34. R3D_SetLightColor(lights[index], ColorFromHSV((float)index/100*360, 1.0f, 1.0f));
  35. R3D_SetLightRange(lights[index], 20.0f);
  36. R3D_SetLightActive(lights[index], true);
  37. }
  38. }
  39. // Setup camera
  40. Camera3D camera = {
  41. .position = {0, 2, 2},
  42. .target = {0, 0, 0},
  43. .up = {0, 1, 0},
  44. .fovy = 60
  45. };
  46. // Main loop
  47. while (!WindowShouldClose())
  48. {
  49. UpdateCamera(&camera, CAMERA_ORBITAL);
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. // Draw scene
  53. R3D_Begin(camera);
  54. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  55. R3D_DrawMeshInstanced(&sphere, &material, transforms, GRID_SIZE*GRID_SIZE);
  56. R3D_End();
  57. // Optionally show lights shapes
  58. if (IsKeyDown(KEY_SPACE)) {
  59. BeginMode3D(camera);
  60. for (int i = 0; i < LIGHT_GRID*LIGHT_GRID; i++)
  61. R3D_DrawLightShape(lights[i]);
  62. EndMode3D();
  63. }
  64. DrawFPS(10, 10);
  65. DrawText("Press SPACE to show the lights", 10, GetScreenHeight()-34, 24, BLACK);
  66. EndDrawing();
  67. }
  68. // Cleanup
  69. RL_FREE(transforms);
  70. R3D_UnloadMesh(&plane);
  71. R3D_UnloadMesh(&sphere);
  72. R3D_Close();
  73. CloseWindow();
  74. return 0;
  75. }