PageRenderTime 67ms CodeModel.GetById 15ms 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

Large files files are truncated, but you can click here to view the full file

  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) * gri

Large files files are truncated, but you can click here to view the full file