当前位置: 首页 > news >正文

0385-Raylib-渲染多边形

环境

  • Time 2024-02-29
  • Zig 0.12.0-dev.3076+6e078883e
  • WSL-Ubuntu 22.04.3 LTS
  • Raylib 5.0

前言

说明

参考资料:

  1. https://www.raylib.com/examples.html

目标

渲染多边形。

main.zig

const std = @import("std");
const ray = @import("raylib.zig");pub fn main() !void {const screenWidth: c_int = 800;const screenHeight: c_int = 450;ray.InitWindow(screenWidth, screenHeight, "raylib [shapes] example");defer ray.CloseWindow();ray.SetTargetFPS(60);// Define texture coordinates to map our texture to polyconst texcoords = [_]ray.Vector2{.{ .x = 0.75 },.{ .x = 0.25 },.{ .y = 0.5 },.{ .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 },};// Define the base poly vertices from the UV's// NOTE: They can be specified in any other wayvar points: [texcoords.len]ray.Vector2 = undefined;for (texcoords, 0..) |coord, i| {points[i].x = (coord.x - 0.5) * 256.0;points[i].y = (coord.y - 0.5) * 256.0;}// Define the vertices drawing position// NOTE: Initially same as points but updated every framevar positions: [texcoords.len]ray.Vector2 = undefined;for (0..texcoords.len) |i| positions[i] = points[i];// Load texture to be mapped to polyconst texture = ray.LoadTexture("res/cat.png");var angle: f32 = 0.0; // Rotation angle (in degrees)while (!ray.WindowShouldClose()) {// Update// Update points rotation with an angle transform// NOTE: Base points position are not modifiedangle += 1;for (0..texcoords.len) |i|positions[i] = ray.Vector2Rotate(points[i], angle * ray.DEG2RAD);// Drawray.BeginDrawing();defer ray.EndDrawing();ray.ClearBackground(ray.RAYWHITE);ray.DrawText("textured polygon", 20, 20, 20, ray.DARKGRAY);drawTexturePoly(texture, .{.x = @as(f32, @floatFromInt(ray.GetScreenWidth())) / 2.0,.y = @as(f32, @floatFromInt(ray.GetScreenHeight())) / 2.0,}, &positions, &texcoords, ray.WHITE);ray.DrawFPS(screenWidth - 100, 10);}
}// 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: ray.Texture2D, center: ray.Vector2, points: []ray.Vector2, texcoords: []const ray.Vector2, tint: ray.Color) void {ray.rlSetTexture(texture.id);// Texturing is only supported on RL_QUADSray.rlBegin(ray.RL_QUADS);ray.rlColor4ub(tint.r, tint.g, tint.b, tint.a);for (0..texcoords.len - 1) |i| {ray.rlTexCoord2f(0.5, 0.5);ray.rlVertex2f(center.x, center.y);ray.rlTexCoord2f(texcoords[i].x, texcoords[i].y);ray.rlVertex2f(points[i].x + center.x, points[i].y + center.y);ray.rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);ray.rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);ray.rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);ray.rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);}ray.rlEnd();ray.rlSetTexture(0);
}

图片:https://github.com/raysan5/raylib/blob/master/examples/textures/resources/

效果

渲染多边形

总结

渲染多边形。

附录

http://www.jsqmd.com/news/1360215/

相关文章:

  • 2026热门小程序开发公司口碑测评 - 品牌品鉴馆
  • 一文讲清楚YOLO模型训练的各种指标
  • 高温导热油工况如何选流量计?国内主流生产厂家选型参考
  • 2026阳泉富士康相机回收就来毓典奢品汇18617962974全国连锁专业靠谱,闲置相机处理全攻略 - 丽坤奢品汇
  • Unity新输入系统集成专业无人机手柄:自定义HID布局与精准映射实战
  • C++包管理利器Vcpkg:从安装到项目集成实战指南
  • 注意!水下沉管施工测评,江苏佩润技术经验优但服务有提升空间
  • GIS空间分析实战:从数据处理到选址建模的完整Python工作流
  • 如何在3分钟内为OBS添加智能背景移除功能:免费AI虚拟绿幕终极指南
  • Agent通信机制,Agent之间怎么交流以及消息协议设计
  • 为什么需要LangGraph,当链式调用不够用的时候
  • 洪水风险建模技术:GIS与HEC-RAS融合应用指南
  • Path of Building深度解析:从流放之路新手到资深BD构建师的进阶指南
  • 2026 宿州航空职业学院成人大专怎么报名?学信网可查吗?有哪些专业? - 最新资讯
  • 10分钟掌握OBS多平台直播:obs-multi-rtmp插件完整攻略
  • 终端数据安全落地预判:2026下半年四大企业内网防护赛道将迎来规模化普及
  • 为什么工地青睐郑州小金牛电动脚手架?源头工厂自研自产更靠谱
  • 首个采用 Musixmatch Sentinel 检测服务平台:扫描输出结果,识别超 20 万家发行商版权内容
  • 2026年河北沧州靠谱对口单招集训机构评测:致学升学凭精细化服务脱颖而出 - 快乐的大脚123
  • 通过预测“下一步”来发现异常。它会根据“正常”数据学习规律并预测,当实际值与预测值偏差过大时,就判定为异常。AGI思考
  • ncmdump终极指南:免费快速解锁网易云音乐NCM加密格式
  • JKSM:你的3DS游戏存档守护神
  • Excel理财应用02-Excel 股票数据还在手动抄价?3 种自动获取渠道 10 分钟搭好你的行情地基,3 个免费数据源 + 完整代码:Excel 股票数据自动获取
  • 移动端UV动画Shader优化:从精度控制到性能调优实战
  • 线路板制板机进化实录:小批量产线的效率破局点
  • 2026徐汇区管道清洗施工推荐,下水管道疏通,管道高压清洗,管道置换,管道漏水检测,地漏管道疏通施工优选指南! - 品牌商讯
  • 7款图片与PDF互转工具实测盘点:在线免费、手机电脑自带的都帮你筛了一遍
  • Windows更新修复终极指南:三分钟解决所有更新卡顿问题
  • Werewolf VFLEX转接器:无需改造设备,让旧设备用上USB - C供电!
  • 滑动窗口最大值算法:原理、优化与应用