Model3D.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. if (dxIndexBuffer)
  375. {
  376. dxIndexBuffer->setLength((int)(indexCount * sizeof(int)));
  377. dxIndexBuffer->setData(indexBuffer);
  378. }
  379. }
  380. // Setzt den Zeiger auf ein standartmäßig verwendete Skelett
  381. // s: Das Skelett, das verwendet werden soll
  382. void Model3DData::setSkelettZ(Skeleton* s)
  383. {
  384. if (skelett) skelett->release();
  385. skelett = s;
  386. }
  387. // Setzt einen Zeiger auf eine Liste mit allen Vertecies des Models
  388. // vertexList: Ein Array mit Vertecies
  389. // anz: Die Anzahl der Vertecies im Array
  390. void Model3DData::setVertecies(Vertex3D* vertexList, int anz)
  391. {
  392. delete[] this->vertexList;
  393. this->vertexList = vertexList;
  394. vertexCount = anz;
  395. maxPos = {-INFINITY, -INFINITY, -INFINITY};
  396. minPos = {INFINITY, INFINITY, INFINITY};
  397. radius = 0;
  398. for (int i = 0; i < anz; i++)
  399. {
  400. float r = vertexList[i].pos.getLength();
  401. if (r > radius) radius = r;
  402. if (vertexList[i].pos.x < minPos.x) minPos.x = vertexList[i].pos.x;
  403. if (vertexList[i].pos.y < minPos.y) minPos.y = vertexList[i].pos.y;
  404. if (vertexList[i].pos.z < minPos.z) minPos.z = vertexList[i].pos.z;
  405. if (vertexList[i].pos.x > maxPos.x) maxPos.x = vertexList[i].pos.x;
  406. if (vertexList[i].pos.y > maxPos.y) maxPos.y = vertexList[i].pos.y;
  407. if (vertexList[i].pos.z > maxPos.z) maxPos.z = vertexList[i].pos.z;
  408. vertexList[i].id = i;
  409. }
  410. if (dxVertexBuffer)
  411. {
  412. dxVertexBuffer->setLength((int)(anz * sizeof(Vertex3D)));
  413. dxVertexBuffer->setData(vertexList);
  414. }
  415. }
  416. // Fügt ein Polygon zum Model hinzu
  417. // polygon: Das Polygon, das hinzugefügt erden soll
  418. void Model3DData::addPolygon(Polygon3D* polygon)
  419. {
  420. polygons->add(polygon);
  421. buildIndexBuffer();
  422. }
  423. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  424. // wird
  425. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  426. void Model3DData::setAmbientFactor(float f)
  427. {
  428. ambientFactor = f;
  429. }
  430. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  431. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  432. void Model3DData::setDiffusFactor(float f)
  433. {
  434. diffusFactor = f;
  435. }
  436. // Git den Factor an, mit dem die Reflektion von Lichtquellen multipliziert wird
  437. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  438. void Model3DData::setSpecularFactor(float f)
  439. {
  440. specularFactor = f;
  441. }
  442. // Konvertiert ein 2d Model zu 3D
  443. // model: Das 2d Model, das zu 3d konvertiert werden soll
  444. // z: Die z koordinate aller punkte des Models
  445. void Model3DData::copyModel2D(Model2DData* model, float z)
  446. {
  447. if (model && model->vListen && model->polygons)
  448. {
  449. clearModel();
  450. int vAnz = 0;
  451. for (const Polygon2D& p : *model->polygons)
  452. vAnz += p.vertex->getEintragAnzahl();
  453. Vertex3D* vertexList = new Vertex3D[vAnz];
  454. int index = 0;
  455. for (auto i : *model->vListen)
  456. {
  457. Polygon3D* p = new Polygon3D();
  458. p->indexAnz = 0;
  459. for (auto j : *i)
  460. {
  461. for (auto k = j->zListe()->begin();
  462. k && k.hasNext() && k.next().hasNext();
  463. k++)
  464. p->indexAnz += 3;
  465. }
  466. p->indexList = new int[p->indexAnz];
  467. p->indexAnz = 0;
  468. for (auto j : *i)
  469. {
  470. for (auto k = j->zListe()->begin(); k; k++)
  471. {
  472. assert(index < vAnz);
  473. if (index < vAnz)
  474. {
  475. vertexList[index].pos
  476. = Vec3<float>(k->punkt->x, k->punkt->y, z);
  477. vertexList[index].tPos = (Vec2<float>)*k->textur;
  478. if (k.hasNext() && k.next().hasNext())
  479. {
  480. p->indexList[p->indexAnz] = index;
  481. p->indexAnz++;
  482. p->indexList[p->indexAnz] = index + 1;
  483. p->indexAnz++;
  484. p->indexList[p->indexAnz] = index + 2;
  485. p->indexAnz++;
  486. }
  487. }
  488. else
  489. break;
  490. index++;
  491. }
  492. }
  493. addPolygon(p);
  494. }
  495. this->setVertecies(vertexList, vAnz);
  496. buildIndexBuffer();
  497. calculateNormals();
  498. }
  499. }
  500. // Entfernt ein Polygon
  501. // index: Der Index des Polygons
  502. void Model3DData::removePolygon(int index)
  503. {
  504. if (!polygons->hat(index)) return;
  505. delete polygons->get(index);
  506. polygons->remove(index);
  507. buildIndexBuffer();
  508. }
  509. // Berechnet die Matrizen der Knochen
  510. // modelMatrix: Die Matrix, die das Skelett in den Raum der Welt transformiert
  511. // matBuffer: Ein Array von Matrizen, der durch die Knochen Matrizen gefüllt
  512. // wird scaleFactor: Die Skallierung des Modells kamMatrix: Die vereiniegung
  513. // der view und projektions Matrizen return: gibt die Anzahl der verwendeten
  514. // Matrizen zurück
  515. int Model3DData::kalkulateMatrix(const Mat4<float>& modelMatrix,
  516. Mat4<float>* matBuffer,
  517. float scaleFactor,
  518. const Mat4<float>& kamMatrix) const
  519. {
  520. if (!skelett) return 0;
  521. return skelett->calculateMatrix(
  522. modelMatrix, matBuffer, scaleFactor, kamMatrix);
  523. }
  524. // Gibt die Anzahl an Polygonen zurück
  525. int Model3DData::getPolygonAnzahl() const
  526. {
  527. return polygons->getEintragAnzahl();
  528. }
  529. // Gibt ein bestimmtes Polygon zurück
  530. // index: Der Index des Polygons
  531. Polygon3D* Model3DData::getPolygon(int index) const
  532. {
  533. if (!polygons->hat(index)) return 0;
  534. return polygons->get(index);
  535. }
  536. // Gibt einen Iterator zurück, mit dem sich die Polygons auflisten lassen
  537. ArrayIterator<Polygon3D*> Model3DData::getPolygons() const
  538. {
  539. return polygons->begin();
  540. }
  541. // Gibt den radius einer Kugel zurück, die das gesammte Model umschließt
  542. float Model3DData::getRadius() const
  543. {
  544. return radius;
  545. }
  546. // Gibt die Id der Daten zurück, wenn sie in einer Model3DList registriert
  547. // wurden. (siehe Framework::zM3DRegister())
  548. int Model3DData::getId() const
  549. {
  550. return id;
  551. }
  552. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  553. // wird
  554. float Model3DData::getAmbientFactor() const
  555. {
  556. return ambientFactor;
  557. }
  558. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  559. float Model3DData::getDiffusFactor() const
  560. {
  561. return diffusFactor;
  562. }
  563. // Git den Factor an, mit dem die Reflektion von Lichtquellen multipliziert wird
  564. float Model3DData::getSpecularFactor() const
  565. {
  566. return specularFactor;
  567. }
  568. // Gibt eine Kopie des Skeletts zurück, welches für annimationen verwendet
  569. // werden kann
  570. Skeleton* Model3DData::copySkelett() const
  571. {
  572. return skelett ? skelett->copySceleton() : 0;
  573. }
  574. // Gibt die Anzahl an Vertices zurück
  575. int Model3DData::getVertexAnzahl() const
  576. {
  577. return vertexCount;
  578. }
  579. // Gibt einen Buffer mit allen Vertecies des Models zurück
  580. const Vertex3D* Model3DData::zVertexBuffer() const
  581. {
  582. return vertexList;
  583. }
  584. //! Gibt eine refferenz auf den beginn des indexBuffers zurück
  585. const int* Model3DData::getIndexBuffer() const
  586. {
  587. return indexBuffer;
  588. }
  589. //! Gibt eine die Anzahl der indizes im indexBuffer zurück
  590. int Model3DData::getIndexCount() const
  591. {
  592. return indexCount;
  593. }
  594. //! Gibt den Index buffer zurück;
  595. DXBuffer* Model3DData::zDXIndexBuffer() const
  596. {
  597. return dxIndexBuffer;
  598. }
  599. //! Gibt den Vertex buffer zurück;
  600. DXBuffer* Model3DData::zDXVertexBuffer() const
  601. {
  602. return dxVertexBuffer;
  603. }
  604. //! gibt den minnimalen Punkt der Bounding box des Models zurück
  605. Vec3<float> Model3DData::getMinPos() const
  606. {
  607. return minPos;
  608. }
  609. //! gibt den maximalen Punkt der bounding box des Mopdels zurück
  610. Vec3<float> Model3DData::getMaxPos() const
  611. {
  612. return maxPos;
  613. }
  614. // Inhalt der Model3DTextur
  615. // Konstruktor
  616. Model3DTextur::Model3DTextur()
  617. : ReferenceCounter()
  618. {
  619. textures = new Textur*[1];
  620. textures[0] = 0;
  621. textureCount = 1;
  622. }
  623. // Destruktor
  624. Model3DTextur::~Model3DTextur()
  625. {
  626. for (int i = 0; i < textureCount; i++)
  627. {
  628. if (textures[i]) textures[i]->release();
  629. }
  630. delete[] textures;
  631. }
  632. // Legt fest, welche Textur für welches Polygon ist
  633. // pI: Der Index des Polygons
  634. // txt: Die Textur des Polygons
  635. void Model3DTextur::setPolygonTextur(int pI, Textur* txt)
  636. {
  637. if (pI >= textureCount)
  638. {
  639. Textur** tmp = textures;
  640. textures = new Textur*[pI + 1];
  641. memcpy(textures, tmp, sizeof(Textur*) * textureCount);
  642. memset(textures + textureCount,
  643. 0,
  644. sizeof(Textur*) * (pI + 1 - textureCount));
  645. delete[] tmp;
  646. textureCount = pI + 1;
  647. }
  648. if (textures[pI]) textures[pI]->release();
  649. textures[pI] = txt;
  650. }
  651. // Gibt einen Zeiger auf die Textur eines Polygons zurück ohne erhöhten
  652. // Reference Counter
  653. // i: Der Index des Polygons
  654. Textur* Model3DTextur::zPolygonTextur(int i) const
  655. {
  656. if (i >= textureCount) return 0;
  657. return textures[i];
  658. }
  659. // Inhalt der Model3D Klasse
  660. // Konstruktor
  661. Model3D::Model3D()
  662. : Zeichnung3D()
  663. {
  664. model = 0;
  665. textur = 0;
  666. skelett = 0;
  667. ambientFactor = 1.f;
  668. diffusFactor = 0.f;
  669. specularFactor = 0.f;
  670. }
  671. // Destruktor
  672. Model3D::~Model3D()
  673. {
  674. if (model) model->release();
  675. if (textur) textur->release();
  676. if (skelett) skelett->release();
  677. }
  678. // Setzt die Daten des Models
  679. // data: Die Daten
  680. void Model3D::setModelDaten(Model3DData* data)
  681. {
  682. if (model) model->release();
  683. if (skelett) skelett = (Skeleton*)skelett->release();
  684. model = data;
  685. if (model)
  686. {
  687. skelett = model->copySkelett();
  688. this->ambientFactor = model->getAmbientFactor();
  689. this->specularFactor = model->getSpecularFactor();
  690. this->diffusFactor = model->getDiffusFactor();
  691. }
  692. }
  693. // Setzt die zum Zeichnen zu benutzenden Texturen
  694. // txt: Ein Liste mit Texturen zu den verschiedenen Polygonen zugeordnet
  695. void Model3D::setModelTextur(Model3DTextur* txt)
  696. {
  697. if (textur) textur->release();
  698. textur = txt;
  699. }
  700. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  701. // wird
  702. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  703. void Framework::Model3D::setAmbientFactor(float f)
  704. {
  705. this->ambientFactor = f;
  706. }
  707. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  708. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  709. void Framework::Model3D::setDiffusFactor(float f)
  710. {
  711. diffusFactor = f;
  712. }
  713. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  714. // wird
  715. // f: der neue Faktor (von 0 bis 1, ambient + specular + diffuse = 1)
  716. void Framework::Model3D::setSpecularFactor(float f)
  717. {
  718. specularFactor = f;
  719. }
  720. // Errechnet die Matrizen aller Knochen des Skeletts des Models
  721. // viewProj: Die miteinander multiplizierten Kameramatrizen
  722. // matBuffer: Ein Array mit Matrizen, der gefüllt werden soll
  723. // return: Die Anzahl der Matrizen, die das Model benötigt
  724. int Model3D::errechneMatrizen(
  725. const Mat4<float>& viewProj, Mat4<float>* matBuffer)
  726. {
  727. int ret = 0;
  728. if (skelett)
  729. ret = skelett->calculateMatrix(welt, matBuffer, size, viewProj);
  730. else if (model)
  731. ret = model->kalkulateMatrix(welt, matBuffer, size, viewProj);
  732. if (!ret) return Zeichnung3D::errechneMatrizen(viewProj, matBuffer);
  733. return ret;
  734. }
  735. // Verarbeitet die vergangene Zeit
  736. // tickval: Die zeit in sekunden, die seit dem letzten Aufruf der Funktion
  737. // vergangen ist return: true, wenn sich das Objekt verändert hat, false
  738. // sonnst.
  739. bool Model3D::tick(double tickval)
  740. {
  741. radius = model ? model->getRadius() : 0;
  742. if (skelett)
  743. {
  744. radius += skelett->getRadius();
  745. }
  746. return Zeichnung3D::tick(tickval);
  747. }
  748. //! zum aktualisieren der shader daten
  749. void Model3D::beforeRender(
  750. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  751. {}
  752. void Model3D::afterRender(
  753. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  754. {}
  755. // Gibt die Textur zurück
  756. Model3DTextur* Model3D::getTextur()
  757. {
  758. return textur ? dynamic_cast<Model3DTextur*>(textur->getThis()) : 0;
  759. }
  760. // Gibt die Textur zurück (ohne erhöhten Reference Counter)
  761. Model3DTextur* Model3D::zTextur()
  762. {
  763. return textur;
  764. }
  765. // Gibt die ModelDaten zurück
  766. Model3DData* Model3D::getModelData()
  767. {
  768. return model ? dynamic_cast<Model3DData*>(model->getThis()) : 0;
  769. }
  770. // Gibt die ModelDaten zurück (ohne erhöhten Reference Counter)
  771. Model3DData* Model3D::zModelData()
  772. {
  773. return model;
  774. }
  775. // prüft, ob ein Strahl dieses Objekt trifft
  776. // point: der startpunkt des Strahls in Weltkoordinaten
  777. // dir: die Richtung des Strahls in Weltkoordinaten
  778. // maxSqDist: Die maximale quadratische distanz die erlaubt ist
  779. // pId: die Id des Polygons, zu dem der Schnittpunkt gehört
  780. // return: den quadratischen Abstand des Schnittpunktes zum Ursprung des
  781. // Strahls oder -1, wenn kein schnittpunkt existiert
  782. float Model3D::traceRay(
  783. const Vec3<float>& p, const Vec3<float>& d, float maxSqDist, int& pId) const
  784. {
  785. if (!model) return -1;
  786. Vec3<float> dir = d;
  787. dir.rotateY(-angle.y);
  788. dir.rotateX(-angle.x);
  789. dir.rotateZ(-angle.z);
  790. Vec3<float> point = p;
  791. point.rotateY(-angle.y);
  792. point.rotateX(-angle.x);
  793. point.rotateZ(-angle.z);
  794. point -= pos;
  795. float nearest = (-dir.x * point.x - dir.y * point.y - dir.z * point.z)
  796. / (dir.x * dir.x + dir.y * dir.y + dir.z * dir.z);
  797. float dist = (point + dir * nearest).getLengthSq();
  798. if (dist > (radius * size) * (radius * size)
  799. || (dir * nearest).getLength() - radius * size > sqrt(maxSqDist)
  800. || (nearest < 0
  801. && (dir * nearest).getLengthSq()
  802. > radius * size * radius
  803. * size)) // es gibt kein schnittpunkt
  804. return -1;
  805. bool existsHit = 0;
  806. if (skelett)
  807. { // todo
  808. }
  809. else
  810. {
  811. int index = 0;
  812. for (auto p = model->getPolygons(); p; p++)
  813. {
  814. for (int j = 0; j < p->indexAnz; j++)
  815. {
  816. if (j % 3 == 0)
  817. {
  818. Vec3<float> a = model->zVertexBuffer()[p->indexList[j]].pos;
  819. Vec3<float> b
  820. = model->zVertexBuffer()[p->indexList[j + 1]].pos;
  821. Vec3<float> c
  822. = model->zVertexBuffer()[p->indexList[j + 2]].pos;
  823. Vec3<float> normal
  824. = (b - a).crossProduct(c - a).normalize();
  825. if (normal * dir < 0) // Prüfe ob die Normale in Richtung
  826. // des Strahl ursprungs zeigt
  827. {
  828. nearest
  829. = (a * normal - point * normal) / (dir * normal);
  830. Vec3<float> hit = point + dir * nearest;
  831. if ((b - a).angle(hit - a) <= (b - a).angle(c - a)
  832. && (c - a).angle(hit - a) <= (b - a).angle(c - a)
  833. && (a - b).angle(hit - b) <= (a - b).angle(c - b))
  834. {
  835. maxSqDist = (hit - point).getLengthSq();
  836. pId = index;
  837. existsHit = 1;
  838. }
  839. }
  840. index++;
  841. }
  842. }
  843. }
  844. }
  845. return existsHit ? maxSqDist : -1;
  846. }
  847. // berechnet die Farbe des Schnittpunktes deines Strahls
  848. // point: der startpunkt des Strahls in Weltkoordinaten
  849. // dir: die Richtung des Strahls in Weltkoordinaten
  850. // zWelt: die Welt, aus der der Strahl kommt
  851. // return: die Farbe des Schnittpunktes
  852. int Model3D::traceRay(
  853. Vec3<float>& p, Vec3<float>& d, int pId, Welt3D* zWelt) const
  854. {
  855. Vec3<float> dir = d;
  856. dir.rotateY(-angle.y);
  857. dir.rotateX(-angle.x);
  858. dir.rotateZ(-angle.z);
  859. Vec3<float> point = p;
  860. point.rotateY(-angle.y);
  861. point.rotateX(-angle.x);
  862. point.rotateZ(-angle.z);
  863. point -= pos;
  864. int index = 0;
  865. for (auto p = model->getPolygons(); p; p++, index++)
  866. {
  867. for (int j = 0; j < p->indexAnz; j++)
  868. {
  869. if (j % 3 == 0)
  870. {
  871. if (pId == 0)
  872. {
  873. const Vec3<float>& a
  874. = model->zVertexBuffer()[p->indexList[j]].pos;
  875. const Vec3<float>& b
  876. = model->zVertexBuffer()[p->indexList[j + 1]].pos;
  877. const Vec3<float>& c
  878. = model->zVertexBuffer()[p->indexList[j + 2]].pos;
  879. Vertex at = model->zVertexBuffer()[p->indexList[j]].tPos;
  880. Vertex bt
  881. = model->zVertexBuffer()[p->indexList[j + 1]].tPos;
  882. Vertex ct
  883. = model->zVertexBuffer()[p->indexList[j + 2]].tPos;
  884. Vec3<float> normal
  885. = (b - a).crossProduct(c - a).normalize();
  886. float t = (a * normal - point * normal) / (dir * normal);
  887. Vec3<float> hit = point + dir * t;
  888. float a0 = (a - b).crossProduct(a - c).getLength() / 2;
  889. float a1
  890. = (b - hit).crossProduct(c - hit).getLength() / 2 / a0;
  891. float a2
  892. = (c - hit).crossProduct(a - hit).getLength() / 2 / a0;
  893. float a3
  894. = (a - hit).crossProduct(b - hit).getLength() / 2 / a0;
  895. Vertex ht = at * a1 + bt * a2 + ct * a3;
  896. Bild* tex = textur->zPolygonTextur(index)->zBild();
  897. if (ht.x >= 0 && ht.y >= 0 && ht.x <= 1 && ht.y <= 1)
  898. return tex->getPixel(
  899. (int)(ht.x * ((float)tex->getBreite() - 1.f)
  900. + 0.5f),
  901. (int)(ht.y * ((float)tex->getHeight() - 1.f)
  902. + 0.5f));
  903. return 0xFF000000;
  904. }
  905. pId--;
  906. }
  907. }
  908. }
  909. return 0xFF000000;
  910. }
  911. // Gibt die Id der Daten zurück, wenn sie in einer Model3DList registriert
  912. // wurden. (siehe Framework::zM3DRegister())
  913. int Model3D::getDatenId() const
  914. {
  915. return model ? model->getId() : -1;
  916. }
  917. // Git den Factor an, mit dem das umgebungslicht (textur farbe) multipliziert
  918. // wird
  919. float Model3D::getAmbientFactor() const
  920. {
  921. return ambientFactor;
  922. }
  923. // Git den Factor an, mit dem die Lichtfarbe von Lichtquellen multipliziert wird
  924. float Model3D::getDiffusFactor() const
  925. {
  926. return diffusFactor;
  927. }
  928. // Git den Factor an, mit dem die Reflektion von Lichtquellen multipliziert wird
  929. float Model3D::getSpecularFactor() const
  930. {
  931. return specularFactor;
  932. }
  933. // Gibt die Anzahl an Vertices zurück
  934. int Model3D::getVertexAnzahl() const
  935. {
  936. return model ? model->getVertexAnzahl() : 0;
  937. }
  938. // Gibt einen Buffer mit allen Vertecies des Models zurück
  939. const Vertex3D* Model3D::zVertexBuffer() const
  940. {
  941. return model ? model->zVertexBuffer() : 0;
  942. }
  943. //! Gibt true zurück wenn ein bestimmtes polygon gezeichnet werden muss
  944. bool Model3D::needRenderPolygon(int index)
  945. {
  946. return 1;
  947. }
  948. Textur* Model3D::zEffectTextur()
  949. {
  950. return 0;
  951. }
  952. float Model3D::getEffectPercentage()
  953. {
  954. return 0;
  955. }