载入中...
搜索中...
未找到
Batcher.cpp
浏览该文件的文档.
1#include "graphics/Batcher.h"
2#include "zeroerr/assert.h"
3
4#include <cmath>
5
6namespace eve::graphics {
7
8void Batcher::clear() { verts.clear(); }
9
10void Batcher::addRect(float x, float y, float w, float h, const Color &color) {
11 addTexturedRect(x, y, w, h, color, 0, 0, 1, 1);
12}
13
14void Batcher::addTexturedRect(float x, float y, float w, float h, const Color &color,
15 float u0, float v0, float u1, float v1, bool rotatedUV) {
16 BatchVertex v0p, v1p, v2p, v3p;
17 if (rotatedUV) {
18 // Atlas-packed 90°-rotated region. Following spine-runtimes UV layout:
19 // quad top edge (TL→TR) samples the texture's left column (v1→v0),
20 // and quad left edge (TL→BL) samples the texture's U axis (u0→u1),
21 // i.e. the quad renders the packed region un-rotated 90°.
22 // TL←(u0,v1), TR←(u0,v0), BL←(u1,v1), BR←(u1,v0)
23 v0p = BatchVertex{{x, y}, color, {u0, v1}};
24 v1p = BatchVertex{{x + w, y}, color, {u0, v0}};
25 v2p = BatchVertex{{x, y + h}, color, {u1, v1}};
26 v3p = BatchVertex{{x + w, y + h}, color, {u1, v0}};
27 } else {
28 v0p = BatchVertex{{x, y}, color, {u0, v0}};
29 v1p = BatchVertex{{x + w, y}, color, {u1, v0}};
30 v2p = BatchVertex{{x, y + h}, color, {u0, v1}};
31 v3p = BatchVertex{{x + w, y + h}, color, {u1, v1}};
32 }
33 verts.push_back(v0p);
34 verts.push_back(v1p);
35 verts.push_back(v2p);
36 verts.push_back(v1p);
37 verts.push_back(v3p);
38 verts.push_back(v2p);
39}
40
41void Batcher::addTexturedRectRotated(float cx, float cy, float w, float h, float degrees,
42 const Color &color, float u0, float v0, float u1, float v1,
43 bool rotatedUV) {
44 const float rad = degrees * static_cast<float>(3.14159265358979323846) / 180.0f;
45 const float cos = std::cos(rad);
46 const float sin = std::sin(rad);
47 const float hw = w * 0.5f;
48 const float hh = h * 0.5f;
49
50 auto corner = [&](float dx, float dy) -> glm::vec2 {
51 return {cx + dx * cos - dy * sin, cy + dx * sin + dy * cos};
52 };
53 const glm::vec2 tl = corner(-hw, -hh);
54 const glm::vec2 tr = corner(hw, -hh);
55 const glm::vec2 bl = corner(-hw, hh);
56 const glm::vec2 br = corner(hw, hh);
57
58 BatchVertex v0p, v1p, v2p, v3p;
59 if (rotatedUV) {
60 // Atlas-packed 90°-rotated region: same corner→UV swizzle as
61 // addTexturedRect (see comment there).
62 v0p = BatchVertex{tl, color, {u0, v1}};
63 v1p = BatchVertex{tr, color, {u0, v0}};
64 v2p = BatchVertex{bl, color, {u1, v1}};
65 v3p = BatchVertex{br, color, {u1, v0}};
66 } else {
67 v0p = BatchVertex{tl, color, {u0, v0}};
68 v1p = BatchVertex{tr, color, {u1, v0}};
69 v2p = BatchVertex{bl, color, {u0, v1}};
70 v3p = BatchVertex{br, color, {u1, v1}};
71 }
72 verts.push_back(v0p);
73 verts.push_back(v1p);
74 verts.push_back(v2p);
75 verts.push_back(v1p);
76 verts.push_back(v3p);
77 verts.push_back(v2p);
78}
79
80void Batcher::addRectRotated(float cx, float cy, float w, float h, float degrees,
81 const Color &color) {
82 const float rad = degrees * static_cast<float>(3.14159265358979323846) / 180.0f;
83 const float cos = std::cos(rad);
84 const float sin = std::sin(rad);
85 const float hw = w * 0.5f;
86 const float hh = h * 0.5f;
87
88 auto corner = [&](float dx, float dy) -> glm::vec2 {
89 return {cx + dx * cos - dy * sin, cy + dx * sin + dy * cos};
90 };
91 const glm::vec2 tl = corner(-hw, -hh);
92 const glm::vec2 tr = corner(hw, -hh);
93 const glm::vec2 bl = corner(-hw, hh);
94 const glm::vec2 br = corner(hw, hh);
95
96 BatchVertex v0p{tl, color, {0.f, 0.f}};
97 BatchVertex v1p{tr, color, {1.f, 0.f}};
98 BatchVertex v2p{bl, color, {0.f, 1.f}};
99 BatchVertex v3p{br, color, {1.f, 1.f}};
100 verts.push_back(v0p);
101 verts.push_back(v1p);
102 verts.push_back(v2p);
103 verts.push_back(v1p);
104 verts.push_back(v3p);
105 verts.push_back(v2p);
106}
107
108void Batcher::toNDC(int logicalW, int logicalH) {
109 ASSERT_GT(logicalW, 0);
110 ASSERT_GT(logicalH, 0);
111 if (logicalW <= 0 || logicalH <= 0) return;
112 const float sx = 2.0f / float(logicalW);
113 const float sy = 2.0f / float(logicalH);
114 for (auto &v : verts) {
115 // Vulkan NDC: (-1,-1) = top-left, (+1,+1) = bottom-right (Y down).
116 // Logical coords are also Y-down with origin at top-left.
117 v.pos.x = v.pos.x * sx - 1.0f;
118 v.pos.y = v.pos.y * sy - 1.0f;
119 }
120}
121
122} // namespace eve::graphics
float cx
Definition CardTypes.cpp:31
float cy
Definition CardTypes.cpp:32
float degrees
Definition CardTypes.cpp:33
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
int h
int w
float tr
int v
image::ImageData::Colorf color
void addRect(float x, float y, float w, float h, const Color &color)
Definition Batcher.cpp:10
void addRectRotated(float cx, float cy, float w, float h, float degrees, const Color &color)
Definition Batcher.cpp:80
void addTexturedRect(float x, float y, float w, float h, const Color &color, float u0, float v0, float u1, float v1, bool rotatedUV=false)
Definition Batcher.cpp:14
void addTexturedRectRotated(float cx, float cy, float w, float h, float degrees, const Color &color, float u0, float v0, float u1, float v1, bool rotatedUV=false)
Textured quad rotated degrees (clockwise, screen Y-down) around (cx, cy).
Definition Batcher.cpp:41
void toNDC(int logicalW, int logicalH)
Definition Batcher.cpp:108
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition AnimTrail.h:6
glm::vec4 Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13
CPU-side vertex for 2D batching (logical or NDC depending on stage).
Definition Batcher.h:12