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

scatter.c « sequence « plugins « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4741b33ce0ee1aad461f05b9ec67ed72490c4971 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "plugin.h"

/* ******************** GLOBAL VARIABLES ***************** */


char name[24]= "scatter";

/* structure for buttons, 
 *  butcode      name           default  min  max  0
 */

VarStruct varstr[]= {
	LABEL,		"Input: 1 strip", 0.0, 0.0, 0.0, "", 
	NUM|INT,	"seed: ",		1.0,	0.0, 10.0, "Offset in random table", 
	NUMSLI|FLO,	"swing: ",		1.0,	0.0, 3.0, "The amplitude, width of the effect", 
	TOG|INT,	"wrap",			0.0,	0.0, 1.0, "Cyclic wrap around the left/right edges", 
	NUM|INT,	"type: ",		1.0,	0.0, 1.0, "Type 1 is random for each frame", 
};

/* The cast struct is for input in the main doit function
   Varstr and Cast must have the same variables in the same order */ 

typedef struct Cast {
	int dummy;			/* because of the 'label' button */
	int seed;
	float swing;
	int wrap;
	int type;
} Cast;

/* cfra: the current frame */

float cfra;

void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);


/* ******************** Fixed functions ***************** */

int plugin_seq_getversion(void) 
{
	return B_PLUGIN_VERSION;
}

void plugin_but_changed(int but) 
{
}

void plugin_init()
{
}

void plugin_getinfo(PluginInfo *info)
{
	info->name= name;
	info->nvars= sizeof(varstr)/sizeof(VarStruct);
	info->cfra= &cfra;

	info->varstr= varstr;

	info->init= plugin_init;
	info->seq_doit= (SeqDoit) plugin_seq_doit;
	info->callback= plugin_but_changed;
}


/* ************************************************************
	Scatter
	
   ************************************************************ */

static void rectcpy(ImBuf *dbuf, ImBuf *sbuf,	
				int destx, int desty, 
				int srcx, int srcy, int width, int height)
{
	uint *drect,*srect;
	float *dfrect, *sfrect;
	int tmp;

	if (dbuf == 0) return;

	if (destx < 0){
		srcx -= destx ;
		width += destx ;
		destx = 0;
	}
	if (srcx < 0){
		destx -= srcx ;
		width += destx ;
		srcx = 0;
	}
	if (desty < 0){
		srcy -= desty ;
		height += desty ;
		desty = 0;
	}
	if (srcy < 0){
		desty -= srcy ;
		height += desty ;
		srcy = 0;
	}

	if (width > dbuf->x - destx) width = dbuf->x - destx;
	if (height > dbuf->y - desty) height = dbuf->y - desty;
	if (sbuf){
		if (width > sbuf->x - srcx) width = sbuf->x - srcx;
		if (height > sbuf->y - srcy) height = sbuf->y - srcy;
		srect = sbuf->rect;
		sfrect = sbuf->rect_float;
	}

	if (width <= 0) return;
	if (height <= 0) return;

	drect = dbuf->rect;
	dfrect = dbuf->rect_float;

	tmp = (desty * dbuf->x + destx);

	if (dbuf->rect_float) dfrect += 4 * tmp;
	else drect += tmp;

	destx = dbuf->x;

	if (sbuf) {
		tmp = (srcy * sbuf->x + srcx );
		if (dbuf->rect_float) sfrect += 4 * tmp; 
		else srect += tmp;
		srcx = sbuf->x;
	} else{
		if (dbuf->rect_float) sfrect = dfrect;
		else srect = drect;
		srcx = destx;
	}

	for (;height > 0; height--){
		if (dbuf->rect_float) {
			memcpy(dfrect,sfrect, 4 * width * sizeof(float));
			dfrect += destx;
			sfrect += srcx;
		} else {
			memcpy(drect,srect, width * sizeof(int));
			drect += destx;
			srect += srcx;
		}
	}
}

static void fill_out(ImBuf *out, float r, float g, float b, float a)
{
	int tot,x;
	float *rectf = out->rect_float;
	unsigned char *rect = (unsigned char *)out->rect;

	tot = out->x * out->y;
	if (out->rect_float) {
		for (x = 0;x < tot; x++) {
			rectf[0] = r;
			rectf[1] = g;
			rectf[2] = b;
			rectf[3] = a;
			rectf += 4;
		}
	} else {
		for (x=0;x < tot;x++) {
			rect[0] = (int)(r * 255);
			rect[1] = (int)(g * 255);
			rect[2] = (int)(b * 255);
			rect[3] = (int)(a * 255);
			rect += 4;
		}
	}
}


void plugin_seq_doit(Cast *cast, float facf0, float facf1, int sx, int sy, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use)
{
	float f1, f2, t1, t2, t3;
	int x, y, lr;
	
	/* fill imbuf 'out' with black */
	fill_out(out, 0,0,0,0);


	switch (cast->type) {
		case 0:
			srand48(cast->seed);
			break;
		case 1:
			srand48(cast->seed + facf0 * 1000);
			break;
	}

	for (y = 0; y < sy; y++) {
		switch (cast->type) {
			case 0:
				if ((y & 1) == 0) {
					f1 = drand48() - 0.5;
					f2 = drand48() - 0.5;
					f1 = cast->swing * f1;
					f2 = cast->swing * f2;
					if (cast->wrap) f2 += 1.0;
					lr = drand48()>0.5;
					t1 = facf0;
				} else t1 = facf1;
				
				t2 = 1.0 - t1;
				t3 = 3.0 * (f1 * t1 * t1 * t2 + f2 * t1 * t2 * t2);
				if (cast->wrap) t3 += t2 * t2 * t2;
				x = sx * t3;
				if (lr) x = -x;
				break;
			case 1:
				f1 = drand48() - 0.5;
				f1 = f1 * cast->swing;
				if ((y & 1) == 0) f1 *= facf0;
				else f1 *= facf1;
				x = f1 * sx;
				break;
		}
		
		rectcpy(out, ibuf1, 0, y, x, y, 32767, 1);
		if (cast->wrap) {
			rectcpy(out, ibuf1, 0, y, x + sx, y, 32767, 1);
			rectcpy(out, ibuf1, 0, y, x + sx + sx, y, 32767, 1);
			rectcpy(out, ibuf1, 0, y, x - sx, y, 32767, 1);
			rectcpy(out, ibuf1, 0, y, x - sx - sx, y, 32767, 1);
		}
	}
}