Model3D.cpp 29 KB

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