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.

119 lines
4.1 KiB

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