Improved billboards example, highlighting rotation and draw order (#2779)

* Improved billboards example, highlighting rotation and draw order

* changes to conform to the raylib conventions

* NOW it conforms

Co-authored-by: nobytesgiven <nobytesgiven@users.noreply.github.com>
This commit is contained in:
nobytesgiven 2022-10-26 20:16:35 +03:00 committed by GitHub
parent 28e8b2add3
commit 5113817507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
//------------------------------------------------------------------------------------
// Program main entry point
@ -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
//--------------------------------------------------------------------------------------
@ -47,6 +65,9 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera);
rotation += 0.4f;
distanceStatic = Vector3Distance(camera.position, billPositionStatic);
distanceRotating = Vector3Distance(camera.position, billPositionRotating);
//----------------------------------------------------------------------------------
// Draw
@ -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);