Skip to content

Commit

Permalink
[rmodels] Implement DrawTexturePro3D
Browse files Browse the repository at this point in the history
  • Loading branch information
bohonghuang committed Jul 6, 2024
1 parent 9a280cd commit d19dfec
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,29 @@ typedef struct Color {

// Rectangle, 4 components
typedef struct Rectangle {
float x; // Rectangle top-left corner position x
float y; // Rectangle top-left corner position y
float width; // Rectangle width
float height; // Rectangle height
union {
struct {
float x; // Rectangle top-left corner position x
float y; // Rectangle top-left corner position y
};
Vector2 position;
};
union {
struct {
float width; // Rectangle width
float height; // Rectangle height
};
Vector2 size;
};
} Rectangle;

// Rectangle3D, rectangle in 3D space
typedef struct Rectangle3D {
Vector3 position; // Origin of two sides (up and right) of the rectangle
Vector3 up; // Length and direction of one side of the rectangle
Vector3 right; // Length and direction of another side of the rectangle
} Rectangle3D;

// Image, pixel data stored in CPU memory (RAM)
typedef struct Image {
void *data; // Image raw data
Expand Down Expand Up @@ -1546,6 +1563,7 @@ RLAPI void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, floa
RLAPI void DrawModelWires(Model model, Vector3 position, float scale, Color tint); // Draw a model wires (with texture if set)
RLAPI void DrawModelWiresEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters
RLAPI void DrawBoundingBox(BoundingBox box, Color color); // Draw bounding box (wires)
RLAPI void DrawTexturePro3D(Texture2D texture, Rectangle source, Rectangle3D dest, Vector2 origin, float rotation, Color tint);
RLAPI void DrawBillboard(Camera camera, Texture2D texture, Vector3 position, float size, Color tint); // Draw a billboard texture
RLAPI void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle source, Vector3 position, Vector2 size, Color tint); // Draw a billboard texture defined by source
RLAPI void DrawBillboardPro(Camera camera, Texture2D texture, Rectangle source, Vector3 position, Vector3 up, Vector2 size, Vector2 origin, float rotation, Color tint); // Draw a billboard texture defined by source and rotation
Expand Down
40 changes: 40 additions & 0 deletions src/rmodels.c
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,46 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle source, Vector
DrawBillboardPro(camera, texture, source, position, up, size, Vector2Zero(), 0.0f, tint);
}

void DrawTexturePro3D(Texture2D texture, Rectangle source, Rectangle3D dest, Vector2 origin, float rotation, Color tint)
{
Vector3 forward;
if (rotation != 0.0) forward = Vector3CrossProduct(dest.right, dest.up);

Vector3 origin3D = Vector3Add(Vector3Scale(Vector3Normalize(dest.up), origin.y), Vector3Scale(Vector3Normalize(dest.right), origin.x));

Vector3 points[4];
points[0] = Vector3Zero();
points[1] = dest.right;
points[2] = Vector3Add(dest.up, dest.right);
points[3] = dest.up;

for (int i = 0; i < 4; i++)
{
points[i] = Vector3Subtract(points[i], origin3D);
if (rotation != 0.0) points[i] = Vector3RotateByAxisAngle(points[i], forward, rotation * DEG2RAD);
points[i] = Vector3Add(points[i], dest.position);
}

Vector2 texcoords[4];
texcoords[0] = (Vector2) { (float)source.x/texture.width, (float)(source.y + source.height)/texture.height };
texcoords[1] = (Vector2) { (float)(source.x + source.width)/texture.width, (float)(source.y + source.height)/texture.height };
texcoords[2] = (Vector2) { (float)(source.x + source.width)/texture.width, (float)source.y/texture.height };
texcoords[3] = (Vector2) { (float)source.x/texture.width, (float)source.y/texture.height };

rlSetTexture(texture.id);
rlBegin(RL_QUADS);

rlColor4ub(tint.r, tint.g, tint.b, tint.a);
for (int i = 0; i < 4; i++)
{
rlTexCoord2f(texcoords[i].x, texcoords[i].y);
rlVertex3f(points[i].x, points[i].y, points[i].z);
}

rlEnd();
rlSetTexture(0);
}

// Draw a billboard with additional parameters
// NOTE: Size defines the destination rectangle size, stretching the source texture as required
void DrawBillboardPro(Camera camera, Texture2D texture, Rectangle source, Vector3 position, Vector3 up, Vector2 size, Vector2 origin, float rotation, Color tint)
Expand Down

0 comments on commit d19dfec

Please sign in to comment.