shaders_texture_tiling.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 contributed by Luis Almeida (@luis605) 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 Luis Almeida (@luis605)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #if defined(PLATFORM_DESKTOP)
  19. #define GLSL_VERSION 330
  20. #else // PLATFORM_ANDROID, PLATFORM_WEB
  21. #define GLSL_VERSION 100
  22. #endif
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture tiling");
  33. // Define the camera to look into our 3d world
  34. Camera3D camera = { 0 };
  35. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; // Camera position
  36. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  37. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  38. camera.fovy = 45.0f; // Camera field-of-view Y
  39. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  40. // Load a cube model
  41. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  42. Model model = LoadModelFromMesh(cube);
  43. // Load a texture and assign to cube model
  44. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
  45. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  46. // Set the texture tiling using a shader
  47. float tiling[2] = { 3.0f, 3.0f };
  48. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/tiling.fs", GLSL_VERSION));
  49. SetShaderValue(shader, GetShaderLocation(shader, "tiling"), tiling, SHADER_UNIFORM_VEC2);
  50. model.materials[0].shader = shader;
  51. DisableCursor(); // Limit cursor to relative movement inside the window
  52. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. // Update
  58. //----------------------------------------------------------------------------------
  59. UpdateCamera(&camera, CAMERA_FREE);
  60. if (IsKeyPressed('Z')) camera.target = (Vector3){ 0.0f, 0.5f, 0.0f };
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. BeginMode3D(camera);
  67. BeginShaderMode(shader);
  68. DrawModel(model, (Vector3){ 0.0f, 0.0f, 0.0f }, 2.0f, WHITE);
  69. EndShaderMode();
  70. DrawGrid(10, 1.0f);
  71. EndMode3D();
  72. DrawText("Use mouse to rotate the camera", 10, 10, 20, DARKGRAY);
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. UnloadModel(model); // Unload model
  79. UnloadShader(shader); // Unload shader
  80. UnloadTexture(texture); // Unload texture
  81. CloseWindow(); // Close window and OpenGL context
  82. //--------------------------------------------------------------------------------------
  83. return 0;
  84. }