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.

94 lines
3.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Texture Tiling
  4. *
  5. * Example demonstrates how to tile a texture on a 3D model using raylib.
  6. *
  7. * Example contributed by Luís Almeida (https://github.com/luis605)
  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 Luís Almeida (https://github.com/luis605)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. const int screenWidth = 800;
  22. const int screenHeight = 600;
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. InitWindow(screenWidth, screenHeight, "Raylib Texture Tiling");
  26. SetTargetFPS(60);
  27. // Load a texture
  28. Texture2D texture = LoadTexture("resources/raylib_logo.png");
  29. // Create a cube mesh
  30. Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
  31. // Load the texture onto the GPU
  32. Model model = LoadModelFromMesh(cube);
  33. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  34. // Set the tiling of the texture
  35. float tiling[2] = {3.0f, 3.0f};
  36. Shader shader = LoadShader(0, "resources/shaders/glsl330/tiling.fs"); // Create a custom shader in a .glsl file
  37. SetShaderValue(shader, GetShaderLocation(shader, "tiling"), tiling, SHADER_UNIFORM_VEC2);
  38. model.materials[0].shader = shader;
  39. // Camera setup
  40. Camera camera = { 0 };
  41. camera.position = (Vector3){ 3.0f, 3.0f, 3.0f };
  42. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  43. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  44. camera.fovy = 45.0f;
  45. camera.projection = CAMERA_PERSPECTIVE;
  46. // Main game loop
  47. while (!WindowShouldClose())
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. BeginDrawing();
  52. ClearBackground(RAYWHITE);
  53. UpdateCamera(&camera, CAMERA_FREE);
  54. // Draw the model
  55. {
  56. BeginMode3D(camera);
  57. BeginShaderMode(shader);
  58. DrawModel(model, (Vector3){ 0.0f, 0.0f, 0.0f }, 5.0f, WHITE);
  59. EndShaderMode();
  60. EndMode3D();
  61. }
  62. DrawText("Use mouse to rotate the camera", 10, 10, 20, DARKGRAY);
  63. EndDrawing();
  64. }
  65. // De-Initialization
  66. //--------------------------------------------------------------------------------------
  67. UnloadTexture(texture); // Unload texture
  68. UnloadModel(model); // Unload model
  69. UnloadShader(shader); // Unload shader
  70. CloseWindow(); // Close window and OpenGL context
  71. return 0;
  72. }