skybox.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <r3d/r3d.h>
  2. #include <raymath.h>
  3. #ifndef RESOURCES_PATH
  4. # define RESOURCES_PATH "./"
  5. #endif
  6. int main(void)
  7. {
  8. // Initialize window
  9. InitWindow(800, 450, "[r3d] - Skybox example");
  10. SetTargetFPS(60);
  11. // Initialize R3D
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. // Create sphere mesh
  14. R3D_Mesh sphere = R3D_GenMeshSphere(0.5f, 64, 64);
  15. // Create grid of materials (metalness and roughness)
  16. R3D_Material materials[7 * 7];
  17. for (int x = 0; x < 7; x++) {
  18. for (int y = 0; y < 7; y++) {
  19. int i = y * 7 + x;
  20. materials[i] = R3D_GetDefaultMaterial();
  21. materials[i].orm.metalness = (float)x / 7;
  22. materials[i].orm.roughness = (float)y / 7;
  23. materials[i].albedo.color = ColorFromHSV(((float)x / 7) * 360, 1, 1);
  24. }
  25. }
  26. // Load and enable skybox
  27. R3D_Skybox skybox = R3D_LoadSkybox(RESOURCES_PATH "sky/skybox1.png", CUBEMAP_LAYOUT_AUTO_DETECT);
  28. R3D_ENVIRONMENT_SET(background.sky, skybox);
  29. // Setup camera
  30. Camera3D camera = {
  31. .position = {0, 0, 5},
  32. .target = {0, 0, 0},
  33. .up = {0, 1, 0},
  34. .fovy = 60
  35. };
  36. // Capture mouse
  37. DisableCursor();
  38. // Main loop
  39. while (!WindowShouldClose())
  40. {
  41. UpdateCamera(&camera, CAMERA_FREE);
  42. BeginDrawing();
  43. ClearBackground(RAYWHITE);
  44. // Draw sphere grid
  45. R3D_Begin(camera);
  46. for (int x = 0; x < 7; x++) {
  47. for (int y = 0; y < 7; y++) {
  48. R3D_DrawMesh(&sphere, &materials[y * 7 + x], MatrixTranslate((float)x - 3, (float)y - 3, 0.0f));
  49. }
  50. }
  51. R3D_End();
  52. EndDrawing();
  53. }
  54. // Cleanup
  55. R3D_UnloadMesh(&sphere);
  56. R3D_UnloadSkybox(skybox);
  57. R3D_Close();
  58. CloseWindow();
  59. return 0;
  60. }