particles.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "device/device.h"
  17. #include "render/particles.h"
  18. #include "render/scene.h"
  19. #include "util/util_foreach.h"
  20. #include "util/util_hash.h"
  21. #include "util/util_logging.h"
  22. #include "util/util_map.h"
  23. #include "util/util_progress.h"
  24. #include "util/util_vector.h"
  25. CCL_NAMESPACE_BEGIN
  26. /* Particle System */
  27. ParticleSystem::ParticleSystem()
  28. {
  29. }
  30. ParticleSystem::~ParticleSystem()
  31. {
  32. }
  33. void ParticleSystem::tag_update(Scene *scene)
  34. {
  35. scene->particle_system_manager->need_update = true;
  36. }
  37. /* Particle System Manager */
  38. ParticleSystemManager::ParticleSystemManager()
  39. {
  40. need_update = true;
  41. }
  42. ParticleSystemManager::~ParticleSystemManager()
  43. {
  44. }
  45. void ParticleSystemManager::device_update_particles(Device *,
  46. DeviceScene *dscene,
  47. Scene *scene,
  48. Progress &progress)
  49. {
  50. /* count particles.
  51. * adds one dummy particle at the beginning to avoid invalid lookups,
  52. * in case a shader uses particle info without actual particle data. */
  53. int num_particles = 1;
  54. for (size_t j = 0; j < scene->particle_systems.size(); j++)
  55. num_particles += scene->particle_systems[j]->particles.size();
  56. KernelParticle *kparticles = dscene->particles.alloc(num_particles);
  57. /* dummy particle */
  58. memset(kparticles, 0, sizeof(KernelParticle));
  59. int i = 1;
  60. for (size_t j = 0; j < scene->particle_systems.size(); j++) {
  61. ParticleSystem *psys = scene->particle_systems[j];
  62. for (size_t k = 0; k < psys->particles.size(); k++) {
  63. /* pack in texture */
  64. Particle &pa = psys->particles[k];
  65. kparticles[i].index = pa.index;
  66. kparticles[i].age = pa.age;
  67. kparticles[i].lifetime = pa.lifetime;
  68. kparticles[i].size = pa.size;
  69. kparticles[i].rotation = pa.rotation;
  70. kparticles[i].location = float3_to_float4(pa.location);
  71. kparticles[i].velocity = float3_to_float4(pa.velocity);
  72. kparticles[i].angular_velocity = float3_to_float4(pa.angular_velocity);
  73. i++;
  74. if (progress.get_cancel())
  75. return;
  76. }
  77. }
  78. dscene->particles.copy_to_device();
  79. }
  80. void ParticleSystemManager::device_update(Device *device,
  81. DeviceScene *dscene,
  82. Scene *scene,
  83. Progress &progress)
  84. {
  85. if (!need_update)
  86. return;
  87. VLOG(1) << "Total " << scene->particle_systems.size() << " particle systems.";
  88. device_free(device, dscene);
  89. progress.set_status("Updating Particle Systems", "Copying Particles to device");
  90. device_update_particles(device, dscene, scene, progress);
  91. if (progress.get_cancel())
  92. return;
  93. need_update = false;
  94. }
  95. void ParticleSystemManager::device_free(Device *, DeviceScene *dscene)
  96. {
  97. dscene->particles.free();
  98. }
  99. void ParticleSystemManager::tag_update(Scene * /*scene*/)
  100. {
  101. need_update = true;
  102. }
  103. CCL_NAMESPACE_END