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.

147 lines
6.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Mesh instancing
  4. *
  5. * Example originally created with raylib 3.7, last time updated with raylib 4.2
  6. *
  7. * Example contributed by @seanpringle and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2020-2024 @seanpringle, Max (@moliad) and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. #define RLIGHTS_IMPLEMENTATION
  18. #include "rlights.h"
  19. #include <stdlib.h> // Required for: calloc(), free()
  20. #if defined(PLATFORM_DESKTOP)
  21. #define GLSL_VERSION 330
  22. #else // PLATFORM_ANDROID, PLATFORM_WEB
  23. #define GLSL_VERSION 100
  24. #endif
  25. #define MAX_INSTANCES 10000
  26. //------------------------------------------------------------------------------------
  27. // Program main entry point
  28. //------------------------------------------------------------------------------------
  29. int main(void)
  30. {
  31. // Initialization
  32. //--------------------------------------------------------------------------------------
  33. const int screenWidth = 800;
  34. const int screenHeight = 450;
  35. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing");
  36. // Define the camera to look into our 3d world
  37. Camera camera = { 0 };
  38. camera.position = (Vector3){ -125.0f, 125.0f, -125.0f }; // Camera position
  39. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  40. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  41. camera.fovy = 45.0f; // Camera field-of-view Y
  42. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  43. // Define mesh to be instanced
  44. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  45. // Define transforms to be uploaded to GPU for instances
  46. Matrix *transforms = (Matrix *)RL_CALLOC(MAX_INSTANCES, sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl
  47. // Translate and rotate cubes randomly
  48. for (int i = 0; i < MAX_INSTANCES; i++)
  49. {
  50. Matrix translation = MatrixTranslate((float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50));
  51. Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) });
  52. float angle = (float)GetRandomValue(0, 10)*DEG2RAD;
  53. Matrix rotation = MatrixRotate(axis, angle);
  54. transforms[i] = MatrixMultiply(rotation, translation);
  55. }
  56. // Load lighting shader
  57. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting_instancing.vs", GLSL_VERSION),
  58. TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
  59. // Get shader locations
  60. shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
  61. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  62. shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform");
  63. // Set shader value: ambient light level
  64. int ambientLoc = GetShaderLocation(shader, "ambient");
  65. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
  66. // Create one light
  67. CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);
  68. // NOTE: We are assigning the intancing shader to material.shader
  69. // to be used on mesh drawing with DrawMeshInstanced()
  70. Material matInstances = LoadMaterialDefault();
  71. matInstances.shader = shader;
  72. matInstances.maps[MATERIAL_MAP_DIFFUSE].color = RED;
  73. // Load default material (using raylib intenral default shader) for non-instanced mesh drawing
  74. // WARNING: Default shader enables vertex color attribute BUT GenMeshCube() does not generate vertex colors, so,
  75. // when drawing the color attribute is disabled and a default color value is provided as input for thevertex attribute
  76. Material matDefault = LoadMaterialDefault();
  77. matDefault.maps[MATERIAL_MAP_DIFFUSE].color = BLUE;
  78. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  79. //--------------------------------------------------------------------------------------
  80. // Main game loop
  81. while (!WindowShouldClose()) // Detect window close button or ESC key
  82. {
  83. // Update
  84. //----------------------------------------------------------------------------------
  85. UpdateCamera(&camera, CAMERA_ORBITAL);
  86. // Update the light shader with the camera view position
  87. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  88. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  89. //----------------------------------------------------------------------------------
  90. // Draw
  91. //----------------------------------------------------------------------------------
  92. BeginDrawing();
  93. ClearBackground(RAYWHITE);
  94. BeginMode3D(camera);
  95. // Draw cube mesh with default material (BLUE)
  96. DrawMesh(cube, matDefault, MatrixTranslate(-10.0f, 0.0f, 0.0f));
  97. // Draw meshes instanced using material containing instancing shader (RED + lighting),
  98. // transforms[] for the instances should be provided, they are dynamically
  99. // updated in GPU every frame, so we can animate the different mesh instances
  100. DrawMeshInstanced(cube, matInstances, transforms, MAX_INSTANCES);
  101. // Draw cube mesh with default material (BLUE)
  102. DrawMesh(cube, matDefault, MatrixTranslate(10.0f, 0.0f, 0.0f));
  103. EndMode3D();
  104. DrawFPS(10, 10);
  105. EndDrawing();
  106. //----------------------------------------------------------------------------------
  107. }
  108. // De-Initialization
  109. //--------------------------------------------------------------------------------------
  110. RL_FREE(transforms); // Free transforms
  111. CloseWindow(); // Close window and OpenGL context
  112. //--------------------------------------------------------------------------------------
  113. return 0;
  114. }