Skip to content

Instantly share code, notes, and snippets.

@Rabios
Created November 5, 2020 22:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rabios/ccbb08d7ff4f41af19d53bf02d803815 to your computer and use it in GitHub Desktop.
Save Rabios/ccbb08d7ff4f41af19d53bf02d803815 to your computer and use it in GitHub Desktop.
Draw 2D texture with raylib in 3D space
// 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();
}
}
@QTChosusTV
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment