skybox.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "./common.h"
  2. #include "r3d.h"
  3. /* === Resources === */
  4. static R3D_Mesh sphere = { 0 };
  5. static R3D_Skybox skybox = { 0 };
  6. static Camera3D camera = { 0 };
  7. static R3D_Material materials[7 * 7] = { 0 };
  8. /* === Examples === */
  9. const char* Init(void)
  10. {
  11. /* --- Initialize R3D with its internal resolution --- */
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. SetTargetFPS(60);
  14. /* --- Generate a sphere mesh --- */
  15. sphere = R3D_GenMeshSphere(0.5f, 64, 64, true);
  16. /* --- Create a grid of materials with varying metalness and roughness --- */
  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 a skybox --- */
  27. skybox = R3D_LoadSkybox(RESOURCES_PATH "sky/skybox1.png", CUBEMAP_LAYOUT_AUTO_DETECT);
  28. R3D_EnableSkybox(skybox);
  29. /* --- Setup the camera --- */
  30. camera = (Camera3D){
  31. .position = (Vector3) { 0, 0, 5 },
  32. .target = (Vector3) { 0, 0, 0 },
  33. .up = (Vector3) { 0, 1, 0 },
  34. .fovy = 60,
  35. };
  36. /* --- Capture the mouse and, action! --- */
  37. DisableCursor();
  38. return "[r3d] - Skybox example";
  39. }
  40. void Update(float delta)
  41. {
  42. UpdateCamera(&camera, CAMERA_FREE);
  43. }
  44. void Draw(void)
  45. {
  46. R3D_Begin(camera);
  47. for (int x = 0; x < 7; x++) {
  48. for (int y = 0; y < 7; y++) {
  49. R3D_DrawMesh(&sphere, &materials[y * 7 + x], MatrixTranslate(x - 3, y - 3, 0.0f));
  50. }
  51. }
  52. R3D_End();
  53. }
  54. void Close(void)
  55. {
  56. R3D_UnloadMesh(&sphere);
  57. R3D_UnloadSkybox(skybox);
  58. R3D_Close();
  59. }