You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
9.8 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - mesh instancing
  4. *
  5. * This example has been created using raylib 3.7 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by @seanpringle and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2020-2021 @seanpringle, Max (@moliad) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include "raymath.h"
  15. #define RLIGHTS_IMPLEMENTATION
  16. #include "rlights.h"
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #if defined(PLATFORM_DESKTOP)
  20. #define GLSL_VERSION 330
  21. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  22. #define GLSL_VERSION 100
  23. #endif
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. const int fps = 60;
  34. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  35. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing");
  36. int speed = 30; // Speed of jump animation
  37. int groups = 2; // Count of separate groups jumping around
  38. float amp = 10; // Maximum amplitude of jump
  39. float variance = 0.8f; // Global variance in jump height
  40. float loop = 0.0f; // Individual cube's computed loop timer
  41. float x = 0.0f, y = 0.0f, z = 0.0f; // Used for various 3D coordinate & vector ops
  42. // Define the camera to look into our 3d world
  43. Camera camera = { 0 };
  44. camera.position = (Vector3){ -125.0f, 125.0f, -125.0f };
  45. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  46. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  47. camera.fovy = 45.0f;
  48. camera.projection = CAMERA_PERSPECTIVE;
  49. const int instances = 10000; // Number of instances to display
  50. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  51. Matrix *rotations = RL_MALLOC(instances*sizeof(Matrix)); // Rotation state of instances
  52. Matrix *rotationsInc = RL_MALLOC(instances*sizeof(Matrix)); // Per-frame rotation animation of instances
  53. Matrix *translations = RL_MALLOC(instances*sizeof(Matrix)); // Locations of instances
  54. // Scatter random cubes around
  55. for (int i = 0; i < instances; i++)
  56. {
  57. x = GetRandomValue(-50, 50);
  58. y = GetRandomValue(-50, 50);
  59. z = GetRandomValue(-50, 50);
  60. translations[i] = MatrixTranslate(x, y, z);
  61. x = GetRandomValue(0, 360);
  62. y = GetRandomValue(0, 360);
  63. z = GetRandomValue(0, 360);
  64. Vector3 axis = Vector3Normalize((Vector3){ x, y, z });
  65. float angle = (float)GetRandomValue(0, 10)*DEG2RAD;
  66. rotationsInc[i] = MatrixRotate(axis, angle);
  67. rotations[i] = MatrixIdentity();
  68. }
  69. Matrix *transforms = RL_MALLOC(instances*sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl
  70. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/base_lighting_instanced.vs", GLSL_VERSION),
  71. TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
  72. // Get some shader loactions
  73. shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
  74. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  75. shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform");
  76. // Ambient light level
  77. int ambientLoc = GetShaderLocation(shader, "ambient");
  78. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
  79. CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);
  80. // NOTE: We are assigning the intancing shader to material.shader
  81. // to be used on mesh drawing with DrawMeshInstanced()
  82. Material material = LoadMaterialDefault();
  83. material.shader = shader;
  84. material.maps[MATERIAL_MAP_DIFFUSE].color = RED;
  85. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  86. int textPositionY = 300;
  87. int framesCounter = 0; // Simple frames counter to manage animation
  88. SetTargetFPS(fps); // Set our game to run at 60 frames-per-second
  89. //--------------------------------------------------------------------------------------
  90. // Main game loop
  91. while (!WindowShouldClose()) // Detect window close button or ESC key
  92. {
  93. // Update
  94. //----------------------------------------------------------------------------------
  95. textPositionY = 300;
  96. framesCounter++;
  97. if (IsKeyDown(KEY_UP)) amp += 0.5f;
  98. if (IsKeyDown(KEY_DOWN)) amp = (amp <= 1)? 1.0f : (amp - 1.0f);
  99. if (IsKeyDown(KEY_LEFT)) variance = (variance <= 0.0f)? 0.0f : (variance - 0.01f);
  100. if (IsKeyDown(KEY_RIGHT)) variance = (variance >= 1.0f)? 1.0f : (variance + 0.01f);
  101. if (IsKeyDown(KEY_ONE)) groups = 1;
  102. if (IsKeyDown(KEY_TWO)) groups = 2;
  103. if (IsKeyDown(KEY_THREE)) groups = 3;
  104. if (IsKeyDown(KEY_FOUR)) groups = 4;
  105. if (IsKeyDown(KEY_FIVE)) groups = 5;
  106. if (IsKeyDown(KEY_SIX)) groups = 6;
  107. if (IsKeyDown(KEY_SEVEN)) groups = 7;
  108. if (IsKeyDown(KEY_EIGHT)) groups = 8;
  109. if (IsKeyDown(KEY_NINE)) groups = 9;
  110. if (IsKeyDown(KEY_W)) { groups = 7; amp = 25; speed = 18; variance = 0.70f; }
  111. if (IsKeyDown(KEY_EQUAL)) speed = (speed <= (fps*0.25f))? (fps*0.25f) : (speed*0.95f);
  112. if (IsKeyDown(KEY_KP_ADD)) speed = (speed <= (fps*0.25f))? (fps*0.25f) : (speed*0.95f);
  113. if (IsKeyDown(KEY_MINUS)) speed = fmaxf(speed*1.02f, speed + 1);
  114. if (IsKeyDown(KEY_KP_SUBTRACT)) speed = fmaxf(speed*1.02f, speed + 1);
  115. // Update the light shader with the camera view position
  116. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  117. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  118. // Apply per-instance transformations
  119. for (int i = 0; i < instances; i++)
  120. {
  121. rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]);
  122. transforms[i] = MatrixMultiply(rotations[i], translations[i]);
  123. // Get the animation cycle's framesCounter for this instance
  124. loop = (float)((framesCounter + (int)(((float)(i%groups)/groups)*speed))%speed)/speed;
  125. // Calculate the y according to loop cycle
  126. y = (sinf(loop*PI*2))*amp*((1 - variance) + (variance*(float)(i%(groups*10))/(groups*10)));
  127. // Clamp to floor
  128. y = (y < 0)? 0.0f : y;
  129. transforms[i] = MatrixMultiply(transforms[i], MatrixTranslate(0.0f, y, 0.0f));
  130. }
  131. UpdateCamera(&camera);
  132. //----------------------------------------------------------------------------------
  133. // Draw
  134. //----------------------------------------------------------------------------------
  135. BeginDrawing();
  136. ClearBackground(RAYWHITE);
  137. BeginMode3D(camera);
  138. //DrawMesh(cube, material, MatrixIdentity());
  139. DrawMeshInstanced(cube, material, transforms, instances);
  140. EndMode3D();
  141. DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, MAROON);
  142. DrawText("PRESS KEYS:", 10, textPositionY, 20, BLACK);
  143. DrawText("1 - 9", 10, textPositionY += 25, 10, BLACK);
  144. DrawText(": Number of groups", 50, textPositionY , 10, BLACK);
  145. DrawText(TextFormat(": %d", groups), 160, textPositionY , 10, BLACK);
  146. DrawText("UP", 10, textPositionY += 15, 10, BLACK);
  147. DrawText(": increase amplitude", 50, textPositionY, 10, BLACK);
  148. DrawText(TextFormat(": %.2f", amp), 160, textPositionY , 10, BLACK);
  149. DrawText("DOWN", 10, textPositionY += 15, 10, BLACK);
  150. DrawText(": decrease amplitude", 50, textPositionY, 10, BLACK);
  151. DrawText("LEFT", 10, textPositionY += 15, 10, BLACK);
  152. DrawText(": decrease variance", 50, textPositionY, 10, BLACK);
  153. DrawText(TextFormat(": %.2f", variance), 160, textPositionY , 10, BLACK);
  154. DrawText("RIGHT", 10, textPositionY += 15, 10, BLACK);
  155. DrawText(": increase variance", 50, textPositionY, 10, BLACK);
  156. DrawText("+/=", 10, textPositionY += 15, 10, BLACK);
  157. DrawText(": increase speed", 50, textPositionY, 10, BLACK);
  158. DrawText(TextFormat(": %d = %f loops/sec", speed, ((float)fps / speed)), 160, textPositionY , 10, BLACK);
  159. DrawText("-", 10, textPositionY += 15, 10, BLACK);
  160. DrawText(": decrease speed", 50, textPositionY, 10, BLACK);
  161. DrawText("W", 10, textPositionY += 15, 10, BLACK);
  162. DrawText(": Wild setup!", 50, textPositionY, 10, BLACK);
  163. DrawFPS(10, 10);
  164. EndDrawing();
  165. //----------------------------------------------------------------------------------
  166. }
  167. // De-Initialization
  168. //--------------------------------------------------------------------------------------
  169. CloseWindow(); // Close window and OpenGL context
  170. //--------------------------------------------------------------------------------------
  171. return 0;
  172. }