@@ -1174,6 +1174,14 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const Platform::Path &filename
1174
1174
// Export the mesh as a VRML text file / WRL.
1175
1175
// -----------------------------------------------------------------------------
1176
1176
void SolveSpaceUI::ExportMeshAsVrmlTo (FILE *f, const Platform::Path &filename, SMesh *sm) {
1177
+ struct STriangleSpan {
1178
+ STriangle *first, *past_last;
1179
+
1180
+ STriangle *begin () const { return first; }
1181
+ STriangle *end () const { return past_last; }
1182
+ };
1183
+
1184
+
1177
1185
std::string basename = filename.FileStem ();
1178
1186
for (auto & c : basename) {
1179
1187
if (!(isalnum (c) || ((unsigned )c >= 0x80 ))) {
@@ -1185,88 +1193,114 @@ void SolveSpaceUI::ExportMeshAsVrmlTo(FILE *f, const Platform::Path &filename, S
1185
1193
" #Exported from SolveSpace %s\n "
1186
1194
" \n "
1187
1195
" DEF %s Transform {\n "
1188
- " children [\n "
1189
- " Shape {\n "
1190
- " appearance Appearance {\n "
1191
- " material DEF %s_material Material {\n "
1192
- " diffuseColor %f %f %f\n "
1193
- " ambientIntensity %f\n "
1194
- " transparency 0.0\n "
1195
- " }\n "
1196
- " }\n "
1197
- " geometry IndexedFaceSet {\n "
1198
- " colorPerVertex TRUE\n "
1199
- " coord Coordinate { point [\n " ,
1196
+ " children [" ,
1200
1197
PACKAGE_VERSION,
1201
- basename.c_str (),
1202
- basename.c_str (),
1203
- SS.ambientIntensity ,
1204
- SS.ambientIntensity ,
1205
- SS.ambientIntensity ,
1206
- SS.ambientIntensity );
1198
+ basename.c_str ());
1207
1199
1208
- SPointList spl = {};
1209
1200
1210
- for (const auto & tr : sm->l ) {
1211
- spl.IncrementTagFor (tr.a );
1212
- spl.IncrementTagFor (tr.b );
1213
- spl.IncrementTagFor (tr.c );
1201
+ std::map<std::uint8_t , std::vector<STriangleSpan>> opacities;
1202
+ STriangle *start = sm->l .begin ();
1203
+ std::uint8_t last_opacity = start->meta .color .alpha ;
1204
+ for (auto & tr : sm->l ) {
1205
+ if (tr.meta .color .alpha != last_opacity) {
1206
+ opacities[last_opacity].push_back (STriangleSpan{start, &tr});
1207
+ start = &tr;
1208
+ last_opacity = start->meta .color .alpha ;
1209
+ }
1214
1210
}
1211
+ opacities[last_opacity].push_back (STriangleSpan{start, sm->l .end ()});
1212
+
1213
+ for (auto && op : opacities) {
1214
+ fprintf (f, " \n "
1215
+ " Shape {\n "
1216
+ " appearance Appearance {\n "
1217
+ " material DEF %s_material_%u Material {\n "
1218
+ " diffuseColor %f %f %f\n "
1219
+ " ambientIntensity %f\n "
1220
+ " transparency %f\n "
1221
+ " }\n "
1222
+ " }\n "
1223
+ " geometry IndexedFaceSet {\n "
1224
+ " colorPerVertex TRUE\n "
1225
+ " coord Coordinate { point [\n " ,
1226
+ basename.c_str (),
1227
+ (unsigned )op.first ,
1228
+ SS.ambientIntensity ,
1229
+ SS.ambientIntensity ,
1230
+ SS.ambientIntensity ,
1231
+ SS.ambientIntensity ,
1232
+ 1 .f - ((float )op.first / 255 .0f ));
1233
+
1234
+ SPointList spl = {};
1235
+
1236
+ for (const auto & sp : op.second ) {
1237
+ for (const auto & tr : sp) {
1238
+ spl.IncrementTagFor (tr.a );
1239
+ spl.IncrementTagFor (tr.b );
1240
+ spl.IncrementTagFor (tr.c );
1241
+ }
1242
+ }
1215
1243
1216
- // Output all the vertices.
1217
- for (auto sp : spl.l ) {
1218
- fprintf (f, " %f %f %f,\n " ,
1219
- sp.p .x / SS.exportScale ,
1220
- sp.p .y / SS.exportScale ,
1221
- sp.p .z / SS.exportScale );
1222
- }
1244
+ // Output all the vertices.
1245
+ for (auto sp : spl.l ) {
1246
+ fprintf (f, " %f %f %f,\n " ,
1247
+ sp.p .x / SS.exportScale ,
1248
+ sp.p .y / SS.exportScale ,
1249
+ sp.p .z / SS.exportScale );
1250
+ }
1223
1251
1224
- fputs (" ] }\n "
1225
- " coordIndex [\n " , f);
1226
- // And now all the triangular faces, in terms of those vertices.
1227
- for (const auto & tr : sm->l ) {
1228
- fprintf (f, " %d, %d, %d, -1,\n " ,
1229
- spl.IndexForPoint (tr.a ),
1230
- spl.IndexForPoint (tr.b ),
1231
- spl.IndexForPoint (tr.c ));
1232
- }
1252
+ fputs (" ] }\n "
1253
+ " coordIndex [\n " , f);
1254
+ // And now all the triangular faces, in terms of those vertices.
1255
+ for (const auto & sp : op.second ) {
1256
+ for (const auto & tr : sp) {
1257
+ fprintf (f, " %d, %d, %d, -1,\n " ,
1258
+ spl.IndexForPoint (tr.a ),
1259
+ spl.IndexForPoint (tr.b ),
1260
+ spl.IndexForPoint (tr.c ));
1261
+ }
1262
+ }
1233
1263
1234
- fputs (" ]\n "
1235
- " color Color { color [\n " , f);
1236
- // Output triangle colors.
1237
- std::vector<int > triangle_colour_ids;
1238
- std::vector<RgbaColor> colours_present;
1239
- for (const auto & tr : sm->l ) {
1240
- const auto colour_itr = std::find_if (colours_present.begin (), colours_present.end (),
1241
- [&](const RgbaColor & c) {
1242
- return c.Equals (tr.meta .color );
1243
- });
1244
- if (colour_itr == colours_present.end ()) {
1245
- fprintf (f, " %.10f %.10f %.10f,\n " ,
1246
- tr.meta .color .redF (),
1247
- tr.meta .color .greenF (),
1248
- tr.meta .color .blueF ());
1249
- triangle_colour_ids.push_back (colours_present.size ());
1250
- colours_present.insert (colours_present.end (), tr.meta .color );
1251
- } else {
1252
- triangle_colour_ids.push_back (colour_itr - colours_present.begin ());
1264
+ fputs (" ]\n "
1265
+ " color Color { color [\n " , f);
1266
+ // Output triangle colors.
1267
+ std::vector<int > triangle_colour_ids;
1268
+ std::vector<RgbaColor> colours_present;
1269
+ for (const auto & sp : op.second ) {
1270
+ for (const auto & tr : sp) {
1271
+ const auto colour_itr = std::find_if (colours_present.begin (), colours_present.end (),
1272
+ [&](const RgbaColor & c) {
1273
+ return c.Equals (tr.meta .color );
1274
+ });
1275
+ if (colour_itr == colours_present.end ()) {
1276
+ fprintf (f, " %.10f %.10f %.10f,\n " ,
1277
+ tr.meta .color .redF (),
1278
+ tr.meta .color .greenF (),
1279
+ tr.meta .color .blueF ());
1280
+ triangle_colour_ids.push_back (colours_present.size ());
1281
+ colours_present.insert (colours_present.end (), tr.meta .color );
1282
+ } else {
1283
+ triangle_colour_ids.push_back (colour_itr - colours_present.begin ());
1284
+ }
1285
+ }
1286
+ }
1287
+
1288
+ fputs (" ] }\n "
1289
+ " colorIndex [\n " , f);
1290
+
1291
+ for (auto colour_idx : triangle_colour_ids) {
1292
+ fprintf (f, " %d, %d, %d, -1,\n " , colour_idx, colour_idx, colour_idx);
1253
1293
}
1254
- }
1255
1294
1256
- fputs (" ] }\n "
1257
- " colorIndex [\n " , f);
1295
+ fputs (" ]\n "
1296
+ " }\n "
1297
+ " }\n " , f);
1258
1298
1259
- for (auto colour_idx : triangle_colour_ids) {
1260
- fprintf (f, " %d, %d, %d, -1,\n " , colour_idx, colour_idx, colour_idx);
1299
+ spl.Clear ();
1261
1300
}
1262
1301
1263
- fputs (" ]\n "
1264
- " }\n "
1265
- " }\n "
1266
- " ]\n "
1302
+ fputs (" ]\n "
1267
1303
" }\n " , f);
1268
-
1269
- spl.Clear ();
1270
1304
}
1271
1305
1272
1306
// -----------------------------------------------------------------------------
0 commit comments