Created
November 5, 2020 22:18
-
-
Save Rabios/ccbb08d7ff4f41af19d53bf02d803815 to your computer and use it in GitHub Desktop.
Draw 2D texture with raylib in 3D space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Draws a texture in 3D space with pro parameters... | |
void DrawTexturePro3D(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector3 origin, float rotation, float posZ, Color tint) | |
{ | |
// Check if texture is valid | |
if (texture.id > 0) | |
{ | |
float width = (float)texture.width; | |
float height = (float)texture.height; | |
bool flipX = false; | |
if (sourceRec.width < 0) { flipX = true; sourceRec.width *= -1; } | |
if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; | |
rlEnableTexture(texture.id); | |
rlPushMatrix(); | |
rlTranslatef(destRec.x, destRec.y, 0.0f); | |
rlRotatef(rotation, 0.0f, 0.0f, 1.0f); | |
rlTranslatef(-origin.x, -origin.y, -origin.z); | |
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-left corner for texture and quad | |
if (flipX) rlTexCoord2f((sourceRec.x + sourceRec.width)/width, sourceRec.y/height); | |
else rlTexCoord2f(sourceRec.x/width, sourceRec.y/height); | |
rlVertex3f(0.0f, 0.0f, posZ); | |
// Bottom-right corner for texture and quad | |
if (flipX) rlTexCoord2f((sourceRec.x + sourceRec.width)/width, (sourceRec.y + sourceRec.height)/height); | |
else rlTexCoord2f(sourceRec.x/width, (sourceRec.y + sourceRec.height)/height); | |
rlVertex3f(0.0f, destRec.height, posZ); | |
// Top-right corner for texture and quad | |
if (flipX) rlTexCoord2f(sourceRec.x/width, (sourceRec.y + sourceRec.height)/height); | |
else rlTexCoord2f((sourceRec.x + sourceRec.width)/width, (sourceRec.y + sourceRec.height)/height); | |
rlVertex3f(destRec.width, destRec.height, posZ); | |
// Top-left corner for texture and quad | |
if (flipX) rlTexCoord2f(sourceRec.x/width, sourceRec.y/height); | |
else rlTexCoord2f((sourceRec.x + sourceRec.width)/width, sourceRec.y/height); | |
rlVertex3f(destRec.width, 0.0f, posZ); | |
rlEnd(); | |
rlPopMatrix(); | |
rlDisableTexture(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you make some examples? I don't really sure what posZ is going on. I just learned a method to draw 2D texture in 3D but matrix got in my attention because it is faster (please forgive me my english is bad)