|
| 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 | + |
| 16 | +#include "raylib.h" |
| 17 | + |
| 18 | +//------------------------------------------------------------------------------------ |
| 19 | +// Program main entry point |
| 20 | +//------------------------------------------------------------------------------------ |
| 21 | + |
| 22 | +int main(void) |
| 23 | +{ |
| 24 | + const int screenWidth = 800; |
| 25 | + const int screenHeight = 600; |
| 26 | + |
| 27 | + // Initialization |
| 28 | + //-------------------------------------------------------------------------------------- |
| 29 | + InitWindow(screenWidth, screenHeight, "Raylib Texture Tiling"); |
| 30 | + |
| 31 | + SetTargetFPS(60); |
| 32 | + |
| 33 | + // Load a texture |
| 34 | + Texture2D texture = LoadTexture("resources/raylib_logo.png"); |
| 35 | + |
| 36 | + // Create a cube mesh |
| 37 | + Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); |
| 38 | + |
| 39 | + // Load the texture onto the GPU |
| 40 | + Model model = LoadModelFromMesh(cube); |
| 41 | + model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; |
| 42 | + |
| 43 | + // Set the tiling of the texture |
| 44 | + float tiling[2] = {3.0f, 3.0f}; |
| 45 | + Shader shader = LoadShader(0, "resources/shaders/glsl330/tiling.fs"); // Create a custom shader in a .glsl file |
| 46 | + SetShaderValue(shader, GetShaderLocation(shader, "tiling"), tiling, SHADER_UNIFORM_VEC2); |
| 47 | + model.materials[0].shader = shader; |
| 48 | + |
| 49 | + // Camera setup |
| 50 | + Camera camera = { 0 }; |
| 51 | + camera.position = (Vector3){ 3.0f, 3.0f, 3.0f }; |
| 52 | + camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; |
| 53 | + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; |
| 54 | + camera.fovy = 45.0f; |
| 55 | + camera.projection = CAMERA_PERSPECTIVE; |
| 56 | + |
| 57 | + // Main game loop |
| 58 | + while (!WindowShouldClose()) |
| 59 | + { |
| 60 | + // Update |
| 61 | + //---------------------------------------------------------------------------------- |
| 62 | + |
| 63 | + BeginDrawing(); |
| 64 | + ClearBackground(RAYWHITE); |
| 65 | + UpdateCamera(&camera, CAMERA_FREE); |
| 66 | + |
| 67 | + // Draw the model |
| 68 | + { |
| 69 | + BeginMode3D(camera); |
| 70 | + BeginShaderMode(shader); |
| 71 | + |
| 72 | + DrawModel(model, (Vector3){ 0.0f, 0.0f, 0.0f }, 5.0f, WHITE); |
| 73 | + |
| 74 | + EndShaderMode(); |
| 75 | + EndMode3D(); |
| 76 | + } |
| 77 | + |
| 78 | + DrawText("Use mouse to rotate the camera", 10, 10, 20, DARKGRAY); |
| 79 | + |
| 80 | + EndDrawing(); |
| 81 | + } |
| 82 | + |
| 83 | + // De-Initialization |
| 84 | + //-------------------------------------------------------------------------------------- |
| 85 | + |
| 86 | + UnloadTexture(texture); // Unload texture |
| 87 | + UnloadModel(model); // Unload model |
| 88 | + UnloadShader(shader); // Unload shader |
| 89 | + |
| 90 | + |
| 91 | + CloseWindow(); // Close window and OpenGL context |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments