载入中...
搜索中...
未找到
SceneNodeRef.cpp
浏览该文件的文档.
2
3#include "scene/Scene.h"
4#include "scene/SceneHost.h"
5
6#include <glm/glm.hpp>
7
8#include <cmath>
9
10namespace eve::scene {
11namespace {
12
13SceneHost *resolveHost(const std::string &hostName) {
14 Scene *s = Scene::create();
15 if (!s) return nullptr;
16 if (hostName.empty()) return s->current();
17 return s->findHost(hostName);
18}
19
20SceneNode *resolveNode(const std::string &hostName, const std::string &nodeId) {
21 if (nodeId.empty()) return nullptr;
22 SceneHost *h = resolveHost(hostName);
23 return h ? h->findById(nodeId) : nullptr;
24}
25
26glm::vec3 normalizedColumn(const glm::mat4 &m, int col) {
27 glm::vec3 v(m[col]);
28 const float len = glm::length(v);
29 if (len > 1e-8f) v /= len;
30 return v;
31}
32
33} // namespace
34
35bool SceneNodeRef::isValid() const { return resolveNode(hostName_, nodeId_) != nullptr; }
36
37Scene *SceneNodeRef::getScene() const { return Scene::create(); }
38
39bool SceneNodeRef::setPosition(float x, float y, float z) {
40 SceneHost *h = resolveHost(hostName_);
41 SceneNode *n = resolveNode(hostName_, nodeId_);
42 if (!h || !n) return false;
43 n->x = x;
44 n->y = y;
45 n->z = z;
46 h->markSubtreeDirtyById(nodeId_);
47 return true;
48}
49
51 const SceneNode *n = resolveNode(hostName_, nodeId_);
52 return n ? n->x : 0.f;
53}
54
56 const SceneNode *n = resolveNode(hostName_, nodeId_);
57 return n ? n->y : 0.f;
58}
59
61 const SceneNode *n = resolveNode(hostName_, nodeId_);
62 return n ? n->z : 0.f;
63}
64
65std::vector<float> SceneNodeRef::getPosition() const {
66 const SceneNode *n = resolveNode(hostName_, nodeId_);
67 if (!n) return {0.f, 0.f, 0.f};
68 return {n->x, n->y, n->z};
69}
70
71bool SceneNodeRef::setRotation(float yaw, float pitch, float roll) {
72 SceneHost *h = resolveHost(hostName_);
73 SceneNode *n = resolveNode(hostName_, nodeId_);
74 if (!h || !n) return false;
75 n->yaw = yaw;
76 n->pitch = pitch;
77 n->roll = roll;
78 h->markSubtreeDirtyById(nodeId_);
79 return true;
80}
81
83 const SceneNode *n = resolveNode(hostName_, nodeId_);
84 return n ? n->yaw : 0.f;
85}
86
88 const SceneNode *n = resolveNode(hostName_, nodeId_);
89 return n ? n->pitch : 0.f;
90}
91
93 const SceneNode *n = resolveNode(hostName_, nodeId_);
94 return n ? n->roll : 0.f;
95}
96
97std::vector<float> SceneNodeRef::getRotation() const {
98 const SceneNode *n = resolveNode(hostName_, nodeId_);
99 if (!n) return {0.f, 0.f, 0.f};
100 return {n->yaw, n->pitch, n->roll};
101}
102
103bool SceneNodeRef::setScale(float sx, float sy, float sz) {
104 SceneHost *h = resolveHost(hostName_);
105 SceneNode *n = resolveNode(hostName_, nodeId_);
106 if (!h || !n) return false;
107 n->sx = sx;
108 n->sy = sy;
109 n->sz = sz;
110 h->markSubtreeDirtyById(nodeId_);
111 return true;
112}
113
115 const SceneNode *n = resolveNode(hostName_, nodeId_);
116 return n ? n->sx : 0.f;
117}
118
120 const SceneNode *n = resolveNode(hostName_, nodeId_);
121 return n ? n->sy : 0.f;
122}
123
125 const SceneNode *n = resolveNode(hostName_, nodeId_);
126 return n ? n->sz : 0.f;
127}
128
129std::vector<float> SceneNodeRef::getScale() const {
130 const SceneNode *n = resolveNode(hostName_, nodeId_);
131 if (!n) return {0.f, 0.f, 0.f};
132 return {n->sx, n->sy, n->sz};
133}
134
136 SceneNode *n = resolveNode(hostName_, nodeId_);
137 if (!n) return false;
138 n->visible = v;
139 SceneHost *h = resolveHost(hostName_);
140 if (h) h->markSubtreeDirtyById(nodeId_);
141 return true;
142}
143
145 const SceneNode *n = resolveNode(hostName_, nodeId_);
146 return n ? n->visible : false;
147}
148
150 const SceneNode *n = resolveNode(hostName_, nodeId_);
151 return n ? n->world[3][0] : 0.f;
152}
153
155 const SceneNode *n = resolveNode(hostName_, nodeId_);
156 return n ? n->world[3][1] : 0.f;
157}
158
160 const SceneNode *n = resolveNode(hostName_, nodeId_);
161 return n ? n->world[3][2] : 0.f;
162}
163
164std::vector<float> SceneNodeRef::getWorldPosition() const {
165 const SceneNode *n = resolveNode(hostName_, nodeId_);
166 if (!n) return {0.f, 0.f, 0.f};
167 return {n->world[3][0], n->world[3][1], n->world[3][2]};
168}
169
170std::vector<float> SceneNodeRef::getWorldMatrix() const {
171 const SceneNode *n = resolveNode(hostName_, nodeId_);
172 if (!n) return std::vector<float>(16, 0.f);
173 std::vector<float> out;
174 out.reserve(16);
175 for (int c = 0; c < 4; ++c)
176 for (int r = 0; r < 4; ++r) out.push_back(n->world[c][r]);
177 return out;
178}
179
180std::vector<float> SceneNodeRef::getForward() const {
181 const SceneNode *n = resolveNode(hostName_, nodeId_);
182 if (!n) return {0.f, 0.f, 1.f};
183 glm::vec3 v = normalizedColumn(n->world, 2);
184 return {v.x, v.y, v.z};
185}
186
187std::vector<float> SceneNodeRef::getRight() const {
188 const SceneNode *n = resolveNode(hostName_, nodeId_);
189 if (!n) return {1.f, 0.f, 0.f};
190 glm::vec3 v = normalizedColumn(n->world, 0);
191 return {v.x, v.y, v.z};
192}
193
194std::vector<float> SceneNodeRef::getUp() const {
195 const SceneNode *n = resolveNode(hostName_, nodeId_);
196 if (!n) return {0.f, 1.f, 0.f};
197 glm::vec3 v = normalizedColumn(n->world, 1);
198 return {v.x, v.y, v.z};
199}
200
201std::string SceneNodeRef::getParentId() const {
202 SceneHost *h = resolveHost(hostName_);
203 if (!h) return {};
204 SceneNode *p = h->getParentById(nodeId_);
205 return p ? p->id : std::string{};
206}
207
209 SceneHost *h = resolveHost(hostName_);
210 return h ? h->getChildCountById(nodeId_) : 0;
211}
212
213std::string SceneNodeRef::getChildIdAt(int ordinal) const {
214 SceneHost *h = resolveHost(hostName_);
215 if (!h) return {};
216 SceneNode *c = h->getChildAtById(nodeId_, ordinal);
217 return c ? c->id : std::string{};
218}
219
220std::string SceneNodeRef::getPath() const {
221 SceneHost *h = resolveHost(hostName_);
222 return h ? h->getPathById(nodeId_) : std::string{};
223}
224
225} // namespace eve::scene
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
glm::vec3 n
Definition Grass.cpp:64
int h
uint32_t c
glm::vec4 p[6]
int v
float m[16]
uint32_t s
Definition Weather.cpp:28
ECS mount point for one scene graph (full scene or nested subtree root). Isomorphic to eve::ui::UIHos...
Definition SceneHost.h:80
float getWorldPositionZ() const
std::vector< float > getForward() const
Normalized local axes in world space (forward = +Z).
bool isValid() const
True when host + node currently resolve.
float getWorldPositionX() const
float getWorldPositionY() const
float getRotationPitch() const
Scene * getScene() const
The eve.Scene module instance (for entity binding forwarding).
std::string getPath() const
std::string getChildIdAt(int ordinal) const
std::vector< float > getPosition() const
std::vector< float > getWorldPosition() const
std::vector< float > getRotation() const
bool setPosition(float x, float y, float z)
bool setScale(float sx, float sy, float sz)
float getRotationRoll() const
std::vector< float > getScale() const
std::string getParentId() const
std::vector< float > getUp() const
bool setRotation(float yaw, float pitch, float roll)
std::vector< float > getRight() const
std::vector< float > getWorldMatrix() const
Column-major 4x4 world matrix, 16 floats.
Declarative scene module (eve.Scene).
Definition Scene.h:48
Retained scene node (arena). Conceptual GameObject; isomorphic to eve::ui::UINode.
Definition SceneHost.h:39