Merge branch 'master' into blender2.8
[blender.git] / source / blender / alembic / intern / abc_hair.cc
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  * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "abc_hair.h"
24
25 #include <cstdio>
26
27 #include "abc_transform.h"
28 #include "abc_util.h"
29
30 extern "C" {
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_modifier_types.h"
34
35 #include "BLI_listbase.h"
36 #include "BLI_math_geom.h"
37
38 #include "BKE_DerivedMesh.h"
39 #include "BKE_object.h"
40 #include "BKE_particle.h"
41 }
42
43 using Alembic::Abc::P3fArraySamplePtr;
44
45 using Alembic::AbcGeom::OCurves;
46 using Alembic::AbcGeom::OCurvesSchema;
47 using Alembic::AbcGeom::ON3fGeomParam;
48 using Alembic::AbcGeom::OV2fGeomParam;
49
50 /* ************************************************************************** */
51
52 AbcHairWriter::AbcHairWriter(Scene *scene,
53                              Object *ob,
54                              AbcTransformWriter *parent,
55                              uint32_t time_sampling,
56                              ExportSettings &settings,
57                              ParticleSystem *psys)
58     : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
59     , m_uv_warning_shown(false)
60 {
61         m_psys = psys;
62
63         OCurves curves(parent->alembicXform(), psys->name, m_time_sampling);
64         m_schema = curves.getSchema();
65 }
66
67 void AbcHairWriter::do_write()
68 {
69         if (!m_psys) {
70                 return;
71         }
72
73         ParticleSystemModifierData *psmd = psys_get_modifier(m_object, m_psys);
74
75         if (!psmd->dm_final) {
76                 return;
77         }
78
79         DerivedMesh *dm = mesh_create_derived_render(m_scene, m_object, CD_MASK_MESH);
80         DM_ensure_tessface(dm);
81
82         std::vector<Imath::V3f> verts;
83         std::vector<int32_t> hvertices;
84         std::vector<Imath::V2f> uv_values;
85         std::vector<Imath::V3f> norm_values;
86
87         if (m_psys->pathcache) {
88                 ParticleSettings *part = m_psys->part;
89
90                 write_hair_sample(dm, part, verts, norm_values, uv_values, hvertices);
91
92                 if (m_settings.export_child_hairs && m_psys->childcache) {
93                         write_hair_child_sample(dm, part, verts, norm_values, uv_values, hvertices);
94                 }
95         }
96
97         dm->release(dm);
98
99         Alembic::Abc::P3fArraySample iPos(verts);
100         m_sample = OCurvesSchema::Sample(iPos, hvertices);
101         m_sample.setBasis(Alembic::AbcGeom::kNoBasis);
102         m_sample.setType(Alembic::AbcGeom::kLinear);
103         m_sample.setWrap(Alembic::AbcGeom::kNonPeriodic);
104
105         if (!uv_values.empty()) {
106                 OV2fGeomParam::Sample uv_smp;
107                 uv_smp.setVals(uv_values);
108                 m_sample.setUVs(uv_smp);
109         }
110
111         if (!norm_values.empty()) {
112                 ON3fGeomParam::Sample norm_smp;
113                 norm_smp.setVals(norm_values);
114                 m_sample.setNormals(norm_smp);
115         }
116
117         m_sample.setSelfBounds(bounds());
118         m_schema.set(m_sample);
119 }
120
121 void AbcHairWriter::write_hair_sample(DerivedMesh *dm,
122                                       ParticleSettings *part,
123                                       std::vector<Imath::V3f> &verts,
124                                       std::vector<Imath::V3f> &norm_values,
125                                       std::vector<Imath::V2f> &uv_values,
126                                       std::vector<int32_t> &hvertices)
127 {
128         /* Get untransformed vertices, there's a xform under the hair. */
129         float inv_mat[4][4];
130         invert_m4_m4_safe(inv_mat, m_object->obmat);
131
132         MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
133         MFace *mface = dm->getTessFaceArray(dm);
134         MVert *mverts = dm->getVertArray(dm);
135
136         if ((!mtface || !mface) && !m_uv_warning_shown) {
137                 std::fprintf(stderr, "Warning, no UV set found for underlying geometry of %s.\n",
138                              m_object->id.name + 2);
139                 m_uv_warning_shown = true;
140         }
141
142         ParticleData * pa = m_psys->particles;
143         int k;
144
145         ParticleCacheKey **cache = m_psys->pathcache;
146         ParticleCacheKey *path;
147         float normal[3];
148         Imath::V3f tmp_nor;
149
150         for (int p = 0; p < m_psys->totpart; ++p, ++pa) {
151                 /* underlying info for faces-only emission */
152                 path = cache[p];
153
154                 if (part->from == PART_FROM_FACE && mtface) {
155                         const int num = pa->num_dmcache >= 0 ? pa->num_dmcache : pa->num;
156
157                         if (num < dm->getNumTessFaces(dm)) {
158                                 MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
159                                 MTFace *tface = mtface + num;
160
161                                 if (mface) {
162                                         float r_uv[2], mapfw[4], vec[3];
163
164                                         psys_interpolate_uvs(tface, face->v4, pa->fuv, r_uv);
165                                         uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
166
167                                         psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, normal, NULL, NULL, NULL, NULL);
168
169                                         copy_yup_from_zup(tmp_nor.getValue(), normal);
170                                         norm_values.push_back(tmp_nor);
171                                 }
172                         }
173                         else {
174                                 std::fprintf(stderr, "Particle to faces overflow (%d/%d)\n", num, dm->getNumTessFaces(dm));
175                         }
176                 }
177                 else if (part->from == PART_FROM_VERT && mtface) {
178                         /* vertex id */
179                         const int num = (pa->num_dmcache >= 0) ? pa->num_dmcache : pa->num;
180
181                         /* iterate over all faces to find a corresponding underlying UV */
182                         for (int n = 0; n < dm->getNumTessFaces(dm); ++n) {
183                                 MFace *face  = static_cast<MFace *>(dm->getTessFaceData(dm, n, CD_MFACE));
184                                 MTFace *tface = mtface + n;
185                                 unsigned int vtx[4];
186                                 vtx[0] = face->v1;
187                                 vtx[1] = face->v2;
188                                 vtx[2] = face->v3;
189                                 vtx[3] = face->v4;
190                                 bool found = false;
191
192                                 for (int o = 0; o < 4; ++o) {
193                                         if (o > 2 && vtx[o] == 0) {
194                                                 break;
195                                         }
196
197                                         if (vtx[o] == num) {
198                                                 uv_values.push_back(Imath::V2f(tface->uv[o][0], tface->uv[o][1]));
199
200                                                 MVert *mv = mverts + vtx[o];
201
202                                                 normal_short_to_float_v3(normal, mv->no);
203                                                 copy_yup_from_zup(tmp_nor.getValue(), normal);
204                                                 norm_values.push_back(tmp_nor);
205                                                 found = true;
206                                                 break;
207                                         }
208                                 }
209
210                                 if (found) {
211                                         break;
212                                 }
213                         }
214                 }
215
216                 int steps = path->segments + 1;
217                 hvertices.push_back(steps);
218
219                 for (k = 0; k < steps; ++k) {
220                         float vert[3];
221                         copy_v3_v3(vert, path->co);
222                         mul_m4_v3(inv_mat, vert);
223
224                         /* Convert Z-up to Y-up. */
225                         verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));
226
227                         ++path;
228                 }
229         }
230 }
231
232 void AbcHairWriter::write_hair_child_sample(DerivedMesh *dm,
233                                             ParticleSettings *part,
234                                             std::vector<Imath::V3f> &verts,
235                                             std::vector<Imath::V3f> &norm_values,
236                                             std::vector<Imath::V2f> &uv_values,
237                                             std::vector<int32_t> &hvertices)
238 {
239         /* Get untransformed vertices, there's a xform under the hair. */
240         float inv_mat[4][4];
241         invert_m4_m4_safe(inv_mat, m_object->obmat);
242
243         MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
244         MVert *mverts = dm->getVertArray(dm);
245
246         ParticleCacheKey **cache = m_psys->childcache;
247         ParticleCacheKey *path;
248
249         ChildParticle *pc = m_psys->child;
250
251         for (int p = 0; p < m_psys->totchild; ++p, ++pc) {
252                 path = cache[p];
253
254                 if (part->from == PART_FROM_FACE &&
255                         part->childtype != PART_CHILD_PARTICLES &&
256                         mtface) {
257                         const int num = pc->num;
258                         if (num < 0) {
259                                 ABC_LOG(m_settings.logger)
260                                         << "Warning, child particle of hair system " << m_psys->name
261                                         << " has unknown face index of geometry of "<< (m_object->id.name + 2)
262                                         << ", skipping child hair." << std::endl;
263                                 continue;
264                         }
265
266                         MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
267                         MTFace *tface = mtface + num;
268
269                         float r_uv[2], tmpnor[3], mapfw[4], vec[3];
270
271                         psys_interpolate_uvs(tface, face->v4, pc->fuv, r_uv);
272                         uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
273
274                         psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, tmpnor, NULL, NULL, NULL, NULL);
275
276                         /* Convert Z-up to Y-up. */
277                         norm_values.push_back(Imath::V3f(tmpnor[0], tmpnor[2], -tmpnor[1]));
278                 }
279                 else {
280                         if (uv_values.size()) {
281                                 uv_values.push_back(uv_values[pc->parent]);
282                         }
283                         if (norm_values.size()) {
284                                 norm_values.push_back(norm_values[pc->parent]);
285                         }
286                 }
287
288                 int steps = path->segments + 1;
289                 hvertices.push_back(steps);
290
291                 for (int k = 0; k < steps; ++k) {
292                         float vert[3];
293                         copy_v3_v3(vert, path->co);
294                         mul_m4_v3(inv_mat, vert);
295
296                         /* Convert Z-up to Y-up. */
297                         verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));
298
299                         ++path;
300                 }
301         }
302 }