Fix part of #20090: boolean modifier would lose link to object
[blender.git] / source / blender / blenkernel / intern / cdderivedmesh.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * The Original Code is Copyright (C) 2006 Blender Foundation.
21 * All rights reserved.
22 *
23 * The Original Code is: all of this file.
24 *
25 * Contributor(s): Ben Batt <benbatt@gmail.com>
26 *
27 * ***** END GPL LICENSE BLOCK *****
28 *
29 * Implementation of CDDerivedMesh.
30 *
31 * BKE_cdderivedmesh.h contains the function prototypes for this file.
32 *
33 */ 
34
35 /* TODO maybe BIF_gl.h should include string.h? */
36 #include <string.h>
37 #include "BIF_gl.h"
38
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_customdata.h"
41 #include "BKE_DerivedMesh.h"
42 #include "BKE_displist.h"
43 #include "BKE_global.h"
44 #include "BKE_mesh.h"
45 #include "BKE_multires.h"
46 #include "BKE_utildefines.h"
47
48 #include "BLI_math.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_edgehash.h"
51 #include "BLI_editVert.h"
52 #include "BLI_pbvh.h"
53
54 #include "DNA_mesh_types.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_modifier_types.h"
57 #include "DNA_object_fluidsim.h"
58 #include "DNA_object_types.h"
59 #include "DNA_scene_types.h"
60
61 #include "MEM_guardedalloc.h"
62
63 #include "gpu_buffers.h"
64 #include "GPU_draw.h"
65 #include "GPU_extensions.h"
66 #include "GPU_material.h"
67
68 #include <string.h>
69 #include <limits.h>
70 #include <math.h>
71
72 typedef struct {
73         DerivedMesh dm;
74
75         /* these point to data in the DerivedMesh custom data layers,
76            they are only here for efficiency and convenience **/
77         MVert *mvert;
78         MEdge *medge;
79         MFace *mface;
80
81         /* Cached */
82         struct PBVH *pbvh;
83         /* Mesh connectivity */
84         struct ListBase *fmap;
85         struct IndexNode *fmap_mem;
86 } CDDerivedMesh;
87
88 /**************** DerivedMesh interface functions ****************/
89 static int cdDM_getNumVerts(DerivedMesh *dm)
90 {
91         return dm->numVertData;
92 }
93
94 static int cdDM_getNumEdges(DerivedMesh *dm)
95 {
96         return dm->numEdgeData;
97 }
98
99 static int cdDM_getNumFaces(DerivedMesh *dm)
100 {
101         return dm->numFaceData;
102 }
103
104 static void cdDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
105 {
106         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
107         *vert_r = cddm->mvert[index];
108 }
109
110 static void cdDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
111 {
112         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
113         *edge_r = cddm->medge[index];
114 }
115
116 static void cdDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
117 {
118         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
119         *face_r = cddm->mface[index];
120 }
121
122 static void cdDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
123 {
124         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
125         memcpy(vert_r, cddm->mvert, sizeof(*vert_r) * dm->numVertData);
126 }
127
128 static void cdDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
129 {
130         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
131         memcpy(edge_r, cddm->medge, sizeof(*edge_r) * dm->numEdgeData);
132 }
133
134 static void cdDM_copyFaceArray(DerivedMesh *dm, MFace *face_r)
135 {
136         CDDerivedMesh *cddm = (CDDerivedMesh *)dm;
137         memcpy(face_r, cddm->mface, sizeof(*face_r) * dm->numFaceData);
138 }
139
140 static void cdDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
141 {
142         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
143         int i;
144
145         if (dm->numVertData) {
146                 for (i=0; i<dm->numVertData; i++) {
147                         DO_MINMAX(cddm->mvert[i].co, min_r, max_r);
148                 }
149         } else {
150                 min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
151         }
152 }
153
154 static void cdDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
155 {
156         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
157
158         VECCOPY(co_r, cddm->mvert[index].co);
159 }
160
161 static void cdDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
162 {
163         MVert *mv = CDDM_get_verts(dm);
164         int i;
165
166         for(i = 0; i < dm->numVertData; i++, mv++)
167                 VECCOPY(cos_r[i], mv->co);
168 }
169
170 static void cdDM_getVertNo(DerivedMesh *dm, int index, float no_r[3])
171 {
172         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
173         short *no = cddm->mvert[index].no;
174
175         no_r[0] = no[0]/32767.f;
176         no_r[1] = no[1]/32767.f;
177         no_r[2] = no[2]/32767.f;
178 }
179
180 static ListBase *cdDM_getFaceMap(Object *ob, DerivedMesh *dm)
181 {
182         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
183
184         if(!cddm->fmap && ob->type == OB_MESH) {
185                 Mesh *me= ob->data;
186
187                 create_vert_face_map(&cddm->fmap, &cddm->fmap_mem, me->mface,
188                                      me->totvert, me->totface);
189         }
190
191         return cddm->fmap;
192 }
193
194 static struct PBVH *cdDM_getPBVH(Object *ob, DerivedMesh *dm)
195 {
196         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
197
198         if(!cddm->pbvh && ob->type == OB_MESH) {
199                 Mesh *me= ob->data;
200
201                 cddm->pbvh = BLI_pbvh_new();
202                 BLI_pbvh_build_mesh(cddm->pbvh, me->mface, me->mvert,
203                                me->totface, me->totvert);
204         }
205
206         return cddm->pbvh;
207 }
208
209 static void cdDM_drawVerts(DerivedMesh *dm)
210 {
211         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
212         MVert *mv = cddm->mvert;
213         int i;
214
215         if( GPU_buffer_legacy(dm) ) {
216                 glBegin(GL_POINTS);
217                 for(i = 0; i < dm->numVertData; i++, mv++)
218                         glVertex3fv(mv->co);
219                 glEnd();
220         }
221         else {  /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
222                 GPU_vertex_setup(dm);
223                 if( !GPU_buffer_legacy(dm) ) {
224                         glDrawArrays(GL_POINTS,0,dm->drawObject->nelements);
225                 }
226                 GPU_buffer_unbind();
227         }
228 }
229
230 static void cdDM_drawUVEdges(DerivedMesh *dm)
231 {
232         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
233         MFace *mf = cddm->mface;
234         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
235         int i;
236
237         if(mf) {
238                 if( GPU_buffer_legacy(dm) ) {
239                         glBegin(GL_LINES);
240                         for(i = 0; i < dm->numFaceData; i++, mf++, tf++) {
241                                 if(!(mf->flag&ME_HIDE)) {
242                                         glVertex2fv(tf->uv[0]);
243                                         glVertex2fv(tf->uv[1]);
244
245                                         glVertex2fv(tf->uv[1]);
246                                         glVertex2fv(tf->uv[2]);
247
248                                         if(!mf->v4) {
249                                                 glVertex2fv(tf->uv[2]);
250                                                 glVertex2fv(tf->uv[0]);
251                                         } else {
252                                                 glVertex2fv(tf->uv[2]);
253                                                 glVertex2fv(tf->uv[3]);
254
255                                                 glVertex2fv(tf->uv[3]);
256                                                 glVertex2fv(tf->uv[0]);
257                                         }
258                                 }
259                         }
260                         glEnd();
261                 }
262                 else {
263                         int prevstart = 0;
264                         int prevdraw = 1;
265                         int draw = 1;
266                         int curpos = 0;
267
268                         GPU_uvedge_setup(dm);
269                         if( !GPU_buffer_legacy(dm) ) {
270                                 for(i = 0; i < dm->numFaceData; i++, mf++) {
271                                         if(mf->flag&ME_LOOSEEDGE) {
272                                                 draw = 1;
273                                         } 
274                                         else {
275                                                 draw = 0;
276                                         }
277                                         if( prevdraw != draw ) {
278                                                 if( prevdraw > 0 && (curpos-prevstart) > 0) {
279                                                         glDrawArrays(GL_LINES,prevstart,curpos-prevstart);
280                                                 }
281                                                 prevstart = curpos;
282                                         }
283                                         if( mf->v4 ) {
284                                                 curpos += 8;
285                                         }
286                                         else {
287                                                 curpos += 6;
288                                         }
289                                         prevdraw = draw;
290                                 }
291                                 if( prevdraw > 0 && (curpos-prevstart) > 0 ) {
292                                         glDrawArrays(GL_LINES,prevstart,curpos-prevstart);
293                                 }
294                         }
295                         GPU_buffer_unbind();
296                 }
297         }
298 }
299
300 static void cdDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
301 {
302         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
303         MVert *mvert = cddm->mvert;
304         MEdge *medge = cddm->medge;
305         int i;
306         
307         if( GPU_buffer_legacy(dm) ) {
308                 DEBUG_VBO( "Using legacy code. cdDM_drawEdges\n" );
309                 glBegin(GL_LINES);
310                 for(i = 0; i < dm->numEdgeData; i++, medge++) {
311                         if((medge->flag&ME_EDGEDRAW)
312                            && (drawLooseEdges || !(medge->flag&ME_LOOSEEDGE))) {
313                                 glVertex3fv(mvert[medge->v1].co);
314                                 glVertex3fv(mvert[medge->v2].co);
315                         }
316                 }
317                 glEnd();
318         }
319         else {  /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
320                 int prevstart = 0;
321                 int prevdraw = 1;
322                 int draw = 1;
323
324                 GPU_edge_setup(dm);
325                 if( !GPU_buffer_legacy(dm) ) {
326                         for(i = 0; i < dm->numEdgeData; i++, medge++) {
327                                 if((medge->flag&ME_EDGEDRAW)
328                                    && (drawLooseEdges || !(medge->flag&ME_LOOSEEDGE))) {
329                                         draw = 1;
330                                 } 
331                                 else {
332                                         draw = 0;
333                                 }
334                                 if( prevdraw != draw ) {
335                                         if( prevdraw > 0 && (i-prevstart) > 0 ) {
336                                                 GPU_buffer_draw_elements( dm->drawObject->edges, GL_LINES, prevstart*2, (i-prevstart)*2  );
337                                         }
338                                         prevstart = i;
339                                 }
340                                 prevdraw = draw;
341                         }
342                         if( prevdraw > 0 && (i-prevstart) > 0 ) {
343                                 GPU_buffer_draw_elements( dm->drawObject->edges, GL_LINES, prevstart*2, (i-prevstart)*2  );
344                         }
345                 }
346                 GPU_buffer_unbind();
347         }
348 }
349
350 static void cdDM_drawLooseEdges(DerivedMesh *dm)
351 {
352         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
353         MVert *mvert = cddm->mvert;
354         MEdge *medge = cddm->medge;
355         int i;
356
357         if( GPU_buffer_legacy(dm) ) {
358                 DEBUG_VBO( "Using legacy code. cdDM_drawLooseEdges\n" );
359                 glBegin(GL_LINES);
360                 for(i = 0; i < dm->numEdgeData; i++, medge++) {
361                         if(medge->flag&ME_LOOSEEDGE) {
362                                 glVertex3fv(mvert[medge->v1].co);
363                                 glVertex3fv(mvert[medge->v2].co);
364                         }
365                 }
366                 glEnd();
367         }
368         else {  /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
369                 int prevstart = 0;
370                 int prevdraw = 1;
371                 int draw = 1;
372
373                 GPU_edge_setup(dm);
374                 if( !GPU_buffer_legacy(dm) ) {
375                         for(i = 0; i < dm->numEdgeData; i++, medge++) {
376                                 if(medge->flag&ME_LOOSEEDGE) {
377                                         draw = 1;
378                                 } 
379                                 else {
380                                         draw = 0;
381                                 }
382                                 if( prevdraw != draw ) {
383                                         if( prevdraw > 0 && (i-prevstart) > 0) {
384                                                 GPU_buffer_draw_elements( dm->drawObject->edges, GL_LINES, prevstart*2, (i-prevstart)*2  );
385                                         }
386                                         prevstart = i;
387                                 }
388                                 prevdraw = draw;
389                         }
390                         if( prevdraw > 0 && (i-prevstart) > 0 ) {
391                                 GPU_buffer_draw_elements( dm->drawObject->edges, GL_LINES, prevstart*2, (i-prevstart)*2  );
392                         }
393                 }
394                 GPU_buffer_unbind();
395         }
396 }
397
398 static void cdDM_drawFacesSolid(DerivedMesh *dm,
399                                 float (*partial_redraw_planes)[4],
400                                 int fast, int (*setMaterial)(int, void *attribs))
401 {
402         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
403         MVert *mvert = cddm->mvert;
404         MFace *mface = cddm->mface;
405         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
406         int a, glmode = -1, shademodel = -1, matnr = -1, drawCurrentMat = 1;
407
408 #define PASSVERT(index) {                                               \
409         if(shademodel == GL_SMOOTH) {                           \
410                 short *no = mvert[index].no;                    \
411                 glNormal3sv(no);                                                \
412         }                                                                                       \
413         glVertex3fv(mvert[index].co);   \
414 }
415
416         if(cddm->pbvh) {
417                 float (*face_nors)[3] = CustomData_get_layer(&dm->faceData, CD_NORMAL);
418
419                 /* should be per face */
420                 if(dm->numFaceData && mface->flag & ME_SMOOTH)
421                         glShadeModel(GL_SMOOTH);
422
423                 BLI_pbvh_draw(cddm->pbvh, partial_redraw_planes, face_nors);
424
425                 glShadeModel(GL_FLAT);
426
427                 return;
428         }
429
430         if( GPU_buffer_legacy(dm) ) {
431                 DEBUG_VBO( "Using legacy code. cdDM_drawFacesSolid\n" );
432                 glBegin(glmode = GL_QUADS);
433                 for(a = 0; a < dm->numFaceData; a++, mface++) {
434                         int new_glmode, new_matnr, new_shademodel;
435
436                         new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
437                         new_matnr = mface->mat_nr + 1;
438                         new_shademodel = (mface->flag & ME_SMOOTH)?GL_SMOOTH:GL_FLAT;
439                         
440                         if(new_glmode != glmode || new_matnr != matnr
441                            || new_shademodel != shademodel) {
442                                 glEnd();
443
444                                 drawCurrentMat = setMaterial(matnr = new_matnr, NULL);
445
446                                 glShadeModel(shademodel = new_shademodel);
447                                 glBegin(glmode = new_glmode);
448                         } 
449                         
450                         if(drawCurrentMat) {
451                                 if(shademodel == GL_FLAT) {
452                                         if (nors) {
453                                                 glNormal3fv(nors);
454                                         }
455                                         else {
456                                                 /* TODO make this better (cache facenormals as layer?) */
457                                                 float nor[3];
458                                                 if(mface->v4) {
459                                                         normal_quad_v3( nor,mvert[mface->v1].co, mvert[mface->v2].co, mvert[mface->v3].co, mvert[mface->v4].co);
460                                                 } else {
461                                                         normal_tri_v3( nor,mvert[mface->v1].co, mvert[mface->v2].co, mvert[mface->v3].co);
462                                                 }
463                                                 glNormal3fv(nor);
464                                         }
465                                 }
466
467                                 PASSVERT(mface->v1);
468                                 PASSVERT(mface->v2);
469                                 PASSVERT(mface->v3);
470                                 if(mface->v4) {
471                                         PASSVERT(mface->v4);
472                                 }
473                         }
474
475                         if(nors) nors += 3;
476                 }
477                 glEnd();
478         }
479         else {  /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
480                 GPU_vertex_setup( dm );
481                 GPU_normal_setup( dm );
482                 if( !GPU_buffer_legacy(dm) ) {
483                         glShadeModel(GL_SMOOTH);
484                         for( a = 0; a < dm->drawObject->nmaterials; a++ ) {
485                                 if( setMaterial(dm->drawObject->materials[a].mat_nr+1, NULL) )
486                                         glDrawArrays(GL_TRIANGLES, dm->drawObject->materials[a].start, dm->drawObject->materials[a].end-dm->drawObject->materials[a].start);
487                         }
488                 }
489                 GPU_buffer_unbind( );
490         }
491
492 #undef PASSVERT
493         glShadeModel(GL_FLAT);
494 }
495
496 static void cdDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2)
497 {
498         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
499         int a, glmode;
500         unsigned char *cp1, *cp2;
501         MVert *mvert = cddm->mvert;
502         MFace *mface = cddm->mface;
503
504         cp1 = col1;
505         if(col2) {
506                 cp2 = col2;
507         } else {
508                 cp2 = NULL;
509                 useTwoSided = 0;
510         }
511
512         /* there's a conflict here... twosided colors versus culling...? */
513         /* defined by history, only texture faces have culling option */
514         /* we need that as mesh option builtin, next to double sided lighting */
515         if(col1 && col2)
516                 glEnable(GL_CULL_FACE);
517
518         if( GPU_buffer_legacy(dm) ) {
519                 DEBUG_VBO( "Using legacy code. cdDM_drawFacesColored\n" );
520                 glShadeModel(GL_SMOOTH);
521                 glBegin(glmode = GL_QUADS);
522                 for(a = 0; a < dm->numFaceData; a++, mface++, cp1 += 16) {
523                         int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
524
525                         if(new_glmode != glmode) {
526                                 glEnd();
527                                 glBegin(glmode = new_glmode);
528                         }
529                                 
530                         glColor3ub(cp1[0], cp1[1], cp1[2]);
531                         glVertex3fv(mvert[mface->v1].co);
532                         glColor3ub(cp1[4], cp1[5], cp1[6]);
533                         glVertex3fv(mvert[mface->v2].co);
534                         glColor3ub(cp1[8], cp1[9], cp1[10]);
535                         glVertex3fv(mvert[mface->v3].co);
536                         if(mface->v4) {
537                                 glColor3ub(cp1[12], cp1[13], cp1[14]);
538                                 glVertex3fv(mvert[mface->v4].co);
539                         }
540                                 
541                         if(useTwoSided) {
542                                 glColor3ub(cp2[8], cp2[9], cp2[10]);
543                                 glVertex3fv(mvert[mface->v3].co );
544                                 glColor3ub(cp2[4], cp2[5], cp2[6]);
545                                 glVertex3fv(mvert[mface->v2].co );
546                                 glColor3ub(cp2[0], cp2[1], cp2[2]);
547                                 glVertex3fv(mvert[mface->v1].co );
548                                 if(mface->v4) {
549                                         glColor3ub(cp2[12], cp2[13], cp2[14]);
550                                         glVertex3fv(mvert[mface->v4].co );
551                                 }
552                         }
553                         if(col2) cp2 += 16;
554                 }
555                 glEnd();
556         }
557         else { /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
558                 GPU_color4_upload(dm,cp1);
559                 GPU_vertex_setup(dm);
560                 GPU_color_setup(dm);
561                 if( !GPU_buffer_legacy(dm) ) {
562                         glShadeModel(GL_SMOOTH);
563                         glDrawArrays(GL_TRIANGLES, 0, dm->drawObject->nelements);
564
565                         if( useTwoSided ) {
566                                 GPU_color4_upload(dm,cp2);
567                                 GPU_color_setup(dm);
568                                 glCullFace(GL_FRONT);
569                                 glDrawArrays(GL_TRIANGLES, 0, dm->drawObject->nelements);
570                                 glCullFace(GL_BACK);
571                         }
572                 }
573                 GPU_buffer_unbind();
574         }
575
576         glShadeModel(GL_FLAT);
577         glDisable(GL_CULL_FACE);
578 }
579
580 static void cdDM_drawFacesTex_common(DerivedMesh *dm,
581                int (*drawParams)(MTFace *tface, MCol *mcol, int matnr),
582                int (*drawParamsMapped)(void *userData, int index),
583                void *userData) 
584 {
585         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
586         MVert *mv = cddm->mvert;
587         MFace *mf = DM_get_face_data_layer(dm, CD_MFACE);
588         MCol *realcol = dm->getFaceDataArray(dm, CD_TEXTURE_MCOL);
589         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
590         MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
591         int i, j, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
592         int startFace = 0, lastFlag = 0xdeadbeef;
593         MCol *mcol = dm->getFaceDataArray(dm, CD_WEIGHT_MCOL);
594         if(!mcol)
595                 mcol = dm->getFaceDataArray(dm, CD_MCOL);
596
597         if( GPU_buffer_legacy(dm) ) {
598                 DEBUG_VBO( "Using legacy code. cdDM_drawFacesTex_common\n" );
599                 for(i = 0; i < dm->numFaceData; i++, mf++) {
600                         MVert *mvert;
601                         int flag;
602                         unsigned char *cp = NULL;
603
604                         if(drawParams) {
605                                 flag = drawParams(tf? &tf[i]: NULL, mcol? &mcol[i*4]: NULL, mf->mat_nr);
606                         }
607                         else {
608                                 if(index) {
609                                         orig = *index++;
610                                         if(orig == ORIGINDEX_NONE)              { if(nors) nors += 3; continue; }
611                                         if(drawParamsMapped) flag = drawParamsMapped(userData, orig);
612                                         else    { if(nors) nors += 3; continue; }
613                                 }
614                                 else
615                                         if(drawParamsMapped) flag = drawParamsMapped(userData, i);
616                                         else    { if(nors) nors += 3; continue; }
617                         }
618                         
619                         if(flag != 0) {
620                                 if (flag==1 && mcol)
621                                         cp= (unsigned char*) &mcol[i*4];
622
623                                 if(!(mf->flag&ME_SMOOTH)) {
624                                         if (nors) {
625                                                 glNormal3fv(nors);
626                                         }
627                                         else {
628                                                 float nor[3];
629                                                 if(mf->v4) {
630                                                         normal_quad_v3( nor,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co, mv[mf->v4].co);
631                                                 } else {
632                                                         normal_tri_v3( nor,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co);
633                                                 }
634                                                 glNormal3fv(nor);
635                                         }
636                                 }
637
638                                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
639                                 if(tf) glTexCoord2fv(tf[i].uv[0]);
640                                 if(cp) glColor3ub(cp[3], cp[2], cp[1]);
641                                 mvert = &mv[mf->v1];
642                                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
643                                 glVertex3fv(mvert->co);
644                                         
645                                 if(tf) glTexCoord2fv(tf[i].uv[1]);
646                                 if(cp) glColor3ub(cp[7], cp[6], cp[5]);
647                                 mvert = &mv[mf->v2];
648                                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
649                                 glVertex3fv(mvert->co);
650
651                                 if(tf) glTexCoord2fv(tf[i].uv[2]);
652                                 if(cp) glColor3ub(cp[11], cp[10], cp[9]);
653                                 mvert = &mv[mf->v3];
654                                 if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
655                                 glVertex3fv(mvert->co);
656
657                                 if(mf->v4) {
658                                         if(tf) glTexCoord2fv(tf[i].uv[3]);
659                                         if(cp) glColor3ub(cp[15], cp[14], cp[13]);
660                                         mvert = &mv[mf->v4];
661                                         if(mf->flag&ME_SMOOTH) glNormal3sv(mvert->no);
662                                         glVertex3fv(mvert->co);
663                                 }
664                                 glEnd();
665                         }
666                         
667                         if(nors) nors += 3;
668                 }
669         } else { /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
670                 MCol *col = realcol;
671                 if(!col)
672                         col = mcol;
673
674                 GPU_vertex_setup( dm );
675                 GPU_normal_setup( dm );
676                 GPU_uv_setup( dm );
677                 if( col != 0 ) {
678                         /*if( realcol && dm->drawObject->colType == CD_TEXTURE_MCOL )  {
679                                 col = 0;
680                         } else if( mcol && dm->drawObject->colType == CD_MCOL ) {
681                                 col = 0;
682                         }
683                         
684                         if( col != 0 ) {*/
685                                 unsigned char *colors = MEM_mallocN(dm->getNumFaces(dm)*4*3*sizeof(unsigned char), "cdDM_drawFacesTex_common");
686                                 for( i=0; i < dm->getNumFaces(dm); i++ ) {
687                                         for( j=0; j < 4; j++ ) {
688                                                 colors[i*12+j*3] = col[i*4+j].r;
689                                                 colors[i*12+j*3+1] = col[i*4+j].g;
690                                                 colors[i*12+j*3+2] = col[i*4+j].b;
691                                         }
692                                 }
693                                 GPU_color3_upload(dm,colors);
694                                 MEM_freeN(colors);
695                                 if(realcol)
696                                         dm->drawObject->colType = CD_TEXTURE_MCOL;
697                                 else if(mcol)
698                                         dm->drawObject->colType = CD_MCOL;
699                         //}
700                         GPU_color_setup( dm );
701                 }
702
703                 if( !GPU_buffer_legacy(dm) ) {
704                         glShadeModel( GL_SMOOTH );
705                         for(i = 0; i < dm->drawObject->nelements/3; i++) {
706                                 int actualFace = dm->drawObject->faceRemap[i];
707                                 int flag = 1;
708
709                                 if(drawParams) {
710                                         flag = drawParams(tf? &tf[actualFace]: NULL, mcol? &mcol[actualFace*4]: NULL, mf[actualFace].mat_nr);
711                                 }
712                                 else {
713                                         if(index) {
714                                                 orig = index[actualFace];
715                                                 if(drawParamsMapped)
716                                                         flag = drawParamsMapped(userData, orig);
717                                         }
718                                         else
719                                                 if(drawParamsMapped)
720                                                         flag = drawParamsMapped(userData, actualFace);
721                                 }
722                                 if( flag != lastFlag ) {
723                                         if( startFace < i ) {
724                                                 if( lastFlag != 0 ) { /* if the flag is 0 it means the face is hidden or invisible */
725                                                         if (lastFlag==1 && mcol)
726                                                                 GPU_color_switch(1);
727                                                         else
728                                                                 GPU_color_switch(0);
729                                                         glDrawArrays(GL_TRIANGLES,startFace*3,(i-startFace)*3);
730                                                 }
731                                         }
732                                         lastFlag = flag;
733                                         startFace = i;
734                                 }
735                         }
736                         if( startFace < dm->drawObject->nelements/3 ) {
737                                 if( lastFlag != 0 ) { /* if the flag is 0 it means the face is hidden or invisible */
738                                         if (lastFlag==1 && mcol)
739                                                 GPU_color_switch(1);
740                                         else
741                                                 GPU_color_switch(0);
742                                         glDrawArrays(GL_TRIANGLES,startFace*3,dm->drawObject->nelements-startFace*3);
743                                 }
744                         }
745                 }
746
747                 GPU_buffer_unbind();
748                 glShadeModel( GL_FLAT );
749         }
750 }
751
752 static void cdDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
753 {
754         cdDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
755 }
756
757 static void cdDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors)
758 {
759         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
760         MVert *mv = cddm->mvert;
761         MFace *mf = cddm->mface;
762         MCol *mc;
763         float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
764         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
765
766         mc = DM_get_face_data_layer(dm, CD_ID_MCOL);
767         if(!mc)
768                 mc = DM_get_face_data_layer(dm, CD_WEIGHT_MCOL);
769         if(!mc)
770                 mc = DM_get_face_data_layer(dm, CD_MCOL);
771
772         if( GPU_buffer_legacy(dm) ) {
773                 DEBUG_VBO( "Using legacy code. cdDM_drawMappedFaces\n" );
774                 for(i = 0; i < dm->numFaceData; i++, mf++) {
775                         int drawSmooth = (mf->flag & ME_SMOOTH);
776
777                         if(index) {
778                                 orig = *index++;
779                                 if(setDrawOptions && orig == ORIGINDEX_NONE)
780                                         { if(nors) nors += 3; continue; }
781                         }
782                         else
783                                 orig = i;
784
785                         if(!setDrawOptions || setDrawOptions(userData, orig, &drawSmooth)) {
786                                 unsigned char *cp = NULL;
787
788                                 if(useColors && mc)
789                                         cp = (unsigned char *)&mc[i * 4];
790
791                                 glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
792                                 glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
793
794                                 if (!drawSmooth) {
795                                         if (nors) {
796                                                 glNormal3fv(nors);
797                                         }
798                                         else {
799                                                 float nor[3];
800                                                 if(mf->v4) {
801                                                         normal_quad_v3( nor,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co, mv[mf->v4].co);
802                                                 } else {
803                                                         normal_tri_v3( nor,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co);
804                                                 }
805                                                 glNormal3fv(nor);
806                                         }
807
808                                         if(cp) glColor3ub(cp[3], cp[2], cp[1]);
809                                         glVertex3fv(mv[mf->v1].co);
810                                         if(cp) glColor3ub(cp[7], cp[6], cp[5]);
811                                         glVertex3fv(mv[mf->v2].co);
812                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
813                                         glVertex3fv(mv[mf->v3].co);
814                                         if(mf->v4) {
815                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
816                                                 glVertex3fv(mv[mf->v4].co);
817                                         }
818                                 } else {
819                                         if(cp) glColor3ub(cp[3], cp[2], cp[1]);
820                                         glNormal3sv(mv[mf->v1].no);
821                                         glVertex3fv(mv[mf->v1].co);
822                                         if(cp) glColor3ub(cp[7], cp[6], cp[5]);
823                                         glNormal3sv(mv[mf->v2].no);
824                                         glVertex3fv(mv[mf->v2].co);
825                                         if(cp) glColor3ub(cp[11], cp[10], cp[9]);
826                                         glNormal3sv(mv[mf->v3].no);
827                                         glVertex3fv(mv[mf->v3].co);
828                                         if(mf->v4) {
829                                                 if(cp) glColor3ub(cp[15], cp[14], cp[13]);
830                                                 glNormal3sv(mv[mf->v4].no);
831                                                 glVertex3fv(mv[mf->v4].co);
832                                         }
833                                 }
834
835                                 glEnd();
836                         }
837                         
838                         if (nors) nors += 3;
839                 }
840         }
841         else { /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
842                 int state = 1;
843                 int prevstate = 1;
844                 int prevstart = 0;
845                 GPU_vertex_setup(dm);
846                 GPU_normal_setup(dm);
847                 if( useColors && mc )
848                         GPU_color_setup(dm);
849                 if( !GPU_buffer_legacy(dm) ) {
850                         glShadeModel(GL_SMOOTH);
851                         for( i = 0; i < dm->drawObject->nelements/3; i++ ) {
852                                 int actualFace = dm->drawObject->faceRemap[i];
853                                 int drawSmooth = (mf[actualFace].flag & ME_SMOOTH);
854                                 int dontdraw = 0;
855                                 if(index) {
856                                         orig = index[actualFace];
857                                         if(setDrawOptions && orig == ORIGINDEX_NONE)
858                                                 dontdraw = 1;
859                                 }
860                                 else
861                                         orig = i;
862                                 if( dontdraw ) {
863                                         state = 0;
864                                 }
865                                 else {
866                                         if(!setDrawOptions || setDrawOptions(userData, orig, &drawSmooth)) {
867                                                 state = 1;
868                                         }
869                                         else {
870                                                 state = 0;
871                                         }
872                                 }
873                                 if( prevstate != state && prevstate == 1 ) {
874                                         if( i-prevstart > 0 ) {
875                                                 glDrawArrays(GL_TRIANGLES,prevstart*3,(i-prevstart)*3);
876                                         }
877                                         prevstart = i;
878                                 }
879                                 prevstate = state;
880                         }
881                         if(state==1) {
882                                 glDrawArrays(GL_TRIANGLES,prevstart*3,dm->drawObject->nelements-prevstart*3);
883                         }
884                         glShadeModel(GL_FLAT);
885                 }
886                 GPU_buffer_unbind();
887         }
888 }
889
890 static void cdDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
891 {
892         cdDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
893 }
894
895 static void cdDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs), int (*setDrawOptions)(void *userData, int index), void *userData)
896 {
897         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
898         GPUVertexAttribs gattribs;
899         DMVertexAttribs attribs;
900         MVert *mvert = cddm->mvert;
901         MFace *mface = cddm->mface;
902         MTFace *tf = dm->getFaceDataArray(dm, CD_MTFACE);
903         float (*nors)[3] = dm->getFaceDataArray(dm, CD_NORMAL);
904         int a, b, dodraw, smoothnormal, matnr, new_matnr;
905         int transp, new_transp, orig_transp;
906         int orig, *index = dm->getFaceDataArray(dm, CD_ORIGINDEX);
907
908         matnr = -1;
909         smoothnormal = 0;
910         dodraw = 0;
911         transp = GPU_get_material_blend_mode();
912         orig_transp = transp;
913
914         glShadeModel(GL_SMOOTH);
915
916         if( GPU_buffer_legacy(dm) || setDrawOptions != 0 ) {
917                 DEBUG_VBO( "Using legacy code. cdDM_drawMappedFacesGLSL\n" );
918                 memset(&attribs, 0, sizeof(attribs));
919
920                 glBegin(GL_QUADS);
921
922                 for(a = 0; a < dm->numFaceData; a++, mface++) {
923                         new_matnr = mface->mat_nr + 1;
924
925                         if(new_matnr != matnr) {
926                                 glEnd();
927
928                                 dodraw = setMaterial(matnr = new_matnr, &gattribs);
929                                 if(dodraw)
930                                         DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
931
932                                 glBegin(GL_QUADS);
933                         }
934
935                         if(!dodraw) {
936                                 continue;
937                         }
938                         else if(setDrawOptions) {
939                                 orig = (index)? index[a]: a;
940
941                                 if(orig == ORIGINDEX_NONE)
942                                         continue;
943                                 else if(!setDrawOptions(userData, orig))
944                                         continue;
945                         }
946
947                         if(tf) {
948                                 new_transp = tf[a].transp;
949
950                                 if(new_transp != transp) {
951                                         glEnd();
952
953                                         if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID)
954                                                 GPU_set_material_blend_mode(orig_transp);
955                                         else
956                                                 GPU_set_material_blend_mode(new_transp);
957                                         transp = new_transp;
958
959                                         glBegin(GL_QUADS);
960                                 }
961                         }
962
963                         smoothnormal = (mface->flag & ME_SMOOTH);
964
965                         if(!smoothnormal) {
966                                 if(nors) {
967                                         glNormal3fv(nors[a]);
968                                 }
969                                 else {
970                                         /* TODO ideally a normal layer should always be available */
971                                         float nor[3];
972                                         if(mface->v4) {
973                                                 normal_quad_v3( nor,mvert[mface->v1].co, mvert[mface->v2].co, mvert[mface->v3].co, mvert[mface->v4].co);
974                                         } else {
975                                                 normal_tri_v3( nor,mvert[mface->v1].co, mvert[mface->v2].co, mvert[mface->v3].co);
976                                         }
977                                         glNormal3fv(nor);
978                                 }
979                         }
980
981 #define PASSVERT(index, vert) {                                                                                                 \
982                 if(attribs.totorco)                                                                                                                     \
983                         glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]);  \
984                 for(b = 0; b < attribs.tottface; b++) {                                                                         \
985                         MTFace *tf = &attribs.tface[b].array[a];                                                                \
986                         glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]);                   \
987                 }                                                                                                                                                       \
988                 for(b = 0; b < attribs.totmcol; b++) {                                                                          \
989                         MCol *cp = &attribs.mcol[b].array[a*4 + vert];                                                  \
990                         GLubyte col[4];                                                                                                                 \
991                         col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;                             \
992                         glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col);                                    \
993                 }                                                                                                                                                       \
994                 if(attribs.tottang) {                                                                                                           \
995                         float *tang = attribs.tang.array[a*4 + vert];                                                   \
996                         glVertexAttrib3fvARB(attribs.tang.glIndex, tang);                                               \
997                 }                                                                                                                                                       \
998                 if(smoothnormal)                                                                                                                        \
999                         glNormal3sv(mvert[index].no);                                                                                   \
1000                 glVertex3fv(mvert[index].co);                                                                                           \
1001         }
1002
1003                         PASSVERT(mface->v1, 0);
1004                         PASSVERT(mface->v2, 1);
1005                         PASSVERT(mface->v3, 2);
1006                         if(mface->v4)
1007                                 PASSVERT(mface->v4, 3)
1008                         else
1009                                 PASSVERT(mface->v3, 2)
1010
1011 #undef PASSVERT
1012                 }
1013                 glEnd();
1014         }
1015         else {
1016                 GPUBuffer *buffer = 0;
1017                 char *varray = 0;
1018                 int numdata = 0, elementsize = 0, offset;
1019                 int start = 0, numfaces = 0, prevdraw = 0, curface = 0;
1020                 GPUAttrib datatypes[32];
1021                 memset(&attribs, 0, sizeof(attribs));
1022
1023                 GPU_vertex_setup(dm);
1024                 GPU_normal_setup(dm);
1025
1026                 if( !GPU_buffer_legacy(dm) ) {
1027                         for(a = 0; a < dm->numFaceData; a++, mface++) {
1028                                 new_matnr = mface->mat_nr + 1;
1029
1030                                 if(new_matnr != matnr ) {
1031                                         numfaces = curface - start;
1032                                         if( numfaces > 0 ) {
1033                                                 if( prevdraw ) {
1034                                                         GPU_buffer_unlock(buffer);
1035                                                         GPU_interleaved_attrib_setup(buffer,datatypes,numdata);
1036                                                         glDrawArrays(GL_TRIANGLES,start*3,numfaces*3);
1037                                                         GPU_buffer_free(buffer,0);
1038                                                 }
1039                                         }
1040                                         start = curface;
1041                                         prevdraw = dodraw;
1042                                         dodraw = setMaterial(matnr = new_matnr, &gattribs);
1043                                         if(dodraw) {
1044                                                 DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
1045
1046                                                 if(attribs.totorco) {
1047                                                         datatypes[numdata].index = attribs.orco.glIndex;
1048                                                         datatypes[numdata].size = 3;
1049                                                         datatypes[numdata].type = GL_FLOAT;
1050                                                         numdata++;
1051                                                 }
1052                                                 for(b = 0; b < attribs.tottface; b++) {
1053                                                         datatypes[numdata].index = attribs.tface[b].glIndex;
1054                                                         datatypes[numdata].size = 2;
1055                                                         datatypes[numdata].type = GL_FLOAT;
1056                                                         numdata++;
1057                                                 }       
1058                                                 for(b = 0; b < attribs.totmcol; b++) {
1059                                                         datatypes[numdata].index = attribs.mcol[b].glIndex;
1060                                                         datatypes[numdata].size = 4;
1061                                                         datatypes[numdata].type = GL_UNSIGNED_BYTE;
1062                                                         numdata++;
1063                                                 }       
1064                                                 if(attribs.tottang) {
1065                                                         datatypes[numdata].index = attribs.tang.glIndex;
1066                                                         datatypes[numdata].size = 3;
1067                                                         datatypes[numdata].type = GL_FLOAT;
1068                                                         numdata++;
1069                                                 }
1070                                                 if( numdata != 0 ) {
1071                                                         elementsize = GPU_attrib_element_size( datatypes, numdata );
1072                                                         buffer = GPU_buffer_alloc( elementsize*dm->drawObject->nelements, 0 );
1073                                                         if( buffer == 0 ) {
1074                                                                 GPU_buffer_unbind();
1075                                                                 dm->drawObject->legacy = 1;
1076                                                                 return;
1077                                                         }
1078                                                         varray = GPU_buffer_lock_stream(buffer);
1079                                                         if( varray == 0 ) {
1080                                                                 GPU_buffer_unbind();
1081                                                                 GPU_buffer_free(buffer, 0);
1082                                                                 dm->drawObject->legacy = 1;
1083                                                                 return;
1084                                                         }
1085                                                 }
1086                                         }
1087                                 }
1088                                 if(!dodraw) {
1089                                         continue;
1090                                 }
1091
1092                                 if(tf) {
1093                                         new_transp = tf[a].transp;
1094
1095                                         if(new_transp != transp) {
1096                                                 numfaces = curface - start;
1097                                                 if( numfaces > 0 ) {
1098                                                         if( dodraw ) {
1099                                                                 if( numdata != 0 ) {
1100                                                                         GPU_buffer_unlock(buffer);
1101                                                                         GPU_interleaved_attrib_setup(buffer,datatypes,numdata);
1102                                                                 }
1103                                                                 glDrawArrays(GL_TRIANGLES,start*3,(curface-start)*3);
1104                                                                 if( numdata != 0 ) {
1105                                                                         varray = GPU_buffer_lock_stream(buffer);
1106                                                                 }
1107                                                         }
1108                                                 }
1109                                                 start = curface;
1110
1111                                                 if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID)
1112                                                         GPU_set_material_blend_mode(orig_transp);
1113                                                 else
1114                                                         GPU_set_material_blend_mode(new_transp);
1115                                                 transp = new_transp;
1116                                         }
1117                                 }
1118                                 
1119                                 if( numdata != 0 ) {
1120                                         offset = 0;
1121                                         if(attribs.totorco) {
1122                                                 VECCOPY((float *)&varray[elementsize*curface*3],(float *)attribs.orco.array[mface->v1]);
1123                                                 VECCOPY((float *)&varray[elementsize*curface*3+elementsize],(float *)attribs.orco.array[mface->v2]);
1124                                                 VECCOPY((float *)&varray[elementsize*curface*3+elementsize*2],(float *)attribs.orco.array[mface->v3]);
1125                                                 offset += sizeof(float)*3;
1126                                         }
1127                                         for(b = 0; b < attribs.tottface; b++) {
1128                                                 MTFace *tf = &attribs.tface[b].array[a];
1129                                                 VECCOPY((float *)&varray[elementsize*curface*3+offset],tf->uv[0]);
1130                                                 VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize],tf->uv[1]);
1131                                                 VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize*2],tf->uv[2]);
1132                                                 offset += sizeof(float)*2;
1133                                         }
1134                                         for(b = 0; b < attribs.totmcol; b++) {
1135                                                 MCol *cp = &attribs.mcol[b].array[a*4 + 0];
1136                                                 GLubyte col[4];
1137                                                 col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;
1138                                                 QUATCOPY((unsigned char *)&varray[elementsize*curface*3+offset], col);
1139                                                 cp = &attribs.mcol[b].array[a*4 + 1];
1140                                                 col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;
1141                                                 QUATCOPY((unsigned char *)&varray[elementsize*curface*3+offset+elementsize], col);
1142                                                 cp = &attribs.mcol[b].array[a*4 + 2];
1143                                                 col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;
1144                                                 QUATCOPY((unsigned char *)&varray[elementsize*curface*3+offset+elementsize*2], col);
1145                                                 offset += sizeof(unsigned char)*4;
1146                                         }       
1147                                         if(attribs.tottang) {
1148                                                 float *tang = attribs.tang.array[a*4 + 0];
1149                                                 VECCOPY((float *)&varray[elementsize*curface*3+offset], tang);
1150                                                 tang = attribs.tang.array[a*4 + 1];
1151                                                 VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize], tang);
1152                                                 tang = attribs.tang.array[a*4 + 2];
1153                                                 VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize], tang);
1154                                                 offset += sizeof(float)*3;
1155                                         }
1156                                 }
1157                                 curface++;
1158                                 if(mface->v4) {
1159                                         if( numdata != 0 ) {
1160                                                 offset = 0;
1161                                                 if(attribs.totorco) {
1162                                                         VECCOPY((float *)&varray[elementsize*curface*3],(float *)attribs.orco.array[mface->v3]);
1163                                                         VECCOPY((float *)&varray[elementsize*curface*3+elementsize],(float *)attribs.orco.array[mface->v4]);
1164                                                         VECCOPY((float *)&varray[elementsize*curface*3+elementsize*2],(float *)attribs.orco.array[mface->v1]);
1165                                                         offset += sizeof(float)*3;
1166                                                 }
1167                                                 for(b = 0; b < attribs.tottface; b++) {
1168                                                         MTFace *tf = &attribs.tface[b].array[a];
1169                                                         VECCOPY((float *)&varray[elementsize*curface*3+offset],tf->uv[2]);
1170                                                         VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize],tf->uv[3]);
1171                                                         VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize*2],tf->uv[0]);
1172                                                         offset += sizeof(float)*2;
1173                                                 }
1174                                                 for(b = 0; b < attribs.totmcol; b++) {
1175                                                         MCol *cp = &attribs.mcol[b].array[a*4 + 2];
1176                                                         GLubyte col[4];
1177                                                         col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;
1178                                                         QUATCOPY((unsigned char *)&varray[elementsize*curface*3+offset], col);
1179                                                         cp = &attribs.mcol[b].array[a*4 + 3];
1180                                                         col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;
1181                                                         QUATCOPY((unsigned char *)&varray[elementsize*curface*3+offset+elementsize], col);
1182                                                         cp = &attribs.mcol[b].array[a*4 + 0];
1183                                                         col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;
1184                                                         QUATCOPY((unsigned char *)&varray[elementsize*curface*3+offset+elementsize*2], col);
1185                                                         offset += sizeof(unsigned char)*4;
1186                                                 }       
1187                                                 if(attribs.tottang) {
1188                                                         float *tang = attribs.tang.array[a*4 + 2];
1189                                                         VECCOPY((float *)&varray[elementsize*curface*3+offset], tang);
1190                                                         tang = attribs.tang.array[a*4 + 3];
1191                                                         VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize], tang);
1192                                                         tang = attribs.tang.array[a*4 + 0];
1193                                                         VECCOPY((float *)&varray[elementsize*curface*3+offset+elementsize], tang);
1194                                                         offset += sizeof(float)*3;
1195                                                 }
1196                                         }
1197                                         curface++;
1198                                 }
1199                         }
1200                         numfaces = curface - start;
1201                         if( numfaces > 0 ) {
1202                                 if( dodraw ) {
1203                                         if( numdata != 0 ) {
1204                                                 GPU_buffer_unlock(buffer);
1205                                                 GPU_interleaved_attrib_setup(buffer,datatypes,numdata);
1206                                         }
1207                                         glDrawArrays(GL_TRIANGLES,start*3,(curface-start)*3);
1208                                 }
1209                         }
1210                         GPU_buffer_unbind();
1211                 }
1212                 GPU_buffer_free( buffer, 0 );
1213         }
1214
1215         glShadeModel(GL_FLAT);
1216 }
1217
1218 static void cdDM_drawFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs))
1219 {
1220         dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL);
1221 }
1222
1223 static void cdDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
1224 {
1225         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
1226         MVert *vert = cddm->mvert;
1227         MEdge *edge = cddm->medge;
1228         int i, orig, *index = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
1229
1230         glBegin(GL_LINES);
1231         for(i = 0; i < dm->numEdgeData; i++, edge++) {
1232                 if(index) {
1233                         orig = *index++;
1234                         if(setDrawOptions && orig == ORIGINDEX_NONE) continue;
1235                 }
1236                 else
1237                         orig = i;
1238
1239                 if(!setDrawOptions || setDrawOptions(userData, orig)) {
1240                         glVertex3fv(vert[edge->v1].co);
1241                         glVertex3fv(vert[edge->v2].co);
1242                 }
1243         }
1244         glEnd();
1245 }
1246
1247 static void cdDM_foreachMappedVert(
1248                            DerivedMesh *dm,
1249                            void (*func)(void *userData, int index, float *co,
1250                                         float *no_f, short *no_s),
1251                            void *userData)
1252 {
1253         MVert *mv = CDDM_get_verts(dm);
1254         int i, orig, *index = DM_get_vert_data_layer(dm, CD_ORIGINDEX);
1255
1256         for(i = 0; i < dm->numVertData; i++, mv++) {
1257                 if(index) {
1258                         orig = *index++;
1259                         if(orig == ORIGINDEX_NONE) continue;
1260                         func(userData, orig, mv->co, NULL, mv->no);
1261                 }
1262                 else
1263                         func(userData, i, mv->co, NULL, mv->no);
1264         }
1265 }
1266
1267 static void cdDM_foreachMappedEdge(
1268                            DerivedMesh *dm,
1269                            void (*func)(void *userData, int index,
1270                                         float *v0co, float *v1co),
1271                            void *userData)
1272 {
1273         CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
1274         MVert *mv = cddm->mvert;
1275         MEdge *med = cddm->medge;
1276         int i, orig, *index = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
1277
1278         for(i = 0; i < dm->numEdgeData; i++, med++) {
1279                 if (index) {
1280                         orig = *index++;
1281                         if(orig == ORIGINDEX_NONE) continue;
1282                         func(userData, orig, mv[med->v1].co, mv[med->v2].co);
1283                 }
1284                 else
1285                         func(userData, i, mv[med->v1].co, mv[med->v2].co);
1286         }
1287 }
1288
1289 static void cdDM_foreachMappedFaceCenter(
1290                            DerivedMesh *dm,
1291                            void (*func)(void *userData, int index,
1292                                         float *cent, float *no),
1293                            void *userData)
1294 {
1295         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1296         MVert *mv = cddm->mvert;
1297         MFace *mf = cddm->mface;
1298         int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
1299
1300         for(i = 0; i < dm->numFaceData; i++, mf++) {
1301                 float cent[3];
1302                 float no[3];
1303
1304                 if (index) {
1305                         orig = *index++;
1306                         if(orig == ORIGINDEX_NONE) continue;
1307                 }
1308                 else
1309                         orig = i;
1310
1311                 VECCOPY(cent, mv[mf->v1].co);
1312                 add_v3_v3v3(cent, cent, mv[mf->v2].co);
1313                 add_v3_v3v3(cent, cent, mv[mf->v3].co);
1314
1315                 if (mf->v4) {
1316                         normal_quad_v3( no,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co, mv[mf->v4].co);
1317                         add_v3_v3v3(cent, cent, mv[mf->v4].co);
1318                         mul_v3_fl(cent, 0.25f);
1319                 } else {
1320                         normal_tri_v3( no,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co);
1321                         mul_v3_fl(cent, 0.33333333333f);
1322                 }
1323
1324                 func(userData, orig, cent, no);
1325         }
1326 }
1327
1328 static void cdDM_free_internal(CDDerivedMesh *cddm)
1329 {
1330         if(cddm->pbvh) BLI_pbvh_free(cddm->pbvh);
1331         if(cddm->fmap) MEM_freeN(cddm->fmap);
1332         if(cddm->fmap_mem) MEM_freeN(cddm->fmap_mem);
1333 }
1334
1335 static void cdDM_release(DerivedMesh *dm)
1336 {
1337         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1338
1339         if (DM_release(dm)) {
1340                 cdDM_free_internal(cddm);
1341                 MEM_freeN(cddm);
1342         }
1343 }
1344
1345 /**************** CDDM interface functions ****************/
1346 static CDDerivedMesh *cdDM_create(const char *desc)
1347 {
1348         CDDerivedMesh *cddm;
1349         DerivedMesh *dm;
1350
1351         cddm = MEM_callocN(sizeof(*cddm), desc);
1352         dm = &cddm->dm;
1353
1354         dm->getMinMax = cdDM_getMinMax;
1355
1356         dm->getNumVerts = cdDM_getNumVerts;
1357         dm->getNumFaces = cdDM_getNumFaces;
1358         dm->getNumEdges = cdDM_getNumEdges;
1359
1360         dm->getVert = cdDM_getVert;
1361         dm->getEdge = cdDM_getEdge;
1362         dm->getFace = cdDM_getFace;
1363         dm->copyVertArray = cdDM_copyVertArray;
1364         dm->copyEdgeArray = cdDM_copyEdgeArray;
1365         dm->copyFaceArray = cdDM_copyFaceArray;
1366         dm->getVertData = DM_get_vert_data;
1367         dm->getEdgeData = DM_get_edge_data;
1368         dm->getFaceData = DM_get_face_data;
1369         dm->getVertDataArray = DM_get_vert_data_layer;
1370         dm->getEdgeDataArray = DM_get_edge_data_layer;
1371         dm->getFaceDataArray = DM_get_face_data_layer;
1372
1373         dm->getVertCos = cdDM_getVertCos;
1374         dm->getVertCo = cdDM_getVertCo;
1375         dm->getVertNo = cdDM_getVertNo;
1376
1377         dm->getPBVH = cdDM_getPBVH;
1378         dm->getFaceMap = cdDM_getFaceMap;
1379
1380         dm->drawVerts = cdDM_drawVerts;
1381
1382         dm->drawUVEdges = cdDM_drawUVEdges;
1383         dm->drawEdges = cdDM_drawEdges;
1384         dm->drawLooseEdges = cdDM_drawLooseEdges;
1385         dm->drawMappedEdges = cdDM_drawMappedEdges;
1386
1387         dm->drawFacesSolid = cdDM_drawFacesSolid;
1388         dm->drawFacesColored = cdDM_drawFacesColored;
1389         dm->drawFacesTex = cdDM_drawFacesTex;
1390         dm->drawFacesGLSL = cdDM_drawFacesGLSL;
1391         dm->drawMappedFaces = cdDM_drawMappedFaces;
1392         dm->drawMappedFacesTex = cdDM_drawMappedFacesTex;
1393         dm->drawMappedFacesGLSL = cdDM_drawMappedFacesGLSL;
1394
1395         dm->foreachMappedVert = cdDM_foreachMappedVert;
1396         dm->foreachMappedEdge = cdDM_foreachMappedEdge;
1397         dm->foreachMappedFaceCenter = cdDM_foreachMappedFaceCenter;
1398
1399         dm->release = cdDM_release;
1400
1401         return cddm;
1402 }
1403
1404 DerivedMesh *CDDM_new(int numVerts, int numEdges, int numFaces)
1405 {
1406         CDDerivedMesh *cddm = cdDM_create("CDDM_new dm");
1407         DerivedMesh *dm = &cddm->dm;
1408
1409         DM_init(dm, DM_TYPE_CDDM, numVerts, numEdges, numFaces);
1410
1411         CustomData_add_layer(&dm->vertData, CD_ORIGINDEX, CD_CALLOC, NULL, numVerts);
1412         CustomData_add_layer(&dm->edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges);
1413         CustomData_add_layer(&dm->faceData, CD_ORIGINDEX, CD_CALLOC, NULL, numFaces);
1414
1415         CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL, numVerts);
1416         CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges);
1417         CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL, numFaces);
1418
1419         cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
1420         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
1421         cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
1422
1423         return dm;
1424 }
1425
1426 DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob)
1427 {
1428         CDDerivedMesh *cddm = cdDM_create("CDDM_from_mesh dm");
1429         DerivedMesh *dm = &cddm->dm;
1430         CustomDataMask mask = CD_MASK_MESH & (~CD_MASK_MDISPS);
1431         int alloctype;
1432
1433         /* this does a referenced copy, with an exception for fluidsim */
1434
1435         DM_init(dm, DM_TYPE_CDDM, mesh->totvert, mesh->totedge, mesh->totface);
1436
1437         dm->deformedOnly = 1;
1438
1439         alloctype= CD_REFERENCE;
1440
1441         CustomData_merge(&mesh->vdata, &dm->vertData, mask, alloctype,
1442                          mesh->totvert);
1443         CustomData_merge(&mesh->edata, &dm->edgeData, mask, alloctype,
1444                          mesh->totedge);
1445         CustomData_merge(&mesh->fdata, &dm->faceData, mask, alloctype,
1446                          mesh->totface);
1447
1448         cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
1449         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
1450         cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
1451
1452         return dm;
1453 }
1454
1455 DerivedMesh *CDDM_from_editmesh(EditMesh *em, Mesh *me)
1456 {
1457         DerivedMesh *dm = CDDM_new(BLI_countlist(&em->verts),
1458                                    BLI_countlist(&em->edges),
1459                                    BLI_countlist(&em->faces));
1460         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1461         EditVert *eve;
1462         EditEdge *eed;
1463         EditFace *efa;
1464         MVert *mvert = cddm->mvert;
1465         MEdge *medge = cddm->medge;
1466         MFace *mface = cddm->mface;
1467         int i, *index;
1468
1469         dm->deformedOnly = 1;
1470
1471         CustomData_merge(&em->vdata, &dm->vertData, CD_MASK_DERIVEDMESH,
1472                          CD_CALLOC, dm->numVertData);
1473         /* CustomData_merge(&em->edata, &dm->edgeData, CD_MASK_DERIVEDMESH,
1474                          CD_CALLOC, dm->numEdgeData); */
1475         CustomData_merge(&em->fdata, &dm->faceData, CD_MASK_DERIVEDMESH,
1476                          CD_CALLOC, dm->numFaceData);
1477
1478         /* set eve->hash to vert index */
1479         for(i = 0, eve = em->verts.first; eve; eve = eve->next, ++i)
1480                 eve->tmp.l = i;
1481
1482         /* Need to be able to mark loose edges */
1483         for(eed = em->edges.first; eed; eed = eed->next) {
1484                 eed->f2 = 0;
1485         }
1486         for(efa = em->faces.first; efa; efa = efa->next) {
1487                 efa->e1->f2 = 1;
1488                 efa->e2->f2 = 1;
1489                 efa->e3->f2 = 1;
1490                 if(efa->e4) efa->e4->f2 = 1;
1491         }
1492
1493         index = dm->getVertDataArray(dm, CD_ORIGINDEX);
1494         for(i = 0, eve = em->verts.first; i < dm->numVertData;
1495             i++, eve = eve->next, index++) {
1496                 MVert *mv = &mvert[i];
1497
1498                 VECCOPY(mv->co, eve->co);
1499
1500                 mv->no[0] = eve->no[0] * 32767.0;
1501                 mv->no[1] = eve->no[1] * 32767.0;
1502                 mv->no[2] = eve->no[2] * 32767.0;
1503                 mv->bweight = (unsigned char) (eve->bweight * 255.0f);
1504
1505                 mv->mat_nr = 0;
1506                 mv->flag = 0;
1507
1508                 *index = i;
1509
1510                 CustomData_from_em_block(&em->vdata, &dm->vertData, eve->data, i);
1511         }
1512
1513         index = dm->getEdgeDataArray(dm, CD_ORIGINDEX);
1514         for(i = 0, eed = em->edges.first; i < dm->numEdgeData;
1515             i++, eed = eed->next, index++) {
1516                 MEdge *med = &medge[i];
1517
1518                 med->v1 = eed->v1->tmp.l;
1519                 med->v2 = eed->v2->tmp.l;
1520                 med->crease = (unsigned char) (eed->crease * 255.0f);
1521                 med->bweight = (unsigned char) (eed->bweight * 255.0f);
1522                 med->flag = ME_EDGEDRAW|ME_EDGERENDER;
1523                 
1524                 if(eed->seam) med->flag |= ME_SEAM;
1525                 if(eed->sharp) med->flag |= ME_SHARP;
1526                 if(!eed->f2) med->flag |= ME_LOOSEEDGE;
1527
1528                 *index = i;
1529
1530                 /* CustomData_from_em_block(&em->edata, &dm->edgeData, eed->data, i); */
1531         }
1532
1533         index = dm->getFaceDataArray(dm, CD_ORIGINDEX);
1534         for(i = 0, efa = em->faces.first; i < dm->numFaceData;
1535             i++, efa = efa->next, index++) {
1536                 MFace *mf = &mface[i];
1537
1538                 mf->v1 = efa->v1->tmp.l;
1539                 mf->v2 = efa->v2->tmp.l;
1540                 mf->v3 = efa->v3->tmp.l;
1541                 mf->v4 = efa->v4 ? efa->v4->tmp.l : 0;
1542                 mf->mat_nr = efa->mat_nr;
1543                 mf->flag = efa->flag;
1544
1545                 *index = i;
1546
1547                 CustomData_from_em_block(&em->fdata, &dm->faceData, efa->data, i);
1548                 test_index_face(mf, &dm->faceData, i, efa->v4?4:3);
1549         }
1550
1551         return dm;
1552 }
1553
1554 DerivedMesh *CDDM_copy(DerivedMesh *source)
1555 {
1556         CDDerivedMesh *cddm = cdDM_create("CDDM_copy cddm");
1557         DerivedMesh *dm = &cddm->dm;
1558         int numVerts = source->numVertData;
1559         int numEdges = source->numEdgeData;
1560         int numFaces = source->numFaceData;
1561
1562         /* ensure these are created if they are made on demand */
1563         source->getVertDataArray(source, CD_ORIGINDEX);
1564         source->getEdgeDataArray(source, CD_ORIGINDEX);
1565         source->getFaceDataArray(source, CD_ORIGINDEX);
1566
1567         /* this initializes dm, and copies all non mvert/medge/mface layers */
1568         DM_from_template(dm, source, DM_TYPE_CDDM, numVerts, numEdges, numFaces);
1569         dm->deformedOnly = source->deformedOnly;
1570
1571         CustomData_copy_data(&source->vertData, &dm->vertData, 0, 0, numVerts);
1572         CustomData_copy_data(&source->edgeData, &dm->edgeData, 0, 0, numEdges);
1573         CustomData_copy_data(&source->faceData, &dm->faceData, 0, 0, numFaces);
1574
1575         /* now add mvert/medge/mface layers */
1576         cddm->mvert = source->dupVertArray(source);
1577         cddm->medge = source->dupEdgeArray(source);
1578         cddm->mface = source->dupFaceArray(source);
1579
1580         CustomData_add_layer(&dm->vertData, CD_MVERT, CD_ASSIGN, cddm->mvert, numVerts);
1581         CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_ASSIGN, cddm->medge, numEdges);
1582         CustomData_add_layer(&dm->faceData, CD_MFACE, CD_ASSIGN, cddm->mface, numFaces);
1583
1584         return dm;
1585 }
1586
1587 DerivedMesh *CDDM_from_template(DerivedMesh *source,
1588                                 int numVerts, int numEdges, int numFaces)
1589 {
1590         CDDerivedMesh *cddm = cdDM_create("CDDM_from_template dest");
1591         DerivedMesh *dm = &cddm->dm;
1592
1593         /* this does a copy of all non mvert/medge/mface layers */
1594         DM_from_template(dm, source, DM_TYPE_CDDM, numVerts, numEdges, numFaces);
1595
1596         /* now add mvert/medge/mface layers */
1597         CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL, numVerts);
1598         CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges);
1599         CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL, numFaces);
1600
1601         if(!CustomData_get_layer(&dm->vertData, CD_ORIGINDEX))
1602                 CustomData_add_layer(&dm->vertData, CD_ORIGINDEX, CD_CALLOC, NULL, numVerts);
1603         if(!CustomData_get_layer(&dm->edgeData, CD_ORIGINDEX))
1604                 CustomData_add_layer(&dm->edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges);
1605         if(!CustomData_get_layer(&dm->faceData, CD_ORIGINDEX))
1606                 CustomData_add_layer(&dm->faceData, CD_ORIGINDEX, CD_CALLOC, NULL, numFaces);
1607
1608         cddm->mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
1609         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
1610         cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
1611
1612         return dm;
1613 }
1614
1615 void CDDM_apply_vert_coords(DerivedMesh *dm, float (*vertCoords)[3])
1616 {
1617         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1618         MVert *vert;
1619         int i;
1620
1621         /* this will just return the pointer if it wasn't a referenced layer */
1622         vert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
1623         cddm->mvert = vert;
1624
1625         for(i = 0; i < dm->numVertData; ++i, ++vert)
1626                 VECCOPY(vert->co, vertCoords[i]);
1627 }
1628
1629 void CDDM_apply_vert_normals(DerivedMesh *dm, short (*vertNormals)[3])
1630 {
1631         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1632         MVert *vert;
1633         int i;
1634
1635         /* this will just return the pointer if it wasn't a referenced layer */
1636         vert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
1637         cddm->mvert = vert;
1638
1639         for(i = 0; i < dm->numVertData; ++i, ++vert)
1640                 VECCOPY(vert->no, vertNormals[i]);
1641 }
1642
1643 /* adapted from mesh_calc_normals */
1644 void CDDM_calc_normals(DerivedMesh *dm)
1645 {
1646         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1647         float (*temp_nors)[3];
1648         float (*face_nors)[3];
1649         int i;
1650         int numVerts = dm->numVertData;
1651         int numFaces = dm->numFaceData;
1652         MFace *mf;
1653         MVert *mv;
1654
1655         if(numVerts == 0) return;
1656
1657         temp_nors = MEM_callocN(numVerts * sizeof(*temp_nors),
1658                                 "CDDM_calc_normals temp_nors");
1659
1660         /* we don't want to overwrite any referenced layers */
1661         mv = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MVERT);
1662         cddm->mvert = mv;
1663
1664         /* make a face normal layer if not present */
1665         face_nors = CustomData_get_layer(&dm->faceData, CD_NORMAL);
1666         if(!face_nors)
1667                 face_nors = CustomData_add_layer(&dm->faceData, CD_NORMAL, CD_CALLOC,
1668                                                  NULL, dm->numFaceData);
1669
1670         /* calculate face normals and add to vertex normals */
1671         mf = CDDM_get_faces(dm);
1672         for(i = 0; i < numFaces; i++, mf++) {
1673                 float *f_no = face_nors[i];
1674
1675                 if(mf->v4)
1676                         normal_quad_v3( f_no,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co, mv[mf->v4].co);
1677                 else
1678                         normal_tri_v3( f_no,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co);
1679                 
1680                 add_v3_v3v3(temp_nors[mf->v1], temp_nors[mf->v1], f_no);
1681                 add_v3_v3v3(temp_nors[mf->v2], temp_nors[mf->v2], f_no);
1682                 add_v3_v3v3(temp_nors[mf->v3], temp_nors[mf->v3], f_no);
1683                 if(mf->v4)
1684                         add_v3_v3v3(temp_nors[mf->v4], temp_nors[mf->v4], f_no);
1685         }
1686
1687         /* normalize vertex normals and assign */
1688         for(i = 0; i < numVerts; i++, mv++) {
1689                 float *no = temp_nors[i];
1690                 
1691                 if (normalize_v3(no) == 0.0) {
1692                         VECCOPY(no, mv->co);
1693                         normalize_v3(no);
1694                 }
1695
1696                 mv->no[0] = (short)(no[0] * 32767.0);
1697                 mv->no[1] = (short)(no[1] * 32767.0);
1698                 mv->no[2] = (short)(no[2] * 32767.0);
1699         }
1700         
1701         MEM_freeN(temp_nors);
1702 }
1703
1704 void CDDM_calc_edges(DerivedMesh *dm)
1705 {
1706         CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
1707         CustomData edgeData;
1708         EdgeHashIterator *ehi;
1709         MFace *mf = cddm->mface;
1710         MEdge *med;
1711         EdgeHash *eh = BLI_edgehash_new();
1712         int i, *index, numEdges, maxFaces = dm->numFaceData;
1713
1714         for (i = 0; i < maxFaces; i++, mf++) {
1715                 if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
1716                         BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
1717                 if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
1718                         BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
1719                 
1720                 if (mf->v4) {
1721                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
1722                                 BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
1723                         if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
1724                                 BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
1725                 } else {
1726                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
1727                                 BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
1728                 }
1729         }
1730
1731         numEdges = BLI_edgehash_size(eh);
1732
1733         /* write new edges into a temporary CustomData */
1734         memset(&edgeData, 0, sizeof(edgeData));
1735         CustomData_add_layer(&edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges);
1736         CustomData_add_layer(&edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges);
1737
1738         ehi = BLI_edgehashIterator_new(eh);
1739         med = CustomData_get_layer(&edgeData, CD_MEDGE);
1740         index = CustomData_get_layer(&edgeData, CD_ORIGINDEX);
1741         for(i = 0; !BLI_edgehashIterator_isDone(ehi);
1742             BLI_edgehashIterator_step(ehi), ++i, ++med, ++index) {
1743                 BLI_edgehashIterator_getKey(ehi, (int*)&med->v1, (int*)&med->v2);
1744
1745                 med->flag = ME_EDGEDRAW|ME_EDGERENDER;
1746                 *index = ORIGINDEX_NONE;
1747         }
1748         BLI_edgehashIterator_free(ehi);
1749
1750         /* free old CustomData and assign new one */
1751         CustomData_free(&dm->edgeData, dm->numEdgeData);
1752         dm->edgeData = edgeData;
1753         dm->numEdgeData = numEdges;
1754
1755         cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
1756
1757         BLI_edgehash_free(eh, NULL);
1758 }
1759
1760 void CDDM_lower_num_verts(DerivedMesh *dm, int numVerts)
1761 {
1762         if (numVerts < dm->numVertData)
1763                 CustomData_free_elem(&dm->vertData, numVerts, dm->numVertData-numVerts);
1764
1765         dm->numVertData = numVerts;
1766 }
1767
1768 void CDDM_lower_num_edges(DerivedMesh *dm, int numEdges)
1769 {
1770         if (numEdges < dm->numEdgeData)
1771                 CustomData_free_elem(&dm->edgeData, numEdges, dm->numEdgeData-numEdges);
1772
1773         dm->numEdgeData = numEdges;
1774 }
1775
1776 void CDDM_lower_num_faces(DerivedMesh *dm, int numFaces)
1777 {
1778         if (numFaces < dm->numFaceData)
1779                 CustomData_free_elem(&dm->faceData, numFaces, dm->numFaceData-numFaces);
1780
1781         dm->numFaceData = numFaces;
1782 }
1783
1784 MVert *CDDM_get_vert(DerivedMesh *dm, int index)
1785 {
1786         return &((CDDerivedMesh*)dm)->mvert[index];
1787 }
1788
1789 MEdge *CDDM_get_edge(DerivedMesh *dm, int index)
1790 {
1791         return &((CDDerivedMesh*)dm)->medge[index];
1792 }
1793
1794 MFace *CDDM_get_face(DerivedMesh *dm, int index)
1795 {
1796         return &((CDDerivedMesh*)dm)->mface[index];
1797 }
1798
1799 MVert *CDDM_get_verts(DerivedMesh *dm)
1800 {
1801         return ((CDDerivedMesh*)dm)->mvert;
1802 }
1803
1804 MEdge *CDDM_get_edges(DerivedMesh *dm)
1805 {
1806         return ((CDDerivedMesh*)dm)->medge;
1807 }
1808
1809 MFace *CDDM_get_faces(DerivedMesh *dm)
1810 {
1811         return ((CDDerivedMesh*)dm)->mface;
1812 }
1813