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.

105 lines
4.2 KiB

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