Model3D.cpp 29 KB

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