Model3D.cpp 28 KB

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