Merged changes in the trunk up to revision 52118.
[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         int num_particles = 1;
62         foreach(ParticleSystem *psys, scene->particle_systems)
63                 num_particles += psys->particles.size();
64         
65         float4 *particles = dscene->particles.resize(PARTICLE_SIZE*num_particles);
66         
67         /* dummy particle */
68         particles[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
69         particles[1] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
70         particles[2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
71         particles[3] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
72         particles[4] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
73         
74         int i = 1;
75         foreach(ParticleSystem *psys, scene->particle_systems) {
76                 foreach(Particle &pa, psys->particles) {
77                         /* pack in texture */
78                         int offset = i*PARTICLE_SIZE;
79                         
80                         particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, pa.size);
81                         particles[offset+1] = pa.rotation;
82                         particles[offset+2] = make_float4(pa.location.x, pa.location.y, pa.location.z, pa.velocity.x);
83                         particles[offset+3] = make_float4(pa.velocity.y, pa.velocity.z, pa.angular_velocity.x, pa.angular_velocity.y);
84                         particles[offset+4] = make_float4(pa.angular_velocity.z, 0.0f, 0.0f, 0.0f);
85                         
86                         i++;
87                         
88                         if(progress.get_cancel()) return;
89                 }
90         }
91         
92         device->tex_alloc("__particles", dscene->particles);
93 }
94
95 void ParticleSystemManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
96 {
97         if(!need_update)
98                 return;
99         
100         device_free(device, dscene);
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