Model3D.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. #include "Model3D.h"
  2. #include "Animation3D.h"
  3. #include "Bild.h"
  4. #include "DXBuffer.h"
  5. #include "Model2D.h"
  6. #include "Textur.h"
  7. #include "Welt3D.h"
  8. #ifdef WIN32
  9. # include <d3d11.h>
  10. #endif
  11. #include <stdexcept>
  12. using namespace Framework;
  13. // Constructor
  14. // \param id the id of the bone
  15. Bone::Bone(int id)
  16. {
  17. pos = Vec3<float>(0, 0, 0);
  18. rot = Vec3<float>(0, 0, 0);
  19. sibling = 0;
  20. child = 0;
  21. this->id = id;
  22. }
  23. //! Destructor
  24. Bone::~Bone()
  25. {
  26. delete sibling;
  27. delete child;
  28. }
  29. //! set the position of the bone relative to the parent bone
  30. //! \param pos the position
  31. void Bone::setPosition(const Vec3<float>& pos)
  32. {
  33. this->pos = pos;
  34. }
  35. //! Set the rotation of the bone relative to the parent bone
  36. //! \param rot thr rotation
  37. void Bone::setRotation(const Vec3<float>& rot)
  38. {
  39. this->rot = rot;
  40. }
  41. //! add a sibling bone to this bone that shares the same parent bone
  42. //! \param b Der Knochen, der hinzugefügt werden soll
  43. void Bone::addSiblingBone(Bone* b)
  44. {
  45. if (!sibling)
  46. sibling = b;
  47. else
  48. sibling->addSiblingBone(b);
  49. }
  50. //! add a child bone to a specific child bone
  51. //! \param id the id of the bone the new bone should be a child of
  52. //! \param b the bone that should be added
  53. bool Bone::addChildBone(int id, Bone* b)
  54. {
  55. if (this->id == id)
  56. {
  57. if (!child)
  58. {
  59. child = b;
  60. return 1;
  61. }
  62. else
  63. {
  64. child->addSiblingBone(b);
  65. return 1;
  66. }
  67. }
  68. else
  69. {
  70. if (child)
  71. {
  72. if (child->addChildBone(id, b))
  73. {
  74. return 1;
  75. }
  76. }
  77. else if (sibling)
  78. {
  79. return sibling->addChildBone(id, b);
  80. }
  81. return 0;
  82. }
  83. }
  84. //! calculates the matrixes of this bone, all child bones and sibling
  85. //! bones
  86. //! \param elternMat the already calculated matrix of the parent bone
  87. //! \param matBuffer the array to store the calculated matrixes
  88. //! \param scaleFactor the scaling of the object
  89. //! \param camMatrix the view-projection matrix of the used camera
  90. void Bone::calculateMatrix(const Mat4<float>& elternMat,
  91. Mat4<float>* matBuffer,
  92. float scaleFactor,
  93. const Mat4<float>& kamMat)
  94. {
  95. if (sibling)
  96. sibling->calculateMatrix(elternMat, matBuffer, scaleFactor, kamMat);
  97. matBuffer[id]
  98. = matBuffer[id].translation(pos * scaleFactor)
  99. * matBuffer[id].rotationZ(rot.z) * matBuffer[id].rotationX(rot.x)
  100. * matBuffer[id].rotationY(rot.y) * matBuffer[id].scaling(scaleFactor);
  101. matBuffer[id] = elternMat * matBuffer[id];
  102. if (child)
  103. child->calculateMatrix(matBuffer[id], matBuffer, scaleFactor, kamMat);
  104. matBuffer[id] = kamMat * matBuffer[id];
  105. }
  106. Bone* Framework::Bone::zFirstSibling() const
  107. {
  108. return sibling;
  109. }
  110. Bone* Framework::Bone::zFirstChild() const
  111. {
  112. return child;
  113. }
  114. //! returns a copy of this bone with copies of all child and
  115. //! sibling bones
  116. Bone* Bone::copyBone() const
  117. {
  118. Bone* ret = new Bone(id);
  119. ret->pos = pos;
  120. ret->rot = rot;
  121. if (sibling) ret->sibling = sibling->copyBone();
  122. if (child) ret->child = child->copyBone();
  123. return ret;
  124. }
  125. //! \return the id of this bone
  126. int Bone::getId() const
  127. {
  128. return id;
  129. }
  130. //! \return the rotation of this bone
  131. Vec3<float> Bone::getRotation() const
  132. {
  133. return rot;
  134. }
  135. //! \return the position of this bone
  136. Vec3<float> Bone::getPosition() const
  137. {
  138. return pos;
  139. }
  140. //! \return the radius of this bone
  141. float Bone::getRadius() const
  142. {
  143. float r = pos.getLength();
  144. if (sibling) r = MAX(r, sibling->getRadius());
  145. if (child) r += child->getRadius();
  146. return r;
  147. }
  148. // Inhalt der Skelett Klasse
  149. // Constructor
  150. Skeleton::Skeleton()
  151. : ReferenceCounter()
  152. {
  153. rootBone = 0;
  154. nextId = 0;
  155. }
  156. // Destructor
  157. Skeleton::~Skeleton()
  158. {
  159. if (rootBone) delete rootBone;
  160. }
  161. Bone* Skeleton::zBone(Bone* zCurrent, int id) const
  162. {
  163. while (zCurrent)
  164. {
  165. if (zCurrent->getId() == id)
  166. {
  167. return zCurrent;
  168. }
  169. if (zCurrent->zFirstChild())
  170. {
  171. Bone* ret = zBone(zCurrent->zFirstChild(), id);
  172. if (ret) return ret;
  173. }
  174. zCurrent = zCurrent->zFirstSibling();
  175. }
  176. return 0;
  177. }
  178. //! add a bone to the sceleton
  179. //! \param pos the position of the bone
  180. //! \param rot the rotation of the bone
  181. //! \param the id of the parent bone where the new bone should be added
  182. //! as a child
  183. //! \return the id of the added bone or -1 if the bone could not be
  184. //! added
  185. int Skeleton::addBone(Vec3<float> pos, Vec3<float> rot, int parentId)
  186. {
  187. if (parentId == -1)
  188. {
  189. if (!this->rootBone)
  190. {
  191. this->rootBone = new Bone(nextId++);
  192. this->rootBone->setPosition(pos);
  193. this->rootBone->setRotation(rot);
  194. return this->rootBone->getId();
  195. }
  196. else
  197. {
  198. Bone* bone = new Bone(nextId++);
  199. bone->setPosition(pos);
  200. bone->setRotation(rot);
  201. this->rootBone->addSiblingBone(bone);
  202. return bone->getId();
  203. }
  204. }
  205. else
  206. {
  207. if (!this->rootBone) return -1;
  208. Bone* bone = new Bone(nextId++);
  209. bone->setPosition(pos);
  210. bone->setRotation(rot);
  211. if (rootBone->addChildBone(parentId, bone))
  212. {
  213. return bone->getId();
  214. }
  215. else
  216. {
  217. nextId--;
  218. return -1;
  219. }
  220. }
  221. }
  222. //! calculates the matrices of all bones in this sceleton
  223. //! \param modelMatrix the already calculated matrix of the used 3d
  224. //! model \param matBuffer the array to store the calculated matrixes
  225. //! \param scaleFactor the scaling of the object
  226. //! \param camMatrix the view-projection matrix of the used camera
  227. int Skeleton::calculateMatrix(const Mat4<float>& modelMatrix,
  228. Mat4<float>* matBuffer,
  229. float scaleFactor,
  230. const Mat4<float>& kamMatrix)
  231. {
  232. rootBone->calculateMatrix(modelMatrix, matBuffer, scaleFactor, kamMatrix);
  233. return nextId;
  234. }
  235. //! \return the radius of the sceleton
  236. float Skeleton::getRadius() const
  237. {
  238. if (rootBone) return rootBone->getRadius();
  239. return 0;
  240. }
  241. //! \return the root bone of the sceleton
  242. Bone* Framework::Skeleton::zRootBone() const
  243. {
  244. return rootBone;
  245. }
  246. //! \return the bone with a specific id
  247. Bone* Framework::Skeleton::zBone(int id) const
  248. {
  249. if (!rootBone) return 0;
  250. return zBone(rootBone, id);
  251. }
  252. //! \return a deep copy of the sceleton
  253. Skeleton* Skeleton::copySceleton() const
  254. {
  255. Skeleton* ret = new Skeleton();
  256. ret->nextId = nextId;
  257. if (rootBone) ret->rootBone = rootBone->copyBone();
  258. return ret;
  259. }
  260. //! \return the next id for a bone ther can be only MAX_KNOCHEN_ANZ
  261. //! bones in a sceleton. if the sceleton is full -1 is returned
  262. int Framework::Skeleton::getNextBoneId() const
  263. {
  264. return nextId;
  265. }
  266. // Inhalt des Polygon3D Struct
  267. // Konstruktor
  268. Polygon3D::Polygon3D()
  269. {
  270. indexAnz = 0;
  271. indexList = 0;
  272. }
  273. // Destruktor
  274. Polygon3D::~Polygon3D()
  275. {
  276. delete[] indexList;
  277. }
  278. // Inhalt der Model3DData Klasse
  279. // Konstruktor
  280. Model3DData::Model3DData(
  281. DXBuffer* dxVertexBuffer, DXBuffer* dxIndexBuffer, int id)
  282. : ReferenceCounter(),
  283. dxIndexBuffer(dxIndexBuffer),
  284. dxVertexBuffer(dxVertexBuffer),
  285. id(id)
  286. {
  287. skelett = 0;
  288. vertexList = 0;
  289. vertexCount = 0;
  290. polygons = new Array<Polygon3D*>();
  291. ambientFactor = 1.f;
  292. diffusFactor = 0.f;
  293. specularFactor = 0.f;
  294. indexCount = 0;
  295. indexBuffer = 0;
  296. radius = 0;
  297. }
  298. // Destruktor
  299. Model3DData::~Model3DData()
  300. {
  301. clearModel();
  302. polygons->release();
  303. if (dxIndexBuffer)
  304. {
  305. dxIndexBuffer->release();
  306. }
  307. if (dxVertexBuffer)
  308. {
  309. dxVertexBuffer->release();
  310. }
  311. delete[] indexBuffer;
  312. }
  313. // updates the DX Buffer gpu memory if changed
  314. DLLEXPORT void Model3DData::updateGPUMemory()
  315. {
  316. dxIndexBuffer->copieren();
  317. dxVertexBuffer->copieren();
  318. }
  319. // Löscht alle Model daten
  320. void Model3DData::clearModel()
  321. {
  322. delete[] vertexList;
  323. vertexCount = 0;
  324. vertexList = 0;
  325. for (Polygon3D* i : *polygons)
  326. delete i;
  327. polygons->leeren();
  328. if (skelett) skelett->release();
  329. skelett = 0;
  330. radius = 0;
  331. delete[] indexBuffer;
  332. indexBuffer = 0;
  333. indexCount = 0;
  334. }
  335. // Berechnet die normalen für die Eckpunkte des Modells
  336. void Model3DData::calculateNormals()
  337. {
  338. for (int i = 0; i < vertexCount; i++)
  339. {
  340. Vec3<float> normal(0, 0, 0);
  341. for (Polygon3D* p : *polygons)
  342. {
  343. int begin = 0;
  344. for (int j = 0; j < p->indexAnz; j++)
  345. {
  346. if (j % 3 == 0) begin = j;
  347. if (p->indexList[j] == i)
  348. {
  349. Vec3<float> a = vertexList[p->indexList[begin]].pos;
  350. Vec3<float> b = vertexList[p->indexList[begin + 1]].pos;
  351. Vec3<float> c = vertexList[p->indexList[begin + 2]].pos;
  352. normal += (b - a).crossProduct(c - a).normalize();
  353. normal.normalize();
  354. }
  355. }
  356. }
  357. vertexList[i].normal = normal;
  358. }
  359. }
  360. //! Erstellt einen buffer für alle polygon indizes
  361. void Model3DData::buildIndexBuffer()
  362. {
  363. delete[] indexBuffer;
  364. indexCount = 0;
  365. for (Polygon3D* p : *polygons)
  366. indexCount += p->indexAnz;
  367. indexBuffer = new int[indexCount];
  368. int current = 0;
  369. for (Polygon3D* p : *polygons)
  370. {
  371. memcpy(indexBuffer + current, p->indexList, sizeof(int) * p->indexAnz);
  372. current += p->indexAnz;
  373. }
  374. dxIndexBuffer->setLength((int)(indexCount * sizeof(int)));
  375. dxIndexBuffer->setData(indexBuffer);
  376. }
  377. // Setzt den Zeiger auf ein standartmäßig verwendete Skelett
  378. // s: Das Skelett, das verwendet werden soll
  379. void Model3DData::setSkelettZ(Skeleton* s)
  380. {
  381. if (skelett) skelett->release();
  382. skelett = s;
  383. }
  384. // Setzt einen Zeiger auf eine Liste mit allen Vertecies des Models
  385. // vertexList: Ein Array mit Vertecies
  386. // anz: Die Anzahl der Vertecies im Array
  387. void Model3DData::setVertecies(Vertex3D* vertexList, int anz)
  388. {
  389. delete[] this->vertexList;
  390. this->vertexList = vertexList;
  391. vertexCount = anz;
  392. maxPos = {-INFINITY, -INFINITY, -INFINITY};
  393. minPos = {INFINITY, INFINITY, INFINITY};
  394. radius = 0;
  395. for (int i = 0; i < anz; i++)
  396. {
  397. float r = vertexList[i].pos.getLength();
  398. if (r > radius) radius = r;
  399. if (vertexList[i].pos.x < minPos.x) minPos.x = vertexList[i].pos.x;
  400. if (vertexList[i].pos.y < minPos.y) minPos.y = vertexList[i].pos.y;
  401. if (vertexList[i].pos.z < minPos.z) minPos.z = vertexList[i].pos.z;
  402. if (vertexList[i].pos.x > maxPos.x) maxPos.x = vertexList[i].pos.x;
  403. if (vertexList[i].pos.y > maxPos.y) maxPos.y = vertexList[i].pos.y;
  404. if (vertexList[i].pos.z > maxPos.z) maxPos.z = vertexList[i].pos.z;
  405. vertexList[i].id = i;
  406. }
  407. dxVertexBuffer->setLength((int)(anz * sizeof(Vertex3D)));
  408. dxVertexBuffer->setData(vertexList);
  409. }
  410. // Fügt ein Polygon zum Model hinzu
  411. // polygon: Das Polygon, das hinzugefügt erden soll
  412. void Model3DData::addPolygon(Polygon3D* polygon)
  413. {
  414. polygons->add(polygon);
  415. buildIndexBuffer();
  416. }
  417. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  418. // wird
  419. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  420. void Model3DData::setAmbientFactor(float f)
  421. {
  422. ambientFactor = f;
  423. }
  424. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  425. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  426. void Model3DData::setDiffusFactor(float f)
  427. {
  428. diffusFactor = f;
  429. }
  430. // Git den Factor an, mit dem die Reflektion von Lichtquellen multipliziert wird
  431. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  432. void Model3DData::setSpecularFactor(float f)
  433. {
  434. specularFactor = f;
  435. }
  436. // Konvertiert ein 2d Model zu 3D
  437. // model: Das 2d Model, das zu 3d konvertiert werden soll
  438. // z: Die z koordinate aller punkte des Models
  439. void Model3DData::copyModel2D(Model2DData* model, float z)
  440. {
  441. if (model && model->vListen && model->polygons)
  442. {
  443. clearModel();
  444. int vAnz = 0;
  445. for (const Polygon2D& p : *model->polygons)
  446. vAnz += p.vertex->getEintragAnzahl();
  447. Vertex3D* vertexList = new Vertex3D[vAnz];
  448. int index = 0;
  449. for (auto i : *model->vListen)
  450. {
  451. Polygon3D* p = new Polygon3D();
  452. p->indexAnz = 0;
  453. for (auto j : *i)
  454. {
  455. for (auto k = j->zListe()->begin();
  456. k && k.hasNext() && k.next().hasNext();
  457. k++)
  458. p->indexAnz += 3;
  459. }
  460. p->indexList = new int[p->indexAnz];
  461. p->indexAnz = 0;
  462. for (auto j : *i)
  463. {
  464. for (auto k = j->zListe()->begin(); k; k++)
  465. {
  466. assert(index < vAnz);
  467. if (index < vAnz)
  468. {
  469. vertexList[index].pos
  470. = Vec3<float>(k->punkt->x, k->punkt->y, z);
  471. vertexList[index].tPos = (Vec2<float>)*k->textur;
  472. if (k.hasNext() && k.next().hasNext())
  473. {
  474. p->indexList[p->indexAnz] = index;
  475. p->indexAnz++;
  476. p->indexList[p->indexAnz] = index + 1;
  477. p->indexAnz++;
  478. p->indexList[p->indexAnz] = index + 2;
  479. p->indexAnz++;
  480. }
  481. }
  482. else
  483. break;
  484. index++;
  485. }
  486. }
  487. addPolygon(p);
  488. }
  489. this->setVertecies(vertexList, vAnz);
  490. buildIndexBuffer();
  491. calculateNormals();
  492. }
  493. }
  494. // Entfernt ein Polygon
  495. // index: Der Index des Polygons
  496. void Model3DData::removePolygon(int index)
  497. {
  498. if (!polygons->hat(index)) return;
  499. delete polygons->get(index);
  500. polygons->remove(index);
  501. buildIndexBuffer();
  502. }
  503. // Berechnet die Matrizen der Knochen
  504. // modelMatrix: Die Matrix, die das Skelett in den Raum der Welt transformiert
  505. // matBuffer: Ein Array von Matrizen, der durch die Knochen Matrizen gefüllt
  506. // wird scaleFactor: Die Skallierung des Modells kamMatrix: Die vereiniegung
  507. // der view und projektions Matrizen return: gibt die Anzahl der verwendeten
  508. // Matrizen zurück
  509. int Model3DData::kalkulateMatrix(const Mat4<float>& modelMatrix,
  510. Mat4<float>* matBuffer,
  511. float scaleFactor,
  512. const Mat4<float>& kamMatrix) const
  513. {
  514. if (!skelett) return 0;
  515. return skelett->calculateMatrix(
  516. modelMatrix, matBuffer, scaleFactor, kamMatrix);
  517. }
  518. // Gibt die Anzahl an Polygonen zurück
  519. int Model3DData::getPolygonAnzahl() const
  520. {
  521. return polygons->getEintragAnzahl();
  522. }
  523. // Gibt ein bestimmtes Polygon zurück
  524. // index: Der Index des Polygons
  525. Polygon3D* Model3DData::getPolygon(int index) const
  526. {
  527. if (!polygons->hat(index)) return 0;
  528. return polygons->get(index);
  529. }
  530. // Gibt einen Iterator zurück, mit dem sich die Polygons auflisten lassen
  531. ArrayIterator<Polygon3D*> Model3DData::getPolygons() const
  532. {
  533. return polygons->begin();
  534. }
  535. // Gibt den radius einer Kugel zurück, die das gesammte Model umschließt
  536. float Model3DData::getRadius() const
  537. {
  538. return radius;
  539. }
  540. // Gibt die Id der Daten zurück, wenn sie in einer Model3DList registriert
  541. // wurden. (siehe Framework::zM3DRegister())
  542. int Model3DData::getId() const
  543. {
  544. return id;
  545. }
  546. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  547. // wird
  548. float Model3DData::getAmbientFactor() const
  549. {
  550. return ambientFactor;
  551. }
  552. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  553. float Model3DData::getDiffusFactor() const
  554. {
  555. return diffusFactor;
  556. }
  557. // Git den Factor an, mit dem die Reflektion von Lichtquellen multipliziert wird
  558. float Model3DData::getSpecularFactor() const
  559. {
  560. return specularFactor;
  561. }
  562. // Gibt eine Kopie des Skeletts zurück, welches für annimationen verwendet
  563. // werden kann
  564. Skeleton* Model3DData::copySkelett() const
  565. {
  566. return skelett ? skelett->copySceleton() : 0;
  567. }
  568. // Gibt die Anzahl an Vertices zurück
  569. int Model3DData::getVertexAnzahl() const
  570. {
  571. return vertexCount;
  572. }
  573. // Gibt einen Buffer mit allen Vertecies des Models zurück
  574. const Vertex3D* Model3DData::zVertexBuffer() const
  575. {
  576. return vertexList;
  577. }
  578. //! Gibt eine refferenz auf den beginn des indexBuffers zurück
  579. const int* Model3DData::getIndexBuffer() const
  580. {
  581. return indexBuffer;
  582. }
  583. //! Gibt eine die Anzahl der indizes im indexBuffer zurück
  584. int Model3DData::getIndexCount() const
  585. {
  586. return indexCount;
  587. }
  588. //! Gibt den Index buffer zurück;
  589. DXBuffer* Model3DData::zDXIndexBuffer() const
  590. {
  591. return dxIndexBuffer;
  592. }
  593. //! Gibt den Vertex buffer zurück;
  594. DXBuffer* Model3DData::zDXVertexBuffer() const
  595. {
  596. return dxVertexBuffer;
  597. }
  598. //! gibt den minnimalen Punkt der Bounding box des Models zurück
  599. Vec3<float> Model3DData::getMinPos() const
  600. {
  601. return minPos;
  602. }
  603. //! gibt den maximalen Punkt der bounding box des Mopdels zurück
  604. Vec3<float> Model3DData::getMaxPos() const
  605. {
  606. return maxPos;
  607. }
  608. // Inhalt der Model3DTextur
  609. // Konstruktor
  610. Model3DTextur::Model3DTextur()
  611. : ReferenceCounter()
  612. {
  613. textures = new Textur*[1];
  614. textures[0] = 0;
  615. textureCount = 1;
  616. }
  617. // Destruktor
  618. Model3DTextur::~Model3DTextur()
  619. {
  620. for (int i = 0; i < textureCount; i++)
  621. {
  622. if (textures[i]) textures[i]->release();
  623. }
  624. delete[] textures;
  625. }
  626. // Legt fest, welche Textur für welches Polygon ist
  627. // pI: Der Index des Polygons
  628. // txt: Die Textur des Polygons
  629. void Model3DTextur::setPolygonTextur(int pI, Textur* txt)
  630. {
  631. if (pI >= textureCount)
  632. {
  633. Textur** tmp = textures;
  634. textures = new Textur*[pI + 1];
  635. memcpy(textures, tmp, sizeof(Textur*) * textureCount);
  636. memset(textures + textureCount,
  637. 0,
  638. sizeof(Textur*) * (pI + 1 - textureCount));
  639. delete[] tmp;
  640. textureCount = pI + 1;
  641. }
  642. if (textures[pI]) textures[pI]->release();
  643. textures[pI] = txt;
  644. }
  645. // Gibt einen Zeiger auf die Textur eines Polygons zurück ohne erhöhten
  646. // Reference Counter
  647. // i: Der Index des Polygons
  648. Textur* Model3DTextur::zPolygonTextur(int i) const
  649. {
  650. if (i >= textureCount) return 0;
  651. return textures[i];
  652. }
  653. // Inhalt der Model3D Klasse
  654. // Konstruktor
  655. Model3D::Model3D()
  656. : Zeichnung3D()
  657. {
  658. model = 0;
  659. textur = 0;
  660. skelett = 0;
  661. ambientFactor = 1.f;
  662. diffusFactor = 0.f;
  663. specularFactor = 0.f;
  664. }
  665. // Destruktor
  666. Model3D::~Model3D()
  667. {
  668. if (model) model->release();
  669. if (textur) textur->release();
  670. if (skelett) skelett->release();
  671. }
  672. // Setzt die Daten des Models
  673. // data: Die Daten
  674. void Model3D::setModelDaten(Model3DData* data)
  675. {
  676. if (model) model->release();
  677. if (skelett) skelett = (Skeleton*)skelett->release();
  678. model = data;
  679. if (model)
  680. {
  681. skelett = model->copySkelett();
  682. this->ambientFactor = model->getAmbientFactor();
  683. this->specularFactor = model->getSpecularFactor();
  684. this->diffusFactor = model->getDiffusFactor();
  685. }
  686. }
  687. // Setzt die zum Zeichnen zu benutzenden Texturen
  688. // txt: Ein Liste mit Texturen zu den verschiedenen Polygonen zugeordnet
  689. void Model3D::setModelTextur(Model3DTextur* txt)
  690. {
  691. if (textur) textur->release();
  692. textur = txt;
  693. }
  694. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  695. // wird
  696. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  697. void Framework::Model3D::setAmbientFactor(float f)
  698. {
  699. this->ambientFactor = f;
  700. }
  701. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  702. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  703. void Framework::Model3D::setDiffusFactor(float f)
  704. {
  705. diffusFactor = f;
  706. }
  707. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  708. // wird
  709. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  710. void Framework::Model3D::setSpecularFactor(float f)
  711. {
  712. specularFactor = f;
  713. }
  714. // Errechnet die Matrizen aller Knochen des Skeletts des Models
  715. // viewProj: Die miteinander multiplizierten Kameramatrizen
  716. // matBuffer: Ein Array mit Matrizen, der gefüllt werden soll
  717. // return: Die Anzahl der Matrizen, die das Model benötigt
  718. int Model3D::errechneMatrizen(
  719. const Mat4<float>& viewProj, Mat4<float>* matBuffer)
  720. {
  721. int ret = 0;
  722. if (skelett)
  723. ret = skelett->calculateMatrix(welt, matBuffer, size, viewProj);
  724. else if (model)
  725. ret = model->kalkulateMatrix(welt, matBuffer, size, viewProj);
  726. if (!ret) return Zeichnung3D::errechneMatrizen(viewProj, matBuffer);
  727. return ret;
  728. }
  729. // Verarbeitet die vergangene Zeit
  730. // tickval: Die zeit in sekunden, die seit dem letzten Aufruf der Funktion
  731. // vergangen ist return: true, wenn sich das Objekt verändert hat, false
  732. // sonnst.
  733. bool Model3D::tick(double tickval)
  734. {
  735. radius = model ? model->getRadius() : 0;
  736. if (skelett)
  737. {
  738. radius += skelett->getRadius();
  739. }
  740. return Zeichnung3D::tick(tickval);
  741. }
  742. //! zum aktualisieren der shader daten
  743. void Model3D::beforeRender(
  744. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  745. {}
  746. void Model3D::afterRender(
  747. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  748. {}
  749. // Gibt die Textur zurück
  750. Model3DTextur* Model3D::getTextur()
  751. {
  752. return textur ? dynamic_cast<Model3DTextur*>(textur->getThis()) : 0;
  753. }
  754. // Gibt die Textur zurück (ohne erhöhten Reference Counter)
  755. Model3DTextur* Model3D::zTextur()
  756. {
  757. return textur;
  758. }
  759. // Gibt die ModelDaten zurück
  760. Model3DData* Model3D::getModelData()
  761. {
  762. return model ? dynamic_cast<Model3DData*>(model->getThis()) : 0;
  763. }
  764. // Gibt die ModelDaten zurück (ohne erhöhten Reference Counter)
  765. Model3DData* Model3D::zModelData()
  766. {
  767. return model;
  768. }
  769. // prüft, ob ein Strahl dieses Objekt trifft
  770. // point: der startpunkt des Strahls in Weltkoordinaten
  771. // dir: die Richtung des Strahls in Weltkoordinaten
  772. // maxSqDist: Die maximale quadratische distanz die erlaubt ist
  773. // pId: die Id des Polygons, zu dem der Schnittpunkt gehört
  774. // return: den quadratischen Abstand des Schnittpunktes zum Ursprung des
  775. // Strahls oder -1, wenn kein schnittpunkt existiert
  776. float Model3D::traceRay(
  777. const Vec3<float>& p, const Vec3<float>& d, float maxSqDist, int& pId) const
  778. {
  779. if (!model) return -1;
  780. Vec3<float> dir = d;
  781. dir.rotateY(-angle.y);
  782. dir.rotateX(-angle.x);
  783. dir.rotateZ(-angle.z);
  784. Vec3<float> point = p;
  785. point.rotateY(-angle.y);
  786. point.rotateX(-angle.x);
  787. point.rotateZ(-angle.z);
  788. point -= pos;
  789. float nearest = (-dir.x * point.x - dir.y * point.y - dir.z * point.z)
  790. / (dir.x * dir.x + dir.y * dir.y + dir.z * dir.z);
  791. float dist = (point + dir * nearest).getLengthSq();
  792. if (dist > (radius * size) * (radius * size)
  793. || (dir * nearest).getLength() - radius * size > sqrt(maxSqDist)
  794. || (nearest < 0
  795. && (dir * nearest).getLengthSq()
  796. > radius * size * radius
  797. * size)) // es gibt kein schnittpunkt
  798. return -1;
  799. bool existsHit = 0;
  800. if (skelett)
  801. { // todo
  802. }
  803. else
  804. {
  805. int index = 0;
  806. for (auto p = model->getPolygons(); p; p++)
  807. {
  808. for (int j = 0; j < p->indexAnz; j++)
  809. {
  810. if (j % 3 == 0)
  811. {
  812. Vec3<float> a = model->zVertexBuffer()[p->indexList[j]].pos;
  813. Vec3<float> b
  814. = model->zVertexBuffer()[p->indexList[j + 1]].pos;
  815. Vec3<float> c
  816. = model->zVertexBuffer()[p->indexList[j + 2]].pos;
  817. Vec3<float> normal
  818. = (b - a).crossProduct(c - a).normalize();
  819. if (normal * dir < 0) // Prüfe ob die Normale in Richtung
  820. // des Strahl ursprungs zeigt
  821. {
  822. nearest
  823. = (a * normal - point * normal) / (dir * normal);
  824. Vec3<float> hit = point + dir * nearest;
  825. if ((b - a).angle(hit - a) <= (b - a).angle(c - a)
  826. && (c - a).angle(hit - a) <= (b - a).angle(c - a)
  827. && (a - b).angle(hit - b) <= (a - b).angle(c - b))
  828. {
  829. maxSqDist = (hit - point).getLengthSq();
  830. pId = index;
  831. existsHit = 1;
  832. }
  833. }
  834. index++;
  835. }
  836. }
  837. }
  838. }
  839. return existsHit ? maxSqDist : -1;
  840. }
  841. // berechnet die Farbe des Schnittpunktes deines Strahls
  842. // point: der startpunkt des Strahls in Weltkoordinaten
  843. // dir: die Richtung des Strahls in Weltkoordinaten
  844. // zWelt: die Welt, aus der der Strahl kommt
  845. // return: die Farbe des Schnittpunktes
  846. int Model3D::traceRay(
  847. Vec3<float>& p, Vec3<float>& d, int pId, Welt3D* zWelt) const
  848. {
  849. Vec3<float> dir = d;
  850. dir.rotateY(-angle.y);
  851. dir.rotateX(-angle.x);
  852. dir.rotateZ(-angle.z);
  853. Vec3<float> point = p;
  854. point.rotateY(-angle.y);
  855. point.rotateX(-angle.x);
  856. point.rotateZ(-angle.z);
  857. point -= pos;
  858. int index = 0;
  859. for (auto p = model->getPolygons(); p; p++, index++)
  860. {
  861. for (int j = 0; j < p->indexAnz; j++)
  862. {
  863. if (j % 3 == 0)
  864. {
  865. if (pId == 0)
  866. {
  867. const Vec3<float>& a
  868. = model->zVertexBuffer()[p->indexList[j]].pos;
  869. const Vec3<float>& b
  870. = model->zVertexBuffer()[p->indexList[j + 1]].pos;
  871. const Vec3<float>& c
  872. = model->zVertexBuffer()[p->indexList[j + 2]].pos;
  873. Vertex at = model->zVertexBuffer()[p->indexList[j]].tPos;
  874. Vertex bt
  875. = model->zVertexBuffer()[p->indexList[j + 1]].tPos;
  876. Vertex ct
  877. = model->zVertexBuffer()[p->indexList[j + 2]].tPos;
  878. Vec3<float> normal
  879. = (b - a).crossProduct(c - a).normalize();
  880. float t = (a * normal - point * normal) / (dir * normal);
  881. Vec3<float> hit = point + dir * t;
  882. float a0 = (a - b).crossProduct(a - c).getLength() / 2;
  883. float a1
  884. = (b - hit).crossProduct(c - hit).getLength() / 2 / a0;
  885. float a2
  886. = (c - hit).crossProduct(a - hit).getLength() / 2 / a0;
  887. float a3
  888. = (a - hit).crossProduct(b - hit).getLength() / 2 / a0;
  889. Vertex ht = at * a1 + bt * a2 + ct * a3;
  890. Bild* tex = textur->zPolygonTextur(index)->zBild();
  891. if (ht.x >= 0 && ht.y >= 0 && ht.x <= 1 && ht.y <= 1)
  892. return tex->getPixel(
  893. (int)(ht.x * ((float)tex->getBreite() - 1.f)
  894. + 0.5f),
  895. (int)(ht.y * ((float)tex->getHeight() - 1.f)
  896. + 0.5f));
  897. return 0xFF000000;
  898. }
  899. pId--;
  900. }
  901. }
  902. }
  903. return 0xFF000000;
  904. }
  905. // Gibt die Id der Daten zurück, wenn sie in einer Model3DList registriert
  906. // wurden. (siehe Framework::zM3DRegister())
  907. int Model3D::getDatenId() const
  908. {
  909. return model ? model->getId() : -1;
  910. }
  911. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  912. // wird
  913. float Model3D::getAmbientFactor() const
  914. {
  915. return ambientFactor;
  916. }
  917. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  918. float Model3D::getDiffusFactor() const
  919. {
  920. return diffusFactor;
  921. }
  922. // Git den Factor an, mit dem die Reflektion von Lichtquellen multipliziert wird
  923. float Model3D::getSpecularFactor() const
  924. {
  925. return specularFactor;
  926. }
  927. // Gibt die Anzahl an Vertices zurück
  928. int Model3D::getVertexAnzahl() const
  929. {
  930. return model ? model->getVertexAnzahl() : 0;
  931. }
  932. // Gibt einen Buffer mit allen Vertecies des Models zurück
  933. const Vertex3D* Model3D::zVertexBuffer() const
  934. {
  935. return model ? model->zVertexBuffer() : 0;
  936. }
  937. //! Gibt true zurück wenn ein bestimmtes polygon gezeichnet werden muss
  938. bool Model3D::needRenderPolygon(int index)
  939. {
  940. return 1;
  941. }
  942. Textur* Model3D::zEffectTextur()
  943. {
  944. return 0;
  945. }
  946. float Model3D::getEffectPercentage()
  947. {
  948. return 0;
  949. }