Added a bunch of additional particle state attributes to the Cycles particle info...
[blender.git] / intern / cycles / render / particles.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "device.h"
20 #include "particles.h"
21 #include "scene.h"
22
23 #include "util_foreach.h"
24 #include "util_map.h"
25 #include "util_progress.h"
26 #include "util_vector.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 /* Particle System */
31
32 ParticleSystem::ParticleSystem()
33 {
34 }
35
36 ParticleSystem::~ParticleSystem()
37 {
38 }
39
40 void ParticleSystem::tag_update(Scene *scene)
41 {
42         scene->particle_system_manager->need_update = true;
43 }
44
45 /* Particle System Manager */
46
47 ParticleSystemManager::ParticleSystemManager()
48 {
49         need_update = true;
50 }
51
52 ParticleSystemManager::~ParticleSystemManager()
53 {
54 }
55
56 void ParticleSystemManager::device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
57 {
58         /* count particles.
59          * adds one dummy particle at the beginning to avoid invalid lookups,
60          * in case a shader uses particle info without actual particle data.
61          */
62         int num_particles = 1;
63         foreach(ParticleSystem *psys, scene->particle_systems)
64                 num_particles += psys->particles.size();
65         
66         float4 *particles = dscene->particles.resize(PARTICLE_SIZE*num_particles);
67         
68         /* dummy particle */
69         particles[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
70         
71         int i = 1;
72         foreach(ParticleSystem *psys, scene->particle_systems) {
73                 foreach(Particle &pa, psys->particles) {
74                         /* pack in texture */
75                         int offset = i*PARTICLE_SIZE;
76                         
77                         particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, pa.size);
78                         particles[offset+1] = pa.rotation;
79                         particles[offset+2] = make_float4(pa.location.x, pa.location.y, pa.location.z, pa.velocity.x);
80                         particles[offset+3] = make_float4(pa.velocity.y, pa.velocity.z, pa.angular_velocity.x, pa.angular_velocity.y);
81                         particles[offset+4] = make_float4(pa.angular_velocity.z, 0.0f, 0.0f, 0.0f);
82                         
83                         i++;
84                         
85                         if(progress.get_cancel()) return;
86                 }
87         }
88         
89         device->tex_alloc("__particles", dscene->particles);
90 }
91
92 void ParticleSystemManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
93 {
94         if(!need_update)
95                 return;
96         
97         device_free(device, dscene);
98
99         if(scene->particle_systems.size() == 0)
100                 return;
101
102         progress.set_status("Updating Particle Systems", "Copying Particles to device");
103         device_update_particles(device, dscene, scene, progress);
104         
105         if(progress.get_cancel()) return;
106         
107         need_update = false;
108 }
109
110 void ParticleSystemManager::device_free(Device *device, DeviceScene *dscene)
111 {
112         device->tex_free(dscene->particles);
113         dscene->particles.clear();
114 }
115
116 void ParticleSystemManager::tag_update(Scene *scene)
117 {
118         need_update = true;
119 }
120
121 CCL_NAMESPACE_END
122