Welcome to mirror list, hosted at ThFree Co, Russian Federation.

particles.cpp « render « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2f154cdab4fc80daa96e05725e2f22369f1e4aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2011-2013 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "device.h"
#include "particles.h"
#include "scene.h"

#include "util_foreach.h"
#include "util_map.h"
#include "util_progress.h"
#include "util_vector.h"

CCL_NAMESPACE_BEGIN

/* Particle System */

ParticleSystem::ParticleSystem()
{
}

ParticleSystem::~ParticleSystem()
{
}

void ParticleSystem::tag_update(Scene *scene)
{
	scene->particle_system_manager->need_update = true;
}

/* Particle System Manager */

ParticleSystemManager::ParticleSystemManager()
{
	need_update = true;
}

ParticleSystemManager::~ParticleSystemManager()
{
}

void ParticleSystemManager::device_update_particles(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
	/* count particles.
	 * adds one dummy particle at the beginning to avoid invalid lookups,
	 * in case a shader uses particle info without actual particle data. */
	int num_particles = 1;
	foreach(ParticleSystem *psys, scene->particle_systems)
		num_particles += psys->particles.size();
	
	float4 *particles = dscene->particles.resize(PARTICLE_SIZE*num_particles);
	
	/* dummy particle */
	particles[0] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
	particles[1] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
	particles[2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
	particles[3] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
	particles[4] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
	
	int i = 1;
	foreach(ParticleSystem *psys, scene->particle_systems) {
		foreach(Particle &pa, psys->particles) {
			/* pack in texture */
			int offset = i*PARTICLE_SIZE;
			
			particles[offset] = make_float4(pa.index, pa.age, pa.lifetime, pa.size);
			particles[offset+1] = pa.rotation;
			particles[offset+2] = make_float4(pa.location.x, pa.location.y, pa.location.z, pa.velocity.x);
			particles[offset+3] = make_float4(pa.velocity.y, pa.velocity.z, pa.angular_velocity.x, pa.angular_velocity.y);
			particles[offset+4] = make_float4(pa.angular_velocity.z, 0.0f, 0.0f, 0.0f);
			
			i++;
			
			if(progress.get_cancel()) return;
		}
	}
	
	device->tex_alloc("__particles", dscene->particles);
}

void ParticleSystemManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
	if(!need_update)
		return;
	
	device_free(device, dscene);

	progress.set_status("Updating Particle Systems", "Copying Particles to device");
	device_update_particles(device, dscene, scene, progress);
	
	if(progress.get_cancel()) return;
	
	need_update = false;
}

void ParticleSystemManager::device_free(Device *device, DeviceScene *dscene)
{
	device->tex_free(dscene->particles);
	dscene->particles.clear();
}

void ParticleSystemManager::tag_update(Scene *scene)
{
	need_update = true;
}

CCL_NAMESPACE_END