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.

146 lines
6.4 KiB

пре 4 година
пре 1 година
пре 4 година
пре 4 година
пре 2 година
пре 3 година
пре 2 година
пре 2 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
  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, 180)*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. // Set shader value: ambient light level
  63. int ambientLoc = GetShaderLocation(shader, "ambient");
  64. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
  65. // Create one light
  66. CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);
  67. // NOTE: We are assigning the intancing shader to material.shader
  68. // to be used on mesh drawing with DrawMeshInstanced()
  69. Material matInstances = LoadMaterialDefault();
  70. matInstances.shader = shader;
  71. matInstances.maps[MATERIAL_MAP_DIFFUSE].color = RED;
  72. // Load default material (using raylib intenral default shader) for non-instanced mesh drawing
  73. // WARNING: Default shader enables vertex color attribute BUT GenMeshCube() does not generate vertex colors, so,
  74. // when drawing the color attribute is disabled and a default color value is provided as input for thevertex attribute
  75. Material matDefault = LoadMaterialDefault();
  76. matDefault.maps[MATERIAL_MAP_DIFFUSE].color = BLUE;
  77. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  78. //--------------------------------------------------------------------------------------
  79. // Main game loop
  80. while (!WindowShouldClose()) // Detect window close button or ESC key
  81. {
  82. // Update
  83. //----------------------------------------------------------------------------------
  84. UpdateCamera(&camera, CAMERA_ORBITAL);
  85. // Update the light shader with the camera view position
  86. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  87. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  88. //----------------------------------------------------------------------------------
  89. // Draw
  90. //----------------------------------------------------------------------------------
  91. BeginDrawing();
  92. ClearBackground(RAYWHITE);
  93. BeginMode3D(camera);
  94. // Draw cube mesh with default material (BLUE)
  95. DrawMesh(cube, matDefault, MatrixTranslate(-10.0f, 0.0f, 0.0f));
  96. // Draw meshes instanced using material containing instancing shader (RED + lighting),
  97. // transforms[] for the instances should be provided, they are dynamically
  98. // updated in GPU every frame, so we can animate the different mesh instances
  99. DrawMeshInstanced(cube, matInstances, transforms, MAX_INSTANCES);
  100. // Draw cube mesh with default material (BLUE)
  101. DrawMesh(cube, matDefault, MatrixTranslate(10.0f, 0.0f, 0.0f));
  102. EndMode3D();
  103. DrawFPS(10, 10);
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }
  107. // De-Initialization
  108. //--------------------------------------------------------------------------------------
  109. RL_FREE(transforms); // Free transforms
  110. CloseWindow(); // Close window and OpenGL context
  111. //--------------------------------------------------------------------------------------
  112. return 0;
  113. }