Model3D.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. #include "Model3D.h"
  2. #include "Animation3D.h"
  3. #include "DXBuffer.h"
  4. #include "Image.h"
  5. #include "Model2D.h"
  6. #include "Texture.h"
  7. #include "World3D.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 the 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 The bone to be added
  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. // Contents of the Skeleton class
  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. // Contents of the Polygon3D struct
  267. // Constructor
  268. Polygon3D::Polygon3D()
  269. {
  270. indexAnz = 0;
  271. indexList = 0;
  272. }
  273. // Destructor
  274. Polygon3D::~Polygon3D()
  275. {
  276. delete[] indexList;
  277. }
  278. // Contents of the Model3DData class
  279. // Constructor
  280. Model3DData::Model3DData(int id)
  281. : ReferenceCounter(),
  282. vertexBufferChanged(0),
  283. indexBufferChanged(0),
  284. id(id)
  285. {
  286. skelett = 0;
  287. vertexList = 0;
  288. vertexCount = 0;
  289. polygons = new Array<Polygon3D*>();
  290. ambientFactor = 1.f;
  291. diffusFactor = 0.f;
  292. specularFactor = 0.f;
  293. indexCount = 0;
  294. indexBuffer = 0;
  295. radius = 0;
  296. }
  297. // Destructor
  298. Model3DData::~Model3DData()
  299. {
  300. clearModel();
  301. polygons->release();
  302. delete[] indexBuffer;
  303. }
  304. // Deletes all model data
  305. void Model3DData::clearModel()
  306. {
  307. delete[] vertexList;
  308. vertexCount = 0;
  309. vertexList = 0;
  310. for (Polygon3D* i : *polygons)
  311. delete i;
  312. polygons->clear();
  313. if (skelett) skelett->release();
  314. skelett = 0;
  315. radius = 0;
  316. delete[] indexBuffer;
  317. indexBuffer = 0;
  318. indexCount = 0;
  319. }
  320. // Calculates the normals for the vertices of the model
  321. void Model3DData::calculateNormals()
  322. {
  323. for (int i = 0; i < vertexCount; i++)
  324. {
  325. Vec3<float> normal(0, 0, 0);
  326. for (Polygon3D* p : *polygons)
  327. {
  328. int begin = 0;
  329. for (int j = 0; j < p->indexAnz; j++)
  330. {
  331. if (j % 3 == 0) begin = j;
  332. if (p->indexList[j] == i)
  333. {
  334. Vec3<float> a = vertexList[p->indexList[begin]].pos;
  335. Vec3<float> b = vertexList[p->indexList[begin + 1]].pos;
  336. Vec3<float> c = vertexList[p->indexList[begin + 2]].pos;
  337. normal += (b - a).crossProduct(c - a).normalize();
  338. normal.normalize();
  339. }
  340. }
  341. }
  342. vertexList[i].normal = normal;
  343. }
  344. }
  345. //! Creates a buffer for all polygon indices
  346. void Model3DData::buildIndexBuffer()
  347. {
  348. cs.lock();
  349. int indexCount = 0;
  350. for (Polygon3D* p : *polygons)
  351. indexCount += p->indexAnz;
  352. if (indexCount != this->indexCount)
  353. {
  354. delete[] indexBuffer;
  355. indexBuffer = new int[indexCount];
  356. this->indexCount = indexCount;
  357. indexBufferChanged = 1;
  358. }
  359. int current = 0;
  360. for (Polygon3D* p : *polygons)
  361. {
  362. for (int i = 0; i < p->indexAnz; i++)
  363. {
  364. if (indexBuffer[current + i] != p->indexList[i])
  365. {
  366. indexBufferChanged = 1;
  367. indexBuffer[current + i] = p->indexList[i];
  368. }
  369. }
  370. current += p->indexAnz;
  371. }
  372. cs.unlock();
  373. }
  374. // Sets the pointer to a default skeleton
  375. // s: The skeleton to be used
  376. void Model3DData::setSkeletonZ(Skeleton* s)
  377. {
  378. if (skelett) skelett->release();
  379. skelett = s;
  380. // the dx12 blas needs to be rebuilt if the skeleton changes
  381. vertexBufferChanged = 1;
  382. }
  383. // Sets a pointer to a list with all vertices of the model
  384. // vertexList: An array of vertices
  385. // anz: The number of vertices in the array
  386. void Model3DData::setVertecies(Vertex3D* vertexList, int anz)
  387. {
  388. cs.lock();
  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. vertexBufferChanged = 1;
  408. cs.unlock();
  409. }
  410. // Adds a polygon to the model
  411. // polygon: The polygon to be added
  412. void Model3DData::addPolygon(Polygon3D* polygon)
  413. {
  414. polygons->add(polygon);
  415. buildIndexBuffer();
  416. }
  417. // Sets the factor by which the ambient light (texture color) is multiplied
  418. // f: the new factor (from 0 to 1, ambient + specular + diffuse = 1)
  419. void Model3DData::setAmbientFactor(float f)
  420. {
  421. ambientFactor = f;
  422. }
  423. // Sets the factor by which the light color from light sources is multiplied
  424. // f: the new factor (from 0 to 1, ambient + specular + diffuse = 1)
  425. void Model3DData::setDiffusFactor(float f)
  426. {
  427. diffusFactor = f;
  428. }
  429. // Sets the factor by which the reflection from light sources is multiplied
  430. // f: the new factor (from 0 to 1, ambient + specular + diffuse = 1)
  431. void Model3DData::setSpecularFactor(float f)
  432. {
  433. specularFactor = f;
  434. }
  435. // Converts a 2D model to 3D
  436. // model: The 2D model to be converted to 3D
  437. // z: The z coordinate of all points of the model
  438. void Model3DData::copyModel2D(Model2DData* model, float z)
  439. {
  440. if (model && model->vLists && model->polygons)
  441. {
  442. clearModel();
  443. int vAnz = 0;
  444. for (const Polygon2D& p : *model->polygons)
  445. vAnz += p.vertex->getEntryCount();
  446. Vertex3D* vertexList = new Vertex3D[vAnz];
  447. int index = 0;
  448. for (auto i : *model->vLists)
  449. {
  450. Polygon3D* p = new Polygon3D();
  451. p->indexAnz = 0;
  452. for (auto j : *i)
  453. {
  454. for (auto k = j->zListe()->begin();
  455. k && k.hasNext() && k.next().hasNext();
  456. k++)
  457. p->indexAnz += 3;
  458. }
  459. p->indexList = new int[p->indexAnz];
  460. p->indexAnz = 0;
  461. for (auto j : *i)
  462. {
  463. for (auto k = j->zListe()->begin(); k; k++)
  464. {
  465. assert(index < vAnz);
  466. if (index < vAnz)
  467. {
  468. vertexList[index].pos
  469. = Vec3<float>(k->point->x, k->point->y, z);
  470. vertexList[index].tPos = (Vec2<float>)*k->texture;
  471. if (k.hasNext() && k.next().hasNext())
  472. {
  473. p->indexList[p->indexAnz] = index;
  474. p->indexAnz++;
  475. p->indexList[p->indexAnz] = index + 1;
  476. p->indexAnz++;
  477. p->indexList[p->indexAnz] = index + 2;
  478. p->indexAnz++;
  479. }
  480. }
  481. else
  482. break;
  483. index++;
  484. }
  485. }
  486. addPolygon(p);
  487. }
  488. this->setVertecies(vertexList, vAnz);
  489. buildIndexBuffer();
  490. calculateNormals();
  491. }
  492. }
  493. // Removes a polygon
  494. // index: The index of the polygon
  495. void Model3DData::removePolygon(int index)
  496. {
  497. if (!polygons->has(index)) return;
  498. delete polygons->get(index);
  499. polygons->remove(index);
  500. buildIndexBuffer();
  501. }
  502. // Calculates the bone matrices
  503. // modelMatrix: The matrix that transforms the skeleton into world space
  504. // matBuffer: An array of matrices to be filled with bone matrices
  505. // scaleFactor: The scaling of the model kamMatrix: The combination of
  506. // the view and projection matrices return: returns the number of used
  507. // matrices
  508. int Model3DData::kalkulateMatrix(const Mat4<float>& modelMatrix,
  509. Mat4<float>* matBuffer,
  510. float scaleFactor,
  511. const Mat4<float>& kamMatrix) const
  512. {
  513. if (!skelett) return 0;
  514. return skelett->calculateMatrix(
  515. modelMatrix, matBuffer, scaleFactor, kamMatrix);
  516. }
  517. // Returns the number of polygons
  518. int Model3DData::getPolygonCount() const
  519. {
  520. return polygons->getEntryCount();
  521. }
  522. // Returns a specific polygon
  523. // index: The index of the polygon
  524. Polygon3D* Model3DData::getPolygon(int index) const
  525. {
  526. if (!polygons->has(index)) return 0;
  527. return polygons->get(index);
  528. }
  529. // Returns an iterator to list the polygons
  530. ArrayIterator<Polygon3D*> Model3DData::getPolygons() const
  531. {
  532. return polygons->begin();
  533. }
  534. // Returns the radius of a sphere that encloses the entire model
  535. float Model3DData::getRadius() const
  536. {
  537. return radius;
  538. }
  539. // Returns the id of the data if registered in a Model3DList
  540. // (see Framework::zM3DRegister())
  541. int Model3DData::getId() const
  542. {
  543. return id;
  544. }
  545. // Returns the factor by which the ambient light (texture color) is multiplied
  546. float Model3DData::getAmbientFactor() const
  547. {
  548. return ambientFactor;
  549. }
  550. // Returns the factor by which the light color from light sources is multiplied
  551. float Model3DData::getDiffusFactor() const
  552. {
  553. return diffusFactor;
  554. }
  555. // Returns the factor by which the reflection from light sources is multiplied
  556. float Model3DData::getSpecularFactor() const
  557. {
  558. return specularFactor;
  559. }
  560. // Returns a copy of the skeleton that can be used for animations
  561. Skeleton* Model3DData::copySkeleton() const
  562. {
  563. return skelett ? skelett->copySceleton() : 0;
  564. }
  565. // Returns a pointer to the skeleton of the model without increasing the
  566. // reference counter
  567. Skeleton* Framework::Model3DData::zSkeleton() const
  568. {
  569. return skelett;
  570. }
  571. // Returns the number of vertices
  572. int Model3DData::getVertexCount() const
  573. {
  574. return vertexCount;
  575. }
  576. // Returns a buffer with all vertices of the model
  577. const Vertex3D* Model3DData::zVertexBuffer() const
  578. {
  579. return vertexList;
  580. }
  581. //! Returns a reference to the beginning of the index buffer
  582. const int* Model3DData::getIndexBuffer() const
  583. {
  584. return indexBuffer;
  585. }
  586. //! Returns the number of indices in the index buffer
  587. int Model3DData::getIndexCount() const
  588. {
  589. return indexCount;
  590. }
  591. //! Returns the minimum point of the bounding box of the model
  592. Vec3<float> Model3DData::getMinPos() const
  593. {
  594. return minPos;
  595. }
  596. //! Returns the maximum point of the bounding box of the model
  597. Vec3<float> Model3DData::getMaxPos() const
  598. {
  599. return maxPos;
  600. }
  601. bool Framework::Model3DData::wasIndexBufferChanged() const
  602. {
  603. return indexBufferChanged;
  604. }
  605. void Framework::Model3DData::setIndexBufferChanged(bool changed)
  606. {
  607. indexBufferChanged = changed;
  608. }
  609. bool Framework::Model3DData::wasVertexBufferChanged() const
  610. {
  611. return vertexBufferChanged;
  612. }
  613. void Framework::Model3DData::setVertexBufferChanged(bool changed)
  614. {
  615. vertexBufferChanged = changed;
  616. }
  617. void Framework::Model3DData::lock()
  618. {
  619. cs.lock();
  620. }
  621. void Framework::Model3DData::unlock()
  622. {
  623. cs.unlock();
  624. }
  625. // Contents of the Model3DTexture class
  626. // Constructor
  627. Model3DTexture::Model3DTexture()
  628. : ReferenceCounter(),
  629. textureIndexBuffer(0),
  630. textureIndexList(new int[1]),
  631. changed(0)
  632. {
  633. textures = new Texture*[1];
  634. textures[0] = 0;
  635. textureCount = 1;
  636. }
  637. // Destructor
  638. Model3DTexture::~Model3DTexture()
  639. {
  640. for (int i = 0; i < textureCount; i++)
  641. {
  642. if (textures[i]) textures[i]->release();
  643. }
  644. delete[] textures;
  645. delete[] textureIndexList;
  646. if (textureIndexBuffer)
  647. {
  648. textureIndexBuffer->release();
  649. }
  650. }
  651. // Sets which texture is for which polygon
  652. // pI: The index of the polygon
  653. // txt: The texture of the polygon
  654. void Model3DTexture::setPolygonTexture(int pI, Texture* txt)
  655. {
  656. if (pI >= textureCount)
  657. {
  658. Texture** tmp = textures;
  659. textures = new Texture*[pI + 1];
  660. memcpy(textures, tmp, sizeof(Texture*) * textureCount);
  661. memset(textures + textureCount,
  662. 0,
  663. sizeof(Texture*) * (pI + 1 - textureCount));
  664. delete[] tmp;
  665. int* tmp2 = new int[pI + 1];
  666. memcpy(tmp2, textureIndexList, sizeof(int) * textureCount);
  667. memset(tmp2 + textureCount, 0, sizeof(int) * (pI + 1 - textureCount));
  668. delete[] textureIndexList;
  669. textureIndexList = tmp2;
  670. textureCount = pI + 1;
  671. }
  672. if (textures[pI]) textures[pI]->release();
  673. textures[pI] = txt;
  674. textureIndexList[pI] = txt ? txt->getId() : -1;
  675. changed = 1;
  676. }
  677. // Returns a pointer to the texture of a polygon without increased
  678. // reference counter
  679. // i: The index of the polygon
  680. Texture* Model3DTexture::zPolygonTexture(int i) const
  681. {
  682. if (i >= textureCount) return 0;
  683. return textures[i];
  684. }
  685. void Framework::Model3DTexture::updateTextureIndexBuffer(GraphicsApi* zApi)
  686. {
  687. if (textureIndexBuffer && !changed) return;
  688. if (!textureIndexBuffer)
  689. {
  690. textureIndexBuffer = zApi->createStructuredBuffer(sizeof(int));
  691. }
  692. textureIndexBuffer->setData(textureIndexList, changed);
  693. textureIndexBuffer->setLength(textureCount * sizeof(int));
  694. textureIndexBuffer->copyToGPU();
  695. changed = 0;
  696. }
  697. DXBuffer* Framework::Model3DTexture::zTextureIndexBuffer() const
  698. {
  699. return textureIndexBuffer;
  700. }
  701. bool Framework::Model3DTexture::wasChanged() const
  702. {
  703. return changed;
  704. }
  705. // Contents of the Model3D class
  706. // Constructor
  707. Model3D::Model3D()
  708. : Drawable3D()
  709. {
  710. model = 0;
  711. texture = 0;
  712. skelett = 0;
  713. renderingData = 0;
  714. ambientFactor = 1.f;
  715. diffusFactor = 0.f;
  716. specularFactor = 0.f;
  717. }
  718. // Destructor
  719. Model3D::~Model3D()
  720. {
  721. if (model) model->release();
  722. if (texture) texture->release();
  723. if (skelett) skelett->release();
  724. if (renderingData) renderingData->release();
  725. }
  726. // Sets the model data
  727. // data: The data
  728. void Model3D::setModelData(Model3DData* data)
  729. {
  730. if (model) model->release();
  731. if (skelett) skelett = (Skeleton*)skelett->release();
  732. model = data;
  733. if (model)
  734. {
  735. skelett = model->copySkeleton();
  736. this->ambientFactor = model->getAmbientFactor();
  737. this->specularFactor = model->getSpecularFactor();
  738. this->diffusFactor = model->getDiffusFactor();
  739. }
  740. }
  741. // Sets the textures to be used for drawing
  742. // txt: A list of textures assigned to the different polygons
  743. void Model3D::setModelTextur(Model3DTexture* txt)
  744. {
  745. if (texture) texture->release();
  746. texture = txt;
  747. }
  748. // Sets the factor by which the ambient light (texture color) is multiplied
  749. // f: the new factor (from 0 to 1, ambient + specular + diffuse = 1)
  750. void Framework::Model3D::setAmbientFactor(float f)
  751. {
  752. this->ambientFactor = f;
  753. }
  754. // Sets the factor by which the light color from light sources is multiplied
  755. // f: the new factor (from 0 to 1, ambient + specular + diffuse = 1)
  756. void Framework::Model3D::setDiffusFactor(float f)
  757. {
  758. diffusFactor = f;
  759. }
  760. // Sets the factor by which the ambient light (texture color) is multiplied
  761. // f: the new factor (from 0 to 1, ambient + specular + diffuse = 1)
  762. void Framework::Model3D::setSpecularFactor(float f)
  763. {
  764. specularFactor = f;
  765. }
  766. // Calculates the matrices of all bones of the model's skeleton
  767. // viewProj: The multiplied camera matrices
  768. // matBuffer: An array of matrices to be filled
  769. // return: The number of matrices the model requires
  770. int Model3D::calculateMatrices(
  771. const Mat4<float>& viewProj, Mat4<float>* matBuffer)
  772. {
  773. int ret = 0;
  774. if (skelett)
  775. ret = skelett->calculateMatrix(welt, matBuffer, size, viewProj);
  776. else if (model)
  777. ret = model->kalkulateMatrix(welt, matBuffer, size, viewProj);
  778. if (!ret) return Drawable3D::calculateMatrices(viewProj, matBuffer);
  779. return ret;
  780. }
  781. // Processes elapsed time
  782. // tickval: The time in seconds that has passed since the last call
  783. // return: true if the object has changed, false otherwise.
  784. bool Model3D::tick(double tickval)
  785. {
  786. radius = model ? model->getRadius() : 0;
  787. if (skelett)
  788. {
  789. radius += skelett->getRadius();
  790. }
  791. return Drawable3D::tick(tickval);
  792. }
  793. //! for updating shader data
  794. void Model3D::beforeRender(
  795. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  796. {}
  797. void Model3D::afterRender(
  798. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  799. {}
  800. // Returns the texture
  801. Model3DTexture* Model3D::getTexture()
  802. {
  803. return texture ? dynamic_cast<Model3DTexture*>(texture->getThis()) : 0;
  804. }
  805. // Returns the texture (without increased reference counter)
  806. Model3DTexture* Model3D::zTexture()
  807. {
  808. return texture;
  809. }
  810. // Returns the model data
  811. Model3DData* Model3D::getModelData()
  812. {
  813. return model ? dynamic_cast<Model3DData*>(model->getThis()) : 0;
  814. }
  815. // Returns the model data (without increased reference counter)
  816. Model3DData* Model3D::zModelData()
  817. {
  818. return model;
  819. }
  820. // Checks if a ray hits this object
  821. // point: the start point of the ray in world coordinates
  822. // dir: the direction of the ray in world coordinates
  823. // maxSqDist: The maximum squared distance allowed
  824. // pId: the id of the polygon the intersection belongs to
  825. // return: the squared distance of the intersection to the ray origin
  826. // or -1 if no intersection exists
  827. float Model3D::traceRay(
  828. const Vec3<float>& p, const Vec3<float>& d, float maxSqDist, int& pId) const
  829. {
  830. if (!model) return -1;
  831. Vec3<float> dir = d;
  832. dir.rotateY(-angle.y);
  833. dir.rotateX(-angle.x);
  834. dir.rotateZ(-angle.z);
  835. Vec3<float> point = p;
  836. point.rotateY(-angle.y);
  837. point.rotateX(-angle.x);
  838. point.rotateZ(-angle.z);
  839. point -= pos;
  840. float nearest = (-dir.x * point.x - dir.y * point.y - dir.z * point.z)
  841. / (dir.x * dir.x + dir.y * dir.y + dir.z * dir.z);
  842. float dist = (point + dir * nearest).getLengthSq();
  843. if (dist > (radius * size) * (radius * size)
  844. || (dir * nearest).getLength() - radius * size > sqrt(maxSqDist)
  845. || (nearest < 0
  846. && (dir * nearest).getLengthSq()
  847. > radius * size * radius * size)) // no intersection exists
  848. return -1;
  849. bool existsHit = 0;
  850. if (skelett)
  851. { // todo
  852. }
  853. else
  854. {
  855. int index = 0;
  856. for (auto p = model->getPolygons(); p; p++)
  857. {
  858. for (int j = 0; j < p->indexAnz; j++)
  859. {
  860. if (j % 3 == 0)
  861. {
  862. Vec3<float> a = model->zVertexBuffer()[p->indexList[j]].pos;
  863. Vec3<float> b
  864. = model->zVertexBuffer()[p->indexList[j + 1]].pos;
  865. Vec3<float> c
  866. = model->zVertexBuffer()[p->indexList[j + 2]].pos;
  867. Vec3<float> normal
  868. = (b - a).crossProduct(c - a).normalize();
  869. if (normal * dir < 0) // Check if the normal points towards
  870. // the ray origin
  871. {
  872. nearest
  873. = (a * normal - point * normal) / (dir * normal);
  874. Vec3<float> hit = point + dir * nearest;
  875. if ((b - a).angle(hit - a) <= (b - a).angle(c - a)
  876. && (c - a).angle(hit - a) <= (b - a).angle(c - a)
  877. && (a - b).angle(hit - b) <= (a - b).angle(c - b))
  878. {
  879. maxSqDist = (hit - point).getLengthSq();
  880. pId = index;
  881. existsHit = 1;
  882. }
  883. }
  884. index++;
  885. }
  886. }
  887. }
  888. }
  889. return existsHit ? maxSqDist : -1;
  890. }
  891. // Calculates the color of the intersection point of a ray
  892. // point: the start point of the ray in world coordinates
  893. // dir: the direction of the ray in world coordinates
  894. // zWorld: the world the ray comes from
  895. // return: the color of the intersection point
  896. int Model3D::traceRay(
  897. Vec3<float>& p, Vec3<float>& d, int pId, World3D* zWorld) const
  898. {
  899. Vec3<float> dir = d;
  900. dir.rotateY(-angle.y);
  901. dir.rotateX(-angle.x);
  902. dir.rotateZ(-angle.z);
  903. Vec3<float> point = p;
  904. point.rotateY(-angle.y);
  905. point.rotateX(-angle.x);
  906. point.rotateZ(-angle.z);
  907. point -= pos;
  908. int index = 0;
  909. for (auto p = model->getPolygons(); p; p++, index++)
  910. {
  911. for (int j = 0; j < p->indexAnz; j++)
  912. {
  913. if (j % 3 == 0)
  914. {
  915. if (pId == 0)
  916. {
  917. const Vec3<float>& a
  918. = model->zVertexBuffer()[p->indexList[j]].pos;
  919. const Vec3<float>& b
  920. = model->zVertexBuffer()[p->indexList[j + 1]].pos;
  921. const Vec3<float>& c
  922. = model->zVertexBuffer()[p->indexList[j + 2]].pos;
  923. Vertex at = model->zVertexBuffer()[p->indexList[j]].tPos;
  924. Vertex bt
  925. = model->zVertexBuffer()[p->indexList[j + 1]].tPos;
  926. Vertex ct
  927. = model->zVertexBuffer()[p->indexList[j + 2]].tPos;
  928. Vec3<float> normal
  929. = (b - a).crossProduct(c - a).normalize();
  930. float t = (a * normal - point * normal) / (dir * normal);
  931. Vec3<float> hit = point + dir * t;
  932. float a0 = (a - b).crossProduct(a - c).getLength() / 2;
  933. float a1
  934. = (b - hit).crossProduct(c - hit).getLength() / 2 / a0;
  935. float a2
  936. = (c - hit).crossProduct(a - hit).getLength() / 2 / a0;
  937. float a3
  938. = (a - hit).crossProduct(b - hit).getLength() / 2 / a0;
  939. Vertex ht = at * a1 + bt * a2 + ct * a3;
  940. Image* tex = texture->zPolygonTexture(index)->zImage();
  941. if (ht.x >= 0 && ht.y >= 0 && ht.x <= 1 && ht.y <= 1)
  942. return tex->getPixel(
  943. (int)(ht.x * ((float)tex->getWidth() - 1.f) + 0.5f),
  944. (int)(ht.y * ((float)tex->getHeight() - 1.f)
  945. + 0.5f));
  946. return 0xFF000000;
  947. }
  948. pId--;
  949. }
  950. }
  951. }
  952. return 0xFF000000;
  953. }
  954. // Returns the id of the data if registered in a Model3DList
  955. // (see Framework::zM3DRegister())
  956. int Model3D::getDataId() const
  957. {
  958. return model ? model->getId() : -1;
  959. }
  960. // Returns the factor by which the ambient light (texture color) is multiplied
  961. float Model3D::getAmbientFactor() const
  962. {
  963. return ambientFactor;
  964. }
  965. // Returns the factor by which the light color from light sources is multiplied
  966. float Model3D::getDiffusFactor() const
  967. {
  968. return diffusFactor;
  969. }
  970. // Returns the factor by which the reflection from light sources is multiplied
  971. float Model3D::getSpecularFactor() const
  972. {
  973. return specularFactor;
  974. }
  975. // Returns the number of vertices
  976. int Model3D::getVertexCount() const
  977. {
  978. return model ? model->getVertexCount() : 0;
  979. }
  980. // Returns a buffer with all vertices of the model
  981. const Vertex3D* Model3D::zVertexBuffer() const
  982. {
  983. return model ? model->zVertexBuffer() : 0;
  984. }
  985. //! Returns true if a specific polygon needs to be rendered
  986. bool Model3D::needRenderPolygon(int index)
  987. {
  988. return 1;
  989. }
  990. Texture* Model3D::zEffectTexture()
  991. {
  992. return 0;
  993. }
  994. float Model3D::getEffectPercentage()
  995. {
  996. return 0;
  997. }
  998. void Framework::Model3D::setRenderingData(ReferenceCounter* data)
  999. {
  1000. if (renderingData)
  1001. {
  1002. renderingData->release();
  1003. }
  1004. renderingData = data;
  1005. }
  1006. ReferenceCounter* Framework::Model3D::zRenderingData() const
  1007. {
  1008. return renderingData;
  1009. }