5ddd1e6c798882f440d939c299d13593c207a281
[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         particles[1] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
71         particles[2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
72         particles[3] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
73         particles[4] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
74         
75         int i = 1;
76         foreach(ParticleSystem *psys, scene->particle_systems) {
77                 foreach(Particle &pa, psys->particles) {
78                         /* pack in texture */
79                         int offset = i*PARTICLE_SIZE;
80                         
81                         particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, pa.size);
82                         particles[offset+1] = pa.rotation;
83                         particles[offset+2] = make_float4(pa.location.x, pa.location.y, pa.location.z, pa.velocity.x);
84                         particles[offset+3] = make_float4(pa.velocity.y, pa.velocity.z, pa.angular_velocity.x, pa.angular_velocity.y);
85                         particles[offset+4] = make_float4(pa.angular_velocity.z, 0.0f, 0.0f, 0.0f);
86                         
87                         i++;
88                         
89                         if(progress.get_cancel()) return;
90                 }
91         }
92         
93         device->tex_alloc("__particles", dscene->particles);
94 }
95
96 void ParticleSystemManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
97 {
98         if(!need_update)
99                 return;
100         
101         device_free(device, dscene);
102
103         if(scene->particle_systems.size() == 0)
104                 return;
105
106         progress.set_status("Updating Particle Systems", "Copying Particles to device");
107         device_update_particles(device, dscene, scene, progress);
108         
109         if(progress.get_cancel()) return;
110         
111         need_update = false;
112 }
113
114 void ParticleSystemManager::device_free(Device *device, DeviceScene *dscene)
115 {
116         device->tex_free(dscene->particles);
117         dscene->particles.clear();
118 }
119
120 void ParticleSystemManager::tag_update(Scene *scene)
121 {
122         need_update = true;
123 }
124
125 CCL_NAMESPACE_END
126