World3D.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "World3D.h"
  2. #include "Drawing3D.h"
  3. #include "DXBuffer.h"
  4. #include "Globals.h"
  5. #include "GraphicsApi.h"
  6. #include "Model3D.h"
  7. #include "MouseEvent.h"
  8. using namespace Framework;
  9. // Contents of the World3D class from World3D.h
  10. // Konstructor
  11. World3D::World3D()
  12. : Model3DCollection(),
  13. id(-1)
  14. {
  15. members = new RCArray<Model3D>();
  16. pointLightCount = 0;
  17. diffuseLightCount = 0;
  18. pointLights = 0;
  19. diffuseLights = 0;
  20. rend = 0;
  21. }
  22. // Destructor
  23. World3D::~World3D()
  24. {
  25. members->release();
  26. delete[] pointLights;
  27. delete[] diffuseLights;
  28. }
  29. // Blocks access to the object and waits for access if necessary
  30. Lock& World3D::readLock()
  31. {
  32. return lock.getReadLock();
  33. }
  34. // Releases the object for other threads
  35. Lock& World3D::writeLock()
  36. {
  37. return lock.getWriteLock();
  38. }
  39. // Adds an object to the world
  40. // obj: The object to be added
  41. void World3D::addDrawable(Model3D* obj)
  42. {
  43. lock.lockWrite();
  44. if (debugDX)
  45. {
  46. for (auto i : *members)
  47. {
  48. if (i == obj) throw std::exception();
  49. }
  50. }
  51. members->add(obj);
  52. rend = 1;
  53. lock.unlockWrite();
  54. }
  55. // Removes an object from the world
  56. // obj: The object to be removed
  57. void World3D::removeDrawable(Model3D* obj)
  58. {
  59. lock.lockWrite();
  60. int index = 0;
  61. for (Model3D* member : *members)
  62. {
  63. if (member == obj)
  64. {
  65. members->remove(index);
  66. rend = 1;
  67. break;
  68. }
  69. index++;
  70. }
  71. lock.unlockWrite();
  72. }
  73. //! Adds a collection of objects to the world
  74. //! \param collection The collection to be added
  75. void World3D::addCollection(Model3DCollection* collection)
  76. {
  77. lock.lockWrite();
  78. modelCollections.add(collection);
  79. rend = 1;
  80. lock.unlockWrite();
  81. }
  82. //! removes a collection of models from the world
  83. //! \param zCollection The collection to be removed
  84. void World3D::removeCollection(Model3DCollection* zCollection)
  85. {
  86. lock.lockWrite();
  87. int index = 0;
  88. for (Model3DCollection* collection : modelCollections)
  89. {
  90. if (collection == zCollection)
  91. {
  92. modelCollections.remove(index);
  93. rend = 1;
  94. break;
  95. }
  96. index++;
  97. }
  98. lock.unlockWrite();
  99. }
  100. // Processes a mouse event
  101. // me: The mouse event to be processed
  102. void World3D::doMouseEvent(MouseEvent3D& me)
  103. {
  104. // cs.lock()
  105. // int anz = 0;
  106. // int index = 0;
  107. // for( Drawable3D **i = members; index < arraySize; i++, index++ )
  108. //{
  109. // if( *i )
  110. // {
  111. // distSq[ anz ] = me.pos.distanceSq( ( *i )->getPos() );
  112. // alphaVS[ anz ] = *i;
  113. // anz++;
  114. // }
  115. // }
  116. // index = 0;
  117. // for( Drawable3D **i = membersAlpha; index < arraySizeAlpha; i++, index++
  118. // )
  119. //{
  120. // if( *i )
  121. // {
  122. // distSq[ anz ] = me.pos.distanceSq( ( *i )->getPos() );
  123. // alphaVS[ anz ] = *i;
  124. // anz++;
  125. // }
  126. // }
  127. // float maxEntf;
  128. // int ind;
  129. // do
  130. //{
  131. // maxEntf = -1;
  132. // ind = -1;
  133. // for( int i = 0; i < anz; i++ )
  134. // {
  135. // if( !used[ i ] && distSq[ i ] > maxEntf )
  136. // {
  137. // maxEntf = distSq[ i ];
  138. // ind = i;
  139. // }
  140. // }
  141. // if( ind >= 0 )
  142. // {
  143. // alphaVS[ ind ]->doMouseEvent( me );
  144. // if( me.processed )
  145. // {
  146. // cs.unlock();
  147. // return;
  148. // }
  149. // used[ ind ] = 1;
  150. // }
  151. // } while( ind >= 0 );
  152. // cs.unlock();
  153. }
  154. // Processes the elapsed time
  155. // tickval: The time in seconds since the last call of the function
  156. // return: true if the object has changed, false
  157. // otherwise.
  158. bool World3D::tick(double tickval)
  159. {
  160. lock.lockRead();
  161. rend |= tick(
  162. [this, &tickval](Model3D* m) { rend |= m->tick(tickval); }, tickval);
  163. lock.unlockRead();
  164. bool tmp = rend;
  165. rend = 0;
  166. return tmp;
  167. }
  168. // Calculates the color of a view ray from a specific point
  169. // in a specific direction
  170. // point: The origin of the ray,
  171. // dir: The direction of the ray
  172. // return: The color of the ray
  173. int World3D::traceRay(Vec3<float>& point, Vec3<float>& dir)
  174. {
  175. float min = INFINITY;
  176. int pId = 0;
  177. Model3D* nearest = 0;
  178. lock.lockRead();
  179. forAll([this, &point, &dir, &pId, &min, &nearest](Model3D* m) {
  180. float tmp = m->traceRay(point, dir, min, pId);
  181. if (min > tmp && tmp >= 0)
  182. {
  183. min = tmp;
  184. nearest = m;
  185. }
  186. });
  187. lock.unlockRead();
  188. if (nearest) return nearest->traceRay(point, dir, pId, this);
  189. return 0xFF000000;
  190. }
  191. //! executes a function on every model
  192. void Framework::World3D::forAll(std::function<void(Model3D*)> f)
  193. {
  194. lock.lockRead();
  195. for (auto m : *members)
  196. f(m);
  197. for (auto c : modelCollections)
  198. c->forAll(f);
  199. lock.unlockRead();
  200. }
  201. //! executes a tick function on every model
  202. bool World3D::tick(std::function<void(Model3D*)> f, double time)
  203. {
  204. lock.lockRead();
  205. for (auto m : *members)
  206. f(m);
  207. bool res = 0;
  208. for (auto c : modelCollections)
  209. res |= c->tick(f, time);
  210. lock.unlockRead();
  211. return res;
  212. }
  213. //! executes a render function on every model
  214. void World3D::render(std::function<void(Model3D*)> f)
  215. {
  216. lock.lockRead();
  217. for (auto m : *members)
  218. f(m);
  219. for (auto c : modelCollections)
  220. c->render(f);
  221. lock.unlockRead();
  222. }
  223. int Framework::World3D::getPointLightCount() const
  224. {
  225. return pointLightCount;
  226. }
  227. int Framework::World3D::getDiffuseLightCount() const
  228. {
  229. return diffuseLightCount;
  230. }
  231. void Framework::World3D::copyLight(DXBuffer* zDiffuse, DXBuffer* zPoints) const
  232. {
  233. zDiffuse->setData(diffuseLights, true);
  234. zDiffuse->setLength(diffuseLightCount * (int)sizeof(DiffuseLight));
  235. zDiffuse->copyToGPU();
  236. zPoints->setData(pointLights, true);
  237. zPoints->setLength(pointLightCount * (int)sizeof(PointLight));
  238. zPoints->copyToGPU();
  239. }
  240. //! adds a new diffuse light source
  241. //! \param light The new light source
  242. void Framework::World3D::addDiffuseLight(DiffuseLight light)
  243. {
  244. lock.lockWrite();
  245. DiffuseLight* tmp = new DiffuseLight[diffuseLightCount + 1];
  246. for (int i = 0; i < diffuseLightCount; i++)
  247. {
  248. tmp[i] = diffuseLights[i];
  249. }
  250. tmp[diffuseLightCount] = light;
  251. delete[] diffuseLights;
  252. diffuseLights = tmp;
  253. diffuseLightCount++;
  254. lock.unlockWrite();
  255. }
  256. //! adds a new point light source
  257. //! \param light The new light source
  258. void Framework::World3D::addPointLight(PointLight light)
  259. {
  260. lock.lockWrite();
  261. PointLight* tmp = new PointLight[pointLightCount + 1];
  262. for (int i = 0; i < pointLightCount; i++)
  263. {
  264. tmp[i] = pointLights[i];
  265. }
  266. tmp[pointLightCount] = light;
  267. delete[] pointLights;
  268. pointLights = tmp;
  269. pointLightCount++;
  270. lock.unlockWrite();
  271. }
  272. //! Returns the reference to a diffuse light source
  273. //! \param index The index of the light source
  274. DiffuseLight& Framework::World3D::getDiffuseLight(int index) const
  275. {
  276. return diffuseLights[index];
  277. }
  278. //! Returns the reference to a point light source
  279. //! \param index The index of the light source
  280. PointLight& Framework::World3D::getPointLight(int index) const
  281. {
  282. return pointLights[index];
  283. }
  284. //! removes a specific fiffuse light from the world
  285. //! \param index the index of the light
  286. DLLEXPORT void Framework::World3D::removeDiffuseLight(int index)
  287. {
  288. lock.lockWrite();
  289. for (int i = index; i < diffuseLightCount - 1; i++)
  290. {
  291. diffuseLights[i] = diffuseLights[i + 1];
  292. }
  293. diffuseLightCount--;
  294. lock.unlockWrite();
  295. }
  296. //! removes a specific point light from the world
  297. //! \param index the index of the light
  298. DLLEXPORT void Framework::World3D::removePointLight(int index)
  299. {
  300. lock.lockWrite();
  301. for (int i = index; i < pointLightCount - 1; i++)
  302. {
  303. pointLights[i] = pointLights[i + 1];
  304. }
  305. pointLightCount--;
  306. lock.unlockWrite();
  307. }
  308. void Framework::World3D::setId(int id)
  309. {
  310. this->id = id;
  311. }
  312. int Framework::World3D::getId() const
  313. {
  314. return id;
  315. }