REVIEWED: DrawTexturePoly()

This commit is contained in:
raysan5 2021-03-28 20:07:59 +02:00
parent 668ba870e4
commit bc6b16beb2
3 changed files with 66 additions and 74 deletions

View file

@ -2,17 +2,21 @@
* *
* raylib [shapes] example - Draw Textured Polygon * raylib [shapes] example - Draw Textured Polygon
* *
* This example has been created using raylib 99.98 (www.raylib.com) * This example has been created using raylib 3.7 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2014 Ramon Santamaria (@raysan5) * Example contributed by Chris Camacho (@codifies - bedroomcoders.co.uk) and
* Copyright (c) 2021 Chris Camacho (codifies - bedroomcoders.co.uk) * reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2021 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
#define MAX_POINTS 11 // 10 points and back to the start
int main(void) int main(void)
{ {
// Initialization // Initialization
@ -20,54 +24,51 @@ int main(void)
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
int numPnts = 11; // 10 points and back to the start Vector2 texcoords[MAX_POINTS] = {
(Vector2){ 0.75f, 0.0f },
Vector2 tPnts[] = { (Vector2){ 0.25f, 0.0f },
(Vector2){.75, 0}, (Vector2){ 0.0f, 0.5f },
(Vector2){.25, 0}, (Vector2){ 0.0f, 0.75f },
(Vector2){0, .5}, (Vector2){ 0.25f, 1.0f},
(Vector2){0, .75}, (Vector2){ 0.375f, 0.875f},
(Vector2){.25, 1}, (Vector2){ 0.625f, 0.875f},
(Vector2){.375, .875}, (Vector2){ 0.75f, 1.0f},
(Vector2){.625, .875}, (Vector2){ 1.0f, 0.75f},
(Vector2){.75, 1}, (Vector2){ 1.0f, 0.5f},
(Vector2){1, .75}, (Vector2){ 0.75f, 0.0f} // Close the poly
(Vector2){1, .5},
(Vector2){.75, 0} // close the poly
}; };
Vector2 pnts[numPnts]; Vector2 points[MAX_POINTS] = { 0 };
// create the poly coords from the UV's // Create the poly coords from the UV's
// you don't have to do this you can specify // you don't have to do this you can specify
// them however you want // them however you want
for (int i=0; i < numPnts; i++) for (int i = 0; i < MAX_POINTS; i++)
{ {
pnts[i].x = (tPnts[i].x - 0.5) * 256.0; points[i].x = (texcoords[i].x - 0.5f)*256.0f;
pnts[i].y = (tPnts[i].y - 0.5) * 256.0; points[i].y = (texcoords[i].y - 0.5f)*256.0f;
} }
InitWindow(screenWidth, screenHeight, "raylib [textures] example - Textured Polygon"); InitWindow(screenWidth, screenHeight, "raylib [textures] example - textured polygon");
Texture tex = LoadTexture("resources/cat.png"); Texture texture = LoadTexture("resources/cat.png");
float ang = 0;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
float ang = 0;
// Main game loop // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Update your variables here
//----------------------------------------------------------------------------------
ang++; ang++;
Vector2 dPnts[numPnts]; Vector2 positions[MAX_POINTS] = { 0 };
for (int i = 0; i < numPnts; i++)
{ for (int i = 0; i < MAX_POINTS; i++) positions[i] = Vector2Rotate(points[i], ang);
dPnts[i] = Vector2Rotate(pnts[i], ang); //----------------------------------------------------------------------------------
}
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -75,19 +76,19 @@ int main(void)
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
DrawText("Textured Polygon", 20, 20, 20, DARKGRAY); DrawText("textured polygon", 20, 20, 20, DARKGRAY);
DrawTexturePoly(tex, screenWidth/2, screenHeight/2, DrawTexturePoly(texture, (Vector2){ GetScreenWidth()/2, GetScreenHeight()/2 },
dPnts, tPnts, numPnts, WHITE); positions, texcoords, MAX_POINTS, WHITE);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
UnloadTexture(tex);
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------

View file

@ -1271,7 +1271,7 @@ RLAPI void DrawTextureQuad(Texture2D texture, Vector2 tiling, Vector2 offset, Re
RLAPI void DrawTextureTiled(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint); // Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest. RLAPI void DrawTextureTiled(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint); // Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest.
RLAPI void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draw a part of a texture defined by a rectangle with 'pro' parameters RLAPI void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draw a part of a texture defined by a rectangle with 'pro' parameters
RLAPI void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draws a texture (or part of it) that stretches or shrinks nicely RLAPI void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draws a texture (or part of it) that stretches or shrinks nicely
RLAPI void DrawTexturePoly(Texture t, float x, float y, Vector2 *points, Vector2 *tPnts, int numPoints, Color colour); // Draw a textured polygon RLAPI void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointsCount, Color tint); // Draw a textured polygon
// Color/pixel related functions // Color/pixel related functions
RLAPI Color Fade(Color color, float alpha); // Returns color with alpha applied, alpha goes from 0.0f to 1.0f RLAPI Color Fade(Color color, float alpha); // Returns color with alpha applied, alpha goes from 0.0f to 1.0f

View file

@ -3510,48 +3510,39 @@ void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest,
} }
} }
// t texture to use // Draw textured polygon, defined by vertex and texturecoordinates
// x,y position to draw the poly (centre) // NOTE: Polygon center must have straight line path to all points
// points points of the poly (relative to 0,0) // without crossing perimeter, points must be in anticlockwise order
// tPnts uv coordinates void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointsCount, Color tint)
// numPoints number of points in the poly
// colour the tint of the poly
//
// NB centre (0,0) must have straight line path to all points
// without crossing perimeter, points must be in anticlockwise
// order
void DrawTexturePoly(Texture t, float x, float y,
Vector2 *points, Vector2 *tPnts,
int numPoints, Color colour)
{ {
rlEnableTexture(t.id); rlCheckRenderBatchLimit((pointsCount - 1)*4);
// for some reason texturing doesn't work on trianglesso make a rlSetTexture(texture.id);
// degenerate QUAD, DrawTriangleFan does this too why ?
rlCheckRenderBatchLimit((numPoints-1)*4); // Texturing is only supported on QUADs
rlBegin(RL_QUADS); rlBegin(RL_QUADS);
rlColor4ub(colour.r, colour.g, colour.b, colour.a);
for (int i = 0; i < numPoints-1; i++) rlColor4ub(tint.r, tint.g, tint.b, tint.a);
for (int i = 0; i < pointsCount - 1; i++)
{ {
rlTexCoord2f(0.5, 0.5); rlTexCoord2f(0.5f, 0.5f);
rlVertex2f(x, y); rlVertex2f(center.x, center.y);
rlTexCoord2f(tPnts[i].x, tPnts[i].y); rlTexCoord2f(texcoords[i].x, texcoords[i].y);
rlVertex2f(points[i].x + x, points[i].y + y); rlVertex2f(points[i].x + center.x, points[i].y + center.y);
rlTexCoord2f(tPnts[i + 1].x, tPnts[i + 1].y); rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
rlVertex2f(points[i + 1].x + x, points[i + 1].y + y); rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
rlTexCoord2f(tPnts[i + 1].x, tPnts[i + 1].y); rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
rlVertex2f(points[i + 1].x + x, points[i + 1].y + y); rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
} }
rlEnd(); rlEnd();
rlDisableTexture();
rlSetTexture(0);
} }
// Returns color with alpha applied, alpha goes from 0.0f to 1.0f // Returns color with alpha applied, alpha goes from 0.0f to 1.0f
Color Fade(Color color, float alpha) Color Fade(Color color, float alpha)
{ {