shaders_rlgl_mesh_instanced.c 9.3 KB

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