|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [models] example - billboard render |
| 4 | +* |
| 5 | +* Example complexity rating: [★★★☆] 3/4 |
| 6 | +* |
| 7 | +* Example originally created with raylib 1.3, last time updated with raylib 3.5 |
| 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) 2015-2025 Ramon Santamaria (@raysan5) |
| 13 | +* |
| 14 | +********************************************************************************************/ |
| 15 | + |
| 16 | +#include "raylib.h" |
| 17 | +#include "raymath.h" |
| 18 | + |
| 19 | +//------------------------------------------------------------------------------------ |
| 20 | +// Program main entry point |
| 21 | +//------------------------------------------------------------------------------------ |
| 22 | +int main(void) |
| 23 | +{ |
| 24 | + // Initialization |
| 25 | + //-------------------------------------------------------------------------------------- |
| 26 | + const int screenWidth = 800; |
| 27 | + const int screenHeight = 450; |
| 28 | + |
| 29 | + InitWindow(screenWidth, screenHeight, "raylib [models] example - billboard render"); |
| 30 | + |
| 31 | + // Define the camera to look into our 3d world |
| 32 | + Camera camera = { 0 }; |
| 33 | + camera.position = (Vector3){ 5.0f, 4.0f, 5.0f }; // Camera position |
| 34 | + camera.target = (Vector3){ 0.0f, 2.0f, 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 | + |
| 39 | + Texture2D bill = LoadTexture("resources/billboard.png"); // Our billboard texture |
| 40 | + Vector3 billPositionStatic = { 0.0f, 2.0f, 0.0f }; // Position of static billboard |
| 41 | + Vector3 billPositionRotating = { 1.0f, 2.0f, 1.0f }; // Position of rotating billboard |
| 42 | + |
| 43 | + // Entire billboard texture, source is used to take a segment from a larger texture |
| 44 | + Rectangle source = { 0.0f, 0.0f, (float)bill.width, (float)bill.height }; |
| 45 | + |
| 46 | + // NOTE: Billboard locked on axis-Y |
| 47 | + Vector3 billUp = { 0.0f, 1.0f, 0.0f }; |
| 48 | + |
| 49 | + // Set the height of the rotating billboard to 1.0 with the aspect ratio fixed |
| 50 | + Vector2 size = { source.width/source.height, 1.0f }; |
| 51 | + |
| 52 | + // Rotate around origin |
| 53 | + // Here we choose to rotate around the image center |
| 54 | + Vector2 origin = Vector2Scale(size, 0.5f); |
| 55 | + |
| 56 | + // Distance is needed for the correct billboard draw order |
| 57 | + // Larger distance (further away from the camera) should be drawn prior to smaller distance |
| 58 | + float distanceStatic; |
| 59 | + float distanceRotating; |
| 60 | + float rotation = 0.0f; |
| 61 | + |
| 62 | + SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 63 | + //-------------------------------------------------------------------------------------- |
| 64 | + |
| 65 | + // Main game loop |
| 66 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 67 | + { |
| 68 | + // Update |
| 69 | + //---------------------------------------------------------------------------------- |
| 70 | + UpdateCamera(&camera, CAMERA_ORBITAL); |
| 71 | + |
| 72 | + rotation += 0.4f; |
| 73 | + distanceStatic = Vector3Distance(camera.position, billPositionStatic); |
| 74 | + distanceRotating = Vector3Distance(camera.position, billPositionRotating); |
| 75 | + //---------------------------------------------------------------------------------- |
| 76 | + |
| 77 | + // Draw |
| 78 | + //---------------------------------------------------------------------------------- |
| 79 | + BeginDrawing(); |
| 80 | + |
| 81 | + ClearBackground(RAYWHITE); |
| 82 | + |
| 83 | + BeginMode3D(camera); |
| 84 | + |
| 85 | + DrawGrid(10, 1.0f); // Draw a grid |
| 86 | + |
| 87 | + // Draw order matters! |
| 88 | + if (distanceStatic > distanceRotating) |
| 89 | + { |
| 90 | + DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE); |
| 91 | + DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, WHITE); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, WHITE); |
| 96 | + DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE); |
| 97 | + } |
| 98 | + |
| 99 | + EndMode3D(); |
| 100 | + |
| 101 | + DrawFPS(10, 10); |
| 102 | + |
| 103 | + EndDrawing(); |
| 104 | + //---------------------------------------------------------------------------------- |
| 105 | + } |
| 106 | + |
| 107 | + // De-Initialization |
| 108 | + //-------------------------------------------------------------------------------------- |
| 109 | + UnloadTexture(bill); // Unload texture |
| 110 | + |
| 111 | + CloseWindow(); // Close window and OpenGL context |
| 112 | + //-------------------------------------------------------------------------------------- |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
0 commit comments