skybox.c 1.8 KB

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