This commit fixes radiosity to correctly preserve and subdivide UV
[blender.git] / source / blender / radiosity / intern / source / radpostprocess.c
1 /* ***************************************
2  *
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30
31
32
33     radpostprocess.c    nov/dec 1992
34                                                 may 1999
35
36     - faces
37     - filtering and node-limit
38     - apply to meshes
39     $Id$
40
41  *************************************** */
42
43 #include <stdlib.h>
44 #include <math.h>
45
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 #include "MEM_guardedalloc.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_arithb.h"
54 #include "BIF_toolbox.h"  // notice()
55
56 #include "DNA_material_types.h"
57 #include "DNA_mesh_types.h"
58 #include "DNA_meshdata_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_radio_types.h"
61
62 #include "BKE_utildefines.h"
63 #include "BKE_global.h"
64 #include "BKE_main.h"
65 #include "BKE_mesh.h"
66 #include "BKE_object.h"
67 #include "BKE_material.h"
68
69 #include "BIF_screen.h"         /* waitcursor */
70 #include "BIF_editview.h"       /* deselectall */
71
72 #include "BDR_editobject.h"     /* delete_obj */
73
74 #include "radio.h"
75
76 /* locals? not. done in radio.h...  */
77 /*  void rad_addmesh(void); */
78 /*  void rad_replacemesh(void); */
79
80 void addaccu(register char *z, register char *t)
81 {
82         register int div, mul;
83
84         mul= *t;
85         div= mul+1;
86         (*t)++;
87
88         t[1]= (mul*t[1]+z[1])/div;
89         t[2]= (mul*t[2]+z[2])/div;
90         t[3]= (mul*t[3]+z[3])/div;
91
92 }
93
94 void addaccuweight(register char *z, register char *t, int w)
95 {
96         register int div, mul;
97         
98         if(w==0) w= 1;
99         
100         mul= *t;
101         div= mul+w;
102         if(div>255) return;
103         (*t)= div;
104
105         t[1]= (mul*t[1]+w*z[1])/div;
106         t[2]= (mul*t[2]+w*z[2])/div;
107         t[3]= (mul*t[3]+w*z[3])/div;
108
109 }
110
111 void triaweight(Face *face, int *w1, int *w2, int *w3)
112 {
113         float n1[3], n2[3], n3[3], temp;
114
115         n1[0]= face->v2[0]-face->v1[0];
116         n1[1]= face->v2[1]-face->v1[1];
117         n1[2]= face->v2[2]-face->v1[2];
118         n2[0]= face->v3[0]-face->v2[0];
119         n2[1]= face->v3[1]-face->v2[1];
120         n2[2]= face->v3[2]-face->v2[2];
121         n3[0]= face->v1[0]-face->v3[0];
122         n3[1]= face->v1[1]-face->v3[1];
123         n3[2]= face->v1[2]-face->v3[2];
124         Normalise(n1);
125         Normalise(n2);
126         Normalise(n3);
127         temp= 32.0/(PI);
128         *w1= 0.5+temp*acos(-n1[0]*n3[0]-n1[1]*n3[1]-n1[2]*n3[2]);
129         *w2= 0.5+temp*acos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
130         *w3= 0.5+temp*acos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
131         
132 }
133
134
135
136 void init_face_tab()
137 {
138         int a= 0;
139
140         if(RG.facebase==0) {
141                 RG.facebase= MEM_callocN(sizeof(void *)*RAD_MAXFACETAB, "init_face_tab");
142         }
143         for(a=0; a<RAD_MAXFACETAB; a++) {
144                 if(RG.facebase[a]==0) break;
145                 MEM_freeN(RG.facebase[a]);
146                 RG.facebase[a]= 0;
147         }
148         RG.totface= 0;
149 }
150
151 Face *addface()
152 {
153         Face *face;
154         int a;
155
156         if(RG.totface<0 || RG.totface>RAD_MAXFACETAB*1024 ) {
157                 printf("error in addface: %d\n", RG.totface);
158                 return 0;
159         }
160         a= RG.totface>>10;
161         face= RG.facebase[a];
162         if(face==0) {
163                 face= MEM_callocN(1024*sizeof(Face),"addface");
164                 RG.facebase[a]= face;
165         }
166         face+= (RG.totface & 1023);
167         
168         RG.totface++;
169         
170         return face;
171
172 }
173
174 #define HALFUV(uv, uv1, uv2) {uv[0] = 0.5 * (uv1[0] + uv2[0]); uv[1] = 0.5 * (uv1[1] + uv2[1]);}
175 #define COPYUV(dst, src) {dst[0] = src[0]; dst[1] = src[1];}
176
177 void dofaceuv(Face *face, float uvs[8][2], short flag1, short flag2, short flag3, short flag4)
178 {
179         COPYUV(face->uv[0], uvs[flag1]);
180         COPYUV(face->uv[1], uvs[flag2]);
181         COPYUV(face->uv[2], uvs[flag3]);
182         COPYUV(face->uv[3], uvs[flag4]);
183 }
184
185 Face * makeface(float *v1, float *v2, float *v3, float *v4, RNode *rn, short hasUV)
186 {
187         Face *face;
188         
189         face= addface();
190         face->v1= v1;
191         face->v2= v2;
192         face->v3= v3;
193         face->v4= v4;
194         face->col= rn->col;
195         face->matindex= rn->par->matindex;
196         face->tface = rn->tface;
197
198         if (hasUV)
199                 memcpy(face->uv, rn->uv, sizeof(float) * 4 * 2);
200
201         return face;
202 }
203
204 void anchorQuadface(RNode *rn, float *v1, float *v2, float *v3, float *v4, int flag)
205 {
206         Face *face;
207         float uvs[8][2];
208
209         memcpy(uvs, rn->uv, sizeof(float) * 4 * 2);
210         HALFUV(uvs[4], rn->uv[0], rn->uv[1]);
211         HALFUV(uvs[5], rn->uv[1], rn->uv[2]);
212         HALFUV(uvs[6], rn->uv[2], rn->uv[3]);
213         HALFUV(uvs[7], rn->uv[3], rn->uv[0]);
214
215         switch(flag) {
216                 case 1:
217                         face = makeface(rn->v1, v1, rn->v4, NULL, rn, 0);
218                         dofaceuv(face, uvs, 0, 4, 3, 0);
219
220                         face = makeface(v1, rn->v3, rn->v4, NULL, rn, 0);
221                         dofaceuv(face, uvs, 4, 2, 3, 0);
222                         
223                         face = makeface(v1, rn->v2, rn->v3, NULL, rn, 0);
224                         dofaceuv(face, uvs, 4, 1, 2, 0);
225
226                         break;
227                 case 2:
228                         face = makeface(rn->v2, v2, rn->v1, NULL, rn, 0);
229                         dofaceuv(face, uvs, 1, 5, 0, 0);
230
231                         face = makeface(v2, rn->v4, rn->v1, NULL, rn, 0);
232                         dofaceuv(face, uvs, 5, 3, 0, 0);
233
234                         face = makeface(v2, rn->v3, rn->v4, NULL, rn, 0);
235                         dofaceuv(face, uvs, 5, 2, 3, 0);
236
237                         break;
238                 case 4:
239                         face = makeface(rn->v3, v3, rn->v2, NULL, rn, 0);
240                         dofaceuv(face, uvs, 2, 6, 1, 0);
241
242                         face = makeface(v3, rn->v1, rn->v2, NULL, rn, 0);
243                         dofaceuv(face, uvs, 6, 0, 1, 0);
244
245                         face = makeface(v3, rn->v4, rn->v1, NULL, rn, 0);
246                         dofaceuv(face, uvs, 6, 3, 0, 0);
247
248                         break;
249                 case 8:
250                         face = makeface(rn->v4, v4, rn->v3, NULL, rn, 0);
251                         dofaceuv(face, uvs, 3, 7, 2, 0);
252
253                         face = makeface(v4, rn->v2, rn->v3, NULL, rn, 0);
254                         dofaceuv(face, uvs, 7, 1, 2, 0);
255
256                         face = makeface(v4, rn->v1, rn->v2, NULL, rn, 0);
257                         dofaceuv(face, uvs, 7, 0, 1, 0);
258
259                         break;
260                         
261                 case 3:
262                         face = makeface(rn->v1, v1, rn->v4, NULL, rn, 0);
263                         dofaceuv(face, uvs, 0, 4, 3, 0);
264
265                         face = makeface(v1, v2, rn->v4, NULL, rn, 0);
266                         dofaceuv(face, uvs, 4, 5, 3, 0);
267
268                         face = makeface(v1, rn->v2, v2, NULL, rn, 0);
269                         dofaceuv(face, uvs, 4, 1, 5, 0);
270
271                         face = makeface(v2, rn->v3, rn->v4, NULL, rn, 0);
272                         dofaceuv(face, uvs, 5, 2, 3, 0);
273
274                         break;
275                 case 6:
276                         face = makeface(rn->v2, v2, rn->v1, NULL, rn, 0);
277                         dofaceuv(face, uvs, 1, 5, 0, 0);
278
279                         face = makeface(v2, v3, rn->v1, NULL, rn, 0);
280                         dofaceuv(face, uvs, 5, 6, 0, 0);
281
282                         face = makeface(v2, rn->v3, v3, NULL, rn, 0);
283                         dofaceuv(face, uvs, 5, 2, 6, 0);
284
285                         face = makeface(v3, rn->v4, rn->v1, NULL, rn, 0);
286                         dofaceuv(face, uvs, 6, 3, 0, 0);
287
288                         break;
289                 case 12:
290                         face = makeface(rn->v3, v3, rn->v2, NULL, rn, 0);
291                         dofaceuv(face, uvs, 2, 6, 1, 0);
292
293                         face = makeface(v3, v4, rn->v2, NULL, rn, 0);
294                         dofaceuv(face, uvs, 6, 7, 1, 0);
295
296                         face = makeface(v3, rn->v4, v4, NULL, rn, 0);
297                         dofaceuv(face, uvs, 6, 3, 7, 0);
298
299                         face = makeface(v4, rn->v1, rn->v2, NULL, rn, 0);
300                         dofaceuv(face, uvs, 7, 0, 1, 0);
301
302                         break;
303                 case 9:
304                         face = makeface(rn->v4, v4, rn->v3, NULL, rn, 0);
305                         dofaceuv(face, uvs, 3, 7, 2, 0);
306
307                         face = makeface(v4, v1, rn->v3, NULL, rn, 0);
308                         dofaceuv(face, uvs, 7, 4, 2, 0);
309
310                         face = makeface(v4, rn->v1, v1, NULL, rn, 0);
311                         dofaceuv(face, uvs, 7, 0, 4, 0);
312
313                         face = makeface(v1, rn->v2, rn->v3, NULL, rn, 0);
314                         dofaceuv(face, uvs, 4, 1, 2, 0);
315
316                         break;
317                         
318                 case 5:
319                         face = makeface(rn->v1, v1, v3, rn->v4, rn, 0);
320                         dofaceuv(face, uvs, 0, 4, 6, 3);
321
322                         face = makeface(v1, rn->v2, rn->v3, v3, rn, 0);
323                         dofaceuv(face, uvs, 4, 1, 2, 6);
324
325                         break;
326                 case 10:
327                         face = makeface(rn->v1, rn->v2, v2, v4, rn, 0);
328                         dofaceuv(face, uvs, 0, 1, 5, 7);
329
330                         face = makeface(v4, v2, rn->v3, rn->v4, rn, 0);
331                         dofaceuv(face, uvs, 7, 5, 2, 3);
332
333                         break;
334                         
335                 case 7:
336                         face = makeface(rn->v1, v1, v3, rn->v4, rn, 0);
337                         dofaceuv(face, uvs, 0, 4, 6, 3);
338
339                         face = makeface(v1, v2, v3, NULL, rn, 0);
340                         dofaceuv(face, uvs, 4, 5, 6, 0);
341
342                         face = makeface(v1, rn->v2, v2, NULL, rn, 0);
343                         dofaceuv(face, uvs, 4, 1, 5, 0);
344
345                         face = makeface(v2, rn->v3, v3, NULL, rn, 0);
346                         dofaceuv(face, uvs, 5, 2, 6, 0);
347
348                         break;
349                 case 14:
350                         face = makeface(rn->v2, v2, v4, rn->v1, rn, 0);
351                         dofaceuv(face, uvs, 1, 5, 7, 0);
352
353                         face = makeface(v2, v3, v4, NULL, rn, 0);
354                         dofaceuv(face, uvs, 5, 6, 7, 0);
355
356                         face = makeface(v2, rn->v3, v3, NULL, rn, 0);
357                         dofaceuv(face, uvs, 5, 2, 6, 0);
358
359                         face = makeface(v3, rn->v4, v4, NULL, rn, 0);
360                         dofaceuv(face, uvs, 6, 3, 7, 0);
361
362                         break;
363                 case 13:
364                         face = makeface(rn->v3, v3, v1, rn->v2, rn, 0);
365                         dofaceuv(face, uvs, 2, 6, 4, 1);
366
367                         face = makeface(v3, v4, v1, NULL, rn, 0);
368                         dofaceuv(face, uvs, 6, 7, 4, 0);
369
370                         face = makeface(v3, rn->v4, v4, NULL, rn, 0);
371                         dofaceuv(face, uvs, 6, 3, 7, 0);
372
373                         face = makeface(v4, rn->v1, v1, NULL, rn, 0);
374                         dofaceuv(face, uvs, 7, 0, 4, 0);
375
376                         break;
377                 case 11:
378                         face = makeface(rn->v4, v4, v2, rn->v3, rn, 0);
379                         dofaceuv(face, uvs, 3, 7, 5, 2);
380
381                         face = makeface(v4, v1, v2, NULL, rn, 0);
382                         dofaceuv(face, uvs, 7, 4, 5, 0);
383
384                         face = makeface(v4, rn->v1, v1, NULL, rn, 0);
385                         dofaceuv(face, uvs, 7, 0, 4, 0);
386
387                         face = makeface(v1, rn->v2, v2, NULL, rn, 0);
388                         dofaceuv(face, uvs, 4, 1, 5, 0);
389
390                         break;
391                 
392                 case 15:
393                         face = makeface(v1, v2, v3, v4, rn, 0);
394                         dofaceuv(face, uvs, 4, 5, 6, 7);
395
396                         face = makeface(v1, rn->v2, v2, NULL, rn, 0);
397                         dofaceuv(face, uvs, 4, 1, 5, 0);
398
399                         face = makeface(v2, rn->v3, v3, NULL, rn, 0);
400                         dofaceuv(face, uvs, 5, 2, 6, 0);
401
402                         face = makeface(v3, rn->v4, v4, NULL, rn, 0);
403                         dofaceuv(face, uvs, 6, 3, 7, 0);
404
405                         face = makeface(v4, rn->v1, v1, NULL, rn, 0);
406                         dofaceuv(face, uvs, 7, 0, 4, 0);
407
408                         break;
409         }
410 }
411
412 void anchorTriface(RNode *rn, float *v1, float *v2, float *v3, int flag)
413 {
414         Face *face;
415         float uvs[8][2];
416
417         memcpy(uvs, rn->uv, sizeof(float) * 4 * 2);
418         HALFUV(uvs[4], rn->uv[0], rn->uv[1]);
419         HALFUV(uvs[5], rn->uv[1], rn->uv[2]);
420         HALFUV(uvs[6], rn->uv[2], rn->uv[0]);
421
422         switch(flag) {
423                 case 1:
424                         face = makeface(rn->v1, v1, rn->v3, NULL, rn, 0);
425                         dofaceuv(face, uvs, 0, 4, 2, 0);
426
427                         face = makeface(v1, rn->v2, rn->v3, NULL, rn, 0);
428                         dofaceuv(face, uvs, 4, 1, 2, 0);
429
430                         break;
431                 case 2:
432                         face = makeface(rn->v2, v2, rn->v1, NULL, rn, 0);
433                         dofaceuv(face, uvs, 1, 5, 0, 0);
434
435                         face = makeface(v2, rn->v3, rn->v1, NULL, rn, 0);
436                         dofaceuv(face, uvs, 5, 2, 0, 0);
437
438                         break;
439                 case 4:
440                         face = makeface(rn->v3, v3, rn->v2, NULL, rn, 0);
441                         dofaceuv(face, uvs, 2, 6, 1, 0);
442
443                         face = makeface(v3, rn->v1, rn->v2, NULL, rn, 0);
444                         dofaceuv(face, uvs, 6, 0, 1, 0);
445
446                         break;
447                         
448                 case 3:
449                         face = makeface(rn->v1, v2, rn->v3, NULL, rn, 0);
450                         dofaceuv(face, uvs, 0, 5, 2, 0);
451
452                         face = makeface(rn->v1, v1, v2, NULL, rn, 0);
453                         dofaceuv(face, uvs, 0, 4, 5, 0);
454
455                         face = makeface(v1, rn->v2, v2, NULL, rn, 0);
456                         dofaceuv(face, uvs, 4, 1, 5, 0);
457
458                         break;
459                 case 6:
460                         face = makeface(rn->v2, v3, rn->v1, NULL, rn, 0);
461                         dofaceuv(face, uvs, 1, 6, 0, 0);
462
463                         face = makeface(rn->v2, v2, v3, NULL, rn, 0);
464                         dofaceuv(face, uvs, 1, 5, 6, 0);
465
466                         face = makeface(v2, rn->v3, v3, NULL, rn, 0);
467                         dofaceuv(face, uvs, 5, 2, 6, 0);
468
469                         break;
470                 case 5:
471                         face = makeface(rn->v3, v1, rn->v2, NULL, rn, 0);
472                         dofaceuv(face, uvs, 2, 4, 1, 0);
473
474                         face = makeface(rn->v3, v3, v1, NULL, rn, 0);
475                         dofaceuv(face, uvs, 2, 6, 4, 0);
476
477                         face = makeface(v3, rn->v1, v1, NULL, rn, 0);
478                         dofaceuv(face, uvs, 6, 0, 4, 0);
479
480                         break;
481                         
482                 case 7:
483                         face = makeface(v1, v2, v3, NULL, rn, 0);
484                         dofaceuv(face, uvs, 4, 5, 6, 0);
485
486                         face = makeface(rn->v1, v1, v3, NULL, rn, 0);
487                         dofaceuv(face, uvs, 0, 4, 6, 0);
488
489                         face = makeface(rn->v2, v2, v1, NULL, rn, 0);
490                         dofaceuv(face, uvs, 1, 5, 4, 0);
491
492                         face = makeface(rn->v3, v3, v2, NULL, rn, 0);
493                         dofaceuv(face, uvs, 2, 6, 5, 0);
494
495                         break;
496         }       
497 }
498
499
500 float *findmiddlevertex(RNode *node, RNode *nb, float *v1, float *v2)
501 {
502         int test= 0;
503
504         if(nb==0) return 0;
505         
506         if(nb->ed1==node) {
507                 if(nb->v1==v1 || nb->v1==v2) test++;
508                 if(nb->v2==v1 || nb->v2==v2) test+=2;
509                 if(test==1) return nb->v2;
510                 else if(test==2) return nb->v1;
511         }
512         else if(nb->ed2==node) {
513                 if(nb->v2==v1 || nb->v2==v2) test++;
514                 if(nb->v3==v1 || nb->v3==v2) test+=2;
515                 if(test==1) return nb->v3;
516                 else if(test==2) return nb->v2;
517         }
518         else if(nb->ed3==node) {
519                 if(nb->type==4) {
520                         if(nb->v3==v1 || nb->v3==v2) test++;
521                         if(nb->v4==v1 || nb->v4==v2) test+=2;
522                         if(test==1) return nb->v4;
523                         else if(test==2) return nb->v3;
524                 }
525                 else {
526                         if(nb->v3==v1 || nb->v3==v2) test++;
527                         if(nb->v1==v1 || nb->v1==v2) test+=2;
528                         if(test==1) return nb->v1;
529                         else if(test==2) return nb->v3;
530                 }
531         }
532         else if(nb->ed4==node) {
533                 if(nb->v4==v1 || nb->v4==v2) test++;
534                 if(nb->v1==v1 || nb->v1==v2) test+=2;
535                 if(test==1) return nb->v1;
536                 else if(test==2) return nb->v4;
537         }
538         return 0;
539 }
540
541 void make_face_tab()    /* takes care of anchoring */
542 {
543         RNode *rn, **el;
544         Face *face = NULL;
545         float *v1, *v2, *v3, *v4;
546         int a, flag, w1, w2, w3;
547         char *charcol;
548
549         if(RG.totelem==0) return;
550         
551         init_face_tab();
552         
553         RG.igamma= 1.0/RG.gamma;
554         RG.radfactor= RG.radfac*pow(64*64, RG.igamma);
555
556         /* convert face colors */
557         el= RG.elem;
558         for(a=RG.totelem; a>0; a--, el++) {
559                 rn= *el;
560                 charcol= (char *)&( rn->col );
561
562                 charcol[3]= calculatecolor(rn->totrad[0]);
563                 charcol[2]= calculatecolor(rn->totrad[1]);
564                 charcol[1]= calculatecolor(rn->totrad[2]);
565         }
566         
567         /* check nodes and make faces */
568         el= RG.elem;
569         for(a=RG.totelem; a>0; a--, el++) {
570
571                 rn= *el;
572                 
573                 rn->v1[3]= 0.0;
574                 rn->v2[3]= 0.0;
575                 rn->v3[3]= 0.0;
576                 if(rn->v4) rn->v4[3]= 0.0;
577                 
578                 /* test edges for subdivide */
579                 flag= 0;
580                 v1= v2= v3= v4= 0;
581                 if(rn->ed1) {
582                         v1= findmiddlevertex(rn, rn->ed1->down1, rn->v1, rn->v2);
583                         if(v1) flag |= 1;
584                 }
585                 if(rn->ed2) {
586                         v2= findmiddlevertex(rn, rn->ed2->down1, rn->v2, rn->v3);
587                         if(v2) flag |= 2;
588                 }
589                 if(rn->ed3) {
590                         if(rn->type==4)
591                                 v3= findmiddlevertex(rn, rn->ed3->down1, rn->v3, rn->v4);
592                         else
593                                 v3= findmiddlevertex(rn, rn->ed3->down1, rn->v3, rn->v1);
594                         if(v3) flag |= 4;
595                 }
596                 if(rn->ed4) {
597                         v4= findmiddlevertex(rn, rn->ed4->down1, rn->v4, rn->v1);
598                         if(v4) flag |= 8;
599                 }
600                 
601                 /* using flag and vertexpointers now Faces can be made */
602                 
603                 if(flag==0) {
604                         makeface(rn->v1, rn->v2, rn->v3, rn->v4, rn, 1);
605                 }
606                 else if(rn->type==4) anchorQuadface(rn, v1, v2, v3, v4, flag);
607                 else anchorTriface(rn, v1, v2, v3, flag);
608         }
609         
610         /* add */
611         for(a=0; a<RG.totface; a++) {
612                 
613                 RAD_NEXTFACE(a);
614                 
615                 if(face->v4) {
616                         addaccuweight( (char *)&(face->col), (char *)(face->v1+3), 16 );
617                         addaccuweight( (char *)&(face->col), (char *)(face->v2+3), 16 );
618                         addaccuweight( (char *)&(face->col), (char *)(face->v3+3), 16 );
619                         addaccuweight( (char *)&(face->col), (char *)(face->v4+3), 16 );
620                 }
621                 else {
622                         triaweight(face, &w1, &w2, &w3);
623                         addaccuweight( (char *)&(face->col), (char *)(face->v1+3), w1 );
624                         addaccuweight( (char *)&(face->col), (char *)(face->v2+3), w2 );
625                         addaccuweight( (char *)&(face->col), (char *)(face->v3+3), w3 );
626                 }
627         }
628
629 }
630
631 void filterFaces()
632 {
633         /* put vertex colors in faces, and put them back */
634         
635         Face *face = NULL;
636         int a, w1, w2, w3;
637
638         if(RG.totface==0) return;
639
640         /* clear */
641         for(a=0; a<RG.totface; a++) {
642                 RAD_NEXTFACE(a);
643                 face->col= 0;
644         }
645         
646         /* add: vertices with faces */
647         for(a=0; a<RG.totface; a++) {
648                 RAD_NEXTFACE(a);
649                 
650                 if(face->v4) {
651                         addaccuweight( (char *)(face->v1+3), (char *)&(face->col), 16 );
652                         addaccuweight( (char *)(face->v2+3), (char *)&(face->col), 16 );
653                         addaccuweight( (char *)(face->v3+3), (char *)&(face->col), 16 );
654                         addaccuweight( (char *)(face->v4+3), (char *)&(face->col), 16 );
655                 }
656                 else {
657                         triaweight(face, &w1, &w2, &w3);
658                         addaccuweight( (char *)(face->v1+3), (char *)&(face->col), w1 );
659                         addaccuweight( (char *)(face->v2+3), (char *)&(face->col), w2 );
660                         addaccuweight( (char *)(face->v3+3), (char *)&(face->col), w3 );
661                 }
662         }
663         
664         /* clear */
665         for(a=0; a<RG.totface; a++) {
666                 RAD_NEXTFACE(a);
667                 face->v1[3]= 0.0;
668                 face->v2[3]= 0.0;
669                 face->v3[3]= 0.0;
670                 if(face->v4) face->v4[3]= 0.0;
671         }
672
673
674         /* add: faces with vertices */
675         for(a=0; a<RG.totface; a++) {
676                 
677                 RAD_NEXTFACE(a);
678                 
679                 if(face->v4) {
680                         addaccuweight( (char *)&(face->col), (char *)(face->v1+3), 16 );
681                         addaccuweight( (char *)&(face->col), (char *)(face->v2+3), 16 );
682                         addaccuweight( (char *)&(face->col), (char *)(face->v3+3), 16 );
683                         addaccuweight( (char *)&(face->col), (char *)(face->v4+3), 16 );
684                 }
685                 else {
686                         triaweight(face, &w1, &w2, &w3);
687                         addaccuweight( (char *)&(face->col), (char *)(face->v1+3), w1 );
688                         addaccuweight( (char *)&(face->col), (char *)(face->v2+3), w2 );
689                         addaccuweight( (char *)&(face->col), (char *)(face->v3+3), w3 );
690                 }
691         }
692 }
693
694 void calcfiltrad(RNode *rn, float *cd)
695 {
696         float area;
697
698         cd[0]= 2.0*rn->totrad[0];
699         cd[1]= 2.0*rn->totrad[1];
700         cd[2]= 2.0*rn->totrad[2];
701         area= 2.0;
702         
703         if(rn->ed1) {
704                 cd[0]+= rn->ed1->totrad[0];
705                 cd[1]+= rn->ed1->totrad[1];
706                 cd[2]+= rn->ed1->totrad[2];
707                 area+= 1.0;
708         }
709         if(rn->ed2) {
710                 cd[0]+= rn->ed2->totrad[0];
711                 cd[1]+= rn->ed2->totrad[1];
712                 cd[2]+= rn->ed2->totrad[2];
713                 area+= 1.0;
714         }
715         if(rn->ed3) {
716                 cd[0]+= rn->ed3->totrad[0];
717                 cd[1]+= rn->ed3->totrad[1];
718                 cd[2]+= rn->ed3->totrad[2];
719                 area+= 1.0;
720         }
721         if(rn->ed4) {
722                 cd[0]+= rn->ed4->totrad[0];
723                 cd[1]+= rn->ed4->totrad[1];
724                 cd[2]+= rn->ed4->totrad[2];
725                 area+= 1.0;
726         }
727         cd[0]/= area;
728         cd[1]/= area;
729         cd[2]/= area;
730         
731 }
732
733 void filterNodes()
734 {
735         /* colors from nodes in tempblock and back */
736         
737         RNode *rn, **el;
738         float *coldata, *cd;
739         int a;
740
741         if(RG.totelem==0) return;
742         /* the up-nodes need a color */
743         el= RG.elem;
744         for(a=0; a<RG.totelem; a++, el++) {
745                 rn= *el;
746                 if(rn->up) {
747                         rn->up->totrad[0]= 0.0;
748                         rn->up->totrad[1]= 0.0;
749                         rn->up->totrad[2]= 0.0;
750                         if(rn->up->up) {
751                                 rn->up->up->totrad[0]= 0.0;
752                                 rn->up->up->totrad[1]= 0.0;
753                                 rn->up->up->totrad[2]= 0.0;
754                         }
755                 }
756         }
757         el= RG.elem;
758         for(a=0; a<RG.totelem; a++, el++) {
759                 rn= *el;
760                 if(rn->up) {
761                         rn->up->totrad[0]+= 0.5*rn->totrad[0];
762                         rn->up->totrad[1]+= 0.5*rn->totrad[1];
763                         rn->up->totrad[2]+= 0.5*rn->totrad[2];
764                         if(rn->up->up) {
765                                 rn->up->up->totrad[0]+= 0.25*rn->totrad[0];
766                                 rn->up->up->totrad[1]+= 0.25*rn->totrad[1];
767                                 rn->up->up->totrad[2]+= 0.25*rn->totrad[2];
768                         }
769                 }
770         }
771         
772         /* add using area */
773         cd= coldata= MEM_mallocN(3*4*RG.totelem, "filterNodes");
774         el= RG.elem;
775         for(a=0; a<RG.totelem; a++, el++) {
776                 calcfiltrad(*el, cd);
777                 cd+= 3;
778         }
779         
780         cd= coldata;
781         el= RG.elem;
782         for(a=0; a<RG.totelem; a++, el++) {
783                 rn= *el;
784                 VECCOPY(rn->totrad, cd);
785                 cd+= 3;
786         }
787         MEM_freeN(coldata);
788 }
789
790 void removeEqualNodes(short limit)
791 {
792         /* nodes with equal colors: remove */
793         RNode **el, *rn, *rn1;
794         float thresh, f1, f2;
795         int a, foundone=1, ok;
796         int c1, c2;
797         
798         if(limit==0) return;
799         
800         thresh= 1.0/(256.0*RG.radfactor);
801         thresh= 3.0*pow(thresh, RG.gamma);
802         
803         waitcursor(1);
804                 
805         while(foundone) {
806                 foundone= 0;
807                 
808                 el= RG.elem;
809                 for(a=RG.totelem; a>1; a--, el++) {
810                         rn= *el;
811                         rn1= *(el+1);
812                         
813                         if(rn!=rn->par->first && rn1!=rn1->par->first) {
814                                 if(rn->up && rn->up==rn1->up) {
815                                         f1= rn->totrad[0]+ rn->totrad[1]+ rn->totrad[2];
816                                         f2= rn1->totrad[0]+ rn1->totrad[1]+ rn1->totrad[2];
817                                         
818                                         ok= 0;
819                                         if(f1<thresh && f2<thresh) ok= 1;
820                                         else {
821                                                 c1= calculatecolor(rn->totrad[0]);
822                                                 c2= calculatecolor(rn1->totrad[0]);
823                                                 
824                                                 if( abs(c1-c2)<=limit ) {
825                                                         c1= calculatecolor(rn->totrad[1]);
826                                                         c2= calculatecolor(rn1->totrad[1]);
827                                                         
828                                                         if( abs(c1-c2)<=limit ) {
829                                                                 c1= calculatecolor(rn->totrad[2]);
830                                                                 c2= calculatecolor(rn1->totrad[2]);
831                                                                 
832                                                                 if( abs(c1-c2)<=limit ) {
833                                                                         ok= 1;
834                                                                 }
835                                                         }
836                                                 }
837                                         }
838                                         
839                                         if(ok) {
840                                                 rn->up->totrad[0]= 0.5f*(rn->totrad[0]+rn1->totrad[0]);
841                                                 rn->up->totrad[1]= 0.5f*(rn->totrad[1]+rn1->totrad[1]);
842                                                 rn->up->totrad[2]= 0.5f*(rn->totrad[2]+rn1->totrad[2]);
843                                                 rn1= rn->up;
844                                                 deleteNodes(rn1);
845                                                 if(rn1->down1) ;
846                                                 else {
847                                                         foundone++;
848                                                         a--; el++;
849                                                 }
850                                         }
851                                 }
852                         }
853                 }
854                 if(foundone) {
855                         makeGlobalElemArray();
856                 }
857         }
858         waitcursor(0);
859 }
860
861 #define BLSIZE  32000
862
863 void rad_addmesh(void)
864 {
865         Face *face = NULL;
866         Object *ob;
867         Mesh *me;
868         MVert *mvert;
869         MFace *mface;
870         TFace *tface;
871         Material *ma=0;
872         unsigned int *md, *coldata, *cd;
873         float **fpp, **poindata;
874         float cent[3], min[3], max[3];
875         int a, vcount, vlnr, startf, endf;
876         
877         if(RG.totface==0) return;
878         
879         if(RG.totmat==MAXMAT) notice("warning: cannot assign more than 16 materials to 1 mesh");
880         
881         /* make sure there's alpha in the color, to distinguish */
882         for(a=0; a<RG.totface; a++) {
883                 RAD_NEXTFACE(a);
884                 *((unsigned int *)face->v1+3) |= 0x1000000; 
885                 *((unsigned int *)face->v2+3) |= 0x1000000;
886                 *((unsigned int *)face->v3+3) |= 0x1000000;
887                 if(face->v4) {
888                         *((unsigned int *)face->v4+3) |= 0x1000000;
889                 }
890         }
891         
892         /* we write in blocks of BLSIZE vertices max */
893         coldata= MEM_mallocN(4*BLSIZE, "writefaces");
894         poindata= MEM_mallocN(sizeof(void *)*BLSIZE, "writefaces1");
895         
896         vlnr= 0; 
897         endf= 0;
898         while(endf<RG.totface) {
899                 
900                 cd= coldata;
901                 fpp= poindata;
902                 startf= endf;
903                 vcount= 0;
904         
905                 face= RG.facebase[(startf-1)>>10]+((startf-1) & 1023);
906                 for(vlnr=startf; vlnr<RG.totface; vlnr++) {
907                         RAD_NEXTFACE(vlnr);
908
909                         if( *((unsigned int *)face->v1+3) & 0x1000000 ) { /* is a color */
910                                 *cd= *((unsigned int *)face->v1+3);
911                                 *((unsigned int *)face->v1+3) = vcount;
912                                 *fpp= face->v1;
913                                 fpp++; vcount++; cd++;
914                         }
915                         if( *((unsigned int *)face->v2+3) & 0x1000000 ) {
916                                 *cd= *((unsigned int *)face->v2+3);
917                                 *((unsigned int *)face->v2+3) = vcount;
918                                 *fpp= face->v2;
919                                 fpp++; vcount++; cd++;
920                         }
921                         if( *((unsigned int *)face->v3+3) & 0x1000000 ) {
922                                 *cd= *((unsigned int *)face->v3+3);
923                                 *((unsigned int *)face->v3+3) = vcount;
924                                 *fpp= face->v3;
925                                 fpp++; vcount++; cd++;
926                         }
927                         if(face->v4 && ( *((unsigned int *)face->v4+3) & 0x1000000 ) ) {
928                                 *cd= *((unsigned int *)face->v4+3);
929                                 *((unsigned int *)face->v4+3) = vcount;
930                                 *fpp= face->v4;
931                                 fpp++; vcount++; cd++;
932                         }
933
934                         if(vcount>=BLSIZE-4) {
935                                 vlnr++;
936                                 break;
937                         }
938                 }
939
940                 /* we now make the Mesh */
941                 endf= vlnr;
942
943                 ob= add_object(OB_MESH);
944                 
945                 me= ob->data;
946                 me->totvert= vcount;
947                 me->totface= endf-startf;
948                 me->flag= 0;
949                 me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
950                 //me->mcol= MEM_callocN(4*me->totface*sizeof(MCol), "mverts");
951                 me->mface= MEM_callocN(me->totface*sizeof(MFace), "mface");
952                 me->tface= MEM_callocN(me->totface*sizeof(TFace), "tface");
953
954
955                 /* materials, and set VCOL flag */
956                 for(a=0; a<RG.totmat; a++) {
957                         assign_material(ob, RG.matar[a], a+1);
958                         ma= RG.matar[a];
959                         if(ma) ma->mode |= MA_VERTEXCOL;
960                 }
961
962                 /* verts */
963                 mvert= me->mvert;
964                 fpp= poindata;
965                 for(a=0; a<me->totvert; a++, mvert++, fpp++) {
966                         VECCOPY(mvert->co, *fpp);
967                 }
968
969                 /* faces, mcol and uv */
970                 mface= me->mface;
971                 tface = me->tface;
972                 md= (unsigned int *)me->mcol;
973
974                 face= RG.facebase[(startf-1)>>10]+((startf-1) & 1023);
975                 for(a=startf; a<endf; a++, md+=4, mface++, tface++) {
976                         RAD_NEXTFACE(a);
977                         mface->v1= *((unsigned int *)face->v1+3);
978                         mface->v2= *((unsigned int *)face->v2+3);
979                         mface->v3= *((unsigned int *)face->v3+3);
980                         if(face->v4) mface->v4= *((unsigned int *)face->v4+3);
981
982                         mface->edcode= 3;
983                         test_index_mface(mface, face->v4 ? 4 : 3);
984                         mface->mat_nr= face->matindex;
985
986                         if (face->tface) {
987                                 memcpy(tface, face->tface, sizeof(TFace));
988                         }
989
990                         memcpy(tface->uv, face->uv, sizeof(float) * 4 * 2);
991                         tface->col[0]= coldata[mface->v1];
992                         tface->col[1]= coldata[mface->v2];
993                         tface->col[2]= coldata[mface->v3];
994                         tface->col[3]= coldata[mface->v4];
995                 }
996                 
997                 /* boundbox and centrenew */
998                 
999                 INIT_MINMAX(min, max);
1000
1001                 mvert= me->mvert;
1002                 for(a=0; a<me->totvert; a++, mvert++) {
1003                         DO_MINMAX(mvert->co, min, max);
1004                 }
1005
1006                 cent[0]= (min[0]+max[0])/2.0f;
1007                 cent[1]= (min[1]+max[1])/2.0f;
1008                 cent[2]= (min[2]+max[2])/2.0f;
1009
1010                 mvert= me->mvert;
1011                 for(a=0; a<me->totvert; a++, mvert++) {
1012                         VecSubf(mvert->co, mvert->co, cent);
1013                 }
1014                 
1015                 VECCOPY(ob->loc, cent);
1016                 tex_space_mesh(me);
1017                 
1018                 /* restore colors */
1019                 face= RG.facebase[(startf-1)>>10]+((startf-1) & 1023);
1020                 for(a=startf; a<endf; a++) {
1021                         RAD_NEXTFACE(a);
1022
1023                         cd= ((unsigned int *)face->v1+3);
1024                         if( *cd < 0x1000000 ) *cd= coldata[*cd];
1025                         cd= ((unsigned int *)face->v2+3);
1026                         if( *cd < 0x1000000 ) *cd= coldata[*cd];
1027                         cd= ((unsigned int *)face->v3+3);
1028                         if( *cd < 0x1000000 ) *cd= coldata[*cd];
1029                         if(face->v4) {
1030                                 cd= ((unsigned int *)face->v4+3);
1031                                 if( *cd < 0x1000000 ) *cd= coldata[*cd];
1032                         }
1033                 }
1034         }
1035                 
1036         MEM_freeN(coldata);
1037         MEM_freeN(poindata);
1038         
1039 }
1040
1041 void rad_replacemesh(void)
1042 {
1043         RPatch *rp;
1044         
1045         deselectall();
1046         
1047         rp= RG.patchbase.first;
1048         while(rp) {
1049                 if( exist_object(rp->from)) rp->from->flag |= SELECT;
1050                 rp= rp->next;
1051         }
1052         
1053         copy_objectflags();
1054         delete_obj(1);
1055         
1056         rad_addmesh();
1057 }
1058