animation.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "./common.h"
  2. /* === Resources === */
  3. static R3D_Mesh plane = { 0 };
  4. static R3D_Material planeMat = { 0 };
  5. static R3D_Model dancer = { 0 };
  6. static Matrix dancerInstances[2*2] = { 0 };
  7. static R3D_AnimationLib dancerAnims = { 0 };
  8. static R3D_AnimationPlayer* dancerPlayer = NULL;
  9. static Camera3D camera = { 0 };
  10. static R3D_Light lights[2] = { 0 };
  11. /* === Example === */
  12. const char* Init(void)
  13. {
  14. /* --- Initialize R3D with FXAA and disable frustum culling --- */
  15. R3D_Init(GetScreenWidth(), GetScreenHeight(), R3D_FLAG_FXAA | R3D_FLAG_NO_FRUSTUM_CULLING);
  16. /* --- Set the application frame rate --- */
  17. SetTargetFPS(60);
  18. /* --- Enable post-processing effects --- */
  19. R3D_SetSSAO(true);
  20. R3D_SetBloomIntensity(0.03f);
  21. R3D_SetBloomMode(R3D_BLOOM_ADDITIVE);
  22. R3D_SetTonemapMode(R3D_TONEMAP_ACES);
  23. /* --- Set background and ambient lighting colors --- */
  24. R3D_SetBackgroundColor(BLACK);
  25. R3D_SetAmbientColor((Color) { 7, 7, 7, 255 });
  26. /* --- Generate a plane to serve as the ground and setup its material --- */
  27. plane = R3D_GenMeshPlane(32, 32, 1, 1);
  28. planeMat = R3D_GetDefaultMaterial();
  29. planeMat.orm.roughness = 0.5f;
  30. planeMat.orm.metalness = 0.5f;
  31. planeMat.uvScale.x = 64.0f;
  32. planeMat.uvScale.y = 64.0f;
  33. Image checked = GenImageChecked(2, 2, 1, 1, (Color) { 20, 20, 20, 255 }, WHITE);
  34. planeMat.albedo.texture = LoadTextureFromImage(checked);
  35. UnloadImage(checked);
  36. SetTextureWrap(planeMat.albedo.texture, TEXTURE_WRAP_REPEAT);
  37. /* --- Load the 3D model and its default material --- */
  38. dancer = R3D_LoadModel(RESOURCES_PATH "dancer.glb");
  39. /* --- Create instance matrices for multiple model copies --- */
  40. for (int z = 0; z < 2; z++) {
  41. for (int x = 0; x < 2; x++) {
  42. dancerInstances[z * 2 + x] = MatrixTranslate((float)x - 0.5f, 0, (float)z - 0.5f);
  43. }
  44. }
  45. /* --- Load model animations --- */
  46. dancerAnims = R3D_LoadAnimationLib(RESOURCES_PATH "dancer.glb");
  47. dancer.player = R3D_LoadAnimationPlayer(&dancer.skeleton, &dancerAnims);
  48. dancer.player->states[0].weight = 1.0f;
  49. dancer.player->states[0].loop = true;
  50. /* --- Setup scene lights with shadows --- */
  51. lights[0] = R3D_CreateLight(R3D_LIGHT_OMNI);
  52. R3D_SetLightPosition(lights[0], (Vector3) { -10.0f, 25.0f, 0.0f });
  53. R3D_EnableShadow(lights[0], 4096);
  54. R3D_SetLightActive(lights[0], true);
  55. lights[1] = R3D_CreateLight(R3D_LIGHT_OMNI);
  56. R3D_SetLightPosition(lights[1], (Vector3) { +10.0f, 25.0f, 0.0f });
  57. R3D_EnableShadow(lights[1], 4096);
  58. R3D_SetLightActive(lights[1], true);
  59. /* --- Setup the camera --- */
  60. camera = (Camera3D) {
  61. .position = (Vector3) { 0, 2.0f, 3.5f },
  62. .target = (Vector3) { 0, 1.0f, 1.5f },
  63. .up = (Vector3) { 0, 1, 0 },
  64. .fovy = 60,
  65. };
  66. /* --- Capture the mouse and let's go! --- */
  67. DisableCursor();
  68. return "[r3d] - Animation example";
  69. }
  70. void Update(float delta)
  71. {
  72. UpdateCamera(&camera, CAMERA_FREE);
  73. R3D_UpdateAnimationPlayer(dancer.player, delta);
  74. R3D_SetLightColor(lights[0], ColorFromHSV(90.0f * (float)GetTime() + 90.0f, 1.0f, 1.0f));
  75. R3D_SetLightColor(lights[1], ColorFromHSV(90.0f * (float)GetTime() - 90.0f, 1.0f, 1.0f));
  76. }
  77. void Draw(void)
  78. {
  79. static int frame = 0;
  80. R3D_Begin(camera);
  81. R3D_DrawMesh(&plane, &planeMat, MatrixIdentity());
  82. R3D_DrawModel(&dancer, (Vector3) { 0, 0, 1.5f }, 1.0f);
  83. R3D_DrawModelInstanced(&dancer, dancerInstances, 2*2);
  84. R3D_End();
  85. DrawCredits("Model made by zhuoyi0904");
  86. }
  87. void Close(void)
  88. {
  89. R3D_UnloadMesh(&plane);
  90. R3D_UnloadModel(&dancer, true);
  91. R3D_UnloadMaterial(&planeMat);
  92. R3D_Close();
  93. }