OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
objvisual.cpp
Go to the documentation of this file.
1#include "objvisual.h"
2
3#include <Tempest/Log>
4
5#include "world/world.h"
6#include "game/serialize.h"
9#include "utils/fileext.h"
10
13
15 setType(other.type);
16 other.type = M_None;
17
18 switch(type) {
19 case M_None:
20 break;
21 case M_Mdl:
22 mdl = std::move(other.mdl);
23 break;
24 case M_Mesh:
25 mesh = std::move(other.mesh);
26 case M_Pfx:
27 pfx = std::move(other.pfx);
28 break;
29 case M_Bundle:
30 bundle = std::move(other.bundle);
31 break;
32 }
33 }
34
36 if(this==&other)
37 return *this;
38 setType(other.type);
39 other.type = M_None;
40
41 switch(type) {
42 case M_None:
43 break;
44 case M_Mdl:
45 mdl = std::move(other.mdl);
46 break;
47 case M_Mesh:
48 mesh = std::move(other.mesh);
49 case M_Pfx:
50 pfx = std::move(other.pfx);
51 break;
52 case M_Bundle:
53 bundle = std::move(other.bundle);
54 break;
55 }
56 return *this;
57 }
58
60 cleanup();
61 }
62
63void ObjVisual::save(Serialize& fout, const Interactive& mob) const {
64 if(type==M_Mdl)
65 return mdl.view.save(fout,mob);
66 }
67
69 if(type==M_Mdl)
70 return mdl.view.load(fin,mob);
71 }
72
73void ObjVisual::cleanup() {
74 switch(type) {
75 case M_None:
76 break;
77 case M_Mdl:
78 mdl.~Mdl();
79 break;
80 case M_Mesh:
81 mesh.~Mesh();
82 break;
83 case M_Pfx:
85 break;
86 case M_Bundle:
87 bundle.~VobBundle();
88 break;
89 }
90 type = M_None;
91 }
92
93void ObjVisual::setType(Type t) {
94 if(t==type)
95 return;
96 cleanup();
97 switch(t) {
98 case M_None:
99 break;
100 case M_Mdl:
101 new(&mdl) Mdl();
102 break;
103 case M_Mesh:
104 new (&mesh) Mesh();
105 break;
106 case M_Pfx:
107 new (&pfx) PfxEmitter();
108 break;
109 case M_Bundle:
110 new(&bundle) VobBundle();
111 break;
112 }
113 type = t;
114 }
115
116void ObjVisual::setVisual(const zenkit::IItem& hitem, World& world, bool staticDraw) {
117 cleanup();
118
119 if(FileExt::hasExt(hitem.visual,"ZEN")) {
120 setType(M_Bundle);
121 bundle = VobBundle(world,hitem.visual,(staticDraw ? Vob::Static : Vob::None));
122 } else {
123 setType(M_Mesh);
124 mesh.view = world.addView(hitem);
125 }
126 }
127
128void ObjVisual::setVisual(const zenkit::VirtualObject& vob, World& world, bool staticDraw) {
129 cleanup();
130
131 const bool enableCollision = vob.cd_dynamic; // collide with player
132 std::string_view visName = vob.visual->name; // *.PFX; *.TGA; *.3DS; *.MDS; *.ASC; *.MMS
133
134 if(visName.empty())
135 return;
136
137 // NOTE: .ZEN / M_Bundle is unused only by items - just assert it for now
138 assert(!FileExt::hasExt(visName,"ZEN"));
139
140 switch (vob.visual->type) {
141 case zenkit::VisualType::MESH:
142 case zenkit::VisualType::MULTI_RESOLUTION_MESH: {
143 auto view = Resources::loadMesh(visName);
144 if(!view)
145 return;
146 setType(M_Mesh);
147 mesh.proto = view;
148 if(vob.show_visual) {
149 mesh.view = world.addStaticView(view,staticDraw);
150 mesh.view.setWind(vob.anim_mode,vob.anim_strength);
151 }
152
153 const bool windy = (vob.anim_mode!=zenkit::AnimationType::NONE && vob.anim_strength>0);
154 if(vob.show_visual && enableCollision && !windy) {
155 mesh.physic = PhysicMesh(*view,*world.physic(),false);
156 }
157 break;
158 }
159 case zenkit::VisualType::MODEL:
160 case zenkit::VisualType::MORPH_MESH:{
161 // *.MDS; *.ASC; *.MMS
162 auto visual = std::string(visName);
163 FileExt::exchangeExt(visual,"ASC","MDL");
164
165 auto view = Resources::loadMesh(visual);
166 if(!view)
167 return;
168 setType(M_Mdl);
169 mdl.proto = view;
170 mdl.view.setYTranslationEnable(false);
171 mdl.view.setVisual(view->skeleton.get());
172
173 if(vob.show_visual) {
174 if((view->skeleton==nullptr || view->skeleton->animation()==nullptr) && enableCollision)
175 mdl.view.setVisualBody(world,world.addStaticView(view,true)); else
176 mdl.view.setVisualBody(world,world.addView(view));
177 }
178
179 if(vob.show_visual && enableCollision) {
180 mdl.physic = PhysicMesh(*view,*world.physic(),true);
181 mdl.physic.setSkeleton(view->skeleton.get());
182 }
183 break;
184 }
185 case zenkit::VisualType::DECAL: {
186 // *.TGA
187 if(vob.sprite_camera_facing_mode!=zenkit::SpriteAlignment::NONE) {
188 setType(M_Pfx);
189 pfx = PfxEmitter(world,vob);
190 pfx.setActive(true);
191 pfx.setLooped(true);
192 }
193 else if(auto decal = dynamic_cast<const zenkit::VisualDecal*>(vob.visual.get())) {
194 setType(M_Mesh);
195 mesh.view = world.addDecalView(*decal);
196 }
197 break;
198 }
199 case zenkit::VisualType::PARTICLE_EFFECT: {
200 // *.PFX
201 setType(M_Pfx);
202 pfx = PfxEmitter(world,vob);
203 pfx.setActive(true);
204 pfx.setLooped(true);
205 break;
206 }
207 case zenkit::VisualType::AI_CAMERA:
208 case zenkit::VisualType::UNKNOWN:
209 break;
210 }
211 }
212
213void ObjVisual::setObjMatrix(const Tempest::Matrix4x4& obj) {
214 switch(type) {
215 case M_None:
216 break;
217 case M_Mdl:
218 mdl.view.setObjMatrix(obj,true);
219 mdl.physic.setObjMatrix(obj);
220 break;
221 case M_Mesh:
222 mesh.view.setObjMatrix(obj);
223 mesh.physic.setObjMatrix(obj);
224 break;
225 case M_Pfx:
226 pfx.setObjMatrix(obj);
227 break;
228 case M_Bundle:
229 bundle.setObjMatrix(obj);
230 break;
231 }
232 }
233
234void ObjVisual::setSlotItem(MeshObjects::Mesh&& itm, std::string_view slot) {
235 if(type==M_Mdl)
236 mdl.view.setSlotItem(std::move(itm), slot);
237 }
238
240 switch(type) {
241 case M_None:
242 case M_Pfx:
243 break;
244 case M_Mdl:
245 mdl.physic.setInteractive(it);
246 break;
247 case M_Mesh:
248 mesh.physic.setInteractive(it);
249 break;
250 case M_Bundle:
251 //bundle.setObjMatrix(obj);
252 break;
253 }
254 }
255
256const Animation::Sequence* ObjVisual::startAnimAndGet(std::string_view name, uint64_t tickCount, bool force) {
257 if(type==M_Mdl) {
258 return mdl.view.startAnimAndGet(name,tickCount,force);
259 }
260 return nullptr;
261 }
262
263bool ObjVisual::isAnimExist(std::string_view name) const {
264 if(type==M_Mdl)
265 return mdl.view.isAnimExist(name);
266 return false;
267 }
268
269bool ObjVisual::updateAnimation(Npc* npc, Interactive* mobsi, World& world, uint64_t dt, bool force) {
270 if(type==M_Mdl) {
271 bool ret = mdl.view.updateAnimation(npc,mobsi,world,dt,force);
272 if(ret)
273 mdl.view.syncAttaches();
274 return ret;
275 }
276 return false;
277 }
278
280 if(type==M_Mdl) {
281 mdl.view.processLayers(world);
282 }
283 }
284
286 if(type==M_Mdl)
287 mdl.physic.setPose(mdl.view.pose());
288 if(type==M_Mesh)
289 ;//mesh.physic.setObjMatrix(mesh.view.);
290 }
291
293 if(type==M_Mdl)
294 return mdl.proto;
295 if(type==M_Mesh)
296 return mesh.proto;
297 return nullptr;
298 }
299
300const Tempest::Matrix4x4& ObjVisual::bone(size_t i) const {
301 if(type==M_Mdl)
302 return mdl.view.pose().bone(i);
303 static const Tempest::Matrix4x4 m;
304 return m;
305 }
void setWind(zenkit::AnimationType m, float intensity)
Definition npc.h:25
void setObjMatrix(const Tempest::Matrix4x4 &obj)
void syncPhysics()
void save(Serialize &fout, const Interactive &mob) const
Definition objvisual.cpp:63
void load(Serialize &fin, Interactive &mob)
Definition objvisual.cpp:68
VobBundle bundle
Definition objvisual.h:64
void setSlotItem(MeshObjects::Mesh &&itm, std::string_view slot)
bool updateAnimation(Npc *npc, Interactive *mobsi, World &world, uint64_t dt, bool force)
PfxEmitter pfx
Definition objvisual.h:63
bool isAnimExist(std::string_view name) const
void setVisual(const zenkit::IItem &visual, World &world, bool staticDraw)
void setInteractive(Interactive *it)
const ProtoMesh * protoMesh() const
ObjVisual & operator=(ObjVisual &&other)
Definition objvisual.cpp:35
const Animation::Sequence * startAnimAndGet(std::string_view name, uint64_t tickCount, bool force=false)
void processLayers(World &world)
const Tempest::Matrix4x4 & bone(size_t i) const
Mesh mesh
Definition objvisual.h:62
void setLooped(bool loop)
void setObjMatrix(const Tempest::Matrix4x4 &mt)
void setActive(bool act)
static const ProtoMesh * loadMesh(std::string_view name)
void setObjMatrix(const Tempest::Matrix4x4 &obj)
Definition vobbundle.cpp:12
@ Static
Definition vob.h:16
@ None
Definition vob.h:14
Definition world.h:31
DynamicWorld * physic() const
Definition world.h:81
MeshObjects::Mesh addView(std::string_view visual) const
Definition world.cpp:251
MeshObjects::Mesh addStaticView(const ProtoMesh *visual, bool staticDraw)
Definition world.cpp:271
MeshObjects::Mesh addDecalView(const zenkit::VisualDecal &decal)
Definition world.cpp:279
bool exchangeExt(std::string &s, const char *extIn, const char *extOut)
Definition fileext.h:43
bool hasExt(std::string_view s, const char *extIn)
Definition fileext.h:8