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.

150 lines
6.4 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-2023 @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_RPI, 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 };
  39. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  40. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  41. camera.fovy = 45.0f;
  42. camera.projection = CAMERA_PERSPECTIVE;
  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. // Set an orbital camera mode
  79. SetCameraMode(camera, CAMERA_ORBITAL);
  80. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  81. //--------------------------------------------------------------------------------------
  82. // Main game loop
  83. while (!WindowShouldClose()) // Detect window close button or ESC key
  84. {
  85. // Update
  86. //----------------------------------------------------------------------------------
  87. UpdateCamera(&camera);
  88. // Update the light shader with the camera view position
  89. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  90. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  91. //----------------------------------------------------------------------------------
  92. // Draw
  93. //----------------------------------------------------------------------------------
  94. BeginDrawing();
  95. ClearBackground(RAYWHITE);
  96. BeginMode3D(camera);
  97. // Draw cube mesh with default material (BLUE)
  98. DrawMesh(cube, matDefault, MatrixTranslate(-10.0f, 0.0f, 0.0f));
  99. // Draw meshes instanced using material containing instancing shader (RED + lighting),
  100. // transforms[] for the instances should be provided, they are dynamically
  101. // updated in GPU every frame, so we can animate the different mesh instances
  102. DrawMeshInstanced(cube, matInstances, transforms, MAX_INSTANCES);
  103. // Draw cube mesh with default material (BLUE)
  104. DrawMesh(cube, matDefault, MatrixTranslate(10.0f, 0.0f, 0.0f));
  105. EndMode3D();
  106. DrawFPS(10, 10);
  107. EndDrawing();
  108. //----------------------------------------------------------------------------------
  109. }
  110. // De-Initialization
  111. //--------------------------------------------------------------------------------------
  112. RL_FREE(transforms); // Free transforms
  113. CloseWindow(); // Close window and OpenGL context
  114. //--------------------------------------------------------------------------------------
  115. return 0;
  116. }