Optimize DrawTexturePro and DrawRectanglePro transformations (#1632)

* Optimize DrawTexturePro and DrawRectanglePro transformations
- Add check so rotation is only applied if rotation != 0.0f.
- Replace matrix usage by calculating the vertex data directly.

* Fix error with windows build and trim whitespace
This commit is contained in:
Chris 2021-03-19 18:19:10 +00:00 committed by GitHub
parent 2f367a905e
commit 45670fbf2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 113 additions and 55 deletions

View file

@ -619,31 +619,60 @@ void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color
{
if (rlCheckBufferLimit(4)) rlglDraw();
Vector2 bl = { 0 };
Vector2 br = { 0 };
Vector2 tr = { 0 };
Vector2 tl = { 0 };
// Only calculate rotation if needed
if (rotation == 0.0f)
{
float x = rec.x - origin.x;
float y = rec.y - origin.y;
bl = (Vector2){ x, y };
br = (Vector2){ x, y + rec.height };
tr = (Vector2){ x + rec.width, y + rec.height };
tl = (Vector2){ x + rec.width, y };
}
else
{
float sinRotation = sinf(rotation * DEG2RAD);
float cosRotation = cosf(rotation * DEG2RAD);
float x = rec.x;
float y = rec.y;
float dx = -origin.x;
float dy = -origin.y;
bl.x = x + dx * cosRotation - (dy + rec.height) * sinRotation;
bl.y = y + dx * sinRotation + (dy + rec.height) * cosRotation;
br.x = x + (dx + rec.width) * cosRotation - (dy + rec.height) * sinRotation;
br.y = y + (dx + rec.width) * sinRotation + (dy + rec.height) * cosRotation;
tr.x = x + (dx + rec.width) * cosRotation - dy * sinRotation;
tr.y = y + (dx + rec.width) * sinRotation + dy * cosRotation;
tl.x = x + dx * cosRotation - dy * sinRotation;
tl.y = y + dx * sinRotation + dy * cosRotation;
}
rlEnableTexture(GetShapesTexture().id);
rlBegin(RL_QUADS);
rlNormal3f(0.0f, 0.0f, 1.0f);
rlColor4ub(color.r, color.g, color.b, color.a);
rlPushMatrix();
rlTranslatef(rec.x, rec.y, 0.0f);
rlRotatef(rotation, 0.0f, 0.0f, 1.0f);
rlTranslatef(-origin.x, -origin.y, 0.0f);
rlTexCoord2f(GetShapesTextureRec().x/GetShapesTexture().width, GetShapesTextureRec().y/GetShapesTexture().height);
rlVertex2f(bl.x, bl.y);
rlBegin(RL_QUADS);
rlNormal3f(0.0f, 0.0f, 1.0f);
rlColor4ub(color.r, color.g, color.b, color.a);
rlTexCoord2f(GetShapesTextureRec().x/GetShapesTexture().width, (GetShapesTextureRec().y + GetShapesTextureRec().height)/GetShapesTexture().height);
rlVertex2f(br.x, br.y);
rlTexCoord2f(GetShapesTextureRec().x/GetShapesTexture().width, GetShapesTextureRec().y/GetShapesTexture().height);
rlVertex2f(0.0f, 0.0f);
rlTexCoord2f(GetShapesTextureRec().x/GetShapesTexture().width, (GetShapesTextureRec().y + GetShapesTextureRec().height)/GetShapesTexture().height);
rlVertex2f(0.0f, rec.height);
rlTexCoord2f((GetShapesTextureRec().x + GetShapesTextureRec().width)/GetShapesTexture().width, (GetShapesTextureRec().y + GetShapesTextureRec().height)/GetShapesTexture().height);
rlVertex2f(rec.width, rec.height);
rlTexCoord2f((GetShapesTextureRec().x + GetShapesTextureRec().width)/GetShapesTexture().width, GetShapesTextureRec().y/GetShapesTexture().height);
rlVertex2f(rec.width, 0.0f);
rlEnd();
rlPopMatrix();
rlTexCoord2f((GetShapesTextureRec().x + GetShapesTextureRec().width)/GetShapesTexture().width, (GetShapesTextureRec().y + GetShapesTextureRec().height)/GetShapesTexture().height);
rlVertex2f(tr.x, tr.y);
rlTexCoord2f((GetShapesTextureRec().x + GetShapesTextureRec().width)/GetShapesTexture().width, GetShapesTextureRec().y/GetShapesTexture().height);
rlVertex2f(tl.x, tl.y);
rlEnd();
rlDisableTexture();
}

View file

@ -3200,39 +3200,68 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2
if (source.width < 0) { flipX = true; source.width *= -1; }
if (source.height < 0) source.y -= source.height;
Vector2 bl = { 0 };
Vector2 br = { 0 };
Vector2 tr = { 0 };
Vector2 tl = { 0 };
// Only calculate rotation if needed
if (rotation == 0.0f)
{
float x = dest.x - origin.x;
float y = dest.y - origin.y;
bl = (Vector2){ x, y };
br = (Vector2){ x, y + dest.height };
tr = (Vector2){ x + dest.width, y + dest.height };
tl = (Vector2){ x + dest.width, y };
}
else
{
float sinRotation = sinf(rotation * DEG2RAD);
float cosRotation = cosf(rotation * DEG2RAD);
float x = dest.x;
float y = dest.y;
float dx = -origin.x;
float dy = -origin.y;
bl.x = x + dx * cosRotation - (dy + dest.height) * sinRotation;
bl.y = y + dx * sinRotation + (dy + dest.height) * cosRotation;
br.x = x + (dx + dest.width) * cosRotation - (dy + dest.height) * sinRotation;
br.y = y + (dx + dest.width) * sinRotation + (dy + dest.height) * cosRotation;
tr.x = x + (dx + dest.width) * cosRotation - dy * sinRotation;
tr.y = y + (dx + dest.width) * sinRotation + dy * cosRotation;
tl.x = x + dx * cosRotation - dy * sinRotation;
tl.y = y + dx * sinRotation + dy * cosRotation;
}
rlEnableTexture(texture.id);
rlBegin(RL_QUADS);
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
rlPushMatrix();
rlTranslatef(dest.x, dest.y, 0.0f);
rlRotatef(rotation, 0.0f, 0.0f, 1.0f);
rlTranslatef(-origin.x, -origin.y, 0.0f);
// Bottom-left corner for texture and quad
if (flipX) rlTexCoord2f((source.x + source.width)/width, source.y/height);
else rlTexCoord2f(source.x/width, source.y/height);
rlVertex2f(bl.x, bl.y);
rlBegin(RL_QUADS);
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
// Bottom-right corner for texture and quad
if (flipX) rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height);
else rlTexCoord2f(source.x/width, (source.y + source.height)/height);
rlVertex2f(br.x, br.y);
// Bottom-left corner for texture and quad
if (flipX) rlTexCoord2f((source.x + source.width)/width, source.y/height);
else rlTexCoord2f(source.x/width, source.y/height);
rlVertex2f(0.0f, 0.0f);
// Bottom-right corner for texture and quad
if (flipX) rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height);
else rlTexCoord2f(source.x/width, (source.y + source.height)/height);
rlVertex2f(0.0f, dest.height);
// Top-right corner for texture and quad
if (flipX) rlTexCoord2f(source.x/width, (source.y + source.height)/height);
else rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height);
rlVertex2f(dest.width, dest.height);
// Top-left corner for texture and quad
if (flipX) rlTexCoord2f(source.x/width, source.y/height);
else rlTexCoord2f((source.x + source.width)/width, source.y/height);
rlVertex2f(dest.width, 0.0f);
rlEnd();
rlPopMatrix();
// Top-right corner for texture and quad
if (flipX) rlTexCoord2f(source.x/width, (source.y + source.height)/height);
else rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height);
rlVertex2f(tr.x, tr.y);
// Top-left corner for texture and quad
if (flipX) rlTexCoord2f(source.x/width, source.y/height);
else rlTexCoord2f((source.x + source.width)/width, source.y/height);
rlVertex2f(tl.x, tl.y);
rlEnd();
rlDisableTexture();
}
}