载入中...
搜索中...
未找到
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::addTriangle(glm::vec2 a, glm::vec2 b, glm::vec2 c, const Color &color) {
16}
17
18void Batcher::addTriangle(glm::vec2 a, glm::vec2 b, glm::vec2 c, const Color &colorA, const Color &colorB,
19 const Color &colorC) {
20 verts.push_back(BatchVertex{a, colorA, {0.f, 0.f}});
21 verts.push_back(BatchVertex{b, colorB, {0.f, 0.f}});
22 verts.push_back(BatchVertex{c, colorC, {0.f, 0.f}});
23}
24
25void Batcher::addTexturedRect(float x, float y, float w, float h, const Color &color,
26 float u0, float v0, float u1, float v1, bool rotatedUV) {
27 BatchVertex v0p, v1p, v2p, v3p;
28 if (rotatedUV) {
29 // Atlas-packed 90°-rotated region. Following spine-runtimes UV layout:
30 // quad top edge (TL→TR) samples the texture's left column (v1→v0),
31 // and quad left edge (TL→BL) samples the texture's U axis (u0→u1),
32 // i.e. the quad renders the packed region un-rotated 90°.
33 // TL←(u0,v1), TR←(u0,v0), BL←(u1,v1), BR←(u1,v0)
34 v0p = BatchVertex{{x, y}, color, {u0, v1}};
35 v1p = BatchVertex{{x + w, y}, color, {u0, v0}};
36 v2p = BatchVertex{{x, y + h}, color, {u1, v1}};
37 v3p = BatchVertex{{x + w, y + h}, color, {u1, v0}};
38 } else {
39 v0p = BatchVertex{{x, y}, color, {u0, v0}};
40 v1p = BatchVertex{{x + w, y}, color, {u1, v0}};
41 v2p = BatchVertex{{x, y + h}, color, {u0, v1}};
42 v3p = BatchVertex{{x + w, y + h}, color, {u1, v1}};
43 }
44 verts.push_back(v0p);
45 verts.push_back(v1p);
46 verts.push_back(v2p);
47 verts.push_back(v1p);
48 verts.push_back(v3p);
49 verts.push_back(v2p);
50}
51
52void Batcher::addTexturedRectRotated(float cx, float cy, float w, float h, float degrees,
53 const Color &color, float u0, float v0, float u1, float v1,
54 bool rotatedUV) {
55 const float rad = degrees * static_cast<float>(3.14159265358979323846) / 180.0f;
56 const float cos = std::cos(rad);
57 const float sin = std::sin(rad);
58 const float hw = w * 0.5f;
59 const float hh = h * 0.5f;
60
61 auto corner = [&](float dx, float dy) -> glm::vec2 {
62 return {cx + dx * cos - dy * sin, cy + dx * sin + dy * cos};
63 };
64 const glm::vec2 tl = corner(-hw, -hh);
65 const glm::vec2 tr = corner(hw, -hh);
66 const glm::vec2 bl = corner(-hw, hh);
67 const glm::vec2 br = corner(hw, hh);
68
69 BatchVertex v0p, v1p, v2p, v3p;
70 if (rotatedUV) {
71 // Atlas-packed 90°-rotated region: same corner→UV swizzle as
72 // addTexturedRect (see comment there).
73 v0p = BatchVertex{tl, color, {u0, v1}};
74 v1p = BatchVertex{tr, color, {u0, v0}};
75 v2p = BatchVertex{bl, color, {u1, v1}};
76 v3p = BatchVertex{br, color, {u1, v0}};
77 } else {
78 v0p = BatchVertex{tl, color, {u0, v0}};
79 v1p = BatchVertex{tr, color, {u1, v0}};
80 v2p = BatchVertex{bl, color, {u0, v1}};
81 v3p = BatchVertex{br, color, {u1, v1}};
82 }
83 verts.push_back(v0p);
84 verts.push_back(v1p);
85 verts.push_back(v2p);
86 verts.push_back(v1p);
87 verts.push_back(v3p);
88 verts.push_back(v2p);
89}
90
91void Batcher::addRectRotated(float cx, float cy, float w, float h, float degrees,
92 const Color &color) {
93 const float rad = degrees * static_cast<float>(3.14159265358979323846) / 180.0f;
94 const float cos = std::cos(rad);
95 const float sin = std::sin(rad);
96 const float hw = w * 0.5f;
97 const float hh = h * 0.5f;
98
99 auto corner = [&](float dx, float dy) -> glm::vec2 {
100 return {cx + dx * cos - dy * sin, cy + dx * sin + dy * cos};
101 };
102 const glm::vec2 tl = corner(-hw, -hh);
103 const glm::vec2 tr = corner(hw, -hh);
104 const glm::vec2 bl = corner(-hw, hh);
105 const glm::vec2 br = corner(hw, hh);
106
107 BatchVertex v0p{tl, color, {0.f, 0.f}};
108 BatchVertex v1p{tr, color, {1.f, 0.f}};
109 BatchVertex v2p{bl, color, {0.f, 1.f}};
110 BatchVertex v3p{br, color, {1.f, 1.f}};
111 verts.push_back(v0p);
112 verts.push_back(v1p);
113 verts.push_back(v2p);
114 verts.push_back(v1p);
115 verts.push_back(v3p);
116 verts.push_back(v2p);
117}
118
119void Batcher::toNDC(int logicalW, int logicalH) {
120 ASSERT_GT(logicalW, 0);
121 ASSERT_GT(logicalH, 0);
122 if (logicalW <= 0 || logicalH <= 0) return;
123 const float sx = 2.0f / float(logicalW);
124 const float sy = 2.0f / float(logicalH);
125 for (auto &v : verts) {
126 // Vulkan NDC: (-1,-1) = top-left, (+1,+1) = bottom-right (Y down).
127 // Logical coords are also Y-down with origin at top-left.
128 v.pos.x = v.pos.x * sx - 1.0f;
129 v.pos.y = v.pos.y * sy - 1.0f;
130 }
131}
132
133} // namespace eve::graphics
float w
Definition AnimClip.cpp:738
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
float degrees
Definition CardTypes.cpp:35
float v
std::int32_t c
int h
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float tr
float dy
float dx
void addRect(float x, float y, float w, float h, const Color &color)
Adds rect.
Definition Batcher.cpp:10
void addTriangle(glm::vec2 a, glm::vec2 b, glm::vec2 c, const Color &color)
Adds one arbitrary solid triangle in logical Canvas coordinates.
Definition Batcher.cpp:14
void addRectRotated(float cx, float cy, float w, float h, float degrees, const Color &color)
Adds rect rotated.
Definition Batcher.cpp:91
void addTexturedRect(float x, float y, float w, float h, const Color &color, float u0, float v0, float u1, float v1, bool rotatedUV=false)
Adds textured rect.
Definition Batcher.cpp:25
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:52
void toNDC(int logicalW, int logicalH)
To ndc.
Definition Batcher.cpp:119
void clear()
Clears .
Definition Batcher.cpp:8
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
eve::Color 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:14
glm::vec4 color