sun.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <r3d/r3d.h>
  2. #include <raymath.h>
  3. #define X_INSTANCES 50
  4. #define Y_INSTANCES 50
  5. #define INSTANCE_COUNT (X_INSTANCES * Y_INSTANCES)
  6. int main(void)
  7. {
  8. // Initialize window
  9. InitWindow(800, 450, "[r3d] - Sun example");
  10. SetTargetFPS(60);
  11. // Initialize R3D
  12. R3D_Init(GetScreenWidth(), GetScreenHeight());
  13. R3D_SetAntiAliasing(R3D_ANTI_ALIASING_FXAA);
  14. // Create meshes and material
  15. R3D_Mesh plane = R3D_GenMeshPlane(1000, 1000, 1, 1);
  16. R3D_Mesh sphere = R3D_GenMeshSphere(0.35f, 16, 32);
  17. R3D_Material material = R3D_GetDefaultMaterial();
  18. // Create transforms for instanced spheres
  19. R3D_InstanceBuffer instances = R3D_LoadInstanceBuffer(INSTANCE_COUNT, R3D_INSTANCE_POSITION);
  20. Vector3* positions = R3D_MapInstances(instances, R3D_INSTANCE_POSITION);
  21. float spacing = 1.5f;
  22. float offsetX = (X_INSTANCES * spacing) / 2.0f;
  23. float offsetZ = (Y_INSTANCES * spacing) / 2.0f;
  24. int idx = 0;
  25. for (int x = 0; x < X_INSTANCES; x++) {
  26. for (int y = 0; y < Y_INSTANCES; y++) {
  27. positions[idx] = (Vector3) {x * spacing - offsetX, 0, y * spacing - offsetZ};
  28. idx++;
  29. }
  30. }
  31. R3D_UnmapInstances(instances, R3D_INSTANCE_POSITION);
  32. // Setup environment
  33. R3D_Cubemap skybox = R3D_GenCubemapSky(1024, R3D_CUBEMAP_SKY_BASE);
  34. R3D_ENVIRONMENT_SET(background.sky, skybox);
  35. R3D_AmbientMap ambientMap = R3D_GenAmbientMap(skybox, R3D_AMBIENT_ILLUMINATION | R3D_AMBIENT_REFLECTION);
  36. R3D_ENVIRONMENT_SET(ambient.map, ambientMap);;
  37. // Create directional light with shadows
  38. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  39. R3D_SetLightDirection(light, (Vector3){-1, -1, -1});
  40. R3D_SetLightActive(light, true);
  41. R3D_SetLightRange(light, 16.0f);
  42. R3D_SetShadowSoftness(light, 2.0f);
  43. R3D_SetShadowDepthBias(light, 0.01f);
  44. R3D_EnableShadow(light);
  45. // Setup camera
  46. Camera3D camera = {
  47. .position = {0, 1, 0},
  48. .target = {1, 1.25f, 1},
  49. .up = {0, 1, 0},
  50. .fovy = 60
  51. };
  52. // Capture mouse
  53. DisableCursor();
  54. // Main loop
  55. while (!WindowShouldClose())
  56. {
  57. UpdateCamera(&camera, CAMERA_FREE);
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. R3D_Begin(camera);
  61. R3D_DrawMesh(plane, material, (Vector3) {0, -0.5f, 0}, 1.0f);
  62. R3D_DrawMeshInstanced(sphere, material, instances, INSTANCE_COUNT);
  63. R3D_End();
  64. EndDrawing();
  65. }
  66. // Cleanup
  67. R3D_UnloadInstanceBuffer(instances);
  68. R3D_UnloadMaterial(material);
  69. R3D_UnloadMesh(sphere);
  70. R3D_UnloadMesh(plane);
  71. R3D_Close();
  72. CloseWindow();
  73. return 0;
  74. }