PageRenderTime 61ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/blender-2.63a/source/blender/blenkernel/intern/subsurf_ccg.c

#
C | 3503 lines | 2713 code | 627 blank | 163 comment | 564 complexity | b01be4dca33570b01c74626b55444347 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-3.0, BSD-2-Clause, Apache-2.0, AGPL-1.0
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2005 Blender Foundation.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file blender/blenkernel/intern/subsurf_ccg.c
  28. * \ingroup bke
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include <float.h>
  35. #include "MEM_guardedalloc.h"
  36. #include "DNA_mesh_types.h"
  37. #include "DNA_meshdata_types.h"
  38. #include "DNA_modifier_types.h"
  39. #include "DNA_object_types.h"
  40. #include "DNA_scene_types.h"
  41. #include "BLI_utildefines.h"
  42. #include "BLI_bitmap.h"
  43. #include "BLI_blenlib.h"
  44. #include "BLI_edgehash.h"
  45. #include "BLI_math.h"
  46. #include "BLI_memarena.h"
  47. #include "BLI_pbvh.h"
  48. #include "BKE_cdderivedmesh.h"
  49. #include "BKE_global.h"
  50. #include "BKE_mesh.h"
  51. #include "BKE_modifier.h"
  52. #include "BKE_multires.h"
  53. #include "BKE_paint.h"
  54. #include "BKE_scene.h"
  55. #include "BKE_subsurf.h"
  56. #include "BKE_tessmesh.h"
  57. #include "PIL_time.h"
  58. #include "BLI_array.h"
  59. #include "GL/glew.h"
  60. #include "GPU_draw.h"
  61. #include "GPU_extensions.h"
  62. #include "GPU_material.h"
  63. #include "CCGSubSurf.h"
  64. extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
  65. static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
  66. int drawInteriorEdges,
  67. int useSubsurfUv,
  68. DerivedMesh *dm);
  69. static int ccgDM_use_grid_pbvh(CCGDerivedMesh *ccgdm);
  70. ///
  71. static void *arena_alloc(CCGAllocatorHDL a, int numBytes)
  72. {
  73. return BLI_memarena_alloc(a, numBytes);
  74. }
  75. static void *arena_realloc(CCGAllocatorHDL a, void *ptr, int newSize, int oldSize)
  76. {
  77. void *p2 = BLI_memarena_alloc(a, newSize);
  78. if (ptr) {
  79. memcpy(p2, ptr, oldSize);
  80. }
  81. return p2;
  82. }
  83. static void arena_free(CCGAllocatorHDL UNUSED(a), void *UNUSED(ptr))
  84. {
  85. /* do nothing */
  86. }
  87. static void arena_release(CCGAllocatorHDL a)
  88. {
  89. BLI_memarena_free(a);
  90. }
  91. typedef enum {
  92. CCG_USE_AGING = 1,
  93. CCG_USE_ARENA = 2,
  94. CCG_CALC_NORMALS = 4
  95. } CCGFlags;
  96. static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels, CCGFlags flags)
  97. {
  98. CCGMeshIFC ifc;
  99. CCGSubSurf *ccgSS;
  100. int useAging = !!(flags & CCG_USE_AGING);
  101. int useArena = flags & CCG_USE_ARENA;
  102. /* subdivLevels==0 is not allowed */
  103. subdivLevels = MAX2(subdivLevels, 1);
  104. if (prevSS) {
  105. int oldUseAging;
  106. ccgSubSurf_getUseAgeCounts(prevSS, &oldUseAging, NULL, NULL, NULL);
  107. if (oldUseAging != useAging) {
  108. ccgSubSurf_free(prevSS);
  109. }
  110. else {
  111. ccgSubSurf_setSubdivisionLevels(prevSS, subdivLevels);
  112. return prevSS;
  113. }
  114. }
  115. if (useAging) {
  116. ifc.vertUserSize = ifc.edgeUserSize = ifc.faceUserSize = 12;
  117. }
  118. else {
  119. ifc.vertUserSize = ifc.edgeUserSize = ifc.faceUserSize = 8;
  120. }
  121. ifc.vertDataSize = sizeof(float) * (flags & CCG_CALC_NORMALS ? 6 : 3);
  122. if (useArena) {
  123. CCGAllocatorIFC allocatorIFC;
  124. CCGAllocatorHDL allocator = BLI_memarena_new((1 << 16), "subsurf arena");
  125. allocatorIFC.alloc = arena_alloc;
  126. allocatorIFC.realloc = arena_realloc;
  127. allocatorIFC.free = arena_free;
  128. allocatorIFC.release = arena_release;
  129. ccgSS = ccgSubSurf_new(&ifc, subdivLevels, &allocatorIFC, allocator);
  130. }
  131. else {
  132. ccgSS = ccgSubSurf_new(&ifc, subdivLevels, NULL, NULL);
  133. }
  134. if (useAging) {
  135. ccgSubSurf_setUseAgeCounts(ccgSS, 1, 8, 8, 8);
  136. }
  137. if (flags & CCG_CALC_NORMALS)
  138. ccgSubSurf_setCalcVertexNormals(ccgSS, 1, offsetof(DMGridData, no));
  139. else
  140. ccgSubSurf_setCalcVertexNormals(ccgSS, 0, 0);
  141. return ccgSS;
  142. }
  143. static int getEdgeIndex(CCGSubSurf *ss, CCGEdge *e, int x, int edgeSize)
  144. {
  145. CCGVert *v0 = ccgSubSurf_getEdgeVert0(e);
  146. CCGVert *v1 = ccgSubSurf_getEdgeVert1(e);
  147. int v0idx = *((int *) ccgSubSurf_getVertUserData(ss, v0));
  148. int v1idx = *((int *) ccgSubSurf_getVertUserData(ss, v1));
  149. int edgeBase = *((int *) ccgSubSurf_getEdgeUserData(ss, e));
  150. if (x == 0) {
  151. return v0idx;
  152. }
  153. else if (x == edgeSize - 1) {
  154. return v1idx;
  155. }
  156. else {
  157. return edgeBase + x - 1;
  158. }
  159. }
  160. static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edgeSize, int gridSize)
  161. {
  162. int faceBase = *((int *) ccgSubSurf_getFaceUserData(ss, f));
  163. int numVerts = ccgSubSurf_getFaceNumVerts(f);
  164. if (x == gridSize - 1 && y == gridSize - 1) {
  165. CCGVert *v = ccgSubSurf_getFaceVert(f, S);
  166. return *((int *) ccgSubSurf_getVertUserData(ss, v));
  167. }
  168. else if (x == gridSize - 1) {
  169. CCGVert *v = ccgSubSurf_getFaceVert(f, S);
  170. CCGEdge *e = ccgSubSurf_getFaceEdge(f, S);
  171. int edgeBase = *((int *) ccgSubSurf_getEdgeUserData(ss, e));
  172. if (v == ccgSubSurf_getEdgeVert0(e)) {
  173. return edgeBase + (gridSize - 1 - y) - 1;
  174. }
  175. else {
  176. return edgeBase + (edgeSize - 2 - 1) - ((gridSize - 1 - y) - 1);
  177. }
  178. }
  179. else if (y == gridSize - 1) {
  180. CCGVert *v = ccgSubSurf_getFaceVert(f, S);
  181. CCGEdge *e = ccgSubSurf_getFaceEdge(f, (S + numVerts - 1) % numVerts);
  182. int edgeBase = *((int *) ccgSubSurf_getEdgeUserData(ss, e));
  183. if (v == ccgSubSurf_getEdgeVert0(e)) {
  184. return edgeBase + (gridSize - 1 - x) - 1;
  185. }
  186. else {
  187. return edgeBase + (edgeSize - 2 - 1) - ((gridSize - 1 - x) - 1);
  188. }
  189. }
  190. else if (x == 0 && y == 0) {
  191. return faceBase;
  192. }
  193. else if (x == 0) {
  194. S = (S + numVerts - 1) % numVerts;
  195. return faceBase + 1 + (gridSize - 2) * S + (y - 1);
  196. }
  197. else if (y == 0) {
  198. return faceBase + 1 + (gridSize - 2) * S + (x - 1);
  199. }
  200. else {
  201. return faceBase + 1 + (gridSize - 2) * numVerts + S * (gridSize - 2) * (gridSize - 2) + (y - 1) * (gridSize - 2) + (x - 1);
  202. }
  203. }
  204. static void get_face_uv_map_vert(UvVertMap *vmap, struct MPoly *mpoly, struct MLoop *ml, int fi, CCGVertHDL *fverts)
  205. {
  206. UvMapVert *v, *nv;
  207. int j, nverts = mpoly[fi].totloop;
  208. for (j = 0; j < nverts; j++) {
  209. for (nv = v = get_uv_map_vert(vmap, ml[j].v); v; v = v->next) {
  210. if (v->separate)
  211. nv = v;
  212. if (v->f == fi)
  213. break;
  214. }
  215. fverts[j] = SET_INT_IN_POINTER(mpoly[nv->f].loopstart + nv->tfindex);
  216. }
  217. }
  218. static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm, MLoopUV *mloopuv)
  219. {
  220. MPoly *mpoly = dm->getPolyArray(dm);
  221. MLoop *mloop = dm->getLoopArray(dm);
  222. MVert *mvert = dm->getVertArray(dm);
  223. int totvert = dm->getNumVerts(dm);
  224. int totface = dm->getNumPolys(dm);
  225. int i, j, seam;
  226. UvMapVert *v;
  227. UvVertMap *vmap;
  228. float limit[2];
  229. CCGVertHDL *fverts = NULL;
  230. BLI_array_declare(fverts);
  231. EdgeHash *ehash;
  232. float creaseFactor = (float)ccgSubSurf_getSubdivisionLevels(ss);
  233. float uv[3] = {0.0f, 0.0f, 0.0f}; /* only first 2 values are written into */
  234. limit[0] = limit[1] = STD_UV_CONNECT_LIMIT;
  235. vmap = make_uv_vert_map(mpoly, mloop, mloopuv, totface, totvert, 0, limit);
  236. if (!vmap)
  237. return 0;
  238. ccgSubSurf_initFullSync(ss);
  239. /* create vertices */
  240. for (i = 0; i < totvert; i++) {
  241. if (!get_uv_map_vert(vmap, i))
  242. continue;
  243. for (v = get_uv_map_vert(vmap, i)->next; v; v = v->next)
  244. if (v->separate)
  245. break;
  246. seam = (v != NULL) || ((mvert + i)->flag & ME_VERT_MERGED);
  247. for (v = get_uv_map_vert(vmap, i); v; v = v->next) {
  248. if (v->separate) {
  249. CCGVert *ssv;
  250. int loopid = mpoly[v->f].loopstart + v->tfindex;
  251. CCGVertHDL vhdl = SET_INT_IN_POINTER(loopid);
  252. copy_v2_v2(uv, mloopuv[loopid].uv);
  253. ccgSubSurf_syncVert(ss, vhdl, uv, seam, &ssv);
  254. }
  255. }
  256. }
  257. /* create edges */
  258. ehash = BLI_edgehash_new();
  259. for (i = 0; i < totface; i++) {
  260. MPoly *mp = &((MPoly *) mpoly)[i];
  261. int nverts = mp->totloop;
  262. CCGFace *origf = ccgSubSurf_getFace(origss, SET_INT_IN_POINTER(i));
  263. /* unsigned int *fv = &mp->v1; */
  264. MLoop *ml = mloop + mp->loopstart;
  265. BLI_array_empty(fverts);
  266. BLI_array_growitems(fverts, nverts);
  267. get_face_uv_map_vert(vmap, mpoly, ml, i, fverts);
  268. for (j = 0; j < nverts; j++) {
  269. int v0 = GET_INT_FROM_POINTER(fverts[j]);
  270. int v1 = GET_INT_FROM_POINTER(fverts[(j + 1) % nverts]);
  271. MVert *mv0 = mvert + (ml[j].v);
  272. MVert *mv1 = mvert + (ml[((j + 1) % nverts)].v);
  273. if (!BLI_edgehash_haskey(ehash, v0, v1)) {
  274. CCGEdge *e, *orige = ccgSubSurf_getFaceEdge(origf, j);
  275. CCGEdgeHDL ehdl = SET_INT_IN_POINTER(mp->loopstart + j);
  276. float crease;
  277. if ((mv0->flag & mv1->flag) & ME_VERT_MERGED)
  278. crease = creaseFactor;
  279. else
  280. crease = ccgSubSurf_getEdgeCrease(orige);
  281. ccgSubSurf_syncEdge(ss, ehdl, fverts[j], fverts[(j + 1) % nverts], crease, &e);
  282. BLI_edgehash_insert(ehash, v0, v1, NULL);
  283. }
  284. }
  285. }
  286. BLI_edgehash_free(ehash, NULL);
  287. /* create faces */
  288. for (i = 0; i < totface; i++) {
  289. MPoly *mp = &mpoly[i];
  290. MLoop *ml = &mloop[mp->loopstart];
  291. int nverts = mp->totloop;
  292. CCGFace *f;
  293. BLI_array_empty(fverts);
  294. BLI_array_growitems(fverts, nverts);
  295. get_face_uv_map_vert(vmap, mpoly, ml, i, fverts);
  296. ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), nverts, fverts, &f);
  297. }
  298. BLI_array_free(fverts);
  299. free_uv_vert_map(vmap);
  300. ccgSubSurf_processSync(ss);
  301. return 1;
  302. }
  303. static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, int n)
  304. {
  305. CCGSubSurf *uvss;
  306. CCGFace **faceMap;
  307. MTFace *tf;
  308. MLoopUV *mluv;
  309. CCGFaceIterator *fi;
  310. int index, gridSize, gridFaces, /*edgeSize,*/ totface, x, y, S;
  311. MLoopUV *dmloopuv = CustomData_get_layer_n(&dm->loopData, CD_MLOOPUV, n);
  312. /* need to update both CD_MTFACE & CD_MLOOPUV, hrmf, we could get away with
  313. * just tface except applying the modifier then looses subsurf UV */
  314. MTFace *tface = CustomData_get_layer_n(&result->faceData, CD_MTFACE, n);
  315. MLoopUV *mloopuv = CustomData_get_layer_n(&result->loopData, CD_MLOOPUV, n);
  316. if (!dmloopuv || (!tface && !mloopuv))
  317. return;
  318. /* create a CCGSubSurf from uv's */
  319. uvss = _getSubSurf(NULL, ccgSubSurf_getSubdivisionLevels(ss), CCG_USE_ARENA);
  320. if (!ss_sync_from_uv(uvss, ss, dm, dmloopuv)) {
  321. ccgSubSurf_free(uvss);
  322. return;
  323. }
  324. /* get some info from CCGSubSurf */
  325. totface = ccgSubSurf_getNumFaces(uvss);
  326. /* edgeSize = ccgSubSurf_getEdgeSize(uvss); */ /*UNUSED*/
  327. gridSize = ccgSubSurf_getGridSize(uvss);
  328. gridFaces = gridSize - 1;
  329. /* make a map from original faces to CCGFaces */
  330. faceMap = MEM_mallocN(totface * sizeof(*faceMap), "facemapuv");
  331. fi = ccgSubSurf_getFaceIterator(uvss);
  332. for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
  333. CCGFace *f = ccgFaceIterator_getCurrent(fi);
  334. faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f))] = f;
  335. }
  336. ccgFaceIterator_free(fi);
  337. /* load coordinates from uvss into tface */
  338. tf = tface;
  339. mluv = mloopuv;
  340. for (index = 0; index < totface; index++) {
  341. CCGFace *f = faceMap[index];
  342. int numVerts = ccgSubSurf_getFaceNumVerts(f);
  343. for (S = 0; S < numVerts; S++) {
  344. float (*faceGridData)[3] = ccgSubSurf_getFaceGridDataArray(uvss, f, S);
  345. for (y = 0; y < gridFaces; y++) {
  346. for (x = 0; x < gridFaces; x++) {
  347. float *a = faceGridData[(y + 0) * gridSize + x + 0];
  348. float *b = faceGridData[(y + 0) * gridSize + x + 1];
  349. float *c = faceGridData[(y + 1) * gridSize + x + 1];
  350. float *d = faceGridData[(y + 1) * gridSize + x + 0];
  351. if (tf) {
  352. copy_v2_v2(tf->uv[0], a);
  353. copy_v2_v2(tf->uv[1], d);
  354. copy_v2_v2(tf->uv[2], c);
  355. copy_v2_v2(tf->uv[3], b);
  356. tf++;
  357. }
  358. if (mluv) {
  359. copy_v2_v2(mluv[0].uv, a);
  360. copy_v2_v2(mluv[1].uv, d);
  361. copy_v2_v2(mluv[2].uv, c);
  362. copy_v2_v2(mluv[3].uv, b);
  363. mluv += 4;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. ccgSubSurf_free(uvss);
  370. MEM_freeN(faceMap);
  371. }
  372. /* face weighting */
  373. typedef struct FaceVertWeightEntry {
  374. FaceVertWeight *weight;
  375. float *w;
  376. int valid;
  377. } FaceVertWeightEntry;
  378. typedef struct WeightTable {
  379. FaceVertWeightEntry *weight_table;
  380. int len;
  381. } WeightTable;
  382. static float *get_ss_weights(WeightTable *wtable, int gridCuts, int faceLen)
  383. {
  384. int x, y, i, j;
  385. float *w, w1, w2, w4, fac, fac2, fx, fy;
  386. if (wtable->len <= faceLen) {
  387. void *tmp = MEM_callocN(sizeof(FaceVertWeightEntry) * (faceLen + 1), "weight table alloc 2");
  388. if (wtable->len) {
  389. memcpy(tmp, wtable->weight_table, sizeof(FaceVertWeightEntry) * wtable->len);
  390. MEM_freeN(wtable->weight_table);
  391. }
  392. wtable->weight_table = tmp;
  393. wtable->len = faceLen + 1;
  394. }
  395. if (!wtable->weight_table[faceLen].valid) {
  396. wtable->weight_table[faceLen].valid = 1;
  397. wtable->weight_table[faceLen].w = w = MEM_callocN(sizeof(float) * faceLen * faceLen * (gridCuts + 2) * (gridCuts + 2), "weight table alloc");
  398. fac = 1.0f / (float)faceLen;
  399. for (i = 0; i < faceLen; i++) {
  400. for (x = 0; x < gridCuts + 2; x++) {
  401. for (y = 0; y < gridCuts + 2; y++) {
  402. fx = 0.5f - (float)x / (float)(gridCuts + 1) / 2.0f;
  403. fy = 0.5f - (float)y / (float)(gridCuts + 1) / 2.0f;
  404. fac2 = faceLen - 4;
  405. w1 = (1.0f - fx) * (1.0f - fy) + (-fac2 * fx * fy * fac);
  406. w2 = (1.0f - fx + fac2 * fx * -fac) * (fy);
  407. w4 = (fx) * (1.0f - fy + -fac2 * fy * fac);
  408. fac2 = 1.0f - (w1 + w2 + w4);
  409. fac2 = fac2 / (float)(faceLen - 3);
  410. for (j = 0; j < faceLen; j++)
  411. w[j] = fac2;
  412. w[i] = w1;
  413. w[(i - 1 + faceLen) % faceLen] = w2;
  414. w[(i + 1) % faceLen] = w4;
  415. w += faceLen;
  416. }
  417. }
  418. }
  419. }
  420. return wtable->weight_table[faceLen].w;
  421. }
  422. static void free_ss_weights(WeightTable *wtable)
  423. {
  424. int i;
  425. for (i = 0; i < wtable->len; i++) {
  426. if (wtable->weight_table[i].valid)
  427. MEM_freeN(wtable->weight_table[i].w);
  428. }
  429. if (wtable->weight_table)
  430. MEM_freeN(wtable->weight_table);
  431. }
  432. static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
  433. float (*vertexCos)[3], int useFlatSubdiv)
  434. {
  435. float creaseFactor = (float) ccgSubSurf_getSubdivisionLevels(ss);
  436. CCGVertHDL *fVerts = NULL;
  437. BLI_array_declare(fVerts);
  438. MVert *mvert = dm->getVertArray(dm);
  439. MEdge *medge = dm->getEdgeArray(dm);
  440. /* MFace *mface = dm->getTessFaceArray(dm); */ /* UNUSED */
  441. MVert *mv;
  442. MEdge *me;
  443. MLoop *mloop = dm->getLoopArray(dm), *ml;
  444. MPoly *mpoly = dm->getPolyArray(dm), *mp;
  445. /*MFace *mf;*/ /*UNUSED*/
  446. int totvert = dm->getNumVerts(dm);
  447. int totedge = dm->getNumEdges(dm);
  448. /*int totface = dm->getNumTessFaces(dm);*/ /*UNUSED*/
  449. /*int totpoly = dm->getNumFaces(dm);*/ /*UNUSED*/
  450. int i, j;
  451. int *index;
  452. ccgSubSurf_initFullSync(ss);
  453. mv = mvert;
  454. index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
  455. for (i = 0; i < totvert; i++, mv++) {
  456. CCGVert *v;
  457. if (vertexCos) {
  458. ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), vertexCos[i], 0, &v);
  459. }
  460. else {
  461. ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), mv->co, 0, &v);
  462. }
  463. ((int *)ccgSubSurf_getVertUserData(ss, v))[1] = (index) ? *index++ : i;
  464. }
  465. me = medge;
  466. index = (int *)dm->getEdgeDataArray(dm, CD_ORIGINDEX);
  467. for (i = 0; i < totedge; i++, me++) {
  468. CCGEdge *e;
  469. float crease;
  470. crease = useFlatSubdiv ? creaseFactor :
  471. me->crease * creaseFactor / 255.0f;
  472. ccgSubSurf_syncEdge(ss, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(me->v1),
  473. SET_INT_IN_POINTER(me->v2), crease, &e);
  474. ((int *)ccgSubSurf_getEdgeUserData(ss, e))[1] = (index) ? *index++ : i;
  475. }
  476. mp = mpoly;
  477. index = DM_get_poly_data_layer(dm, CD_ORIGINDEX);
  478. for (i = 0; i < dm->numPolyData; i++, mp++) {
  479. CCGFace *f;
  480. BLI_array_empty(fVerts);
  481. BLI_array_growitems(fVerts, mp->totloop);
  482. ml = mloop + mp->loopstart;
  483. for (j = 0; j < mp->totloop; j++, ml++) {
  484. fVerts[j] = SET_INT_IN_POINTER(ml->v);
  485. }
  486. /* this is very bad, means mesh is internally inconsistent.
  487. * it is not really possible to continue without modifying
  488. * other parts of code significantly to handle missing faces.
  489. * since this really shouldn't even be possible we just bail.*/
  490. if (ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), mp->totloop,
  491. fVerts, &f) == eCCGError_InvalidValue) {
  492. static int hasGivenError = 0;
  493. if (!hasGivenError) {
  494. //XXX error("Unrecoverable error in SubSurf calculation,"
  495. // " mesh is inconsistent.");
  496. hasGivenError = 1;
  497. }
  498. return;
  499. }
  500. ((int *)ccgSubSurf_getFaceUserData(ss, f))[1] = (index) ? *index++ : i;
  501. }
  502. ccgSubSurf_processSync(ss);
  503. BLI_array_free(fVerts);
  504. }
  505. /***/
  506. static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v)
  507. {
  508. return ((int *) ccgSubSurf_getVertUserData(ss, v))[1];
  509. }
  510. static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e)
  511. {
  512. return ((int *) ccgSubSurf_getEdgeUserData(ss, e))[1];
  513. }
  514. static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f)
  515. {
  516. return ((int *) ccgSubSurf_getFaceUserData(ss, f))[1];
  517. }
  518. static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
  519. {
  520. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  521. CCGSubSurf *ss = ccgdm->ss;
  522. CCGVertIterator *vi = ccgSubSurf_getVertIterator(ss);
  523. CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
  524. CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
  525. int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
  526. int gridSize = ccgSubSurf_getGridSize(ss);
  527. if (!ccgSubSurf_getNumVerts(ss))
  528. min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
  529. for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
  530. CCGVert *v = ccgVertIterator_getCurrent(vi);
  531. float *co = ccgSubSurf_getVertData(ss, v);
  532. DO_MINMAX(co, min_r, max_r);
  533. }
  534. for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
  535. CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
  536. DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
  537. for (i = 0; i < edgeSize; i++)
  538. DO_MINMAX(edgeData[i].co, min_r, max_r);
  539. }
  540. for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
  541. CCGFace *f = ccgFaceIterator_getCurrent(fi);
  542. int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
  543. for (S = 0; S < numVerts; S++) {
  544. DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  545. for (y = 0; y < gridSize; y++)
  546. for (x = 0; x < gridSize; x++)
  547. DO_MINMAX(faceGridData[y * gridSize + x].co, min_r, max_r);
  548. }
  549. }
  550. ccgFaceIterator_free(fi);
  551. ccgEdgeIterator_free(ei);
  552. ccgVertIterator_free(vi);
  553. }
  554. static int ccgDM_getNumVerts(DerivedMesh *dm)
  555. {
  556. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  557. return ccgSubSurf_getNumFinalVerts(ccgdm->ss);
  558. }
  559. static int ccgDM_getNumEdges(DerivedMesh *dm)
  560. {
  561. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  562. return ccgSubSurf_getNumFinalEdges(ccgdm->ss);
  563. }
  564. static int ccgDM_getNumTessFaces(DerivedMesh *dm)
  565. {
  566. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  567. return ccgSubSurf_getNumFinalFaces(ccgdm->ss);
  568. }
  569. static int ccgDM_getNumLoops(DerivedMesh *dm)
  570. {
  571. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  572. /* All subsurf faces are quads */
  573. return 4 * ccgSubSurf_getNumFinalFaces(ccgdm->ss);
  574. }
  575. static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
  576. {
  577. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  578. CCGSubSurf *ss = ccgdm->ss;
  579. DMGridData *vd;
  580. int i;
  581. memset(mv, 0, sizeof(*mv));
  582. if ((vertNum < ccgdm->edgeMap[0].startVert) && (ccgSubSurf_getNumFaces(ss) > 0)) {
  583. /* this vert comes from face data */
  584. int lastface = ccgSubSurf_getNumFaces(ss) - 1;
  585. CCGFace *f;
  586. int x, y, grid, numVerts;
  587. int offset;
  588. int gridSize = ccgSubSurf_getGridSize(ss);
  589. int gridSideVerts;
  590. int gridInternalVerts;
  591. int gridSideEnd;
  592. int gridInternalEnd;
  593. i = 0;
  594. while (i < lastface && vertNum >= ccgdm->faceMap[i + 1].startVert)
  595. ++i;
  596. f = ccgdm->faceMap[i].face;
  597. numVerts = ccgSubSurf_getFaceNumVerts(f);
  598. gridSideVerts = gridSize - 2;
  599. gridInternalVerts = gridSideVerts * gridSideVerts;
  600. gridSideEnd = 1 + numVerts * gridSideVerts;
  601. gridInternalEnd = gridSideEnd + numVerts * gridInternalVerts;
  602. offset = vertNum - ccgdm->faceMap[i].startVert;
  603. if (offset < 1) {
  604. vd = ccgSubSurf_getFaceCenterData(f);
  605. copy_v3_v3(mv->co, vd->co);
  606. normal_float_to_short_v3(mv->no, vd->no);
  607. }
  608. else if (offset < gridSideEnd) {
  609. offset -= 1;
  610. grid = offset / gridSideVerts;
  611. x = offset % gridSideVerts + 1;
  612. vd = ccgSubSurf_getFaceGridEdgeData(ss, f, grid, x);
  613. copy_v3_v3(mv->co, vd->co);
  614. normal_float_to_short_v3(mv->no, vd->no);
  615. }
  616. else if (offset < gridInternalEnd) {
  617. offset -= gridSideEnd;
  618. grid = offset / gridInternalVerts;
  619. offset %= gridInternalVerts;
  620. y = offset / gridSideVerts + 1;
  621. x = offset % gridSideVerts + 1;
  622. vd = ccgSubSurf_getFaceGridData(ss, f, grid, x, y);
  623. copy_v3_v3(mv->co, vd->co);
  624. normal_float_to_short_v3(mv->no, vd->no);
  625. }
  626. }
  627. else if ((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) {
  628. /* this vert comes from edge data */
  629. CCGEdge *e;
  630. int lastedge = ccgSubSurf_getNumEdges(ss) - 1;
  631. int x;
  632. i = 0;
  633. while (i < lastedge && vertNum >= ccgdm->edgeMap[i + 1].startVert)
  634. ++i;
  635. e = ccgdm->edgeMap[i].edge;
  636. x = vertNum - ccgdm->edgeMap[i].startVert + 1;
  637. vd = ccgSubSurf_getEdgeData(ss, e, x);
  638. copy_v3_v3(mv->co, vd->co);
  639. normal_float_to_short_v3(mv->no, vd->no);
  640. }
  641. else {
  642. /* this vert comes from vert data */
  643. CCGVert *v;
  644. i = vertNum - ccgdm->vertMap[0].startVert;
  645. v = ccgdm->vertMap[i].vert;
  646. vd = ccgSubSurf_getVertData(ss, v);
  647. copy_v3_v3(mv->co, vd->co);
  648. normal_float_to_short_v3(mv->no, vd->no);
  649. }
  650. }
  651. static void ccgDM_getFinalVertCo(DerivedMesh *dm, int vertNum, float co_r[3])
  652. {
  653. MVert mvert;
  654. ccgDM_getFinalVert(dm, vertNum, &mvert);
  655. copy_v3_v3(co_r, mvert.co);
  656. }
  657. static void ccgDM_getFinalVertNo(DerivedMesh *dm, int vertNum, float no_r[3])
  658. {
  659. MVert mvert;
  660. ccgDM_getFinalVert(dm, vertNum, &mvert);
  661. normal_short_to_float_v3(no_r, mvert.no);
  662. }
  663. static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med)
  664. {
  665. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  666. CCGSubSurf *ss = ccgdm->ss;
  667. int i;
  668. memset(med, 0, sizeof(*med));
  669. if (edgeNum < ccgdm->edgeMap[0].startEdge) {
  670. /* this edge comes from face data */
  671. int lastface = ccgSubSurf_getNumFaces(ss) - 1;
  672. CCGFace *f;
  673. int x, y, grid /*, numVerts*/;
  674. int offset;
  675. int gridSize = ccgSubSurf_getGridSize(ss);
  676. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  677. int gridSideEdges;
  678. int gridInternalEdges;
  679. /* code added in bmesh but works correctly without, commenting - campbell */
  680. #if 0
  681. int lasti, previ;
  682. i = lastface;
  683. lasti = 0;
  684. while (1) {
  685. previ = i;
  686. if (ccgdm->faceMap[i].startEdge >= edgeNum) {
  687. i -= fabsf(i - lasti) / 2.0f;
  688. }
  689. else if (ccgdm->faceMap[i].startEdge < edgeNum) {
  690. i += fabsf(i - lasti) / 2.0f;
  691. }
  692. else {
  693. break;
  694. }
  695. if (i < 0) {
  696. i = 0;
  697. break;
  698. }
  699. if (i > lastface) {
  700. i = lastface;
  701. break;
  702. }
  703. if (i == lasti)
  704. break;
  705. lasti = previ;
  706. }
  707. i = i > 0 ? i - 1 : i;
  708. #endif
  709. i = 0;
  710. while (i < lastface && edgeNum >= ccgdm->faceMap[i + 1].startEdge)
  711. ++i;
  712. f = ccgdm->faceMap[i].face;
  713. /* numVerts = ccgSubSurf_getFaceNumVerts(f); */ /*UNUSED*/
  714. gridSideEdges = gridSize - 1;
  715. gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2;
  716. offset = edgeNum - ccgdm->faceMap[i].startEdge;
  717. grid = offset / (gridSideEdges + gridInternalEdges);
  718. offset %= (gridSideEdges + gridInternalEdges);
  719. if (offset < gridSideEdges) {
  720. x = offset;
  721. med->v1 = getFaceIndex(ss, f, grid, x, 0, edgeSize, gridSize);
  722. med->v2 = getFaceIndex(ss, f, grid, x + 1, 0, edgeSize, gridSize);
  723. }
  724. else {
  725. offset -= gridSideEdges;
  726. x = (offset / 2) / gridSideEdges + 1;
  727. y = (offset / 2) % gridSideEdges;
  728. if (offset % 2 == 0) {
  729. med->v1 = getFaceIndex(ss, f, grid, x, y, edgeSize, gridSize);
  730. med->v2 = getFaceIndex(ss, f, grid, x, y + 1, edgeSize, gridSize);
  731. }
  732. else {
  733. med->v1 = getFaceIndex(ss, f, grid, y, x, edgeSize, gridSize);
  734. med->v2 = getFaceIndex(ss, f, grid, y + 1, x, edgeSize, gridSize);
  735. }
  736. }
  737. }
  738. else {
  739. /* this vert comes from edge data */
  740. CCGEdge *e;
  741. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  742. int x;
  743. short *edgeFlag;
  744. unsigned int flags = 0;
  745. i = (edgeNum - ccgdm->edgeMap[0].startEdge) / (edgeSize - 1);
  746. e = ccgdm->edgeMap[i].edge;
  747. if (!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
  748. x = edgeNum - ccgdm->edgeMap[i].startEdge;
  749. med->v1 = getEdgeIndex(ss, e, x, edgeSize);
  750. med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
  751. edgeFlag = (ccgdm->edgeFlags) ? &ccgdm->edgeFlags[i] : NULL;
  752. if (edgeFlag)
  753. flags |= (*edgeFlag & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER;
  754. else
  755. flags |= ME_EDGEDRAW | ME_EDGERENDER;
  756. med->flag = flags;
  757. }
  758. }
  759. static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf)
  760. {
  761. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  762. CCGSubSurf *ss = ccgdm->ss;
  763. int gridSize = ccgSubSurf_getGridSize(ss);
  764. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  765. int gridSideEdges = gridSize - 1;
  766. int gridFaces = gridSideEdges * gridSideEdges;
  767. int i;
  768. CCGFace *f;
  769. /*int numVerts;*/
  770. int offset;
  771. int grid;
  772. int x, y;
  773. /*int lastface = ccgSubSurf_getNumFaces(ss) - 1;*/ /*UNUSED*/
  774. DMFlagMat *faceFlags = ccgdm->faceFlags;
  775. memset(mf, 0, sizeof(*mf));
  776. if (faceNum >= ccgdm->dm.numTessFaceData)
  777. return;
  778. i = ccgdm->reverseFaceMap[faceNum];
  779. f = ccgdm->faceMap[i].face;
  780. /*numVerts = ccgSubSurf_getFaceNumVerts(f);*/ /*UNUSED*/
  781. offset = faceNum - ccgdm->faceMap[i].startFace;
  782. grid = offset / gridFaces;
  783. offset %= gridFaces;
  784. y = offset / gridSideEdges;
  785. x = offset % gridSideEdges;
  786. mf->v1 = getFaceIndex(ss, f, grid, x + 0, y + 0, edgeSize, gridSize);
  787. mf->v2 = getFaceIndex(ss, f, grid, x + 0, y + 1, edgeSize, gridSize);
  788. mf->v3 = getFaceIndex(ss, f, grid, x + 1, y + 1, edgeSize, gridSize);
  789. mf->v4 = getFaceIndex(ss, f, grid, x + 1, y + 0, edgeSize, gridSize);
  790. if (faceFlags) {
  791. mf->flag = faceFlags[i].flag;
  792. mf->mat_nr = faceFlags[i].mat_nr;
  793. }
  794. else mf->flag = ME_SMOOTH;
  795. }
  796. /* Translate GridHidden into the ME_HIDE flag for MVerts. Assumes
  797. * vertices are in the order output by ccgDM_copyFinalVertArray. */
  798. void subsurf_copy_grid_hidden(DerivedMesh *dm, const MPoly *mpoly,
  799. MVert *mvert, const MDisps *mdisps)
  800. {
  801. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  802. CCGSubSurf *ss = ccgdm->ss;
  803. int level = ccgSubSurf_getSubdivisionLevels(ss);
  804. int gridSize = ccgSubSurf_getGridSize(ss);
  805. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  806. int totface = ccgSubSurf_getNumFaces(ss);
  807. int i, j, x, y;
  808. for (i = 0; i < totface; i++) {
  809. CCGFace *f = ccgdm->faceMap[i].face;
  810. for (j = 0; j < mpoly[i].totloop; j++) {
  811. const MDisps *md = &mdisps[mpoly[i].loopstart + j];
  812. int hidden_gridsize = ccg_gridsize(md->level);
  813. int factor = ccg_factor(level, md->level);
  814. if (!md->hidden)
  815. continue;
  816. for (y = 0; y < gridSize; y++) {
  817. for (x = 0; x < gridSize; x++) {
  818. int vndx, offset;
  819. vndx = getFaceIndex(ss, f, j, x, y, edgeSize, gridSize);
  820. offset = (y * factor) * hidden_gridsize + (x * factor);
  821. if (BLI_BITMAP_GET(md->hidden, offset))
  822. mvert[vndx].flag |= ME_HIDE;
  823. }
  824. }
  825. }
  826. }
  827. }
  828. static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
  829. {
  830. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  831. CCGSubSurf *ss = ccgdm->ss;
  832. DMGridData *vd;
  833. int index;
  834. int totvert, totedge, totface;
  835. int gridSize = ccgSubSurf_getGridSize(ss);
  836. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  837. int i = 0;
  838. totface = ccgSubSurf_getNumFaces(ss);
  839. for (index = 0; index < totface; index++) {
  840. CCGFace *f = ccgdm->faceMap[index].face;
  841. int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
  842. vd = ccgSubSurf_getFaceCenterData(f);
  843. copy_v3_v3(mvert[i].co, vd->co);
  844. normal_float_to_short_v3(mvert[i].no, vd->no);
  845. i++;
  846. for (S = 0; S < numVerts; S++) {
  847. for (x = 1; x < gridSize - 1; x++, i++) {
  848. vd = ccgSubSurf_getFaceGridEdgeData(ss, f, S, x);
  849. copy_v3_v3(mvert[i].co, vd->co);
  850. normal_float_to_short_v3(mvert[i].no, vd->no);
  851. }
  852. }
  853. for (S = 0; S < numVerts; S++) {
  854. for (y = 1; y < gridSize - 1; y++) {
  855. for (x = 1; x < gridSize - 1; x++, i++) {
  856. vd = ccgSubSurf_getFaceGridData(ss, f, S, x, y);
  857. copy_v3_v3(mvert[i].co, vd->co);
  858. normal_float_to_short_v3(mvert[i].no, vd->no);
  859. }
  860. }
  861. }
  862. }
  863. totedge = ccgSubSurf_getNumEdges(ss);
  864. for (index = 0; index < totedge; index++) {
  865. CCGEdge *e = ccgdm->edgeMap[index].edge;
  866. int x;
  867. for (x = 1; x < edgeSize - 1; x++, i++) {
  868. vd = ccgSubSurf_getEdgeData(ss, e, x);
  869. copy_v3_v3(mvert[i].co, vd->co);
  870. /* This gives errors with -debug-fpe
  871. * the normals don't seem to be unit length.
  872. * this is most likely caused by edges with no
  873. * faces which are now zerod out, see comment in:
  874. * ccgSubSurf__calcVertNormals(), - campbell */
  875. normal_float_to_short_v3(mvert[i].no, vd->no);
  876. }
  877. }
  878. totvert = ccgSubSurf_getNumVerts(ss);
  879. for (index = 0; index < totvert; index++) {
  880. CCGVert *v = ccgdm->vertMap[index].vert;
  881. vd = ccgSubSurf_getVertData(ss, v);
  882. copy_v3_v3(mvert[i].co, vd->co);
  883. normal_float_to_short_v3(mvert[i].no, vd->no);
  884. i++;
  885. }
  886. }
  887. static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
  888. {
  889. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  890. CCGSubSurf *ss = ccgdm->ss;
  891. int index;
  892. int totedge, totface;
  893. int gridSize = ccgSubSurf_getGridSize(ss);
  894. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  895. int i = 0;
  896. short *edgeFlags = ccgdm->edgeFlags;
  897. totface = ccgSubSurf_getNumFaces(ss);
  898. for (index = 0; index < totface; index++) {
  899. CCGFace *f = ccgdm->faceMap[index].face;
  900. int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
  901. for (S = 0; S < numVerts; S++) {
  902. for (x = 0; x < gridSize - 1; x++) {
  903. MEdge *med = &medge[i];
  904. if (ccgdm->drawInteriorEdges)
  905. med->flag = ME_EDGEDRAW | ME_EDGERENDER;
  906. med->v1 = getFaceIndex(ss, f, S, x, 0, edgeSize, gridSize);
  907. med->v2 = getFaceIndex(ss, f, S, x + 1, 0, edgeSize, gridSize);
  908. i++;
  909. }
  910. for (x = 1; x < gridSize - 1; x++) {
  911. for (y = 0; y < gridSize - 1; y++) {
  912. MEdge *med;
  913. med = &medge[i];
  914. if (ccgdm->drawInteriorEdges)
  915. med->flag = ME_EDGEDRAW | ME_EDGERENDER;
  916. med->v1 = getFaceIndex(ss, f, S, x, y,
  917. edgeSize, gridSize);
  918. med->v2 = getFaceIndex(ss, f, S, x, y + 1,
  919. edgeSize, gridSize);
  920. i++;
  921. med = &medge[i];
  922. if (ccgdm->drawInteriorEdges)
  923. med->flag = ME_EDGEDRAW | ME_EDGERENDER;
  924. med->v1 = getFaceIndex(ss, f, S, y, x,
  925. edgeSize, gridSize);
  926. med->v2 = getFaceIndex(ss, f, S, y + 1, x,
  927. edgeSize, gridSize);
  928. i++;
  929. }
  930. }
  931. }
  932. }
  933. totedge = ccgSubSurf_getNumEdges(ss);
  934. for (index = 0; index < totedge; index++) {
  935. CCGEdge *e = ccgdm->edgeMap[index].edge;
  936. unsigned int flags = 0;
  937. int x;
  938. int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
  939. if (!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
  940. if (edgeFlags) {
  941. if (edgeIdx != -1) {
  942. flags |= (edgeFlags[index] & (ME_SEAM | ME_SHARP))
  943. | ME_EDGEDRAW | ME_EDGERENDER;
  944. }
  945. }
  946. else {
  947. flags |= ME_EDGEDRAW | ME_EDGERENDER;
  948. }
  949. for (x = 0; x < edgeSize - 1; x++) {
  950. MEdge *med = &medge[i];
  951. med->v1 = getEdgeIndex(ss, e, x, edgeSize);
  952. med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
  953. med->flag = flags;
  954. i++;
  955. }
  956. }
  957. }
  958. static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface)
  959. {
  960. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  961. CCGSubSurf *ss = ccgdm->ss;
  962. int index;
  963. int totface;
  964. int gridSize = ccgSubSurf_getGridSize(ss);
  965. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  966. int i = 0;
  967. DMFlagMat *faceFlags = ccgdm->faceFlags;
  968. totface = ccgSubSurf_getNumFaces(ss);
  969. for (index = 0; index < totface; index++) {
  970. CCGFace *f = ccgdm->faceMap[index].face;
  971. int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
  972. /* keep types in sync with MFace, avoid many conversions */
  973. char flag = (faceFlags) ? faceFlags[index].flag : ME_SMOOTH;
  974. short mat_nr = (faceFlags) ? faceFlags[index].mat_nr : 0;
  975. for (S = 0; S < numVerts; S++) {
  976. for (y = 0; y < gridSize - 1; y++) {
  977. for (x = 0; x < gridSize - 1; x++) {
  978. MFace *mf = &mface[i];
  979. mf->v1 = getFaceIndex(ss, f, S, x + 0, y + 0,
  980. edgeSize, gridSize);
  981. mf->v2 = getFaceIndex(ss, f, S, x + 0, y + 1,
  982. edgeSize, gridSize);
  983. mf->v3 = getFaceIndex(ss, f, S, x + 1, y + 1,
  984. edgeSize, gridSize);
  985. mf->v4 = getFaceIndex(ss, f, S, x + 1, y + 0,
  986. edgeSize, gridSize);
  987. mf->mat_nr = mat_nr;
  988. mf->flag = flag;
  989. i++;
  990. }
  991. }
  992. }
  993. }
  994. }
  995. static void ccgDM_copyFinalLoopArray(DerivedMesh *dm, MLoop *mloop)
  996. {
  997. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  998. CCGSubSurf *ss = ccgdm->ss;
  999. int index;
  1000. int totface;
  1001. int gridSize = ccgSubSurf_getGridSize(ss);
  1002. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  1003. int i = 0;
  1004. MLoop *mv;
  1005. /* DMFlagMat *faceFlags = ccgdm->faceFlags; */ /* UNUSED */
  1006. if (!ccgdm->ehash) {
  1007. MEdge *medge;
  1008. ccgdm->ehash = BLI_edgehash_new();
  1009. medge = ccgdm->dm.getEdgeArray((DerivedMesh *)ccgdm);
  1010. for (i = 0; i < ccgdm->dm.numEdgeData; i++) {
  1011. BLI_edgehash_insert(ccgdm->ehash, medge[i].v1, medge[i].v2, SET_INT_IN_POINTER(i));
  1012. }
  1013. }
  1014. totface = ccgSubSurf_getNumFaces(ss);
  1015. mv = mloop;
  1016. for (index = 0; index < totface; index++) {
  1017. CCGFace *f = ccgdm->faceMap[index].face;
  1018. int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1019. /* int flag = (faceFlags)? faceFlags[index*2]: ME_SMOOTH; */ /* UNUSED */
  1020. /* int mat_nr = (faceFlags)? faceFlags[index*2+1]: 0; */ /* UNUSED */
  1021. for (S = 0; S < numVerts; S++) {
  1022. for (y = 0; y < gridSize - 1; y++) {
  1023. for (x = 0; x < gridSize - 1; x++) {
  1024. int v1, v2, v3, v4;
  1025. v1 = getFaceIndex(ss, f, S, x + 0, y + 0,
  1026. edgeSize, gridSize);
  1027. v2 = getFaceIndex(ss, f, S, x + 0, y + 1,
  1028. edgeSize, gridSize);
  1029. v3 = getFaceIndex(ss, f, S, x + 1, y + 1,
  1030. edgeSize, gridSize);
  1031. v4 = getFaceIndex(ss, f, S, x + 1, y + 0,
  1032. edgeSize, gridSize);
  1033. mv->v = v1;
  1034. mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v1, v2));
  1035. mv++, i++;
  1036. mv->v = v2;
  1037. mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v2, v3));
  1038. mv++, i++;
  1039. mv->v = v3;
  1040. mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v3, v4));
  1041. mv++, i++;
  1042. mv->v = v4;
  1043. mv->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(ccgdm->ehash, v4, v1));
  1044. mv++, i++;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. }
  1050. static void ccgDM_copyFinalPolyArray(DerivedMesh *dm, MPoly *mpoly)
  1051. {
  1052. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1053. CCGSubSurf *ss = ccgdm->ss;
  1054. int index;
  1055. int totface;
  1056. int gridSize = ccgSubSurf_getGridSize(ss);
  1057. /* int edgeSize = ccgSubSurf_getEdgeSize(ss); */ /* UNUSED */
  1058. int i = 0, k = 0;
  1059. DMFlagMat *faceFlags = ccgdm->faceFlags;
  1060. totface = ccgSubSurf_getNumFaces(ss);
  1061. for (index = 0; index < totface; index++) {
  1062. CCGFace *f = ccgdm->faceMap[index].face;
  1063. int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1064. int flag = (faceFlags) ? faceFlags[index].flag : ME_SMOOTH;
  1065. int mat_nr = (faceFlags) ? faceFlags[index].mat_nr : 0;
  1066. for (S = 0; S < numVerts; S++) {
  1067. for (y = 0; y < gridSize - 1; y++) {
  1068. for (x = 0; x < gridSize - 1; x++) {
  1069. MPoly *mp = &mpoly[i];
  1070. mp->mat_nr = mat_nr;
  1071. mp->flag = flag;
  1072. mp->loopstart = k;
  1073. mp->totloop = 4;
  1074. k += 4;
  1075. i++;
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3])
  1082. {
  1083. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1084. CCGSubSurf *ss = ccgdm->ss;
  1085. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  1086. int gridSize = ccgSubSurf_getGridSize(ss);
  1087. int i;
  1088. CCGVertIterator *vi;
  1089. CCGEdgeIterator *ei;
  1090. CCGFaceIterator *fi;
  1091. CCGFace **faceMap2;
  1092. CCGEdge **edgeMap2;
  1093. CCGVert **vertMap2;
  1094. int index, totvert, totedge, totface;
  1095. totvert = ccgSubSurf_getNumVerts(ss);
  1096. vertMap2 = MEM_mallocN(totvert * sizeof(*vertMap2), "vertmap");
  1097. vi = ccgSubSurf_getVertIterator(ss);
  1098. for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
  1099. CCGVert *v = ccgVertIterator_getCurrent(vi);
  1100. vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v;
  1101. }
  1102. ccgVertIterator_free(vi);
  1103. totedge = ccgSubSurf_getNumEdges(ss);
  1104. edgeMap2 = MEM_mallocN(totedge * sizeof(*edgeMap2), "edgemap");
  1105. ei = ccgSubSurf_getEdgeIterator(ss);
  1106. for (i = 0; !ccgEdgeIterator_isStopped(ei); i++, ccgEdgeIterator_next(ei)) {
  1107. CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
  1108. edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e;
  1109. }
  1110. totface = ccgSubSurf_getNumFaces(ss);
  1111. faceMap2 = MEM_mallocN(totface * sizeof(*faceMap2), "facemap");
  1112. fi = ccgSubSurf_getFaceIterator(ss);
  1113. for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
  1114. CCGFace *f = ccgFaceIterator_getCurrent(fi);
  1115. faceMap2[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f))] = f;
  1116. }
  1117. ccgFaceIterator_free(fi);
  1118. i = 0;
  1119. for (index = 0; index < totface; index++) {
  1120. CCGFace *f = faceMap2[index];
  1121. int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1122. copy_v3_v3(cos[i++], ccgSubSurf_getFaceCenterData(f));
  1123. for (S = 0; S < numVerts; S++) {
  1124. for (x = 1; x < gridSize - 1; x++) {
  1125. copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
  1126. }
  1127. }
  1128. for (S = 0; S < numVerts; S++) {
  1129. for (y = 1; y < gridSize - 1; y++) {
  1130. for (x = 1; x < gridSize - 1; x++) {
  1131. copy_v3_v3(cos[i++], ccgSubSurf_getFaceGridData(ss, f, S, x, y));
  1132. }
  1133. }
  1134. }
  1135. }
  1136. for (index = 0; index < totedge; index++) {
  1137. CCGEdge *e = edgeMap2[index];
  1138. int x;
  1139. for (x = 1; x < edgeSize - 1; x++) {
  1140. copy_v3_v3(cos[i++], ccgSubSurf_getEdgeData(ss, e, x));
  1141. }
  1142. }
  1143. for (index = 0; index < totvert; index++) {
  1144. CCGVert *v = vertMap2[index];
  1145. copy_v3_v3(cos[i++], ccgSubSurf_getVertData(ss, v));
  1146. }
  1147. MEM_freeN(vertMap2);
  1148. MEM_freeN(edgeMap2);
  1149. MEM_freeN(faceMap2);
  1150. }
  1151. static void ccgDM_foreachMappedVert(
  1152. DerivedMesh *dm,
  1153. void (*func)(void *userData, int index, const float co[3], const float no_f[3], const short no_s[3]),
  1154. void *userData)
  1155. {
  1156. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1157. CCGVertIterator *vi = ccgSubSurf_getVertIterator(ccgdm->ss);
  1158. for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
  1159. CCGVert *v = ccgVertIterator_getCurrent(vi);
  1160. DMGridData *vd = ccgSubSurf_getVertData(ccgdm->ss, v);
  1161. int index = ccgDM_getVertMapIndex(ccgdm->ss, v);
  1162. if (index != -1)
  1163. func(userData, index, vd->co, vd->no, NULL);
  1164. }
  1165. ccgVertIterator_free(vi);
  1166. }
  1167. static void ccgDM_foreachMappedEdge(
  1168. DerivedMesh *dm,
  1169. void (*func)(void *userData, int index, const float v0co[3], const float v1co[3]),
  1170. void *userData)
  1171. {
  1172. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1173. CCGSubSurf *ss = ccgdm->ss;
  1174. CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
  1175. int i, edgeSize = ccgSubSurf_getEdgeSize(ss);
  1176. for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
  1177. CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
  1178. DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
  1179. int index = ccgDM_getEdgeMapIndex(ss, e);
  1180. if (index != -1) {
  1181. for (i = 0; i < edgeSize - 1; i++)
  1182. func(userData, index, edgeData[i].co, edgeData[i + 1].co);
  1183. }
  1184. }
  1185. ccgEdgeIterator_free(ei);
  1186. }
  1187. static void ccgDM_drawVerts(DerivedMesh *dm)
  1188. {
  1189. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1190. CCGSubSurf *ss = ccgdm->ss;
  1191. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  1192. int gridSize = ccgSubSurf_getGridSize(ss);
  1193. CCGVertIterator *vi;
  1194. CCGEdgeIterator *ei;
  1195. CCGFaceIterator *fi;
  1196. glBegin(GL_POINTS);
  1197. vi = ccgSubSurf_getVertIterator(ss);
  1198. for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
  1199. CCGVert *v = ccgVertIterator_getCurrent(vi);
  1200. glVertex3fv(ccgSubSurf_getVertData(ss, v));
  1201. }
  1202. ccgVertIterator_free(vi);
  1203. ei = ccgSubSurf_getEdgeIterator(ss);
  1204. for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
  1205. CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
  1206. int x;
  1207. for (x = 1; x < edgeSize - 1; x++)
  1208. glVertex3fv(ccgSubSurf_getEdgeData(ss, e, x));
  1209. }
  1210. ccgEdgeIterator_free(ei);
  1211. fi = ccgSubSurf_getFaceIterator(ss);
  1212. for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
  1213. CCGFace *f = ccgFaceIterator_getCurrent(fi);
  1214. int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1215. glVertex3fv(ccgSubSurf_getFaceCenterData(f));
  1216. for (S = 0; S < numVerts; S++)
  1217. for (x = 1; x < gridSize - 1; x++)
  1218. glVertex3fv(ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
  1219. for (S = 0; S < numVerts; S++)
  1220. for (y = 1; y < gridSize - 1; y++)
  1221. for (x = 1; x < gridSize - 1; x++)
  1222. glVertex3fv(ccgSubSurf_getFaceGridData(ss, f, S, x, y));
  1223. }
  1224. ccgFaceIterator_free(fi);
  1225. glEnd();
  1226. }
  1227. static void ccgdm_pbvh_update(CCGDerivedMesh *ccgdm)
  1228. {
  1229. if (ccgdm->pbvh && ccgDM_use_grid_pbvh(ccgdm)) {
  1230. CCGFace **faces;
  1231. int totface;
  1232. BLI_pbvh_get_grid_updates(ccgdm->pbvh, 1, (void ***)&faces, &totface);
  1233. if (totface) {
  1234. ccgSubSurf_updateFromFaces(ccgdm->ss, 0, faces, totface);
  1235. ccgSubSurf_updateNormals(ccgdm->ss, faces, totface);
  1236. MEM_freeN(faces);
  1237. }
  1238. }
  1239. }
  1240. static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges, int drawAllEdges)
  1241. {
  1242. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1243. CCGSubSurf *ss = ccgdm->ss;
  1244. int i, j, edgeSize = ccgSubSurf_getEdgeSize(ss);
  1245. int totedge = ccgSubSurf_getNumEdges(ss);
  1246. int gridSize = ccgSubSurf_getGridSize(ss);
  1247. int useAging;
  1248. ccgdm_pbvh_update(ccgdm);
  1249. ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
  1250. for (j = 0; j < totedge; j++) {
  1251. CCGEdge *e = ccgdm->edgeMap[j].edge;
  1252. DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
  1253. if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e))
  1254. continue;
  1255. if (!drawAllEdges && ccgdm->edgeFlags && !(ccgdm->edgeFlags[j] & ME_EDGEDRAW))
  1256. continue;
  1257. if (useAging && !(G.f & G_BACKBUFSEL)) {
  1258. int ageCol = 255 - ccgSubSurf_getEdgeAge(ss, e) * 4;
  1259. glColor3ub(0, ageCol > 0 ? ageCol : 0, 0);
  1260. }
  1261. glBegin(GL_LINE_STRIP);
  1262. for (i = 0; i < edgeSize - 1; i++) {
  1263. glVertex3fv(edgeData[i].co);
  1264. glVertex3fv(edgeData[i + 1].co);
  1265. }
  1266. glEnd();
  1267. }
  1268. if (useAging && !(G.f & G_BACKBUFSEL)) {
  1269. glColor3ub(0, 0, 0);
  1270. }
  1271. if (ccgdm->drawInteriorEdges) {
  1272. int totface = ccgSubSurf_getNumFaces(ss);
  1273. for (j = 0; j < totface; j++) {
  1274. CCGFace *f = ccgdm->faceMap[j].face;
  1275. int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1276. for (S = 0; S < numVerts; S++) {
  1277. DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  1278. glBegin(GL_LINE_STRIP);
  1279. for (x = 0; x < gridSize; x++)
  1280. glVertex3fv(faceGridData[x].co);
  1281. glEnd();
  1282. for (y = 1; y < gridSize - 1; y++) {
  1283. glBegin(GL_LINE_STRIP);
  1284. for (x = 0; x < gridSize; x++)
  1285. glVertex3fv(faceGridData[y * gridSize + x].co);
  1286. glEnd();
  1287. }
  1288. for (x = 1; x < gridSize - 1; x++) {
  1289. glBegin(GL_LINE_STRIP);
  1290. for (y = 0; y < gridSize; y++)
  1291. glVertex3fv(faceGridData[y * gridSize + x].co);
  1292. glEnd();
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. static void ccgDM_drawLooseEdges(DerivedMesh *dm)
  1299. {
  1300. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1301. CCGSubSurf *ss = ccgdm->ss;
  1302. int totedge = ccgSubSurf_getNumEdges(ss);
  1303. int i, j, edgeSize = ccgSubSurf_getEdgeSize(ss);
  1304. for (j = 0; j < totedge; j++) {
  1305. CCGEdge *e = ccgdm->edgeMap[j].edge;
  1306. DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
  1307. if (!ccgSubSurf_getEdgeNumFaces(e)) {
  1308. glBegin(GL_LINE_STRIP);
  1309. for (i = 0; i < edgeSize - 1; i++) {
  1310. glVertex3fv(edgeData[i].co);
  1311. glVertex3fv(edgeData[i + 1].co);
  1312. }
  1313. glEnd();
  1314. }
  1315. }
  1316. }
  1317. static void ccgDM_glNormalFast(float *a, float *b, float *c, float *d)
  1318. {
  1319. float a_cX = c[0] - a[0], a_cY = c[1] - a[1], a_cZ = c[2] - a[2];
  1320. float b_dX = d[0] - b[0], b_dY = d[1] - b[1], b_dZ = d[2] - b[2];
  1321. float no[3];
  1322. no[0] = b_dY * a_cZ - b_dZ * a_cY;
  1323. no[1] = b_dZ * a_cX - b_dX * a_cZ;
  1324. no[2] = b_dX * a_cY - b_dY * a_cX;
  1325. /* don't normalize, GL_NORMALIZE is enabled */
  1326. glNormal3fv(no);
  1327. }
  1328. /* Only used by non-editmesh types */
  1329. static void ccgDM_drawFacesSolid(DerivedMesh *dm, float (*partial_redraw_planes)[4], int fast, DMSetMaterial setMaterial)
  1330. {
  1331. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1332. CCGSubSurf *ss = ccgdm->ss;
  1333. int gridSize = ccgSubSurf_getGridSize(ss);
  1334. DMFlagMat *faceFlags = ccgdm->faceFlags;
  1335. int step = (fast) ? gridSize - 1 : 1;
  1336. int i, totface = ccgSubSurf_getNumFaces(ss);
  1337. int drawcurrent = 0, matnr = -1, shademodel = -1;
  1338. ccgdm_pbvh_update(ccgdm);
  1339. if (ccgdm->pbvh && ccgdm->multires.mmd && !fast) {
  1340. if (dm->numTessFaceData) {
  1341. BLI_pbvh_draw(ccgdm->pbvh, partial_redraw_planes, NULL, setMaterial);
  1342. glShadeModel(GL_FLAT);
  1343. }
  1344. return;
  1345. }
  1346. for (i = 0; i < totface; i++) {
  1347. CCGFace *f = ccgdm->faceMap[i].face;
  1348. int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1349. int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
  1350. int new_matnr, new_shademodel;
  1351. if (faceFlags) {
  1352. new_shademodel = (faceFlags[index].flag & ME_SMOOTH) ? GL_SMOOTH : GL_FLAT;
  1353. new_matnr = faceFlags[index].mat_nr;
  1354. }
  1355. else {
  1356. new_shademodel = GL_SMOOTH;
  1357. new_matnr = 0;
  1358. }
  1359. if (shademodel != new_shademodel || matnr != new_matnr) {
  1360. matnr = new_matnr;
  1361. shademodel = new_shademodel;
  1362. drawcurrent = setMaterial(matnr + 1, NULL);
  1363. glShadeModel(shademodel);
  1364. }
  1365. if (!drawcurrent)
  1366. continue;
  1367. for (S = 0; S < numVerts; S++) {
  1368. DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  1369. if (shademodel == GL_SMOOTH) {
  1370. for (y = 0; y < gridSize - 1; y += step) {
  1371. glBegin(GL_QUAD_STRIP);
  1372. for (x = 0; x < gridSize; x += step) {
  1373. DMGridData *a = &faceGridData[(y + 0) * gridSize + x];
  1374. DMGridData *b = &faceGridData[(y + step) * gridSize + x];
  1375. glNormal3fv(a->no);
  1376. glVertex3fv(a->co);
  1377. glNormal3fv(b->no);
  1378. glVertex3fv(b->co);
  1379. }
  1380. glEnd();
  1381. }
  1382. }
  1383. else {
  1384. glBegin(GL_QUADS);
  1385. for (y = 0; y < gridSize - 1; y += step) {
  1386. for (x = 0; x < gridSize - 1; x += step) {
  1387. float *a = faceGridData[(y + 0) * gridSize + x].co;
  1388. float *b = faceGridData[(y + 0) * gridSize + x + step].co;
  1389. float *c = faceGridData[(y + step) * gridSize + x + step].co;
  1390. float *d = faceGridData[(y + step) * gridSize + x].co;
  1391. ccgDM_glNormalFast(a, b, c, d);
  1392. glVertex3fv(d);
  1393. glVertex3fv(c);
  1394. glVertex3fv(b);
  1395. glVertex3fv(a);
  1396. }
  1397. }
  1398. glEnd();
  1399. }
  1400. }
  1401. }
  1402. }
  1403. /* Only used by non-editmesh types */
  1404. static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm,
  1405. DMSetMaterial setMaterial,
  1406. DMSetDrawOptions setDrawOptions,
  1407. void *userData)
  1408. {
  1409. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1410. CCGSubSurf *ss = ccgdm->ss;
  1411. GPUVertexAttribs gattribs;
  1412. DMVertexAttribs attribs = {{{NULL}}};
  1413. /* MTFace *tf = dm->getTessFaceDataArray(dm, CD_MTFACE); */ /* UNUSED */
  1414. int gridSize = ccgSubSurf_getGridSize(ss);
  1415. int gridFaces = gridSize - 1;
  1416. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  1417. DMFlagMat *faceFlags = ccgdm->faceFlags;
  1418. int a, b, i, doDraw, numVerts, matnr, new_matnr, totface;
  1419. ccgdm_pbvh_update(ccgdm);
  1420. doDraw = 0;
  1421. matnr = -1;
  1422. #define PASSATTRIB(dx, dy, vert) { \
  1423. if (attribs.totorco) { \
  1424. index = getFaceIndex(ss, f, S, x + dx, y + dy, edgeSize, gridSize); \
  1425. glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]); \
  1426. } \
  1427. for (b = 0; b < attribs.tottface; b++) { \
  1428. MTFace *tf = &attribs.tface[b].array[a]; \
  1429. glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]); \
  1430. } \
  1431. for (b = 0; b < attribs.totmcol; b++) { \
  1432. MCol *cp = &attribs.mcol[b].array[a * 4 + vert]; \
  1433. GLubyte col[4]; \
  1434. col[0] = cp->b; col[1] = cp->g; col[2] = cp->r; col[3] = cp->a; \
  1435. glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col); \
  1436. } \
  1437. if (attribs.tottang) { \
  1438. float *tang = attribs.tang.array[a * 4 + vert]; \
  1439. glVertexAttrib4fvARB(attribs.tang.glIndex, tang); \
  1440. } \
  1441. }
  1442. totface = ccgSubSurf_getNumFaces(ss);
  1443. for (a = 0, i = 0; i < totface; i++) {
  1444. CCGFace *f = ccgdm->faceMap[i].face;
  1445. int S, x, y, drawSmooth;
  1446. int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
  1447. int origIndex = ccgDM_getFaceMapIndex(ss, f);
  1448. numVerts = ccgSubSurf_getFaceNumVerts(f);
  1449. if (faceFlags) {
  1450. drawSmooth = (faceFlags[index].flag & ME_SMOOTH);
  1451. new_matnr = faceFlags[index].mat_nr + 1;
  1452. }
  1453. else {
  1454. drawSmooth = 1;
  1455. new_matnr = 1;
  1456. }
  1457. if (new_matnr != matnr) {
  1458. doDraw = setMaterial(matnr = new_matnr, &gattribs);
  1459. if (doDraw)
  1460. DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
  1461. }
  1462. if (!doDraw || (setDrawOptions && (origIndex != ORIGINDEX_NONE) &&
  1463. (setDrawOptions(userData, origIndex) == DM_DRAW_OPTION_SKIP)))
  1464. {
  1465. a += gridFaces * gridFaces * numVerts;
  1466. continue;
  1467. }
  1468. glShadeModel(drawSmooth ? GL_SMOOTH : GL_FLAT);
  1469. for (S = 0; S < numVerts; S++) {
  1470. DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  1471. DMGridData *vda, *vdb;
  1472. if (drawSmooth) {
  1473. for (y = 0; y < gridFaces; y++) {
  1474. glBegin(GL_QUAD_STRIP);
  1475. for (x = 0; x < gridFaces; x++) {
  1476. vda = &faceGridData[(y + 0) * gridSize + x];
  1477. vdb = &faceGridData[(y + 1) * gridSize + x];
  1478. PASSATTRIB(0, 0, 0);
  1479. glNormal3fv(vda->no);
  1480. glVertex3fv(vda->co);
  1481. PASSATTRIB(0, 1, 1);
  1482. glNormal3fv(vdb->no);
  1483. glVertex3fv(vdb->co);
  1484. if (x != gridFaces - 1)
  1485. a++;
  1486. }
  1487. vda = &faceGridData[(y + 0) * gridSize + x];
  1488. vdb = &faceGridData[(y + 1) * gridSize + x];
  1489. PASSATTRIB(0, 0, 3);
  1490. glNormal3fv(vda->no);
  1491. glVertex3fv(vda->co);
  1492. PASSATTRIB(0, 1, 2);
  1493. glNormal3fv(vdb->no);
  1494. glVertex3fv(vdb->co);
  1495. glEnd();
  1496. a++;
  1497. }
  1498. }
  1499. else {
  1500. glBegin(GL_QUADS);
  1501. for (y = 0; y < gridFaces; y++) {
  1502. for (x = 0; x < gridFaces; x++) {
  1503. float *aco = faceGridData[(y + 0) * gridSize + x].co;
  1504. float *bco = faceGridData[(y + 0) * gridSize + x + 1].co;
  1505. float *cco = faceGridData[(y + 1) * gridSize + x + 1].co;
  1506. float *dco = faceGridData[(y + 1) * gridSize + x].co;
  1507. ccgDM_glNormalFast(aco, bco, cco, dco);
  1508. PASSATTRIB(0, 1, 1);
  1509. glVertex3fv(dco);
  1510. PASSATTRIB(1, 1, 2);
  1511. glVertex3fv(cco);
  1512. PASSATTRIB(1, 0, 3);
  1513. glVertex3fv(bco);
  1514. PASSATTRIB(0, 0, 0);
  1515. glVertex3fv(aco);
  1516. a++;
  1517. }
  1518. }
  1519. glEnd();
  1520. }
  1521. }
  1522. }
  1523. #undef PASSATTRIB
  1524. }
  1525. static void ccgDM_drawFacesGLSL(DerivedMesh *dm, DMSetMaterial setMaterial)
  1526. {
  1527. dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL);
  1528. }
  1529. /* Only used by non-editmesh types */
  1530. static void ccgDM_drawMappedFacesMat(DerivedMesh *dm, void (*setMaterial)(void *userData, int, void *attribs), int (*setFace)(void *userData, int index), void *userData)
  1531. {
  1532. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1533. CCGSubSurf *ss = ccgdm->ss;
  1534. GPUVertexAttribs gattribs;
  1535. DMVertexAttribs attribs = {{{NULL}}};
  1536. int gridSize = ccgSubSurf_getGridSize(ss);
  1537. int gridFaces = gridSize - 1;
  1538. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  1539. DMFlagMat *faceFlags = ccgdm->faceFlags;
  1540. int a, b, i, numVerts, matnr, new_matnr, totface;
  1541. ccgdm_pbvh_update(ccgdm);
  1542. matnr = -1;
  1543. #define PASSATTRIB(dx, dy, vert) { \
  1544. if (attribs.totorco) { \
  1545. index = getFaceIndex(ss, f, S, x + dx, y + dy, edgeSize, gridSize); \
  1546. if (attribs.orco.glTexco) \
  1547. glTexCoord3fv(attribs.orco.array[index]); \
  1548. else \
  1549. glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]); \
  1550. } \
  1551. for (b = 0; b < attribs.tottface; b++) { \
  1552. MTFace *tf = &attribs.tface[b].array[a]; \
  1553. if (attribs.tface[b].glTexco) \
  1554. glTexCoord2fv(tf->uv[vert]); \
  1555. else \
  1556. glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]); \
  1557. } \
  1558. for (b = 0; b < attribs.totmcol; b++) { \
  1559. MCol *cp = &attribs.mcol[b].array[a * 4 + vert]; \
  1560. GLubyte col[4]; \
  1561. col[0] = cp->b; col[1] = cp->g; col[2] = cp->r; col[3] = cp->a; \
  1562. glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col); \
  1563. } \
  1564. if (attribs.tottang) { \
  1565. float *tang = attribs.tang.array[a * 4 + vert]; \
  1566. glVertexAttrib4fvARB(attribs.tang.glIndex, tang); \
  1567. } \
  1568. }
  1569. totface = ccgSubSurf_getNumFaces(ss);
  1570. for (a = 0, i = 0; i < totface; i++) {
  1571. CCGFace *f = ccgdm->faceMap[i].face;
  1572. int S, x, y, drawSmooth;
  1573. int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
  1574. int origIndex = ccgDM_getFaceMapIndex(ss, f);
  1575. numVerts = ccgSubSurf_getFaceNumVerts(f);
  1576. /* get flags */
  1577. if (faceFlags) {
  1578. drawSmooth = (faceFlags[index].flag & ME_SMOOTH);
  1579. new_matnr = faceFlags[index].mat_nr + 1;
  1580. }
  1581. else {
  1582. drawSmooth = 1;
  1583. new_matnr = 1;
  1584. }
  1585. /* material */
  1586. if (new_matnr != matnr) {
  1587. setMaterial(userData, matnr = new_matnr, &gattribs);
  1588. DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
  1589. }
  1590. /* face hiding */
  1591. if ((setFace && (origIndex != ORIGINDEX_NONE) && !setFace(userData, origIndex))) {
  1592. a += gridFaces * gridFaces * numVerts;
  1593. continue;
  1594. }
  1595. /* draw face*/
  1596. glShadeModel(drawSmooth ? GL_SMOOTH : GL_FLAT);
  1597. for (S = 0; S < numVerts; S++) {
  1598. DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  1599. DMGridData *vda, *vdb;
  1600. if (drawSmooth) {
  1601. for (y = 0; y < gridFaces; y++) {
  1602. glBegin(GL_QUAD_STRIP);
  1603. for (x = 0; x < gridFaces; x++) {
  1604. vda = &faceGridData[(y + 0) * gridSize + x];
  1605. vdb = &faceGridData[(y + 1) * gridSize + x];
  1606. PASSATTRIB(0, 0, 0);
  1607. glNormal3fv(vda->no);
  1608. glVertex3fv(vda->co);
  1609. PASSATTRIB(0, 1, 1);
  1610. glNormal3fv(vdb->no);
  1611. glVertex3fv(vdb->co);
  1612. if (x != gridFaces - 1)
  1613. a++;
  1614. }
  1615. vda = &faceGridData[(y + 0) * gridSize + x];
  1616. vdb = &faceGridData[(y + 1) * gridSize + x];
  1617. PASSATTRIB(0, 0, 3);
  1618. glNormal3fv(vda->no);
  1619. glVertex3fv(vda->co);
  1620. PASSATTRIB(0, 1, 2);
  1621. glNormal3fv(vdb->no);
  1622. glVertex3fv(vdb->co);
  1623. glEnd();
  1624. a++;
  1625. }
  1626. }
  1627. else {
  1628. glBegin(GL_QUADS);
  1629. for (y = 0; y < gridFaces; y++) {
  1630. for (x = 0; x < gridFaces; x++) {
  1631. float *aco = faceGridData[(y + 0) * gridSize + x].co;
  1632. float *bco = faceGridData[(y + 0) * gridSize + x + 1].co;
  1633. float *cco = faceGridData[(y + 1) * gridSize + x + 1].co;
  1634. float *dco = faceGridData[(y + 1) * gridSize + x].co;
  1635. ccgDM_glNormalFast(aco, bco, cco, dco);
  1636. PASSATTRIB(0, 1, 1);
  1637. glVertex3fv(dco);
  1638. PASSATTRIB(1, 1, 2);
  1639. glVertex3fv(cco);
  1640. PASSATTRIB(1, 0, 3);
  1641. glVertex3fv(bco);
  1642. PASSATTRIB(0, 0, 0);
  1643. glVertex3fv(aco);
  1644. a++;
  1645. }
  1646. }
  1647. glEnd();
  1648. }
  1649. }
  1650. }
  1651. #undef PASSATTRIB
  1652. }
  1653. static void ccgDM_drawFacesTex_common(DerivedMesh *dm,
  1654. DMSetDrawOptionsTex drawParams,
  1655. DMSetDrawOptions drawParamsMapped,
  1656. DMCompareDrawOptions compareDrawOptions,
  1657. void *userData)
  1658. {
  1659. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1660. CCGSubSurf *ss = ccgdm->ss;
  1661. MCol *mcol = dm->getTessFaceDataArray(dm, CD_PREVIEW_MCOL);
  1662. MTFace *tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
  1663. DMFlagMat *faceFlags = ccgdm->faceFlags;
  1664. DMDrawOption draw_option;
  1665. int i, totface, gridSize = ccgSubSurf_getGridSize(ss);
  1666. int gridFaces = gridSize - 1;
  1667. (void) compareDrawOptions;
  1668. ccgdm_pbvh_update(ccgdm);
  1669. if (!mcol)
  1670. mcol = dm->getTessFaceDataArray(dm, CD_MCOL);
  1671. if (!mcol)
  1672. mcol = dm->getTessFaceDataArray(dm, CD_TEXTURE_MCOL);
  1673. totface = ccgSubSurf_getNumFaces(ss);
  1674. for (i = 0; i < totface; i++) {
  1675. CCGFace *f = ccgdm->faceMap[i].face;
  1676. int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1677. int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
  1678. int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
  1679. unsigned char *cp = NULL;
  1680. int mat_nr;
  1681. if (faceFlags) {
  1682. drawSmooth = (faceFlags[origIndex].flag & ME_SMOOTH);
  1683. mat_nr = faceFlags[origIndex].mat_nr;
  1684. }
  1685. else {
  1686. drawSmooth = 1;
  1687. mat_nr = 0;
  1688. }
  1689. if (drawParams)
  1690. draw_option = drawParams(tf, (mcol != NULL), mat_nr);
  1691. else if (index != ORIGINDEX_NONE)
  1692. draw_option = (drawParamsMapped) ? drawParamsMapped(userData, index) : DM_DRAW_OPTION_NORMAL;
  1693. else
  1694. draw_option = GPU_enable_material(mat_nr, NULL) ? DM_DRAW_OPTION_NORMAL : DM_DRAW_OPTION_SKIP;
  1695. if (draw_option == DM_DRAW_OPTION_SKIP) {
  1696. if (tf) tf += gridFaces * gridFaces * numVerts;
  1697. if (mcol) mcol += gridFaces * gridFaces * numVerts * 4;
  1698. continue;
  1699. }
  1700. /* flag 1 == use vertex colors */
  1701. if (mcol) {
  1702. if (draw_option != DM_DRAW_OPTION_NO_MCOL)
  1703. cp = (unsigned char *)mcol;
  1704. mcol += gridFaces * gridFaces * numVerts * 4;
  1705. }
  1706. for (S = 0; S < numVerts; S++) {
  1707. DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  1708. DMGridData *a, *b;
  1709. if (drawSmooth) {
  1710. glShadeModel(GL_SMOOTH);
  1711. for (y = 0; y < gridFaces; y++) {
  1712. glBegin(GL_QUAD_STRIP);
  1713. for (x = 0; x < gridFaces; x++) {
  1714. a = &faceGridData[(y + 0) * gridSize + x];
  1715. b = &faceGridData[(y + 1) * gridSize + x];
  1716. if (tf) glTexCoord2fv(tf->uv[0]);
  1717. if (cp) glColor3ub(cp[3], cp[2], cp[1]);
  1718. glNormal3fv(a->no);
  1719. glVertex3fv(a->co);
  1720. if (tf) glTexCoord2fv(tf->uv[1]);
  1721. if (cp) glColor3ub(cp[7], cp[6], cp[5]);
  1722. glNormal3fv(b->no);
  1723. glVertex3fv(b->co);
  1724. if (x != gridFaces - 1) {
  1725. if (tf) tf++;
  1726. if (cp) cp += 16;
  1727. }
  1728. }
  1729. a = &faceGridData[(y + 0) * gridSize + x];
  1730. b = &faceGridData[(y + 1) * gridSize + x];
  1731. if (tf) glTexCoord2fv(tf->uv[3]);
  1732. if (cp) glColor3ub(cp[15], cp[14], cp[13]);
  1733. glNormal3fv(a->no);
  1734. glVertex3fv(a->co);
  1735. if (tf) glTexCoord2fv(tf->uv[2]);
  1736. if (cp) glColor3ub(cp[11], cp[10], cp[9]);
  1737. glNormal3fv(b->no);
  1738. glVertex3fv(b->co);
  1739. if (tf) tf++;
  1740. if (cp) cp += 16;
  1741. glEnd();
  1742. }
  1743. }
  1744. else {
  1745. glShadeModel((cp)? GL_SMOOTH: GL_FLAT);
  1746. glBegin(GL_QUADS);
  1747. for (y = 0; y < gridFaces; y++) {
  1748. for (x = 0; x < gridFaces; x++) {
  1749. float *a_co = faceGridData[(y + 0) * gridSize + x].co;
  1750. float *b_co = faceGridData[(y + 0) * gridSize + x + 1].co;
  1751. float *c_co = faceGridData[(y + 1) * gridSize + x + 1].co;
  1752. float *d_co = faceGridData[(y + 1) * gridSize + x].co;
  1753. ccgDM_glNormalFast(a_co, b_co, c_co, d_co);
  1754. if (tf) glTexCoord2fv(tf->uv[1]);
  1755. if (cp) glColor3ub(cp[7], cp[6], cp[5]);
  1756. glVertex3fv(d_co);
  1757. if (tf) glTexCoord2fv(tf->uv[2]);
  1758. if (cp) glColor3ub(cp[11], cp[10], cp[9]);
  1759. glVertex3fv(c_co);
  1760. if (tf) glTexCoord2fv(tf->uv[3]);
  1761. if (cp) glColor3ub(cp[15], cp[14], cp[13]);
  1762. glVertex3fv(b_co);
  1763. if (tf) glTexCoord2fv(tf->uv[0]);
  1764. if (cp) glColor3ub(cp[3], cp[2], cp[1]);
  1765. glVertex3fv(a_co);
  1766. if (tf) tf++;
  1767. if (cp) cp += 16;
  1768. }
  1769. }
  1770. glEnd();
  1771. }
  1772. }
  1773. }
  1774. }
  1775. static void ccgDM_drawFacesTex(DerivedMesh *dm,
  1776. DMSetDrawOptionsTex setDrawOptions,
  1777. DMCompareDrawOptions compareDrawOptions,
  1778. void *userData)
  1779. {
  1780. ccgDM_drawFacesTex_common(dm, setDrawOptions, NULL, compareDrawOptions, userData);
  1781. }
  1782. static void ccgDM_drawMappedFacesTex(DerivedMesh *dm,
  1783. DMSetDrawOptions setDrawOptions,
  1784. DMCompareDrawOptions compareDrawOptions,
  1785. void *userData)
  1786. {
  1787. ccgDM_drawFacesTex_common(dm, NULL, setDrawOptions, compareDrawOptions, userData);
  1788. }
  1789. static void ccgDM_drawUVEdges(DerivedMesh *dm)
  1790. {
  1791. MFace *mf = dm->getTessFaceArray(dm);
  1792. MTFace *tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
  1793. int i;
  1794. if (tf) {
  1795. glBegin(GL_LINES);
  1796. for (i = 0; i < dm->numTessFaceData; i++, mf++, tf++) {
  1797. if (!(mf->flag & ME_HIDE)) {
  1798. glVertex2fv(tf->uv[0]);
  1799. glVertex2fv(tf->uv[1]);
  1800. glVertex2fv(tf->uv[1]);
  1801. glVertex2fv(tf->uv[2]);
  1802. if (!mf->v4) {
  1803. glVertex2fv(tf->uv[2]);
  1804. glVertex2fv(tf->uv[0]);
  1805. }
  1806. else {
  1807. glVertex2fv(tf->uv[2]);
  1808. glVertex2fv(tf->uv[3]);
  1809. glVertex2fv(tf->uv[3]);
  1810. glVertex2fv(tf->uv[0]);
  1811. }
  1812. }
  1813. }
  1814. glEnd();
  1815. }
  1816. }
  1817. static void ccgDM_drawMappedFaces(DerivedMesh *dm,
  1818. DMSetDrawOptions setDrawOptions,
  1819. DMSetMaterial setMaterial,
  1820. DMCompareDrawOptions compareDrawOptions,
  1821. void *userData, DMDrawFlag flag)
  1822. {
  1823. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1824. CCGSubSurf *ss = ccgdm->ss;
  1825. MCol *mcol = NULL;
  1826. int i, gridSize = ccgSubSurf_getGridSize(ss);
  1827. DMFlagMat *faceFlags = ccgdm->faceFlags;
  1828. int useColors = flag & DM_DRAW_USE_COLORS;
  1829. int gridFaces = gridSize - 1, totface;
  1830. /* currently unused -- each original face is handled separately */
  1831. (void)compareDrawOptions;
  1832. if (useColors) {
  1833. mcol = dm->getTessFaceDataArray(dm, CD_PREVIEW_MCOL);
  1834. if (!mcol)
  1835. mcol = dm->getTessFaceDataArray(dm, CD_MCOL);
  1836. }
  1837. totface = ccgSubSurf_getNumFaces(ss);
  1838. for (i = 0; i < totface; i++) {
  1839. CCGFace *f = ccgdm->faceMap[i].face;
  1840. int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
  1841. int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
  1842. int origIndex;
  1843. unsigned char *cp = NULL;
  1844. origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
  1845. if (flag & DM_DRAW_ALWAYS_SMOOTH) drawSmooth = 1;
  1846. else if (faceFlags) drawSmooth = (faceFlags[origIndex].flag & ME_SMOOTH);
  1847. else drawSmooth = 1;
  1848. if (mcol) {
  1849. cp = (unsigned char *)mcol;
  1850. mcol += gridFaces * gridFaces * numVerts * 4;
  1851. }
  1852. {
  1853. DMDrawOption draw_option = DM_DRAW_OPTION_NORMAL;
  1854. if (index == ORIGINDEX_NONE)
  1855. draw_option = setMaterial(faceFlags ? faceFlags[origIndex].mat_nr + 1 : 1, NULL); /* XXX, no faceFlags no material */
  1856. else if (setDrawOptions)
  1857. draw_option = setDrawOptions(userData, index);
  1858. if (draw_option != DM_DRAW_OPTION_SKIP) {
  1859. if (draw_option == DM_DRAW_OPTION_STIPPLE) {
  1860. glEnable(GL_POLYGON_STIPPLE);
  1861. glPolygonStipple(stipple_quarttone);
  1862. }
  1863. /* no need to set shading mode to flat because
  1864. * normals are already used to change shading */
  1865. glShadeModel(GL_SMOOTH);
  1866. for (S = 0; S < numVerts; S++) {
  1867. DMGridData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  1868. if (drawSmooth) {
  1869. for (y = 0; y < gridFaces; y++) {
  1870. DMGridData *a, *b;
  1871. glBegin(GL_QUAD_STRIP);
  1872. for (x = 0; x < gridFaces; x++) {
  1873. a = &faceGridData[(y + 0) * gridSize + x];
  1874. b = &faceGridData[(y + 1) * gridSize + x];
  1875. if (cp) glColor3ub(cp[3], cp[2], cp[1]);
  1876. glNormal3fv(a->no);
  1877. glVertex3fv(a->co);
  1878. if (cp) glColor3ub(cp[7], cp[6], cp[5]);
  1879. glNormal3fv(b->no);
  1880. glVertex3fv(b->co);
  1881. if (x != gridFaces - 1) {
  1882. if (cp) cp += 16;
  1883. }
  1884. }
  1885. a = &faceGridData[(y + 0) * gridSize + x];
  1886. b = &faceGridData[(y + 1) * gridSize + x];
  1887. if (cp) glColor3ub(cp[15], cp[14], cp[13]);
  1888. glNormal3fv(a->no);
  1889. glVertex3fv(a->co);
  1890. if (cp) glColor3ub(cp[11], cp[10], cp[9]);
  1891. glNormal3fv(b->no);
  1892. glVertex3fv(b->co);
  1893. if (cp) cp += 16;
  1894. glEnd();
  1895. }
  1896. }
  1897. else {
  1898. glBegin(GL_QUADS);
  1899. for (y = 0; y < gridFaces; y++) {
  1900. for (x = 0; x < gridFaces; x++) {
  1901. float *a = faceGridData[(y + 0) * gridSize + x].co;
  1902. float *b = faceGridData[(y + 0) * gridSize + x + 1].co;
  1903. float *c = faceGridData[(y + 1) * gridSize + x + 1].co;
  1904. float *d = faceGridData[(y + 1) * gridSize + x].co;
  1905. ccgDM_glNormalFast(a, b, c, d);
  1906. if (cp) glColor3ub(cp[7], cp[6], cp[5]);
  1907. glVertex3fv(d);
  1908. if (cp) glColor3ub(cp[11], cp[10], cp[9]);
  1909. glVertex3fv(c);
  1910. if (cp) glColor3ub(cp[15], cp[14], cp[13]);
  1911. glVertex3fv(b);
  1912. if (cp) glColor3ub(cp[3], cp[2], cp[1]);
  1913. glVertex3fv(a);
  1914. if (cp) cp += 16;
  1915. }
  1916. }
  1917. glEnd();
  1918. }
  1919. }
  1920. if (draw_option == DM_DRAW_OPTION_STIPPLE)
  1921. glDisable(GL_POLYGON_STIPPLE);
  1922. }
  1923. }
  1924. }
  1925. }
  1926. static void ccgDM_drawMappedEdges(DerivedMesh *dm,
  1927. DMSetDrawOptions setDrawOptions,
  1928. void *userData)
  1929. {
  1930. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1931. CCGSubSurf *ss = ccgdm->ss;
  1932. CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
  1933. int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss);
  1934. ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
  1935. for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
  1936. CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
  1937. DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
  1938. int index = ccgDM_getEdgeMapIndex(ss, e);
  1939. glBegin(GL_LINE_STRIP);
  1940. if (index != -1 && (!setDrawOptions || (setDrawOptions(userData, index) != DM_DRAW_OPTION_SKIP))) {
  1941. if (useAging && !(G.f & G_BACKBUFSEL)) {
  1942. int ageCol = 255 - ccgSubSurf_getEdgeAge(ss, e) * 4;
  1943. glColor3ub(0, ageCol > 0 ? ageCol : 0, 0);
  1944. }
  1945. for (i = 0; i < edgeSize - 1; i++) {
  1946. glVertex3fv(edgeData[i].co);
  1947. glVertex3fv(edgeData[i + 1].co);
  1948. }
  1949. }
  1950. glEnd();
  1951. }
  1952. ccgEdgeIterator_free(ei);
  1953. }
  1954. static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm,
  1955. DMSetDrawOptions setDrawOptions,
  1956. DMSetDrawInterpOptions setDrawInterpOptions,
  1957. void *userData)
  1958. {
  1959. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1960. CCGSubSurf *ss = ccgdm->ss;
  1961. CCGEdgeIterator *ei = ccgSubSurf_getEdgeIterator(ss);
  1962. int i, useAging, edgeSize = ccgSubSurf_getEdgeSize(ss);
  1963. ccgSubSurf_getUseAgeCounts(ss, &useAging, NULL, NULL, NULL);
  1964. for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
  1965. CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
  1966. DMGridData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
  1967. int index = ccgDM_getEdgeMapIndex(ss, e);
  1968. glBegin(GL_LINE_STRIP);
  1969. if (index != -1 && (!setDrawOptions || (setDrawOptions(userData, index) != DM_DRAW_OPTION_SKIP))) {
  1970. for (i = 0; i < edgeSize; i++) {
  1971. setDrawInterpOptions(userData, index, (float) i / (edgeSize - 1));
  1972. if (useAging && !(G.f & G_BACKBUFSEL)) {
  1973. int ageCol = 255 - ccgSubSurf_getEdgeAge(ss, e) * 4;
  1974. glColor3ub(0, ageCol > 0 ? ageCol : 0, 0);
  1975. }
  1976. glVertex3fv(edgeData[i].co);
  1977. }
  1978. }
  1979. glEnd();
  1980. }
  1981. ccgEdgeIterator_free(ei);
  1982. }
  1983. static void ccgDM_foreachMappedFaceCenter(
  1984. DerivedMesh *dm,
  1985. void (*func)(void *userData, int index, const float co[3], const float no[3]),
  1986. void *userData)
  1987. {
  1988. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  1989. CCGSubSurf *ss = ccgdm->ss;
  1990. CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
  1991. for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
  1992. CCGFace *f = ccgFaceIterator_getCurrent(fi);
  1993. int index = ccgDM_getFaceMapIndex(ss, f);
  1994. if (index != -1) {
  1995. /* Face center data normal isn't updated atm. */
  1996. DMGridData *vd = ccgSubSurf_getFaceGridData(ss, f, 0, 0, 0);
  1997. func(userData, index, vd->co, vd->no);
  1998. }
  1999. }
  2000. ccgFaceIterator_free(fi);
  2001. }
  2002. static void ccgDM_release(DerivedMesh *dm)
  2003. {
  2004. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
  2005. if (DM_release(dm)) {
  2006. /* Before freeing, need to update the displacement map */
  2007. if (ccgdm->multires.modified_flags) {
  2008. /* Check that mmd still exists */
  2009. if (!ccgdm->multires.local_mmd &&
  2010. BLI_findindex(&ccgdm->multires.ob->modifiers, ccgdm->multires.mmd) < 0)
  2011. ccgdm->multires.mmd = NULL;
  2012. if (ccgdm->multires.mmd) {
  2013. if (ccgdm->multires.modified_flags & MULTIRES_COORDS_MODIFIED)
  2014. multires_modifier_update_mdisps(dm);
  2015. if (ccgdm->multires.modified_flags & MULTIRES_HIDDEN_MODIFIED)
  2016. multires_modifier_update_hidden(dm);
  2017. }
  2018. }
  2019. if (ccgdm->ehash)
  2020. BLI_edgehash_free(ccgdm->ehash, NULL);
  2021. if (ccgdm->reverseFaceMap) MEM_freeN(ccgdm->reverseFaceMap);
  2022. if (ccgdm->gridFaces) MEM_freeN(ccgdm->gridFaces);
  2023. if (ccgdm->gridData) MEM_freeN(ccgdm->gridData);
  2024. if (ccgdm->gridAdjacency) MEM_freeN(ccgdm->gridAdjacency);
  2025. if (ccgdm->gridOffset) MEM_freeN(ccgdm->gridOffset);
  2026. if (ccgdm->gridFlagMats) MEM_freeN(ccgdm->gridFlagMats);
  2027. if (ccgdm->gridHidden) {
  2028. int i, numGrids = dm->getNumGrids(dm);
  2029. for (i = 0; i < numGrids; i++) {
  2030. if (ccgdm->gridHidden[i])
  2031. MEM_freeN(ccgdm->gridHidden[i]);
  2032. }
  2033. MEM_freeN(ccgdm->gridHidden);
  2034. }
  2035. if (ccgdm->freeSS) ccgSubSurf_free(ccgdm->ss);
  2036. if (ccgdm->pmap) MEM_freeN(ccgdm->pmap);
  2037. if (ccgdm->pmap_mem) MEM_freeN(ccgdm->pmap_mem);
  2038. MEM_freeN(ccgdm->edgeFlags);
  2039. MEM_freeN(ccgdm->faceFlags);
  2040. MEM_freeN(ccgdm->vertMap);
  2041. MEM_freeN(ccgdm->edgeMap);
  2042. MEM_freeN(ccgdm->faceMap);
  2043. MEM_freeN(ccgdm);
  2044. }
  2045. }
  2046. static void ccg_loops_to_corners(CustomData *fdata, CustomData *ldata,
  2047. CustomData *pdata, int loopstart, int findex, int polyindex,
  2048. const int numTex, const int numCol, const int hasPCol, const int hasOrigSpace)
  2049. {
  2050. MTFace *texface;
  2051. MTexPoly *texpoly;
  2052. MCol *mcol;
  2053. MLoopCol *mloopcol;
  2054. MLoopUV *mloopuv;
  2055. int i, j;
  2056. for (i = 0; i < numTex; i++) {
  2057. texface = CustomData_get_n(fdata, CD_MTFACE, findex, i);
  2058. texpoly = CustomData_get_n(pdata, CD_MTEXPOLY, polyindex, i);
  2059. ME_MTEXFACE_CPY(texface, texpoly);
  2060. mloopuv = CustomData_get_n(ldata, CD_MLOOPUV, loopstart, i);
  2061. for (j = 0; j < 4; j++, mloopuv++) {
  2062. copy_v2_v2(texface->uv[j], mloopuv->uv);
  2063. }
  2064. }
  2065. for (i = 0; i < numCol; i++) {
  2066. mloopcol = CustomData_get_n(ldata, CD_MLOOPCOL, loopstart, i);
  2067. mcol = CustomData_get_n(fdata, CD_MCOL, findex, i);
  2068. for (j = 0; j < 4; j++, mloopcol++) {
  2069. MESH_MLOOPCOL_TO_MCOL(mloopcol, &mcol[j]);
  2070. }
  2071. }
  2072. if (hasPCol) {
  2073. mloopcol = CustomData_get(ldata, loopstart, CD_PREVIEW_MLOOPCOL);
  2074. mcol = CustomData_get(fdata, findex, CD_PREVIEW_MCOL);
  2075. for (j = 0; j < 4; j++, mloopcol++) {
  2076. MESH_MLOOPCOL_TO_MCOL(mloopcol, &mcol[j]);
  2077. }
  2078. }
  2079. if (hasOrigSpace) {
  2080. OrigSpaceFace *of = CustomData_get(fdata, findex, CD_ORIGSPACE);
  2081. OrigSpaceLoop *lof;
  2082. lof = CustomData_get(ldata, loopstart, CD_ORIGSPACE_MLOOP);
  2083. for (j = 0; j < 4; j++, lof++) {
  2084. copy_v2_v2(of->uv[j], lof->uv);
  2085. }
  2086. }
  2087. }
  2088. static void *ccgDM_get_vert_data_layer(DerivedMesh *dm, int type)
  2089. {
  2090. if (type == CD_ORIGINDEX) {
  2091. /* create origindex on demand to save memory */
  2092. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2093. CCGSubSurf *ss = ccgdm->ss;
  2094. int *origindex;
  2095. int a, index, totnone, totorig;
  2096. /* Avoid re-creation if the layer exists already */
  2097. origindex = DM_get_vert_data_layer(dm, CD_ORIGINDEX);
  2098. if (origindex) {
  2099. return origindex;
  2100. }
  2101. DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
  2102. origindex = DM_get_vert_data_layer(dm, CD_ORIGINDEX);
  2103. totorig = ccgSubSurf_getNumVerts(ss);
  2104. totnone = dm->numVertData - totorig;
  2105. /* original vertices are at the end */
  2106. for (a = 0; a < totnone; a++)
  2107. origindex[a] = ORIGINDEX_NONE;
  2108. for (index = 0; index < totorig; index++, a++) {
  2109. CCGVert *v = ccgdm->vertMap[index].vert;
  2110. origindex[a] = ccgDM_getVertMapIndex(ccgdm->ss, v);
  2111. }
  2112. return origindex;
  2113. }
  2114. return DM_get_vert_data_layer(dm, type);
  2115. }
  2116. static void *ccgDM_get_edge_data_layer(DerivedMesh *dm, int type)
  2117. {
  2118. if (type == CD_ORIGINDEX) {
  2119. /* create origindex on demand to save memory */
  2120. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2121. CCGSubSurf *ss = ccgdm->ss;
  2122. int *origindex;
  2123. int a, i, index, totnone, totorig, totedge;
  2124. int edgeSize = ccgSubSurf_getEdgeSize(ss);
  2125. /* Avoid re-creation if the layer exists already */
  2126. origindex = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
  2127. if (origindex) {
  2128. return origindex;
  2129. }
  2130. DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
  2131. origindex = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
  2132. totedge = ccgSubSurf_getNumEdges(ss);
  2133. totorig = totedge * (edgeSize - 1);
  2134. totnone = dm->numEdgeData - totorig;
  2135. /* original edges are at the end */
  2136. for (a = 0; a < totnone; a++)
  2137. origindex[a] = ORIGINDEX_NONE;
  2138. for (index = 0; index < totedge; index++) {
  2139. CCGEdge *e = ccgdm->edgeMap[index].edge;
  2140. int mapIndex = ccgDM_getEdgeMapIndex(ss, e);
  2141. for (i = 0; i < edgeSize - 1; i++, a++)
  2142. origindex[a] = mapIndex;
  2143. }
  2144. return origindex;
  2145. }
  2146. return DM_get_edge_data_layer(dm, type);
  2147. }
  2148. static void *ccgDM_get_tessface_data_layer(DerivedMesh *dm, int type)
  2149. {
  2150. if (type == CD_ORIGINDEX) {
  2151. /* create origindex on demand to save memory */
  2152. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2153. CCGSubSurf *ss = ccgdm->ss;
  2154. int *origindex;
  2155. int a, i, index, totface;
  2156. int gridFaces = ccgSubSurf_getGridSize(ss) - 1;
  2157. /* Avoid re-creation if the layer exists already */
  2158. origindex = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
  2159. if (origindex) {
  2160. return origindex;
  2161. }
  2162. DM_add_tessface_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
  2163. origindex = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
  2164. totface = ccgSubSurf_getNumFaces(ss);
  2165. for (a = 0, index = 0; index < totface; index++) {
  2166. CCGFace *f = ccgdm->faceMap[index].face;
  2167. int numVerts = ccgSubSurf_getFaceNumVerts(f);
  2168. int mapIndex = ccgDM_getFaceMapIndex(ss, f);
  2169. for (i = 0; i < gridFaces * gridFaces * numVerts; i++, a++)
  2170. origindex[a] = mapIndex;
  2171. }
  2172. return origindex;
  2173. }
  2174. return DM_get_tessface_data_layer(dm, type);
  2175. }
  2176. static void *ccgDM_get_vert_data(DerivedMesh *dm, int index, int type)
  2177. {
  2178. if (type == CD_ORIGINDEX) {
  2179. /* ensure creation of CD_ORIGINDEX layer */
  2180. ccgDM_get_vert_data_layer(dm, type);
  2181. }
  2182. return DM_get_vert_data(dm, index, type);
  2183. }
  2184. static void *ccgDM_get_edge_data(DerivedMesh *dm, int index, int type)
  2185. {
  2186. if (type == CD_ORIGINDEX) {
  2187. /* ensure creation of CD_ORIGINDEX layer */
  2188. ccgDM_get_edge_data_layer(dm, type);
  2189. }
  2190. return DM_get_edge_data(dm, index, type);
  2191. }
  2192. static void *ccgDM_get_tessface_data(DerivedMesh *dm, int index, int type)
  2193. {
  2194. if (type == CD_ORIGINDEX) {
  2195. /* ensure creation of CD_ORIGINDEX layer */
  2196. ccgDM_get_tessface_data_layer(dm, type);
  2197. }
  2198. return DM_get_tessface_data(dm, index, type);
  2199. }
  2200. static int ccgDM_getNumGrids(DerivedMesh *dm)
  2201. {
  2202. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2203. int index, numFaces, numGrids;
  2204. numFaces = ccgSubSurf_getNumFaces(ccgdm->ss);
  2205. numGrids = 0;
  2206. for (index = 0; index < numFaces; index++) {
  2207. CCGFace *f = ccgdm->faceMap[index].face;
  2208. numGrids += ccgSubSurf_getFaceNumVerts(f);
  2209. }
  2210. return numGrids;
  2211. }
  2212. static int ccgDM_getGridSize(DerivedMesh *dm)
  2213. {
  2214. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2215. return ccgSubSurf_getGridSize(ccgdm->ss);
  2216. }
  2217. static int ccgdm_adjacent_grid(int *gridOffset, CCGFace *f, int S, int offset)
  2218. {
  2219. CCGFace *adjf;
  2220. CCGEdge *e;
  2221. int i, j = 0, numFaces, fIndex, numEdges = 0;
  2222. e = ccgSubSurf_getFaceEdge(f, S);
  2223. numFaces = ccgSubSurf_getEdgeNumFaces(e);
  2224. if (numFaces != 2)
  2225. return -1;
  2226. for (i = 0; i < numFaces; i++) {
  2227. adjf = ccgSubSurf_getEdgeFace(e, i);
  2228. if (adjf != f) {
  2229. numEdges = ccgSubSurf_getFaceNumVerts(adjf);
  2230. for (j = 0; j < numEdges; j++)
  2231. if (ccgSubSurf_getFaceEdge(adjf, j) == e)
  2232. break;
  2233. if (j != numEdges)
  2234. break;
  2235. }
  2236. }
  2237. if (numEdges == 0)
  2238. return -1;
  2239. fIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(adjf));
  2240. return gridOffset[fIndex] + (j + offset) % numEdges;
  2241. }
  2242. static void ccgdm_create_grids(DerivedMesh *dm)
  2243. {
  2244. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2245. CCGSubSurf *ss = ccgdm->ss;
  2246. DMGridData **gridData;
  2247. DMGridAdjacency *gridAdjacency, *adj;
  2248. DMFlagMat *gridFlagMats;
  2249. CCGFace **gridFaces;
  2250. int *gridOffset;
  2251. int index, numFaces, numGrids, S, gIndex /*, gridSize*/;
  2252. if (ccgdm->gridData)
  2253. return;
  2254. numGrids = ccgDM_getNumGrids(dm);
  2255. numFaces = ccgSubSurf_getNumFaces(ss);
  2256. /*gridSize = ccgDM_getGridSize(dm);*/ /*UNUSED*/
  2257. /* compute offset into grid array for each face */
  2258. gridOffset = MEM_mallocN(sizeof(int) * numFaces, "ccgdm.gridOffset");
  2259. for (gIndex = 0, index = 0; index < numFaces; index++) {
  2260. CCGFace *f = ccgdm->faceMap[index].face;
  2261. int numVerts = ccgSubSurf_getFaceNumVerts(f);
  2262. gridOffset[index] = gIndex;
  2263. gIndex += numVerts;
  2264. }
  2265. /* compute grid data */
  2266. gridData = MEM_mallocN(sizeof(DMGridData *) * numGrids, "ccgdm.gridData");
  2267. gridAdjacency = MEM_mallocN(sizeof(DMGridAdjacency) * numGrids, "ccgdm.gridAdjacency");
  2268. gridFaces = MEM_mallocN(sizeof(CCGFace *) * numGrids, "ccgdm.gridFaces");
  2269. gridFlagMats = MEM_mallocN(sizeof(DMFlagMat) * numGrids, "ccgdm.gridFlagMats");
  2270. ccgdm->gridHidden = MEM_callocN(sizeof(BLI_bitmap) * numGrids, "ccgdm.gridHidden");
  2271. for (gIndex = 0, index = 0; index < numFaces; index++) {
  2272. CCGFace *f = ccgdm->faceMap[index].face;
  2273. int numVerts = ccgSubSurf_getFaceNumVerts(f);
  2274. for (S = 0; S < numVerts; S++, gIndex++) {
  2275. int prevS = (S - 1 + numVerts) % numVerts;
  2276. int nextS = (S + 1 + numVerts) % numVerts;
  2277. gridData[gIndex] = ccgSubSurf_getFaceGridDataArray(ss, f, S);
  2278. gridFaces[gIndex] = f;
  2279. gridFlagMats[gIndex] = ccgdm->faceFlags[index];
  2280. adj = &gridAdjacency[gIndex];
  2281. adj->index[0] = gIndex - S + nextS;
  2282. adj->rotation[0] = 3;
  2283. adj->index[1] = ccgdm_adjacent_grid(gridOffset, f, prevS, 0);
  2284. adj->rotation[1] = 1;
  2285. adj->index[2] = ccgdm_adjacent_grid(gridOffset, f, S, 1);
  2286. adj->rotation[2] = 3;
  2287. adj->index[3] = gIndex - S + prevS;
  2288. adj->rotation[3] = 1;
  2289. }
  2290. }
  2291. ccgdm->gridData = gridData;
  2292. ccgdm->gridFaces = gridFaces;
  2293. ccgdm->gridAdjacency = gridAdjacency;
  2294. ccgdm->gridOffset = gridOffset;
  2295. ccgdm->gridFlagMats = gridFlagMats;
  2296. }
  2297. static DMGridData **ccgDM_getGridData(DerivedMesh *dm)
  2298. {
  2299. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2300. ccgdm_create_grids(dm);
  2301. return ccgdm->gridData;
  2302. }
  2303. static DMGridAdjacency *ccgDM_getGridAdjacency(DerivedMesh *dm)
  2304. {
  2305. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2306. ccgdm_create_grids(dm);
  2307. return ccgdm->gridAdjacency;
  2308. }
  2309. static int *ccgDM_getGridOffset(DerivedMesh *dm)
  2310. {
  2311. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2312. ccgdm_create_grids(dm);
  2313. return ccgdm->gridOffset;
  2314. }
  2315. static DMFlagMat *ccgDM_getGridFlagMats(DerivedMesh *dm)
  2316. {
  2317. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2318. ccgdm_create_grids(dm);
  2319. return ccgdm->gridFlagMats;
  2320. }
  2321. static BLI_bitmap *ccgDM_getGridHidden(DerivedMesh *dm)
  2322. {
  2323. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2324. ccgdm_create_grids(dm);
  2325. return ccgdm->gridHidden;
  2326. }
  2327. static const MeshElemMap *ccgDM_getPolyMap(Object *ob, DerivedMesh *dm)
  2328. {
  2329. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2330. if (!ccgdm->multires.mmd && !ccgdm->pmap && ob->type == OB_MESH) {
  2331. Mesh *me = ob->data;
  2332. create_vert_poly_map(&ccgdm->pmap, &ccgdm->pmap_mem,
  2333. me->mpoly, me->mloop,
  2334. me->totvert, me->totpoly, me->totloop);
  2335. }
  2336. return ccgdm->pmap;
  2337. }
  2338. static int ccgDM_use_grid_pbvh(CCGDerivedMesh *ccgdm)
  2339. {
  2340. MultiresModifierData *mmd = ccgdm->multires.mmd;
  2341. /* both of multires and subsurf modifiers are CCG, but
  2342. * grids should only be used when sculpting on multires */
  2343. if (!mmd)
  2344. return 0;
  2345. return 1;
  2346. }
  2347. static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm)
  2348. {
  2349. CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
  2350. int gridSize, numGrids, grid_pbvh;
  2351. if (!ob) {
  2352. ccgdm->pbvh = NULL;
  2353. return NULL;
  2354. }
  2355. if (!ob->sculpt)
  2356. return NULL;
  2357. grid_pbvh = ccgDM_use_grid_pbvh(ccgdm);
  2358. if (ob->sculpt->pbvh) {
  2359. if (grid_pbvh) {
  2360. /* pbvh's grids, gridadj and gridfaces points to data inside ccgdm
  2361. * but this can be freed on ccgdm release, this updates the pointers
  2362. * when the ccgdm gets remade, the assumption is that the topology
  2363. * does not change. */
  2364. ccgdm_create_grids(dm);
  2365. BLI_pbvh_grids_update(ob->sculpt->pbvh, ccgdm->gridData, ccgdm->gridAdjacency, (void **)ccgdm->gridFaces);
  2366. }
  2367. ccgdm->pbvh = ob->sculpt->pbvh;
  2368. }
  2369. if (ccgdm->pbvh)
  2370. return ccgdm->pbvh;
  2371. /* no pbvh exists yet, we need to create one. only in case of multires
  2372. * we build a pbvh over the modified mesh, in other cases the base mesh
  2373. * is being sculpted, so we build a pbvh from that. */
  2374. if (grid_pbvh) {
  2375. ccgdm_create_grids(dm);
  2376. gridSize = ccgDM_getGridSize(dm);
  2377. numGrids = ccgDM_getNumGrids(dm);
  2378. ob->sculpt->pbvh = ccgdm->pbvh = BLI_pbvh_new();
  2379. BLI_pbvh_build_grids(ccgdm->pbvh, ccgdm->gridData, ccgdm->gridAdjacency,
  2380. numGrids, gridSize, (void **)ccgdm->gridFaces, ccgdm->gridFlagMats, ccgdm->gridHidden);
  2381. }
  2382. else if (ob->type == OB_MESH) {
  2383. Mesh *me = ob->data;
  2384. ob->sculpt->pbvh = ccgdm->pbvh = BLI_pbvh_new();
  2385. BLI_assert(!(me->mface == NULL && me->mpoly != NULL)); /* BMESH ONLY complain if mpoly is valid but not mface */
  2386. BLI_pbvh_build_mesh(ccgdm->pbvh, me->mface, me->mvert,
  2387. me->totface, me->totvert);
  2388. }
  2389. return ccgdm->pbvh;
  2390. }
  2391. static void ccgDM_recalcTessellation(DerivedMesh *UNUSED(dm))
  2392. {
  2393. /* Nothing to do: CCG handles creating its own tessfaces */
  2394. }
  2395. static void ccgDM_calcNormals(DerivedMesh *UNUSED(dm))
  2396. {
  2397. /* Nothing to do: CCG calculates normals during drawing */
  2398. }
  2399. static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
  2400. int drawInteriorEdges,
  2401. int useSubsurfUv,
  2402. DerivedMesh *dm)
  2403. {
  2404. CCGDerivedMesh *ccgdm = MEM_callocN(sizeof(*ccgdm), "ccgdm");
  2405. CCGVertIterator *vi;
  2406. CCGEdgeIterator *ei;
  2407. CCGFaceIterator *fi;
  2408. int index, totvert, totedge, totface;
  2409. int i;
  2410. int vertNum, edgeNum, faceNum;
  2411. int *vertOrigIndex, *faceOrigIndex, *polyOrigIndex, *base_polyOrigIndex; /* *edgeOrigIndex - as yet, unused */
  2412. short *edgeFlags;
  2413. DMFlagMat *faceFlags;
  2414. int *loopidx = NULL, *vertidx = NULL, *polyidx = NULL;
  2415. BLI_array_declare(loopidx);
  2416. BLI_array_declare(vertidx);
  2417. int loopindex, loopindex2;
  2418. int edgeSize, has_edge_origindex;
  2419. int gridSize;
  2420. int gridFaces, gridCuts;
  2421. /*int gridSideVerts;*/
  2422. int gridSideEdges;
  2423. int numTex, numCol;
  2424. int hasPCol, hasOrigSpace;
  2425. int gridInternalEdges;
  2426. float *w = NULL;
  2427. WeightTable wtable = {0};
  2428. /* MCol *mcol; */ /* UNUSED */
  2429. MEdge *medge = NULL;
  2430. /* MFace *mface = NULL; */
  2431. MPoly *mpoly = NULL;
  2432. DM_from_template(&ccgdm->dm, dm, DM_TYPE_CCGDM,
  2433. ccgSubSurf_getNumFinalVerts(ss),
  2434. ccgSubSurf_getNumFinalEdges(ss),
  2435. ccgSubSurf_getNumFinalFaces(ss),
  2436. ccgSubSurf_getNumFinalFaces(ss) * 4,
  2437. ccgSubSurf_getNumFinalFaces(ss));
  2438. CustomData_free_layer_active(&ccgdm->dm.polyData, CD_NORMAL,
  2439. ccgdm->dm.numPolyData);
  2440. numTex = CustomData_number_of_layers(&ccgdm->dm.loopData, CD_MLOOPUV);
  2441. numCol = CustomData_number_of_layers(&ccgdm->dm.loopData, CD_MLOOPCOL);
  2442. hasPCol = CustomData_has_layer(&ccgdm->dm.loopData, CD_PREVIEW_MLOOPCOL);
  2443. hasOrigSpace = CustomData_has_layer(&ccgdm->dm.loopData, CD_ORIGSPACE_MLOOP);
  2444. if (
  2445. (numTex && CustomData_number_of_layers(&ccgdm->dm.faceData, CD_MTFACE) != numTex) ||
  2446. (numCol && CustomData_number_of_layers(&ccgdm->dm.faceData, CD_MCOL) != numCol) ||
  2447. (hasPCol && !CustomData_has_layer(&ccgdm->dm.faceData, CD_PREVIEW_MCOL)) ||
  2448. (hasOrigSpace && !CustomData_has_layer(&ccgdm->dm.faceData, CD_ORIGSPACE)) )
  2449. {
  2450. CustomData_from_bmeshpoly(&ccgdm->dm.faceData,
  2451. &ccgdm->dm.polyData,
  2452. &ccgdm->dm.loopData,
  2453. ccgSubSurf_getNumFinalFaces(ss));
  2454. }
  2455. /* We absolutely need that layer, else it's no valid tessellated data! */
  2456. polyidx = CustomData_add_layer(&ccgdm->dm.faceData, CD_POLYINDEX, CD_CALLOC,
  2457. NULL, ccgSubSurf_getNumFinalFaces(ss));
  2458. ccgdm->dm.getMinMax = ccgDM_getMinMax;
  2459. ccgdm->dm.getNumVerts = ccgDM_getNumVerts;
  2460. ccgdm->dm.getNumEdges = ccgDM_getNumEdges;
  2461. ccgdm->dm.getNumTessFaces = ccgDM_getNumTessFaces;
  2462. ccgdm->dm.getNumLoops = ccgDM_getNumLoops;
  2463. /* reuse of ccgDM_getNumTessFaces is intentional here: subsurf polys are just created from tessfaces */
  2464. ccgdm->dm.getNumPolys = ccgDM_getNumTessFaces;
  2465. ccgdm->dm.getNumGrids = ccgDM_getNumGrids;
  2466. ccgdm->dm.getPBVH = ccgDM_getPBVH;
  2467. ccgdm->dm.getVert = ccgDM_getFinalVert;
  2468. ccgdm->dm.getEdge = ccgDM_getFinalEdge;
  2469. ccgdm->dm.getTessFace = ccgDM_getFinalFace;
  2470. ccgdm->dm.getVertCo = ccgDM_getFinalVertCo;
  2471. ccgdm->dm.getVertNo = ccgDM_getFinalVertNo;
  2472. ccgdm->dm.copyVertArray = ccgDM_copyFinalVertArray;
  2473. ccgdm->dm.copyEdgeArray = ccgDM_copyFinalEdgeArray;
  2474. ccgdm->dm.copyTessFaceArray = ccgDM_copyFinalFaceArray;
  2475. ccgdm->dm.copyLoopArray = ccgDM_copyFinalLoopArray;
  2476. ccgdm->dm.copyPolyArray = ccgDM_copyFinalPolyArray;
  2477. ccgdm->dm.getVertData = ccgDM_get_vert_data;
  2478. ccgdm->dm.getEdgeData = ccgDM_get_edge_data;
  2479. ccgdm->dm.getTessFaceData = ccgDM_get_tessface_data;
  2480. ccgdm->dm.getVertDataArray = ccgDM_get_vert_data_layer;
  2481. ccgdm->dm.getEdgeDataArray = ccgDM_get_edge_data_layer;
  2482. ccgdm->dm.getTessFaceDataArray = ccgDM_get_tessface_data_layer;
  2483. ccgdm->dm.getNumGrids = ccgDM_getNumGrids;
  2484. ccgdm->dm.getGridSize = ccgDM_getGridSize;
  2485. ccgdm->dm.getGridData = ccgDM_getGridData;
  2486. ccgdm->dm.getGridAdjacency = ccgDM_getGridAdjacency;
  2487. ccgdm->dm.getGridOffset = ccgDM_getGridOffset;
  2488. ccgdm->dm.getGridFlagMats = ccgDM_getGridFlagMats;
  2489. ccgdm->dm.getGridHidden = ccgDM_getGridHidden;
  2490. ccgdm->dm.getPolyMap = ccgDM_getPolyMap;
  2491. ccgdm->dm.getPBVH = ccgDM_getPBVH;
  2492. ccgdm->dm.getTessFace = ccgDM_getFinalFace;
  2493. ccgdm->dm.copyVertArray = ccgDM_copyFinalVertArray;
  2494. ccgdm->dm.copyEdgeArray = ccgDM_copyFinalEdgeArray;
  2495. ccgdm->dm.copyTessFaceArray = ccgDM_copyFinalFaceArray;
  2496. ccgdm->dm.calcNormals = ccgDM_calcNormals;
  2497. ccgdm->dm.recalcTessellation = ccgDM_recalcTessellation;
  2498. ccgdm->dm.getVertCos = ccgdm_getVertCos;
  2499. ccgdm->dm.foreachMappedVert = ccgDM_foreachMappedVert;
  2500. ccgdm->dm.foreachMappedEdge = ccgDM_foreachMappedEdge;
  2501. ccgdm->dm.foreachMappedFaceCenter = ccgDM_foreachMappedFaceCenter;
  2502. ccgdm->dm.drawVerts = ccgDM_drawVerts;
  2503. ccgdm->dm.drawEdges = ccgDM_drawEdges;
  2504. ccgdm->dm.drawLooseEdges = ccgDM_drawLooseEdges;
  2505. ccgdm->dm.drawFacesSolid = ccgDM_drawFacesSolid;
  2506. ccgdm->dm.drawFacesTex = ccgDM_drawFacesTex;
  2507. ccgdm->dm.drawFacesGLSL = ccgDM_drawFacesGLSL;
  2508. ccgdm->dm.drawMappedFaces = ccgDM_drawMappedFaces;
  2509. ccgdm->dm.drawMappedFacesTex = ccgDM_drawMappedFacesTex;
  2510. ccgdm->dm.drawMappedFacesGLSL = ccgDM_drawMappedFacesGLSL;
  2511. ccgdm->dm.drawMappedFacesMat = ccgDM_drawMappedFacesMat;
  2512. ccgdm->dm.drawUVEdges = ccgDM_drawUVEdges;
  2513. ccgdm->dm.drawMappedEdgesInterp = ccgDM_drawMappedEdgesInterp;
  2514. ccgdm->dm.drawMappedEdges = ccgDM_drawMappedEdges;
  2515. ccgdm->dm.release = ccgDM_release;
  2516. ccgdm->ss = ss;
  2517. ccgdm->drawInteriorEdges = drawInteriorEdges;
  2518. ccgdm->useSubsurfUv = useSubsurfUv;
  2519. totvert = ccgSubSurf_getNumVerts(ss);
  2520. ccgdm->vertMap = MEM_mallocN(totvert * sizeof(*ccgdm->vertMap), "vertMap");
  2521. vi = ccgSubSurf_getVertIterator(ss);
  2522. for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
  2523. CCGVert *v = ccgVertIterator_getCurrent(vi);
  2524. ccgdm->vertMap[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))].vert = v;
  2525. }
  2526. ccgVertIterator_free(vi);
  2527. totedge = ccgSubSurf_getNumEdges(ss);
  2528. ccgdm->edgeMap = MEM_mallocN(totedge * sizeof(*ccgdm->edgeMap), "edgeMap");
  2529. ei = ccgSubSurf_getEdgeIterator(ss);
  2530. for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
  2531. CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
  2532. ccgdm->edgeMap[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))].edge = e;
  2533. }
  2534. totface = ccgSubSurf_getNumFaces(ss);
  2535. ccgdm->faceMap = MEM_mallocN(totface * sizeof(*ccgdm->faceMap), "faceMap");
  2536. fi = ccgSubSurf_getFaceIterator(ss);
  2537. for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
  2538. CCGFace *f = ccgFaceIterator_getCurrent(fi);
  2539. ccgdm->faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f))].face = f;
  2540. }
  2541. ccgFaceIterator_free(fi);
  2542. ccgdm->reverseFaceMap = MEM_callocN(sizeof(int) * ccgSubSurf_getNumFinalFaces(ss), "reverseFaceMap");
  2543. edgeSize = ccgSubSurf_getEdgeSize(ss);
  2544. gridSize = ccgSubSurf_getGridSize(ss);
  2545. gridFaces = gridSize - 1;
  2546. gridCuts = gridSize - 2;
  2547. /*gridInternalVerts = gridSideVerts * gridSideVerts; - as yet, unused */
  2548. gridSideEdges = gridSize - 1;
  2549. gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2;
  2550. vertNum = 0;
  2551. edgeNum = 0;
  2552. faceNum = 0;
  2553. /* mvert = dm->getVertArray(dm); */ /* UNUSED */
  2554. medge = dm->getEdgeArray(dm);
  2555. /* mface = dm->getTessFaceArray(dm); */ /* UNUSED */
  2556. mpoly = CustomData_get_layer(&dm->polyData, CD_MPOLY);
  2557. base_polyOrigIndex = CustomData_get_layer(&dm->polyData, CD_ORIGINDEX);
  2558. /*CDDM hack*/
  2559. edgeFlags = ccgdm->edgeFlags = MEM_callocN(sizeof(short) * totedge, "edgeFlags");
  2560. faceFlags = ccgdm->faceFlags = MEM_callocN(sizeof(DMFlagMat) * totface, "faceFlags");
  2561. vertOrigIndex = DM_get_vert_data_layer(&ccgdm->dm, CD_ORIGINDEX);
  2562. /*edgeOrigIndex = DM_get_edge_data_layer(&ccgdm->dm, CD_ORIGINDEX);*/
  2563. faceOrigIndex = DM_get_tessface_data_layer(&ccgdm->dm, CD_ORIGINDEX);
  2564. polyOrigIndex = DM_get_poly_data_layer(&ccgdm->dm, CD_ORIGINDEX);
  2565. #if 0
  2566. /* this is not in trunk, can gives problems because colors initialize
  2567. * as black, just don't do it!, it works fine - campbell */
  2568. if (!CustomData_has_layer(&ccgdm->dm.faceData, CD_MCOL))
  2569. DM_add_tessface_layer(&ccgdm->dm, CD_MCOL, CD_CALLOC, NULL);
  2570. mcol = DM_get_tessface_data_layer(&ccgdm->dm, CD_MCOL);
  2571. #endif
  2572. has_edge_origindex = CustomData_has_layer(&ccgdm->dm.edgeData, CD_ORIGINDEX);
  2573. loopindex = loopindex2 = 0; //current loop index
  2574. for (index = 0; index < totface; index++) {
  2575. CCGFace *f = ccgdm->faceMap[index].face;
  2576. int numVerts = ccgSubSurf_getFaceNumVerts(f);
  2577. int numFinalEdges = numVerts * (gridSideEdges + gridInternalEdges);
  2578. int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
  2579. int g2_wid = gridCuts + 2;
  2580. float *w2;
  2581. int s, x, y;
  2582. w = get_ss_weights(&wtable, gridCuts, numVerts);
  2583. ccgdm->faceMap[index].startVert = vertNum;
  2584. ccgdm->faceMap[index].startEdge = edgeNum;
  2585. ccgdm->faceMap[index].startFace = faceNum;
  2586. faceFlags->flag = mpoly ? mpoly[origIndex].flag : 0;
  2587. faceFlags->mat_nr = mpoly ? mpoly[origIndex].mat_nr : 0;
  2588. faceFlags++;
  2589. origIndex = base_polyOrigIndex ? base_polyOrigIndex[origIndex] : origIndex;
  2590. /* set the face base vert */
  2591. *((int *)ccgSubSurf_getFaceUserData(ss, f)) = vertNum;
  2592. BLI_array_empty(loopidx);
  2593. BLI_array_growitems(loopidx, numVerts);
  2594. for (s = 0; s < numVerts; s++) {
  2595. loopidx[s] = loopindex++;
  2596. }
  2597. BLI_array_empty(vertidx);
  2598. BLI_array_growitems(vertidx, numVerts);
  2599. for (s = 0; s < numVerts; s++) {
  2600. CCGVert *v = ccgSubSurf_getFaceVert(f, s);
  2601. vertidx[s] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
  2602. }
  2603. /*I think this is for interpolating the center vert?*/
  2604. w2 = w; // + numVerts*(g2_wid-1)*(g2_wid-1); //numVerts*((g2_wid-1)*g2_wid+g2_wid-1);
  2605. DM_interp_vert_data(dm, &ccgdm->dm, vertidx, w2,
  2606. numVerts, vertNum);
  2607. if (vertOrigIndex) {
  2608. *vertOrigIndex = ORIGINDEX_NONE;
  2609. ++vertOrigIndex;
  2610. }
  2611. ++vertNum;
  2612. /*interpolate per-vert data*/
  2613. for (s = 0; s < numVerts; s++) {
  2614. for (x = 1; x < gridFaces; x++) {
  2615. w2 = w + s * numVerts * g2_wid * g2_wid + x * numVerts;
  2616. DM_interp_vert_data(dm, &ccgdm->dm, vertidx, w2,
  2617. numVerts, vertNum);
  2618. if (vertOrigIndex) {
  2619. *vertOrigIndex = ORIGINDEX_NONE;
  2620. ++vertOrigIndex;
  2621. }
  2622. ++vertNum;
  2623. }
  2624. }
  2625. /*interpolate per-vert data*/
  2626. for (s = 0; s < numVerts; s++) {
  2627. for (y = 1; y < gridFaces; y++) {
  2628. for (x = 1; x < gridFaces; x++) {
  2629. w2 = w + s * numVerts * g2_wid * g2_wid + (y * g2_wid + x) * numVerts;
  2630. DM_interp_vert_data(dm, &ccgdm->dm, vertidx, w2,
  2631. numVerts, vertNum);
  2632. if (vertOrigIndex) {
  2633. *vertOrigIndex = ORIGINDEX_NONE;
  2634. ++vertOrigIndex;
  2635. }
  2636. ++vertNum;
  2637. }
  2638. }
  2639. }
  2640. if (has_edge_origindex) {
  2641. for (i = 0; i < numFinalEdges; ++i)
  2642. *(int *)DM_get_edge_data(&ccgdm->dm, edgeNum + i,
  2643. CD_ORIGINDEX) = ORIGINDEX_NONE;
  2644. }
  2645. for (s = 0; s < numVerts; s++) {
  2646. /*interpolate per-face data*/
  2647. for (y = 0; y < gridFaces; y++) {
  2648. for (x = 0; x < gridFaces; x++) {
  2649. w2 = w + s * numVerts * g2_wid * g2_wid + (y * g2_wid + x) * numVerts;
  2650. CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
  2651. loopidx, w2, NULL, numVerts, loopindex2);
  2652. loopindex2++;
  2653. w2 = w + s * numVerts * g2_wid * g2_wid + ((y + 1) * g2_wid + (x)) * numVerts;
  2654. CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
  2655. loopidx, w2, NULL, numVerts, loopindex2);
  2656. loopindex2++;
  2657. w2 = w + s * numVerts * g2_wid * g2_wid + ((y + 1) * g2_wid + (x + 1)) * numVerts;
  2658. CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
  2659. loopidx, w2, NULL, numVerts, loopindex2);
  2660. loopindex2++;
  2661. w2 = w + s * numVerts * g2_wid * g2_wid + ((y) * g2_wid + (x + 1)) * numVerts;
  2662. CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
  2663. loopidx, w2, NULL, numVerts, loopindex2);
  2664. loopindex2++;
  2665. /*copy over poly data, e.g. mtexpoly*/
  2666. CustomData_copy_data(&dm->polyData, &ccgdm->dm.polyData, origIndex, faceNum, 1);
  2667. /*generate tessellated face data used for drawing*/
  2668. ccg_loops_to_corners(&ccgdm->dm.faceData, &ccgdm->dm.loopData,
  2669. &ccgdm->dm.polyData, loopindex2 - 4, faceNum, faceNum,
  2670. numTex, numCol, hasPCol, hasOrigSpace);
  2671. /*set original index data*/
  2672. if (faceOrigIndex) {
  2673. *faceOrigIndex = origIndex;
  2674. faceOrigIndex++;
  2675. }
  2676. if (polyOrigIndex) {
  2677. *polyOrigIndex = origIndex;
  2678. polyOrigIndex++;
  2679. }
  2680. ccgdm->reverseFaceMap[faceNum] = index;
  2681. /* This is a simple one to one mapping, here... */
  2682. polyidx[faceNum] = faceNum;
  2683. faceNum++;
  2684. }
  2685. }
  2686. }
  2687. edgeNum += numFinalEdges;
  2688. }
  2689. for (index = 0; index < totedge; ++index) {
  2690. CCGEdge *e = ccgdm->edgeMap[index].edge;
  2691. int numFinalEdges = edgeSize - 1;
  2692. int mapIndex = ccgDM_getEdgeMapIndex(ss, e);
  2693. int x;
  2694. int vertIdx[2];
  2695. int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
  2696. CCGVert *v;
  2697. v = ccgSubSurf_getEdgeVert0(e);
  2698. vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
  2699. v = ccgSubSurf_getEdgeVert1(e);
  2700. vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
  2701. ccgdm->edgeMap[index].startVert = vertNum;
  2702. ccgdm->edgeMap[index].startEdge = edgeNum;
  2703. if (edgeIdx >= 0 && edgeFlags)
  2704. edgeFlags[edgeIdx] = medge[edgeIdx].flag;
  2705. /* set the edge base vert */
  2706. *((int *)ccgSubSurf_getEdgeUserData(ss, e)) = vertNum;
  2707. for (x = 1; x < edgeSize - 1; x++) {
  2708. float w[2];
  2709. w[1] = (float) x / (edgeSize - 1);
  2710. w[0] = 1 - w[1];
  2711. DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, w, 2, vertNum);
  2712. if (vertOrigIndex) {
  2713. *vertOrigIndex = ORIGINDEX_NONE;
  2714. ++vertOrigIndex;
  2715. }
  2716. ++vertNum;
  2717. }
  2718. for (i = 0; i < numFinalEdges; ++i) {
  2719. if (has_edge_origindex) {
  2720. *(int *)DM_get_edge_data(&ccgdm->dm, edgeNum + i, CD_ORIGINDEX) = mapIndex;
  2721. }
  2722. }
  2723. edgeNum += numFinalEdges;
  2724. }
  2725. if (useSubsurfUv) {
  2726. CustomData *ldata = &ccgdm->dm.loopData;
  2727. CustomData *dmldata = &dm->loopData;
  2728. int numlayer = CustomData_number_of_layers(ldata, CD_MLOOPUV);
  2729. int dmnumlayer = CustomData_number_of_layers(dmldata, CD_MLOOPUV);
  2730. for (i = 0; i < numlayer && i < dmnumlayer; i++)
  2731. set_subsurf_uv(ss, dm, &ccgdm->dm, i);
  2732. }
  2733. for (index = 0; index < totvert; ++index) {
  2734. CCGVert *v = ccgdm->vertMap[index].vert;
  2735. int mapIndex = ccgDM_getVertMapIndex(ccgdm->ss, v);
  2736. int vertIdx;
  2737. vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
  2738. ccgdm->vertMap[index].startVert = vertNum;
  2739. /* set the vert base vert */
  2740. *((int *) ccgSubSurf_getVertUserData(ss, v)) = vertNum;
  2741. DM_copy_vert_data(dm, &ccgdm->dm, vertIdx, vertNum, 1);
  2742. if (vertOrigIndex) {
  2743. *vertOrigIndex = mapIndex;
  2744. ++vertOrigIndex;
  2745. }
  2746. ++vertNum;
  2747. }
  2748. ccgdm->dm.numVertData = vertNum;
  2749. ccgdm->dm.numEdgeData = edgeNum;
  2750. ccgdm->dm.numTessFaceData = faceNum;
  2751. ccgdm->dm.numLoopData = loopindex2;
  2752. ccgdm->dm.numPolyData = faceNum;
  2753. /* All tessellated CD layers were updated! */
  2754. ccgdm->dm.dirty &= ~DM_DIRTY_TESS_CDLAYERS;
  2755. BLI_array_free(vertidx);
  2756. BLI_array_free(loopidx);
  2757. free_ss_weights(&wtable);
  2758. return ccgdm;
  2759. }
  2760. /***/
  2761. struct DerivedMesh *subsurf_make_derived_from_derived(
  2762. struct DerivedMesh *dm,
  2763. struct SubsurfModifierData *smd,
  2764. int useRenderParams, float (*vertCos)[3],
  2765. int isFinalCalc, int forEditMode, int inEditMode)
  2766. {
  2767. int useSimple = smd->subdivType == ME_SIMPLE_SUBSURF;
  2768. CCGFlags useAging = smd->flags & eSubsurfModifierFlag_DebugIncr ? CCG_USE_AGING : 0;
  2769. int useSubsurfUv = smd->flags & eSubsurfModifierFlag_SubsurfUv;
  2770. int drawInteriorEdges = !(smd->flags & eSubsurfModifierFlag_ControlEdges);
  2771. CCGDerivedMesh *result;
  2772. if (forEditMode) {
  2773. int levels = (smd->modifier.scene) ? get_render_subsurf_level(&smd->modifier.scene->r, smd->levels) : smd->levels;
  2774. smd->emCache = _getSubSurf(smd->emCache, levels, useAging | CCG_CALC_NORMALS);
  2775. ss_sync_from_derivedmesh(smd->emCache, dm, vertCos, useSimple);
  2776. result = getCCGDerivedMesh(smd->emCache,
  2777. drawInteriorEdges,
  2778. useSubsurfUv, dm);
  2779. }
  2780. else if (useRenderParams) {
  2781. /* Do not use cache in render mode. */
  2782. CCGSubSurf *ss;
  2783. int levels = (smd->modifier.scene) ? get_render_subsurf_level(&smd->modifier.scene->r, smd->renderLevels) : smd->renderLevels;
  2784. if (levels == 0)
  2785. return dm;
  2786. ss = _getSubSurf(NULL, levels, CCG_USE_ARENA | CCG_CALC_NORMALS);
  2787. ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
  2788. result = getCCGDerivedMesh(ss,
  2789. drawInteriorEdges, useSubsurfUv, dm);
  2790. result->freeSS = 1;
  2791. }
  2792. else {
  2793. int useIncremental = (smd->flags & eSubsurfModifierFlag_Incremental);
  2794. int levels = (smd->modifier.scene) ? get_render_subsurf_level(&smd->modifier.scene->r, smd->levels) : smd->levels;
  2795. CCGSubSurf *ss;
  2796. /* It is quite possible there is a much better place to do this. It
  2797. * depends a bit on how rigorously we expect this function to never
  2798. * be called in editmode. In semi-theory we could share a single
  2799. * cache, but the handles used inside and outside editmode are not
  2800. * the same so we would need some way of converting them. Its probably
  2801. * not worth the effort. But then why am I even writing this long
  2802. * comment that no one will read? Hmmm. - zr
  2803. *
  2804. * Addendum: we can't really ensure that this is never called in edit
  2805. * mode, so now we have a parameter to verify it. - brecht
  2806. */
  2807. if (!inEditMode && smd->emCache) {
  2808. ccgSubSurf_free(smd->emCache);
  2809. smd->emCache = NULL;
  2810. }
  2811. if (useIncremental && isFinalCalc) {
  2812. smd->mCache = ss = _getSubSurf(smd->mCache, levels, useAging | CCG_CALC_NORMALS);
  2813. ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
  2814. result = getCCGDerivedMesh(smd->mCache,
  2815. drawInteriorEdges,
  2816. useSubsurfUv, dm);
  2817. }
  2818. else {
  2819. if (smd->mCache && isFinalCalc) {
  2820. ccgSubSurf_free(smd->mCache);
  2821. smd->mCache = NULL;
  2822. }
  2823. ss = _getSubSurf(NULL, levels, CCG_USE_ARENA | CCG_CALC_NORMALS);
  2824. ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
  2825. result = getCCGDerivedMesh(ss, drawInteriorEdges, useSubsurfUv, dm);
  2826. if (isFinalCalc)
  2827. smd->mCache = ss;
  2828. else
  2829. result->freeSS = 1;
  2830. }
  2831. }
  2832. return (DerivedMesh *)result;
  2833. }
  2834. void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3])
  2835. {
  2836. /* Finds the subsurf limit positions for the verts in a mesh
  2837. * and puts them in an array of floats. Please note that the
  2838. * calculated vert positions is incorrect for the verts
  2839. * on the boundary of the mesh.
  2840. */
  2841. CCGSubSurf *ss = _getSubSurf(NULL, 1, CCG_USE_ARENA);
  2842. float edge_sum[3], face_sum[3];
  2843. CCGVertIterator *vi;
  2844. DerivedMesh *dm = CDDM_from_mesh(me, NULL);
  2845. ss_sync_from_derivedmesh(ss, dm, NULL, 0);
  2846. vi = ccgSubSurf_getVertIterator(ss);
  2847. for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
  2848. CCGVert *v = ccgVertIterator_getCurrent(vi);
  2849. int idx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
  2850. int N = ccgSubSurf_getVertNumEdges(v);
  2851. int numFaces = ccgSubSurf_getVertNumFaces(v);
  2852. float *co;
  2853. int i;
  2854. edge_sum[0] = edge_sum[1] = edge_sum[2] = 0.0;
  2855. face_sum[0] = face_sum[1] = face_sum[2] = 0.0;
  2856. for (i = 0; i < N; i++) {
  2857. CCGEdge *e = ccgSubSurf_getVertEdge(v, i);
  2858. add_v3_v3v3(edge_sum, edge_sum, ccgSubSurf_getEdgeData(ss, e, 1));
  2859. }
  2860. for (i = 0; i < numFaces; i++) {
  2861. CCGFace *f = ccgSubSurf_getVertFace(v, i);
  2862. add_v3_v3(face_sum, ccgSubSurf_getFaceCenterData(f));
  2863. }
  2864. /* ad-hoc correction for boundary vertices, to at least avoid them
  2865. * moving completely out of place (brecht) */
  2866. if (numFaces && numFaces != N)
  2867. mul_v3_fl(face_sum, (float)N / (float)numFaces);
  2868. co = ccgSubSurf_getVertData(ss, v);
  2869. positions_r[idx][0] = (co[0] * N * N + edge_sum[0] * 4 + face_sum[0]) / (N * (N + 5));
  2870. positions_r[idx][1] = (co[1] * N * N + edge_sum[1] * 4 + face_sum[1]) / (N * (N + 5));
  2871. positions_r[idx][2] = (co[2] * N * N + edge_sum[2] * 4 + face_sum[2]) / (N * (N + 5));
  2872. }
  2873. ccgVertIterator_free(vi);
  2874. ccgSubSurf_free(ss);
  2875. dm->release(dm);
  2876. }