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.

121 lines
4.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Vertex displacement
  4. *
  5. * Example complexity rating: [] 3/4
  6. *
  7. * Example originally created with raylib 5.0, last time updated with raylib 4.5
  8. *
  9. * Example contributed by Alex ZH (@ZzzhHe) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2023-2025 Alex ZH (@ZzzhHe)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "rlgl.h"
  19. #define RLIGHTS_IMPLEMENTATION
  20. #include "rlights.h"
  21. #if defined(PLATFORM_DESKTOP)
  22. #define GLSL_VERSION 330
  23. #else // PLATFORM_ANDROID, PLATFORM_WEB
  24. #define GLSL_VERSION 100
  25. #endif
  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 - vertex displacement");
  36. // set up camera
  37. Camera camera = {0};
  38. camera.position = (Vector3) {20.0f, 5.0f, -20.0f};
  39. camera.target = (Vector3) {0.0f, 0.0f, 0.0f};
  40. camera.up = (Vector3) {0.0f, 1.0f, 0.0f};
  41. camera.fovy = 60.0f;
  42. camera.projection = CAMERA_PERSPECTIVE;
  43. // Load vertex and fragment shaders
  44. Shader shader = LoadShader(
  45. TextFormat("resources/shaders/glsl%i/vertex_displacement.vs", GLSL_VERSION),
  46. TextFormat("resources/shaders/glsl%i/vertex_displacement.fs", GLSL_VERSION));
  47. // Load perlin noise texture
  48. Image perlinNoiseImage = GenImagePerlinNoise(512, 512, 0, 0, 1.0f);
  49. Texture perlinNoiseMap = LoadTextureFromImage(perlinNoiseImage);
  50. UnloadImage(perlinNoiseImage);
  51. // Set shader uniform location
  52. int perlinNoiseMapLoc = GetShaderLocation(shader, "perlinNoiseMap");
  53. rlEnableShader(shader.id);
  54. rlActiveTextureSlot(1);
  55. rlEnableTexture(perlinNoiseMap.id);
  56. rlSetUniformSampler(perlinNoiseMapLoc, 1);
  57. // Create a plane mesh and model
  58. Mesh planeMesh = GenMeshPlane(50, 50, 50, 50);
  59. Model planeModel = LoadModelFromMesh(planeMesh);
  60. // Set plane model material
  61. planeModel.materials[0].shader = shader;
  62. float time = 0.0f;
  63. SetTargetFPS(60);
  64. //--------------------------------------------------------------------------------------
  65. // Main game loop
  66. while (!WindowShouldClose()) // Detect window close button or ESC key
  67. {
  68. // Update
  69. //----------------------------------------------------------------------------------
  70. UpdateCamera(&camera, CAMERA_FREE); // Update camera
  71. time += GetFrameTime(); // Update time variable
  72. SetShaderValue(shader, GetShaderLocation(shader, "time"), &time, SHADER_UNIFORM_FLOAT); // Send time value to shader
  73. // Draw
  74. //----------------------------------------------------------------------------------
  75. BeginDrawing();
  76. ClearBackground(RAYWHITE);
  77. BeginMode3D(camera);
  78. BeginShaderMode(shader);
  79. // Draw plane model
  80. DrawModel(planeModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, (Color) {255, 255, 255, 255});
  81. EndShaderMode();
  82. EndMode3D();
  83. DrawText("Vertex displacement", 10, 10, 20, DARKGRAY);
  84. DrawFPS(10, 40);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. UnloadShader(shader);
  91. UnloadModel(planeModel);
  92. UnloadTexture(perlinNoiseMap);
  93. CloseWindow(); // Close window and OpenGL context
  94. //--------------------------------------------------------------------------------------
  95. return 0;
  96. }