shaders_texture_tiling.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - texture tiling
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example demonstrates how to tile a texture on a 3D model using raylib
  8. *
  9. * Example originally created with raylib 4.5, last time updated with raylib 4.5
  10. *
  11. * Example contributed by Luis Almeida (@luis605) and reviewed by Ramon Santamaria (@raysan5)
  12. *
  13. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  14. * BSD-like license that allows static linking with closed source software
  15. *
  16. * Copyright (c) 2023-2025 Luis Almeida (@luis605)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. #if defined(PLATFORM_DESKTOP)
  21. #define GLSL_VERSION 330
  22. #else // PLATFORM_ANDROID, PLATFORM_WEB
  23. #define GLSL_VERSION 100
  24. #endif
  25. //------------------------------------------------------------------------------------
  26. // Program main entry point
  27. //------------------------------------------------------------------------------------
  28. int main(void)
  29. {
  30. // Initialization
  31. //--------------------------------------------------------------------------------------
  32. const int screenWidth = 800;
  33. const int screenHeight = 450;
  34. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture tiling");
  35. // Define the camera to look into our 3d world
  36. Camera3D camera = { 0 };
  37. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; // Camera position
  38. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  39. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  40. camera.fovy = 45.0f; // Camera field-of-view Y
  41. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  42. // Load a cube model
  43. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  44. Model model = LoadModelFromMesh(cube);
  45. // Load a texture and assign to cube model
  46. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
  47. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  48. // Set the texture tiling using a shader
  49. float tiling[2] = { 3.0f, 3.0f };
  50. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/tiling.fs", GLSL_VERSION));
  51. SetShaderValue(shader, GetShaderLocation(shader, "tiling"), tiling, SHADER_UNIFORM_VEC2);
  52. model.materials[0].shader = shader;
  53. DisableCursor(); // Limit cursor to relative movement inside the window
  54. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  55. //--------------------------------------------------------------------------------------
  56. // Main game loop
  57. while (!WindowShouldClose()) // Detect window close button or ESC key
  58. {
  59. // Update
  60. //----------------------------------------------------------------------------------
  61. UpdateCamera(&camera, CAMERA_FREE);
  62. if (IsKeyPressed('Z')) camera.target = (Vector3){ 0.0f, 0.5f, 0.0f };
  63. //----------------------------------------------------------------------------------
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(RAYWHITE);
  68. BeginMode3D(camera);
  69. BeginShaderMode(shader);
  70. DrawModel(model, (Vector3){ 0.0f, 0.0f, 0.0f }, 2.0f, WHITE);
  71. EndShaderMode();
  72. DrawGrid(10, 1.0f);
  73. EndMode3D();
  74. DrawText("Use mouse to rotate the camera", 10, 10, 20, DARKGRAY);
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. UnloadModel(model); // Unload model
  81. UnloadShader(shader); // Unload shader
  82. UnloadTexture(texture); // Unload texture
  83. CloseWindow(); // Close window and OpenGL context
  84. //--------------------------------------------------------------------------------------
  85. return 0;
  86. }