Welt3D.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "Welt3D.h"
  2. #include "DXBuffer.h"
  3. #include "Globals.h"
  4. #include "GraphicsApi.h"
  5. #include "MausEreignis.h"
  6. #include "Model3D.h"
  7. #include "Zeichnung3D.h"
  8. using namespace Framework;
  9. // Inhalt der Welt3D Klasse aus Welt3D.h
  10. // Konstructor
  11. Welt3D::Welt3D()
  12. : ReferenceCounter()
  13. {
  14. members = new RCArray<Model3D>();
  15. pointLightCount = 0;
  16. diffuseLightCount = 0;
  17. pointLights = 0;
  18. diffuseLights = 0;
  19. rend = 0;
  20. }
  21. // Destruktor
  22. Welt3D::~Welt3D()
  23. {
  24. members->release();
  25. delete[] pointLights;
  26. delete[] diffuseLights;
  27. }
  28. // Blockiert den zugriff auf das Objekt und wartet gegebenfalls auf den Zugriff
  29. void Welt3D::lock()
  30. {
  31. cs.lock();
  32. }
  33. // Gibt das Objekt für andere Threads frei
  34. void Welt3D::unlock()
  35. {
  36. cs.unlock();
  37. }
  38. // Fügt der Welt ein Objekt hinzu
  39. // obj: Das Objekt, was hinzugefügt werden soll
  40. void Welt3D::addZeichnung(Model3D* obj)
  41. {
  42. cs.lock();
  43. if (debugDX)
  44. {
  45. for (auto i : *members)
  46. {
  47. if (i == obj) throw std::exception();
  48. }
  49. }
  50. members->add(obj);
  51. rend = 1;
  52. cs.unlock();
  53. }
  54. // Entfernt ein Objekt aus der Welt
  55. // obj: Das Objekt, das entwernt werden soll
  56. void Welt3D::removeZeichnung(Model3D* obj)
  57. {
  58. cs.lock();
  59. int index = 0;
  60. for (Model3D* member : *members)
  61. {
  62. if (member == obj)
  63. {
  64. members->remove(index);
  65. rend = 1;
  66. break;
  67. }
  68. index++;
  69. }
  70. cs.unlock();
  71. }
  72. //! Fügt der Welt eine Collection von Objekten hinzu
  73. //! \param collection Die Collection, die hinzugefügt werden soll
  74. void Welt3D::addCollection(Model3DCollection* collection)
  75. {
  76. cs.lock();
  77. modelCollections.add(collection);
  78. rend = 1;
  79. cs.unlock();
  80. }
  81. //! removes a collection of models from the world
  82. //! \param zCollection Die Collection die entfernt werden soll
  83. void Welt3D::removeCollection(Model3DCollection* zCollection)
  84. {
  85. cs.lock();
  86. int index = 0;
  87. for (Model3DCollection* collection : modelCollections)
  88. {
  89. if (collection == zCollection)
  90. {
  91. modelCollections.remove(index);
  92. rend = 1;
  93. break;
  94. }
  95. index++;
  96. }
  97. cs.unlock();
  98. }
  99. // Verarbeitet ein Mausereignis
  100. // me: Das Mausereignis, das verarbeitet werden soll
  101. void Welt3D::doMausEreignis(MausEreignis3D& me)
  102. {
  103. // cs.lock()
  104. // int anz = 0;
  105. // int index = 0;
  106. // for( Zeichnung3D **i = members; index < arraySize; i++, index++ )
  107. //{
  108. // if( *i )
  109. // {
  110. // distSq[ anz ] = me.pos.abstandSq( ( *i )->getPos() );
  111. // alphaVS[ anz ] = *i;
  112. // anz++;
  113. // }
  114. // }
  115. // index = 0;
  116. // for( Zeichnung3D **i = membersAlpha; index < arraySizeAlpha; i++, index++
  117. // )
  118. //{
  119. // if( *i )
  120. // {
  121. // distSq[ anz ] = me.pos.abstandSq( ( *i )->getPos() );
  122. // alphaVS[ anz ] = *i;
  123. // anz++;
  124. // }
  125. // }
  126. // float maxEntf;
  127. // int ind;
  128. // do
  129. //{
  130. // maxEntf = -1;
  131. // ind = -1;
  132. // for( int i = 0; i < anz; i++ )
  133. // {
  134. // if( !used[ i ] && distSq[ i ] > maxEntf )
  135. // {
  136. // maxEntf = distSq[ i ];
  137. // ind = i;
  138. // }
  139. // }
  140. // if( ind >= 0 )
  141. // {
  142. // alphaVS[ ind ]->doMausEreignis( me );
  143. // if( me.verarbeitet )
  144. // {
  145. // cs.unlock();
  146. // return;
  147. // }
  148. // used[ ind ] = 1;
  149. // }
  150. // } while( ind >= 0 );
  151. // cs.unlock();
  152. }
  153. // Verarbeitet die vergangene Zeit
  154. // tickval: Die zeit in sekunden, die seit dem letzten Aufruf der Funktion
  155. // vergangen ist return: true, wenn sich das Objekt verändert hat, false
  156. // sonnst.
  157. bool Welt3D::tick(double tickval)
  158. {
  159. cs.lock();
  160. tick([this, &tickval](Model3D* m) { rend |= m->tick(tickval); });
  161. cs.unlock();
  162. bool tmp = rend;
  163. rend = 0;
  164. return tmp;
  165. }
  166. // brerechnet die Farbe eines Sichtstrahls, der von einem bestimmten punkt aus
  167. // in eine bestimmte richtung schaut
  168. // point: Der ursprung des Strahls,
  169. // dir: Die Richtung des Strahls
  170. // return: Die Farbe des Strahls
  171. int Welt3D::traceRay(Vec3<float>& point, Vec3<float>& dir)
  172. {
  173. float min = INFINITY;
  174. int pId = 0;
  175. Model3D* nearest = 0;
  176. forAll([this, &point, &dir, &pId, &min, &nearest](Model3D* m) {
  177. float tmp = m->traceRay(point, dir, min, pId);
  178. if (min > tmp && tmp >= 0)
  179. {
  180. min = tmp;
  181. nearest = m;
  182. }
  183. });
  184. if (nearest) return nearest->traceRay(point, dir, pId, this);
  185. return 0xFF000000;
  186. }
  187. //! führt eine funktion auf jedem Model aus
  188. void Framework::Welt3D::forAll(std::function<void(Model3D*)> f)
  189. {
  190. for (auto m : *members)
  191. f(m);
  192. for (auto c : modelCollections)
  193. c->forAll(f);
  194. }
  195. //! führt eine tick funktion auf jedem Model aus
  196. void Welt3D::tick(std::function<void(Model3D*)> f)
  197. {
  198. for (auto m : *members)
  199. f(m);
  200. for (auto c : modelCollections)
  201. c->tick(f);
  202. }
  203. //! führt eine render funktion auf jedem Model aus
  204. void Welt3D::render(std::function<void(Model3D*)> f)
  205. {
  206. for (auto m : *members)
  207. f(m);
  208. for (auto c : modelCollections)
  209. c->render(f);
  210. }
  211. int Framework::Welt3D::getPointLightCount() const
  212. {
  213. return pointLightCount;
  214. }
  215. int Framework::Welt3D::getDiffuseLightCount() const
  216. {
  217. return diffuseLightCount;
  218. }
  219. void Framework::Welt3D::copyLight(DXBuffer* zDiffuse, DXBuffer* zPoints) const
  220. {
  221. zDiffuse->setData(diffuseLights);
  222. zDiffuse->setLength(diffuseLightCount * (int)sizeof(DiffuseLight));
  223. zDiffuse->copieren();
  224. zPoints->setData(pointLights);
  225. zPoints->setLength(pointLightCount * (int)sizeof(PointLight));
  226. zPoints->copieren();
  227. }
  228. //! fügt eine neue diffuse lichtquelle hinzu
  229. //! \param light Die neue Lichtquelle
  230. void Framework::Welt3D::addDiffuseLight(DiffuseLight light)
  231. {
  232. DiffuseLight* tmp = new DiffuseLight[diffuseLightCount + 1];
  233. memcpy(tmp, diffuseLights, sizeof(DiffuseLight) * diffuseLightCount);
  234. tmp[diffuseLightCount] = light;
  235. delete[] diffuseLights;
  236. diffuseLights = tmp;
  237. diffuseLightCount++;
  238. }
  239. //! fügt eine neue Punkt lichtquelle hinzu
  240. //! \param light Die neue Lichtquelle
  241. void Framework::Welt3D::addPointLight(PointLight light)
  242. {
  243. PointLight* tmp = new PointLight[pointLightCount + 1];
  244. memcpy(tmp, pointLights, sizeof(PointLight) * pointLightCount);
  245. tmp[pointLightCount] = light;
  246. delete[] pointLights;
  247. pointLights = tmp;
  248. pointLightCount++;
  249. }
  250. //! Gibt die Referenz auf eine Diffuse Lichtquelle zurück
  251. //! \param index Der Index der Lichtquelle
  252. DiffuseLight& Framework::Welt3D::getDiffuseLight(int index) const
  253. {
  254. return diffuseLights[index];
  255. }
  256. //! Gibt die Referenz auf eine Punkt Lichtquelle zurück
  257. //! \param index Der Index der Lichtquelle
  258. PointLight& Framework::Welt3D::getPointLight(int index) const
  259. {
  260. return pointLights[index];
  261. }