720d02ee7bb1356e33d943f544df8c3e3dd0794a
[blender.git] / source / blender / blenkernel / intern / mesh.c
1
2 /*  mesh.c      MIXED MODEL
3  * 
4  *  jan/maart 95
5  *  
6  * 
7  * $Id$
8  *
9  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version. The Blender
15  * Foundation also sells licenses for use in proprietary software under
16  * the Blender License.  See http://www.blender.org/BL/ for information
17  * about this.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  *
28  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
29  * All rights reserved.
30  *
31  * The Original Code is: all of this file.
32  *
33  * Contributor(s): none yet.
34  *
35  * ***** END GPL/BL DUAL LICENSE BLOCK *****
36  */
37
38 #ifdef WIN32
39 #include "BLI_winstuff.h"
40 #endif
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <math.h>
46
47 #include "MEM_guardedalloc.h"
48
49 #include "DNA_ID.h"
50 #include "DNA_curve_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_image_types.h"
54 #include "DNA_key_types.h"
55 #include "DNA_mesh_types.h"
56
57 #include "BKE_main.h"
58 #include "BKE_global.h"
59 #include "BKE_mesh.h"
60 #include "BKE_subsurf.h"
61 #include "BKE_displist.h"
62 #include "BKE_library.h"
63 #include "BKE_material.h"
64 #include "BKE_key.h"
65 /* these 2 are only used by conversion functions */
66 #include "BKE_curve.h"
67 /* -- */
68 #include "BKE_object.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_bad_level_calls.h"
71
72 #include "BLI_blenlib.h"
73 #include "BLI_editVert.h"
74 #include "BLI_arithb.h"
75
76
77
78 int update_realtime_texture(TFace *tface, double time)
79 {
80         Image *ima;
81         int     inc = 0;
82         float   diff;
83         int     newframe;
84
85         ima = tface->tpage;
86
87         if (!ima)
88                 return 0;
89
90         if (ima->lastupdate<0)
91                 ima->lastupdate = 0;
92
93         if (ima->lastupdate>time)
94                 ima->lastupdate=(float)time;
95
96         if(ima->tpageflag & IMA_TWINANIM) {
97                 if(ima->twend >= ima->xrep*ima->yrep) ima->twend= ima->xrep*ima->yrep-1;
98                 
99                 /* check: zit de bindcode niet het array? Vrijgeven. (nog doen) */
100                 
101                 diff = (float)(time-ima->lastupdate);
102
103                 inc = (int)(diff*(float)ima->animspeed);
104
105                 ima->lastupdate+=((float)inc/(float)ima->animspeed);
106
107                 newframe = ima->lastframe+inc;
108
109                 if (newframe > (int)ima->twend)
110                         newframe = (int)ima->twsta-1 + (newframe-ima->twend)%(ima->twend-ima->twsta);
111
112                 ima->lastframe = newframe;
113         }
114         return inc;
115 }
116
117 float get_mvert_weight (Object *ob, int vert, int defgroup)
118 {
119         int     i;
120         Mesh *me;
121         float   result;
122
123         me=ob->data;
124
125         if (!me->dvert)
126                 return 0.0F;
127
128         result=0.0F;
129
130         for (i=0; i<me->dvert[vert].totweight; i++){
131                 if (me->dvert[vert].dw[i].def_nr==defgroup)
132                         result+=me->dvert[vert].dw[i].weight;           
133         }
134
135         return result;
136 }
137
138 void unlink_mesh(Mesh *me)
139 {
140         int a;
141         
142         if(me==0) return;
143         
144         for(a=0; a<me->totcol; a++) {
145                 if(me->mat[a]) me->mat[a]->id.us--;
146                 me->mat[a]= 0;
147         }
148         if(me->key) me->key->id.us--;
149         me->key= 0;
150         
151         if(me->texcomesh) me->texcomesh= 0;
152 }
153
154
155 /* niet mesh zelf vrijgeven */
156 void free_mesh(Mesh *me)
157 {
158
159         unlink_mesh(me);
160
161         if(me->mat) MEM_freeN(me->mat);
162         if(me->orco) MEM_freeN(me->orco);
163         if(me->mface) MEM_freeN(me->mface);
164         if(me->tface) MEM_freeN(me->tface);
165         if(me->mvert) MEM_freeN(me->mvert);
166         if(me->dvert) free_dverts(me->dvert, me->totvert);
167         if(me->mcol) MEM_freeN(me->mcol);
168         if(me->msticky) MEM_freeN(me->msticky);
169         if(me->bb) MEM_freeN(me->bb);
170         if(me->disp.first) freedisplist(&me->disp);
171 }
172
173 void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
174 {
175         /* Assumes dst is already set up */
176         int i;
177
178         if (!src || !dst)
179                 return;
180
181         memcpy (dst, src, copycount * sizeof(MDeformVert));
182         
183         for (i=0; i<copycount; i++){
184                 if (src[i].dw){
185                         dst[i].dw = MEM_callocN (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
186                         memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
187                 }
188         }
189
190 }
191 void free_dverts(MDeformVert *dvert, int totvert)
192 {
193         /* Instead of freeing the verts directly,
194         call this function to delete any special
195         vert data */
196         int     i;
197
198         if (!dvert)
199                 return;
200
201         /* Free any special data from the verts */
202         for (i=0; i<totvert; i++){
203                 if (dvert[i].dw) MEM_freeN (dvert[i].dw);
204         }
205         MEM_freeN (dvert);
206 }
207
208 Mesh *add_mesh()
209 {
210         Mesh *me;
211         
212         me= alloc_libblock(&G.main->mesh, ID_ME, "Mesh");
213         
214         me->size[0]= me->size[1]= me->size[2]= 1.0;
215         me->smoothresh= 30;
216         me->texflag= AUTOSPACE;
217         me->flag= ME_TWOSIDED;
218         me->subdiv= 1;
219         me->subdivr = 1;
220         me->bb= unit_boundbox();
221         
222         return me;
223 }
224
225 Mesh *copy_mesh(Mesh *me)
226 {
227         Mesh *men;
228         int a;
229         
230         men= copy_libblock(me);
231         
232         men->mat= MEM_dupallocN(me->mat);
233         for(a=0; a<men->totcol; a++) {
234                 id_us_plus((ID *)men->mat[a]);
235         }
236         id_us_plus((ID *)men->texcomesh);
237         men->mface= MEM_dupallocN(me->mface);
238
239         men->tface= MEM_dupallocN(me->tface);
240
241         men->dface= 0;
242         men->mvert= MEM_dupallocN(me->mvert);
243         memcpy (men->mvert, me->mvert, sizeof (MVert)*me->totvert);
244         if (me->dvert){
245                 men->dvert = MEM_mallocN (sizeof (MDeformVert)*me->totvert, "MDeformVert");
246                 copy_dverts(men->dvert, me->dvert, me->totvert);
247         }
248
249         men->mcol= MEM_dupallocN(me->mcol);
250         men->msticky= MEM_dupallocN(me->msticky);
251         men->texcomesh= 0;
252         men->orco= 0;
253         men->bb= MEM_dupallocN(men->bb);
254         
255         copy_displist(&men->disp, &me->disp);
256         
257         men->key= copy_key(me->key);
258         if(men->key) men->key->from= (ID *)men;
259         
260         return men;
261 }
262
263 void make_local_tface(Mesh *me)
264 {
265         TFace *tface;
266         Image *ima;
267         int a;
268         
269         if(me->tface==0) return;
270         
271         a= me->totface;
272         tface= me->tface;
273         while(a--) {
274                 
275                 /* speciaal geval: ima altijd meteen lokaal */
276                 if(tface->tpage) {
277                         ima= tface->tpage;
278                         if(ima->id.lib) {
279                                 ima->id.lib= 0;
280                                 ima->id.flag= LIB_LOCAL;
281                                 new_id(0, (ID *)ima, 0);
282                         }
283                 }
284                 tface++;
285         }
286         
287 }
288
289 void make_local_mesh(Mesh *me)
290 {
291         Object *ob;
292         Mesh *men;
293         int local=0, lib=0;
294         
295         /* - zijn er alleen lib users: niet doen
296          * - zijn er alleen locale users: flag zetten
297          * - mixed: copy
298          */
299         
300         if(me->id.lib==0) return;
301         if(me->id.us==1) {
302                 me->id.lib= 0;
303                 me->id.flag= LIB_LOCAL;
304                 new_id(0, (ID *)me, 0);
305                 
306                 if(me->tface) make_local_tface(me);
307                 
308                 return;
309         }
310         
311         ob= G.main->object.first;
312         while(ob) {
313                 if( me==get_mesh(ob) ) {
314                         if(ob->id.lib) lib= 1;
315                         else local= 1;
316                 }
317                 ob= ob->id.next;
318         }
319         
320         if(local && lib==0) {
321                 me->id.lib= 0;
322                 me->id.flag= LIB_LOCAL;
323                 new_id(0, (ID *)me, 0);
324                 
325                 if(me->tface) make_local_tface(me);
326                 
327         }
328         else if(local && lib) {
329                 men= copy_mesh(me);
330                 men->id.us= 0;
331                 
332                 ob= G.main->object.first;
333                 while(ob) {
334                         if( me==get_mesh(ob) ) {                                
335                                 if(ob->id.lib==0) {
336                                         set_mesh(ob, men);
337                                 }
338                         }
339                         ob= ob->id.next;
340                 }
341         }
342 }
343
344 void boundbox_mesh(Mesh *me, float *loc, float *size)
345 {
346         MVert *mvert;
347         BoundBox *bb;
348         float min[3], max[3];
349         float mloc[3], msize[3];
350         int a;
351         
352         if(me->bb==0) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
353         bb= me->bb;
354         
355         INIT_MINMAX(min, max);
356
357         if (!loc) loc= mloc;
358         if (!size) size= msize;
359         
360         mvert= me->mvert;
361         for(a=0; a<me->totvert; a++, mvert++) {
362                 DO_MINMAX(mvert->co, min, max);
363         }
364
365         if(me->totvert) {
366                 loc[0]= (min[0]+max[0])/2.0f;
367                 loc[1]= (min[1]+max[1])/2.0f;
368                 loc[2]= (min[2]+max[2])/2.0f;
369                 
370                 size[0]= (max[0]-min[0])/2.0f;
371                 size[1]= (max[1]-min[1])/2.0f;
372                 size[2]= (max[2]-min[2])/2.0f;
373         }
374         else {
375                 loc[0]= loc[1]= loc[2]= 0.0;
376                 size[0]= size[1]= size[2]= 0.0;
377         }
378         
379         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= loc[0]-size[0];
380         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= loc[0]+size[0];
381         
382         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= loc[1]-size[1];
383         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= loc[1]+size[1];
384
385         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= loc[2]-size[2];
386         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= loc[2]+size[2];
387 }
388
389 void tex_space_mesh(Mesh *me)
390 {
391         KeyBlock *kb;
392         float *fp, loc[3], size[3], min[3], max[3];
393         int a;
394
395         boundbox_mesh(me, loc, size);
396
397         if(me->texflag & AUTOSPACE) {
398                 if(me->key) {
399                         kb= me->key->refkey;
400                         if (kb) {
401                                 
402                                 INIT_MINMAX(min, max);
403                                 
404                                 fp= kb->data;
405                                 for(a=0; a<kb->totelem; a++, fp+=3) {   
406                                         DO_MINMAX(fp, min, max);
407                                 }
408                                 if(kb->totelem) {
409                                         loc[0]= (min[0]+max[0])/2.0f; loc[1]= (min[1]+max[1])/2.0f; loc[2]= (min[2]+max[2])/2.0f;
410                                         size[0]= (max[0]-min[0])/2.0f; size[1]= (max[1]-min[1])/2.0f; size[2]= (max[2]-min[2])/2.0f;
411                                 }
412                                 else {
413                                         loc[0]= loc[1]= loc[2]= 0.0;
414                                         size[0]= size[1]= size[2]= 0.0;
415                                 }
416                                 
417                         }
418                 }
419
420                 VECCOPY(me->loc, loc);
421                 VECCOPY(me->size, size);
422                 me->rot[0]= me->rot[1]= me->rot[2]= 0.0;
423
424                 if(me->size[0]==0.0) me->size[0]= 1.0;
425                 else if(me->size[0]>0.0 && me->size[0]<0.00001) me->size[0]= 0.00001;
426                 else if(me->size[0]<0.0 && me->size[0]> -0.00001) me->size[0]= -0.00001;
427         
428                 if(me->size[1]==0.0) me->size[1]= 1.0;
429                 else if(me->size[1]>0.0 && me->size[1]<0.00001) me->size[1]= 0.00001;
430                 else if(me->size[1]<0.0 && me->size[1]> -0.00001) me->size[1]= -0.00001;
431         
432                 if(me->size[2]==0.0) me->size[2]= 1.0;
433                 else if(me->size[2]>0.0 && me->size[2]<0.00001) me->size[2]= 0.00001;
434                 else if(me->size[2]<0.0 && me->size[2]> -0.00001) me->size[2]= -0.00001;
435         }
436         
437 }
438
439 void make_orco_displist_mesh(Object *ob, int subdivlvl)
440 {
441         Mesh *me;
442         DispList *dl;
443         DispListMesh *dlm;
444         int i;
445         
446         me= ob->data;
447
448                 /* if there's a key, set the first one */
449         if(me->key && me->texcomesh==0) {
450                 cp_key(0, me->totvert, me->totvert, (char*) me->mvert->co, me->key, me->key->refkey, 0);
451         }
452
453                 /* Rebuild the displist */
454         dl= subsurf_mesh_to_displist(me, NULL, (short)subdivlvl);
455         
456                 /* Restore correct key */
457         do_ob_key(ob);
458
459                 /* XXX Assume dl is a DL_MESH (it is), 
460                  * should be explicit -zr 
461                  */
462         dlm= dl->mesh;
463         
464         me->orco= MEM_mallocN(dlm->totvert*3*sizeof(float), "mesh displist orco");
465                 
466         for (i=0; i<dlm->totvert; i++) {
467                 float *fp= &me->orco[i*3];
468                 VECCOPY(fp, dlm->mvert[i].co);
469         }
470                 
471         for(i=0; i<dlm->totvert; i++) {
472                 float *fp= &me->orco[i*3];
473                 fp[0]= (fp[0]-me->loc[0])/me->size[0];
474                 fp[1]= (fp[1]-me->loc[1])/me->size[1];
475                 fp[2]= (fp[2]-me->loc[2])/me->size[2];
476         }
477         
478         free_disp_elem(dl);
479 }
480
481 void make_orco_mesh(Mesh *me)
482 {
483         MVert *mvert;
484         KeyBlock *kb;
485         float *orco, *fp;
486         int a, totvert;
487         
488         totvert= me->totvert;
489         if(totvert==0) return;
490         orco= me->orco= MEM_mallocN(sizeof(float)*3*totvert, "orco mesh");
491
492         if(me->key && me->texcomesh==0) {
493                 kb= me->key->refkey;
494                 if (kb) {               /***** BUG *****/
495                         fp= kb->data;
496                         
497                         for(a=0; a<totvert; a++, orco+=3) {
498                                 orco[0]= (fp[0]-me->loc[0])/me->size[0];
499                                 orco[1]= (fp[1]-me->loc[1])/me->size[1];
500                                 orco[2]= (fp[2]-me->loc[2])/me->size[2];
501                                 
502                                 /* mvert alleen ophogen als totvert <= kb->totelem */
503                                 if(a<kb->totelem) fp+=3;
504                         }
505                 }
506         }
507         else {
508                 if(me->texcomesh) {
509                         me= me->texcomesh;
510                 }       
511         
512                 mvert= me->mvert;
513                 for(a=0; a<totvert; a++, orco+=3) {
514                         orco[0]= (mvert->co[0]-me->loc[0])/me->size[0];
515                         orco[1]= (mvert->co[1]-me->loc[1])/me->size[1];
516                         orco[2]= (mvert->co[2]-me->loc[2])/me->size[2];
517                         
518                         /* mvert alleen ophogen als totvert <= me->totvert */
519                         if(a<me->totvert) mvert++;
520                 }
521         }
522 }
523
524 /** rotates the vertices of a face in case v[2] or v[3] (vertex index)
525   * is = 0.
526   * Helaas, the MFace structure has no pointer to its
527   * texture face, therefore, texture can not be fixed inside
528   * this function. 
529   * 
530   * see also blender/src/editmesh.c, fix_faceindices()
531
532   * THIS FUNCTION WILL BE DINOSOURCE. For the moment, another hack
533         is added to fix texture coordinates / vertex colors:
534
535         void test_index_face(MFace *mface, TFace *tface, int nr)
536   */
537
538 void test_index_mface(MFace *mface, int nr)
539 {
540         int a;
541
542         
543         /* first test if the face is legal */
544
545         if(mface->v3 && mface->v3==mface->v4) {
546                 mface->v4= 0;
547                 nr--;
548         }
549         if(mface->v2 && mface->v2==mface->v3) {
550                 mface->v3= mface->v4;
551                 mface->v4= 0;
552                 nr--;
553         }
554         if(mface->v1==mface->v2) {
555                 mface->v2= mface->v3;
556                 mface->v3= mface->v4;
557                 mface->v4= 0;
558                 nr--;
559         }
560
561         /* voorkom dat een nul op de verkeerde plek staat */
562         if(nr==2) {
563                 if(mface->v2==0) SWAP(int, mface->v1, mface->v2);
564         }
565         else if(nr==3) {
566                 if(mface->v3==0) {
567                         SWAP(int, mface->v1, mface->v2);
568                         SWAP(int, mface->v2, mface->v3);
569                         
570                         a= mface->edcode;
571                         mface->edcode= 0;
572                         if(a & ME_V1V2) mface->edcode |= ME_V3V1;
573                         if(a & ME_V2V3) mface->edcode |= ME_V1V2;
574                         if(a & ME_V3V1) mface->edcode |= ME_V2V3;
575                         
576                         a= mface->puno;
577                         mface->puno &= ~15;
578                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV2;
579                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV3;
580                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
581                 }
582         }
583         else if(nr==4) {
584                 if(mface->v3==0 || mface->v4==0) {
585                         SWAP(int, mface->v1, mface->v3);
586                         SWAP(int, mface->v2, mface->v4);
587                         a= mface->edcode;
588                         mface->edcode= 0;
589                         if(a & ME_V1V2) mface->edcode |= ME_V3V4;
590                         if(a & ME_V2V3) mface->edcode |= ME_V2V3;
591                         if(a & ME_V3V4) mface->edcode |= ME_V1V2;
592                         if(a & ME_V4V1) mface->edcode |= ME_V4V1;
593
594                         a= mface->puno;
595                         mface->puno &= ~15;
596                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV3;
597                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV4;
598                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
599                         if(a & ME_FLIPV4) mface->puno |= ME_FLIPV2;
600                 }
601         }
602 }
603
604 /**     This function should die as soon as there is another mesh
605         structure. Functionality is the same as
606
607                 void test_index_mface()
608
609         but it fixes texture coordinates as well.
610 */
611
612 #define UVCOPY(t, s) memcpy(t, s, 2 * sizeof(float));
613 void test_index_face(MFace *mface, TFace *tface, int nr)
614 {
615         int a;
616         float tmpuv[2];
617         unsigned int tmpcol;
618
619
620         /* first test if the face is legal */
621
622         if(mface->v3 && mface->v3==mface->v4) {
623                 mface->v4= 0;
624                 nr--;
625         }
626         if(mface->v2 && mface->v2==mface->v3) {
627                 mface->v3= mface->v4;
628                 mface->v4= 0;
629                 nr--;
630         }
631         if(mface->v1==mface->v2) {
632                 mface->v2= mface->v3;
633                 mface->v3= mface->v4;
634                 mface->v4= 0;
635                 nr--;
636         }
637
638         /* voorkom dat een nul op de verkeerde plek staat */
639         if(nr==2) {
640                 if(mface->v2==0) SWAP(int, mface->v1, mface->v2);
641         }
642         else if(nr==3) {
643                 if(mface->v3==0) {
644                         SWAP(int, mface->v1, mface->v2);
645                         SWAP(int, mface->v2, mface->v3);
646                         /* rotate face UV coordinates, too */
647                         UVCOPY(tmpuv, tface->uv[0]);
648                         UVCOPY(tface->uv[0], tface->uv[1]);
649                         UVCOPY(tface->uv[1], tface->uv[2]);
650                         UVCOPY(tface->uv[2], tmpuv);
651                         /* same with vertex colours */
652                         tmpcol = tface->col[0];
653                         tface->col[0] = tface->col[1];
654                         tface->col[1] = tface->col[2];
655                         tface->col[2] = tmpcol;
656
657                         
658                         a= mface->edcode;
659                         mface->edcode= 0;
660                         if(a & ME_V1V2) mface->edcode |= ME_V3V1;
661                         if(a & ME_V2V3) mface->edcode |= ME_V1V2;
662                         if(a & ME_V3V1) mface->edcode |= ME_V2V3;
663                         
664                         a= mface->puno;
665                         mface->puno &= ~15;
666                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV2;
667                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV3;
668                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
669                 }
670         }
671         else if(nr==4) {
672                 if(mface->v3==0 || mface->v4==0) {
673                         SWAP(int, mface->v1, mface->v3);
674                         SWAP(int, mface->v2, mface->v4);
675                         /* swap UV coordinates */
676                         UVCOPY(tmpuv, tface->uv[0]);
677                         UVCOPY(tface->uv[0], tface->uv[2]);
678                         UVCOPY(tface->uv[2], tmpuv);
679                         UVCOPY(tmpuv, tface->uv[1]);
680                         UVCOPY(tface->uv[1], tface->uv[3]);
681                         UVCOPY(tface->uv[3], tmpuv);
682                         /* swap vertex colours */
683                         tmpcol = tface->col[0];
684                         tface->col[0] = tface->col[2];
685                         tface->col[2] = tmpcol;
686                         tmpcol = tface->col[1];
687                         tface->col[1] = tface->col[3];
688                         tface->col[3] = tmpcol;
689
690                         a= mface->edcode;
691                         mface->edcode= 0;
692                         if(a & ME_V1V2) mface->edcode |= ME_V3V4;
693                         if(a & ME_V2V3) mface->edcode |= ME_V2V3;
694                         if(a & ME_V3V4) mface->edcode |= ME_V1V2;
695                         if(a & ME_V4V1) mface->edcode |= ME_V4V1;
696
697                         a= mface->puno;
698                         mface->puno &= ~15;
699                         if(a & ME_FLIPV1) mface->puno |= ME_FLIPV3;
700                         if(a & ME_FLIPV2) mface->puno |= ME_FLIPV4;
701                         if(a & ME_FLIPV3) mface->puno |= ME_FLIPV1;
702                         if(a & ME_FLIPV4) mface->puno |= ME_FLIPV2;
703                 }
704         }
705 }
706
707 void flipnorm_mesh(Mesh *me)
708 {
709         MFace *mface;
710         MVert *mvert;
711         DispList *dl;
712         float *fp;
713         int a, temp;
714         
715         mvert= me->mvert;
716         a= me->totvert;
717         while(a--) {
718                 mvert->no[0]= -mvert->no[0];
719                 mvert->no[1]= -mvert->no[1];
720                 mvert->no[2]= -mvert->no[2];
721                 mvert++;
722         }
723         
724         mface= me->mface;
725         a= me->totface;
726         while(a--) {
727                 if(mface->v3) {
728                         if(mface->v4) {
729                                 SWAP(int, mface->v4, mface->v1);
730                                 SWAP(int, mface->v3, mface->v2);
731                                 test_index_mface(mface, 4);
732                                 temp= mface->puno;
733                                 mface->puno &= ~15;
734                                 if(temp & ME_FLIPV1) mface->puno |= ME_FLIPV4;
735                                 if(temp & ME_FLIPV2) mface->puno |= ME_FLIPV3;
736                                 if(temp & ME_FLIPV3) mface->puno |= ME_FLIPV2;
737                                 if(temp & ME_FLIPV4) mface->puno |= ME_FLIPV1;
738                         }
739                         else {
740                                 SWAP(int, mface->v3, mface->v1);
741                                 test_index_mface(mface, 3);
742                                 temp= mface->puno;
743                                 mface->puno &= ~15;
744                                 if(temp & ME_FLIPV1) mface->puno |= ME_FLIPV3;
745                                 if(temp & ME_FLIPV2) mface->puno |= ME_FLIPV2;
746                                 if(temp & ME_FLIPV3) mface->puno |= ME_FLIPV1;
747                         }
748                 }
749                 mface++;
750         }
751
752         if(me->disp.first) {
753                 dl= me->disp.first;
754                 fp= dl->nors;
755                 if(fp) {
756                         a= dl->nr;
757                         while(a--) {
758                                 fp[0]= -fp[0];
759                                 fp[1]= -fp[1];
760                                 fp[2]= -fp[2];
761                                 fp+= 3;
762                         }
763                 }
764         }
765 }
766
767 Mesh *get_mesh(Object *ob)
768 {
769         
770         if(ob==0) return 0;
771         if(ob->type==OB_MESH) return ob->data;
772         else return 0;
773 }
774
775 void set_mesh(Object *ob, Mesh *me)
776 {
777         Mesh *old=0;
778         
779         if(ob==0) return;
780         
781         if(ob->type==OB_MESH) {
782                 old= ob->data;
783                 old->id.us--;
784                 ob->data= me;
785                 id_us_plus((ID *)me);
786         }
787         
788         test_object_materials((ID *)me);
789 }
790
791 void mball_to_mesh(ListBase *lb, Mesh *me)
792 {
793         DispList *dl;
794         MVert *mvert;
795         MFace *mface;
796         float *nors, *verts;
797         int a, *index;
798         
799         dl= lb->first;
800         if(dl==0) return;
801
802         if(dl->type==DL_INDEX4) {
803                 me->flag= ME_NOPUNOFLIP;
804                 me->totvert= dl->nr;
805                 me->totface= dl->parts;
806                 
807                 me->mvert=mvert= MEM_callocN(dl->nr*sizeof(MVert), "mverts");
808                 a= dl->nr;
809                 nors= dl->nors;
810                 verts= dl->verts;
811                 while(a--) {
812                         VECCOPY(mvert->co, verts);
813                         mvert->no[0]= (short int)(nors[0]*32767.0);
814                         mvert->no[1]= (short int)(nors[1]*32767.0);
815                         mvert->no[2]= (short int)(nors[2]*32767.0);
816                         mvert++;
817                         nors+= 3;
818                         verts+= 3;
819                 }
820                 
821                 me->mface=mface= MEM_callocN(dl->parts*sizeof(MFace), "mface");
822                 a= dl->parts;
823                 index= dl->index;
824                 while(a--) {
825                         mface->v1= index[0];
826                         mface->v2= index[1];
827                         mface->v3= index[2];
828                         mface->v4= index[3];
829
830                         mface->puno= 0;
831                         mface->edcode= ME_V1V2+ME_V2V3;
832                         mface->flag = ME_SMOOTH;
833                         
834                         mface++;
835                         index+= 4;
836                 }
837         }       
838 }
839
840 void nurbs_to_mesh(Object *ob)
841 {
842         Object *ob1;
843         DispList *dl;
844         Mesh *me;
845         Curve *cu;
846         MVert *mvert;
847         MFace *mface;
848         float *data;
849         int a, b, ofs, vertcount, startvert, totvert=0, totvlak=0;
850         int p1, p2, p3, p4, *index;
851
852         cu= ob->data;
853
854         if(ob->type==OB_CURVE) {
855                 /* regel: dl->type INDEX3 altijd vooraan in lijst */
856                 dl= cu->disp.first;
857                 if(dl->type!=DL_INDEX3) {
858                         curve_to_filledpoly(ob->data, &cu->disp);
859                 }
860         }
861
862         /* tellen */
863         dl= cu->disp.first;
864         while(dl) {
865                 if(dl->type==DL_SEGM) {
866                         totvert+= dl->parts*dl->nr;
867                         totvlak+= dl->parts*(dl->nr-1);
868                 }
869                 else if(dl->type==DL_POLY) {
870                         /* cyclic polys are filled. except when 3D */
871                         if(cu->flag & CU_3D) {
872                                 totvert+= dl->parts*dl->nr;
873                                 totvlak+= dl->parts*dl->nr;
874                         }
875                 }
876                 else if(dl->type==DL_SURF) {
877                         totvert+= dl->parts*dl->nr;
878                         totvlak+= (dl->parts-1+((dl->flag & 2)==2))*(dl->nr-1+(dl->flag & 1));
879                 }
880                 else if(dl->type==DL_INDEX3) {
881                         totvert+= dl->nr;
882                         totvlak+= dl->parts;
883                 }
884                 dl= dl->next;
885         }
886         if(totvert==0) {
887                 error("can't convert");
888                 return;
889         }
890
891         /* mesh maken */
892         me= add_mesh();
893         me->totvert= totvert;
894         me->totface= totvlak;
895
896         me->totcol= cu->totcol;
897         me->mat= cu->mat;
898         cu->mat= 0;
899         cu->totcol= 0;
900
901         mvert=me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "cumesh1");
902         mface=me->mface= MEM_callocN(me->totface*sizeof(MFace), "cumesh2");
903
904         /* verts en vlakken */
905         vertcount= 0;
906
907         dl= cu->disp.first;
908         while(dl) {
909                 if(dl->type==DL_SEGM) {
910                         startvert= vertcount;
911                         a= dl->parts*dl->nr;
912                         data= dl->verts;
913                         while(a--) {
914                                 VECCOPY(mvert->co, data);
915                                 data+=3;
916                                 vertcount++;
917                                 mvert++;
918                         }
919
920                         for(a=0; a<dl->parts; a++) {
921                                 ofs= a*dl->nr;
922                                 for(b=1; b<dl->nr; b++) {
923                                         mface->v1= startvert+ofs+b-1;
924                                         mface->v2= startvert+ofs+b;
925                                         mface->edcode= ME_V1V2;
926                                         test_index_mface(mface, 2);
927                                         mface++;
928                                 }
929                         }
930
931                 }
932                 else if(dl->type==DL_POLY) {
933                         /* cyclic polys are filled */
934                         /* startvert= vertcount;
935                         a= dl->parts*dl->nr;
936                         data= dl->verts;
937                         while(a--) {
938                                 VECCOPY(mvert->co, data);
939                                 data+=3;
940                                 vertcount++;
941                                 mvert++;
942                         }
943
944                         for(a=0; a<dl->parts; a++) {
945                                 ofs= a*dl->nr;
946                                 for(b=0; b<dl->nr; b++) {
947                                         mface->v1= startvert+ofs+b;
948                                         if(b==dl->nr-1) mface->v2= startvert+ofs;
949                                         else mface->v2= startvert+ofs+b+1;
950                                         mface->edcode= ME_V1V2;
951                                         test_index_mface(mface, 2);
952                                         mface++;
953                                 }
954                         }
955                         */
956                 }
957                 else if(dl->type==DL_INDEX3) {
958                         startvert= vertcount;
959                         a= dl->nr;
960                         data= dl->verts;
961                         while(a--) {
962                                 VECCOPY(mvert->co, data);
963                                 data+=3;
964                                 vertcount++;
965                                 mvert++;
966                         }
967
968                         a= dl->parts;
969                         index= dl->index;
970                         while(a--) {
971                                 mface->v1= startvert+index[0];
972                                 mface->v2= startvert+index[1];
973                                 mface->v3= startvert+index[2];
974                                 mface->v4= 0;
975         
976                                 mface->puno= 7;
977                                 mface->edcode= ME_V1V2+ME_V2V3;
978                                 test_index_mface(mface, 3);
979                                 
980                                 mface++;
981                                 index+= 3;
982                         }
983         
984         
985                 }
986                 else if(dl->type==DL_SURF) {
987                         startvert= vertcount;
988                         a= dl->parts*dl->nr;
989                         data= dl->verts;
990                         while(a--) {
991                                 VECCOPY(mvert->co, data);
992                                 data+=3;
993                                 vertcount++;
994                                 mvert++;
995                         }
996
997                         for(a=0; a<dl->parts; a++) {
998
999                                 if( (dl->flag & 2)==0 && a==dl->parts-1) break;
1000
1001                                 if(dl->flag & 1) {                              /* p2 -> p1 -> */
1002                                         p1= startvert+ dl->nr*a;        /* p4 -> p3 -> */
1003                                         p2= p1+ dl->nr-1;                       /* -----> volgende rij */
1004                                         p3= p1+ dl->nr;
1005                                         p4= p2+ dl->nr;
1006                                         b= 0;
1007                                 }
1008                                 else {
1009                                         p2= startvert+ dl->nr*a;
1010                                         p1= p2+1;
1011                                         p4= p2+ dl->nr;
1012                                         p3= p1+ dl->nr;
1013                                         b= 1;
1014                                 }
1015                                 if( (dl->flag & 2) && a==dl->parts-1) {
1016                                         p3-= dl->parts*dl->nr;
1017                                         p4-= dl->parts*dl->nr;
1018                                 }
1019
1020                                 for(; b<dl->nr; b++) {
1021                                         mface->v1= p1;
1022                                         mface->v2= p3;
1023                                         mface->v3= p4;
1024                                         mface->v4= p2;
1025                                         mface->mat_nr= (unsigned char)dl->col;
1026                                         mface->edcode= ME_V1V2+ME_V2V3;
1027                                         test_index_mface(mface, 4);
1028                                         mface++;
1029
1030                                         p4= p3; 
1031                                         p3++;
1032                                         p2= p1; 
1033                                         p1++;
1034                                 }
1035                         }
1036
1037                 }
1038
1039                 dl= dl->next;
1040         }
1041
1042         if(ob->data) {
1043                 free_libblock(&G.main->curve, ob->data);
1044         }
1045         ob->data= me;
1046         ob->type= OB_MESH;
1047         
1048         tex_space_mesh(me);
1049         
1050         /* andere users */
1051         ob1= G.main->object.first;
1052         while(ob1) {
1053                 if(ob1->data==cu) {
1054                         ob1->type= OB_MESH;
1055                 
1056                         ob1->data= ob->data;
1057                         id_us_plus((ID *)ob->data);
1058                 }
1059                 ob1= ob1->id.next;
1060         }
1061
1062 }
1063
1064 void edge_drawflags_mesh(Mesh *me)
1065 {
1066         MFace *mface;
1067         int a;
1068         
1069         mface= me->mface;
1070         for(a=0; a<me->totface; a++, mface++) {
1071                 mface->edcode= ME_V1V2|ME_V2V3|ME_V3V4|ME_V4V1;
1072         }
1073 }
1074
1075 void tface_to_mcol(Mesh *me)
1076 {
1077         TFace *tface;
1078         unsigned int *mcol;
1079         int a;
1080         
1081         me->mcol= MEM_mallocN(4*sizeof(int)*me->totface, "nepmcol");
1082         mcol= (unsigned int *)me->mcol;
1083         
1084         a= me->totface;
1085         tface= me->tface;
1086         while(a--) {
1087                 memcpy(mcol, tface->col, 16);
1088                 mcol+= 4;
1089                 tface++;
1090         }
1091 }
1092
1093 void mcol_to_tface(Mesh *me, int freedata)
1094 {
1095         TFace *tface;
1096         unsigned int *mcol;
1097         int a;
1098         
1099         a= me->totface;
1100         tface= me->tface;
1101         mcol= (unsigned int *)me->mcol;
1102         while(a--) {
1103                 memcpy(tface->col, mcol, 16);
1104                 mcol+= 4;
1105                 tface++;
1106         }
1107         
1108         if(freedata) {
1109                 MEM_freeN(me->mcol);
1110                 me->mcol= 0;
1111         }
1112 }
1113
1114 int mesh_uses_displist(Mesh *me) {
1115         return (me->flag&ME_SUBSURF && (me->subdiv>0));
1116 }
1117
1118 int rendermesh_uses_displist(Mesh *me) {
1119         return (me->flag&ME_SUBSURF);
1120 }