decal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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] - Decal example");
  10. SetTargetFPS(60);
  11. // Initialize R3D
  12. R3D_Init(GetScreenWidth(), GetScreenHeight());
  13. // Create meshes
  14. R3D_Mesh plane = R3D_GenMeshPlane(5.0f, 5.0f, 1, 1);
  15. R3D_Mesh sphere = R3D_GenMeshSphere(0.5f, 64, 64);
  16. R3D_Mesh cylinder = R3D_GenMeshCylinder(0.5f, 0.5f, 1, 64);
  17. R3D_Material material = R3D_GetDefaultMaterial();
  18. material.albedo.color = GRAY;
  19. // Create decal
  20. R3D_Decal decal = R3D_DECAL_BASE;
  21. R3D_SetTextureFilter(TEXTURE_FILTER_BILINEAR);
  22. decal.albedo = R3D_LoadAlbedoMap(RESOURCES_PATH "images/decal.png", WHITE);
  23. decal.normal = R3D_LoadNormalMap(RESOURCES_PATH "images/decal_normal.png", 1.0f);
  24. decal.normalThreshold = 45.0f;
  25. decal.fadeWidth = 20.0f;
  26. // Create data for instanced drawing
  27. R3D_InstanceBuffer instances = R3D_LoadInstanceBuffer(3, R3D_INSTANCE_POSITION);
  28. Vector3* positions = R3D_MapInstances(instances, R3D_INSTANCE_POSITION);
  29. positions[0] = (Vector3){ -1.25f, 0, 1 };
  30. positions[1] = (Vector3){ 0, 0, 1 };
  31. positions[2] = (Vector3){ 1.25f, 0, 1 };
  32. R3D_UnmapInstances(instances, R3D_INSTANCE_POSITION);
  33. // Setup environment
  34. R3D_ENVIRONMENT_SET(ambient.color, (Color){ 10, 10, 10, 255 });
  35. // Create light
  36. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  37. R3D_SetLightDirection(light, (Vector3){ 0.5f, -1, -0.5f });
  38. R3D_SetShadowDepthBias(light, 0.005f);
  39. R3D_EnableShadow(light);
  40. R3D_SetLightActive(light, true);
  41. // Setup camera
  42. Camera3D camera = (Camera3D){
  43. .position = (Vector3){0, 3, 3},
  44. .target = (Vector3){0, 0, 0},
  45. .up = (Vector3){0, 1, 0},
  46. .fovy = 60,
  47. };
  48. // Capture mouse
  49. DisableCursor();
  50. // Main loop
  51. while (!WindowShouldClose())
  52. {
  53. UpdateCamera(&camera, CAMERA_FREE);
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. R3D_Begin(camera);
  57. R3D_DrawMesh(plane, material, (Vector3){ 0, 0, 0 }, 1.0f);
  58. R3D_DrawMesh(sphere, material, (Vector3){ -1, 0.5f, -1 }, 1.0f);
  59. R3D_DrawMeshEx(cylinder, material, (Vector3){ 1, 0.5f, -1 }, QuaternionFromEuler(0, 0, PI/2), Vector3One());
  60. R3D_DrawDecal(decal, (Vector3){ -1, 1, -1 }, 1.0f);
  61. R3D_DrawDecalEx(decal, (Vector3){ 1, 0.5f, -0.5f }, QuaternionFromEuler(PI/2, 0, 0), (Vector3){1.25f, 1.25f, 1.25f});
  62. R3D_DrawDecalInstanced(decal, instances, 3);
  63. R3D_End();
  64. EndDrawing();
  65. }
  66. // Cleanup
  67. R3D_UnloadMesh(plane);
  68. R3D_UnloadMesh(sphere);
  69. R3D_UnloadMesh(cylinder);
  70. R3D_UnloadMaterial(material);
  71. R3D_UnloadDecalMaps(decal);
  72. R3D_Close();
  73. CloseWindow();
  74. return 0;
  75. }