lights.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /* --- Initialize R3D with its internal resolution --- */
  16. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  17. SetTargetFPS(60);
  18. /* --- Generates a plane and sphere meshes and a default material to render them --- */
  19. plane = R3D_GenMeshPlane(1000, 1000, 1, 1, true);
  20. sphere = R3D_GenMeshSphere(0.35f, 16, 16, true);
  21. material = R3D_GetDefaultMaterial();
  22. /* --- Calculating transformations for all sphere instances --- */
  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, 0, z);
  28. }
  29. }
  30. /* --- Setup 100 omni-directional lights --- */
  31. for (int x = -5; x < 5; x++) {
  32. for (int z = -5; z < 5; z++) {
  33. int index = (z + 5) * 10 + (x + 5);
  34. lights[index] = R3D_CreateLight(R3D_LIGHT_OMNI);
  35. R3D_SetLightPosition(lights[index], (Vector3) { x * 10, 10, z * 10 });
  36. R3D_SetLightColor(lights[index], ColorFromHSV((float)index / 100 * 360, 1.0f, 1.0f));
  37. R3D_SetLightRange(lights[index], 20.0f);
  38. R3D_SetLightActive(lights[index], true);
  39. }
  40. }
  41. /* --- Setup the camera --- */
  42. camera = (Camera3D) {
  43. .position = (Vector3) { 0, 2, 2 },
  44. .target = (Vector3) { 0, 0, 0 },
  45. .up = (Vector3) { 0, 1, 0 },
  46. .fovy = 60,
  47. };
  48. return "[r3d] - Many lights example";
  49. }
  50. void Update(float delta)
  51. {
  52. UpdateCamera(&camera, CAMERA_ORBITAL);
  53. }
  54. void Draw(void)
  55. {
  56. R3D_Begin(camera);
  57. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  58. R3D_DrawMeshInstanced(&sphere, &material, transforms, 100 * 100);
  59. R3D_End();
  60. if (IsKeyDown(KEY_SPACE)) {
  61. BeginMode3D(camera);
  62. for (int i = 0; i < 100; i++) {
  63. R3D_DrawLightShape(lights[i]);
  64. }
  65. EndMode3D();
  66. }
  67. DrawFPS(10, 10);
  68. DrawText("Press SPACE to show the lights", 10, GetScreenHeight() - 34, 24, BLACK);
  69. }
  70. void Close(void)
  71. {
  72. R3D_UnloadMesh(&plane);
  73. R3D_UnloadMesh(&sphere);
  74. R3D_Close();
  75. }