Skip to content

Commit

Permalink
Improved billboards example, highlighting rotation and draw order (#2779
Browse files Browse the repository at this point in the history
)

* Improved billboards example, highlighting rotation and draw order

* changes to conform to the raylib conventions

* NOW it conforms

Co-authored-by: nobytesgiven <[email protected]>
  • Loading branch information
nobytesgiven and nobytesgiven authored Oct 26, 2022
1 parent 28e8b2a commit 5113817
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions examples/models/models_billboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
********************************************************************************************/

#include "raylib.h"
#include "raymath.h"

//------------------------------------------------------------------------------------
// Program main entry point
Expand All @@ -33,11 +34,28 @@ int main(void)
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;

Texture2D bill = LoadTexture("resources/billboard.png"); // Our texture billboard
Vector3 billPosition = { 0.0f, 2.0f, 0.0f }; // Position where draw billboard
Texture2D bill = LoadTexture("resources/billboard.png"); // Our billboard texture
Vector3 billPositionStatic = { 0.0f, 2.0f, 0.0f }; // Position of billboard
Vector3 billPositionRotating = { 1.0f, 2.0f, 1.0f };

// Entire billboard texture, source is used to take a segment from a larger texture.
Rectangle source = { 0.0f, 0.0f, (float)bill.width, (float)bill.height };

// NOTE: Billboard locked on axis-Y
Vector3 billUp = { 0.0f, 1.0f, 0.0f };

// Rotate around origin
// Here we choose to rotate around the image center
// NOTE: (-1, 1) is the range where origin.x, origin.y is inside the texture
Vector2 rotateOrigin = { 0.0f };
SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode

// Distance is needed for the correct billboard draw order
// Larger distance (further away from the camera) should be drawn prior to smaller distance.
float distanceStatic;
float distanceRotating;

float rotation = 0.0f;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

Expand All @@ -47,6 +65,9 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera);
rotation += 0.4f;
distanceStatic = Vector3Distance(camera.position, billPositionStatic);
distanceRotating = Vector3Distance(camera.position, billPositionRotating);
//----------------------------------------------------------------------------------

// Draw
Expand All @@ -59,8 +80,18 @@ int main(void)

DrawGrid(10, 1.0f); // Draw a grid

DrawBillboard(camera, bill, billPosition, 2.0f, WHITE);

// Draw order matters!
if (distanceStatic > distanceRotating)
{
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
}
else
{
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, (Vector2) {1.0f, 1.0f}, rotateOrigin, rotation, WHITE);
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
}

EndMode3D();

DrawFPS(10, 10);
Expand Down

0 comments on commit 5113817

Please sign in to comment.