polygon drawing
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [textures] example - polygon drawing
//!
//! Zig-API translation of examples/textures/textures_polygon_drawing.c (raylib 6.2).
//! Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2021-2025 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
const rl = @import("raylib");
const max_points = 11; // 10 points and back to the start
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [textures] example - polygon drawing");
defer rl.closeWindow(); // Close window and OpenGL context
// Define texture coordinates to map our texture to poly
const texcoords = [max_points]rl.Vector2{
.{ .x = 0.75, .y = 0.0 },
.{ .x = 0.25, .y = 0.0 },
.{ .x = 0.0, .y = 0.5 },
.{ .x = 0.0, .y = 0.75 },
.{ .x = 0.25, .y = 1.0 },
.{ .x = 0.375, .y = 0.875 },
.{ .x = 0.625, .y = 0.875 },
.{ .x = 0.75, .y = 1.0 },
.{ .x = 1.0, .y = 0.75 },
.{ .x = 1.0, .y = 0.5 },
.{ .x = 0.75, .y = 0.0 }, // Close the poly
};
// Define the base poly vertices from the UV's
// NOTE: They can be specified in any other way
var points: [max_points]rl.Vector2 = undefined;
for (&points, texcoords) |*point, uv| {
point.* = .{ .x = (uv.x - 0.5) * 256.0, .y = (uv.y - 0.5) * 256.0 };
}
// Define the vertices drawing position
// NOTE: Initially same as points but updated every frame
var positions = points;
// Load texture to be mapped to poly
const texture = try rl.loadTexture("resources/cat.png");
defer rl.unloadTexture(texture); // Unload texture
var angle: f32 = 0.0; // Rotation angle (in degrees)
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
// Update points rotation with an angle transform
// NOTE: Base points position are not modified
angle += 1;
for (&positions, points) |*position, point| position.* = rl.math.vector2Rotate(point, angle * rl.deg2rad);
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
rl.drawText("textured polygon", 20, 20, 20, .dark_gray);
const center: rl.Vector2 = .{
.x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0,
.y = @as(f32, @floatFromInt(rl.getScreenHeight())) / 2.0,
};
drawTexturePoly(texture, center, &positions, &texcoords, .white);
}
}
/// Draw textured polygon, defined by vertex and texture coordinates
/// NOTE: Polygon center must have straight line path to all points
/// without crossing perimeter, points must be in anticlockwise order
fn drawTexturePoly(texture: rl.Texture2D, center: rl.Vector2, points: []const rl.Vector2, texcoords: []const rl.Vector2, tint: rl.Color) void {
rl.gl.setTexture(texture.id);
defer rl.gl.setTexture(0);
rl.gl.begin(rl.gl.triangles);
defer rl.gl.end();
rl.gl.color4ub(tint.r, tint.g, tint.b, tint.a);
for (0..points.len - 1) |i| {
rl.gl.texCoord2f(0.5, 0.5);
rl.gl.vertex2f(center.x, center.y);
rl.gl.texCoord2f(texcoords[i].x, texcoords[i].y);
rl.gl.vertex2f(points[i].x + center.x, points[i].y + center.y);
rl.gl.texCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
rl.gl.vertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
}
}/*******************************************************************************************
*
* raylib [textures] example - polygon drawing
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 3.7, last time updated with raylib 3.7
*
* Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2021-2025 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "rlgl.h" // Required for: Vertex definition
#include "raymath.h"
#define MAX_POINTS 11 // 10 points and back to the start
// Draw textured polygon, defined by vertex and texture coordinates
void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointCount, Color tint);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - polygon drawing");
// Define texture coordinates to map our texture to poly
Vector2 texcoords[MAX_POINTS] = {
(Vector2){ 0.75f, 0.0f },
(Vector2){ 0.25f, 0.0f },
(Vector2){ 0.0f, 0.5f },
(Vector2){ 0.0f, 0.75f },
(Vector2){ 0.25f, 1.0f},
(Vector2){ 0.375f, 0.875f},
(Vector2){ 0.625f, 0.875f},
(Vector2){ 0.75f, 1.0f},
(Vector2){ 1.0f, 0.75f},
(Vector2){ 1.0f, 0.5f},
(Vector2){ 0.75f, 0.0f} // Close the poly
};
// Define the base poly vertices from the UV's
// NOTE: They can be specified in any other way
Vector2 points[MAX_POINTS] = { 0 };
for (int i = 0; i < MAX_POINTS; i++)
{
points[i].x = (texcoords[i].x - 0.5f)*256.0f;
points[i].y = (texcoords[i].y - 0.5f)*256.0f;
}
// Define the vertices drawing position
// NOTE: Initially same as points but updated every frame
Vector2 positions[MAX_POINTS] = { 0 };
for (int i = 0; i < MAX_POINTS; i++) positions[i] = points[i];
// Load texture to be mapped to poly
Texture texture = LoadTexture("resources/cat.png");
float angle = 0.0f; // Rotation angle (in degrees)
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Update points rotation with an angle transform
// NOTE: Base points position are not modified
angle++;
for (int i = 0; i < MAX_POINTS; i++) positions[i] = Vector2Rotate(points[i], angle*DEG2RAD);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("textured polygon", 20, 20, 20, DARKGRAY);
DrawTexturePoly(texture, (Vector2){ GetScreenWidth()/2.0f, GetScreenHeight()/2.0f },
positions, texcoords, MAX_POINTS, WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
// Draw textured polygon, defined by vertex and texture coordinates
// NOTE: Polygon center must have straight line path to all points
// without crossing perimeter, points must be in anticlockwise order
void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointCount, Color tint)
{
rlSetTexture(texture.id);
rlBegin(RL_TRIANGLES);
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
for (int i = 0; i < pointCount - 1; i++)
{
rlTexCoord2f(0.5f, 0.5f);
rlVertex2f(center.x, center.y);
rlTexCoord2f(texcoords[i].x, texcoords[i].y);
rlVertex2f(points[i].x + center.x, points[i].y + center.y);
rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
}
rlEnd();
rlSetTexture(0);
}From raylib 6.2. The original on GitHub.