@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
27
27
#include " minimap.h"
28
28
#include " content_mapblock.h"
29
29
#include " util/directiontables.h"
30
+ #include " client/meshgen/collector.h"
30
31
#include " client/renderingengine.h"
31
32
#include < array>
32
33
@@ -450,16 +451,14 @@ static void getNodeTextureCoords(v3f base, const v3f &scale, v3s16 dir, float *u
450
451
451
452
struct FastFace
452
453
{
453
- TileLayer layer ;
454
+ TileSpec tile ;
454
455
video::S3DVertex vertices[4 ]; // Precalculated vertices
455
456
/* !
456
457
* The face is divided into two triangles. If this is true,
457
458
* vertices 0 and 2 are connected, othervise vertices 1 and 3
458
459
* are connected.
459
460
*/
460
461
bool vertex_0_2_connected;
461
- u8 layernum;
462
- bool world_aligned;
463
462
};
464
463
465
464
static void makeFastFace (const TileSpec &tile, u16 li0, u16 li1, u16 li2, u16 li3,
@@ -630,35 +629,25 @@ static void makeFastFace(const TileSpec &tile, u16 li0, u16 li1, u16 li2, u16 li
630
629
core::vector2d<f32>(x0, y0 ),
631
630
core::vector2d<f32>(x0 + w * abs_scale, y0 ) };
632
631
633
- for (int layernum = 0 ; layernum < MAX_TILE_LAYERS; layernum++) {
634
- const TileLayer *layer = &tile.layers [layernum];
635
- if (layer->texture_id == 0 )
636
- continue ;
637
-
638
- // equivalent to dest.push_back(FastFace()) but faster
639
- dest.emplace_back ();
640
- FastFace& face = *dest.rbegin ();
641
-
642
- for (u8 i = 0 ; i < 4 ; i++) {
643
- video::SColor c = encode_light (li[i], tile.emissive_light );
644
- if (!tile.emissive_light )
645
- applyFacesShading (c, normal );
646
-
647
- face.vertices [i] = video::S3DVertex (vertex_pos[i], normal , c, f[i]);
648
- }
649
-
650
- /*
651
- Revert triangles for nicer looking gradient if the
652
- brightness of vertices 1 and 3 differ less than
653
- the brightness of vertices 0 and 2.
654
- */
655
- face.vertex_0_2_connected = vertex_0_2_connected;
632
+ // equivalent to dest.push_back(FastFace()) but faster
633
+ dest.emplace_back ();
634
+ FastFace& face = *dest.rbegin ();
656
635
657
- face.layer = *layer;
658
- face.layernum = layernum;
636
+ for (u8 i = 0 ; i < 4 ; i++) {
637
+ video::SColor c = encode_light (li[i], tile.emissive_light );
638
+ if (!tile.emissive_light )
639
+ applyFacesShading (c, normal );
659
640
660
- face.world_aligned = tile. world_aligned ;
641
+ face.vertices [i] = video::S3DVertex (vertex_pos[i], normal , c, f[i]) ;
661
642
}
643
+
644
+ /*
645
+ Revert triangles for nicer looking gradient if the
646
+ brightness of vertices 1 and 3 differ less than
647
+ the brightness of vertices 0 and 2.
648
+ */
649
+ face.vertex_0_2_connected = vertex_0_2_connected;
650
+ face.tile = tile;
662
651
}
663
652
664
653
/*
@@ -1012,6 +1001,20 @@ static void updateAllFastFaceRows(MeshMakeData *data,
1012
1001
dest);
1013
1002
}
1014
1003
1004
+ static void applyTileColor (PreMeshBuffer &pmb)
1005
+ {
1006
+ video::SColor tc = pmb.layer .color ;
1007
+ if (tc == video::SColor (0xFFFFFFFF ))
1008
+ return ;
1009
+ for (video::S3DVertex &vertex : pmb.vertices ) {
1010
+ video::SColor *c = &vertex.Color ;
1011
+ c->set (c->getAlpha (),
1012
+ c->getRed () * tc.getRed () / 255 ,
1013
+ c->getGreen () * tc.getGreen () / 255 ,
1014
+ c->getBlue () * tc.getBlue () / 255 );
1015
+ }
1016
+ }
1017
+
1015
1018
/*
1016
1019
MapBlockMesh
1017
1020
*/
@@ -1061,7 +1064,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1061
1064
Convert FastFaces to MeshCollector
1062
1065
*/
1063
1066
1064
- MeshCollector collector (m_use_tangent_vertices) ;
1067
+ MeshCollector collector;
1065
1068
1066
1069
{
1067
1070
// avg 0ms (100ms spikes when loading textures the first time)
@@ -1071,15 +1074,9 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1071
1074
for (const FastFace &f : fastfaces_new) {
1072
1075
static const u16 indices[] = {0 , 1 , 2 , 2 , 3 , 0 };
1073
1076
static const u16 indices_alternate[] = {0 , 1 , 3 , 2 , 3 , 1 };
1074
-
1075
- if (!f.layer .texture )
1076
- continue ;
1077
-
1078
1077
const u16 *indices_p =
1079
1078
f.vertex_0_2_connected ? indices : indices_alternate;
1080
-
1081
- collector.append (f.layer , f.vertices , 4 , indices_p, 6 ,
1082
- f.layernum , f.world_aligned );
1079
+ collector.append (f.tile , f.vertices , 4 , indices_p, 6 );
1083
1080
}
1084
1081
}
1085
1082
@@ -1096,8 +1093,6 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1096
1093
generator.generate ();
1097
1094
}
1098
1095
1099
- collector.applyTileColors ();
1100
-
1101
1096
/*
1102
1097
Convert MeshCollector to SMesh
1103
1098
*/
@@ -1107,6 +1102,8 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1107
1102
{
1108
1103
PreMeshBuffer &p = collector.prebuffers [layer][i];
1109
1104
1105
+ applyTileColor (p);
1106
+
1110
1107
// Generate animation data
1111
1108
// - Cracks
1112
1109
if (p.layer .material_flags & MATERIAL_FLAG_CRACK) {
@@ -1151,16 +1148,10 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1151
1148
// Dummy sunlight to handle non-sunlit areas
1152
1149
video::SColorf sunlight;
1153
1150
get_sunlight_color (&sunlight, 0 );
1154
- u32 vertex_count = m_use_tangent_vertices ?
1155
- p.tangent_vertices .size () : p.vertices .size ();
1151
+ u32 vertex_count = p.vertices .size ();
1156
1152
for (u32 j = 0 ; j < vertex_count; j++) {
1157
- video::SColor *vc;
1158
- if (m_use_tangent_vertices) {
1159
- vc = &p.tangent_vertices [j].Color ;
1160
- } else {
1161
- vc = &p.vertices [j].Color ;
1162
- }
1163
- video::SColor copy (*vc);
1153
+ video::SColor *vc = &p.vertices [j].Color ;
1154
+ video::SColor copy = *vc;
1164
1155
if (vc->getAlpha () == 0 ) // No sunlight - no need to animate
1165
1156
final_color_blend (vc, copy, sunlight); // Finalize color
1166
1157
else // Record color to animate
@@ -1197,24 +1188,23 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1197
1188
if (m_use_tangent_vertices) {
1198
1189
scene::SMeshBufferTangents *buf =
1199
1190
new scene::SMeshBufferTangents ();
1200
- // Set material
1201
1191
buf->Material = material;
1202
- // Add to mesh
1192
+ buf->Vertices .reallocate (p.vertices .size ());
1193
+ buf->Indices .reallocate (p.indices .size ());
1194
+ for (const video::S3DVertex &v: p.vertices )
1195
+ buf->Vertices .push_back (video::S3DVertexTangents (v.Pos , v.Color , v.TCoords ));
1196
+ for (u16 i: p.indices )
1197
+ buf->Indices .push_back (i);
1198
+ buf->recalculateBoundingBox ();
1203
1199
mesh->addMeshBuffer (buf);
1204
- // Mesh grabbed it
1205
1200
buf->drop ();
1206
- buf->append (&p.tangent_vertices [0 ], p.tangent_vertices .size (),
1207
- &p.indices [0 ], p.indices .size ());
1208
1201
} else {
1209
1202
scene::SMeshBuffer *buf = new scene::SMeshBuffer ();
1210
- // Set material
1211
1203
buf->Material = material;
1212
- // Add to mesh
1213
- mesh->addMeshBuffer (buf);
1214
- // Mesh grabbed it
1215
- buf->drop ();
1216
1204
buf->append (&p.vertices [0 ], p.vertices .size (),
1217
1205
&p.indices [0 ], p.indices .size ());
1206
+ mesh->addMeshBuffer (buf);
1207
+ buf->drop ();
1218
1208
}
1219
1209
}
1220
1210
@@ -1370,193 +1360,6 @@ void MapBlockMesh::updateCameraOffset(v3s16 camera_offset)
1370
1360
}
1371
1361
}
1372
1362
1373
- /*
1374
- MeshCollector
1375
- */
1376
-
1377
- void MeshCollector::append (const TileSpec &tile,
1378
- const video::S3DVertex *vertices, u32 numVertices,
1379
- const u16 *indices, u32 numIndices)
1380
- {
1381
- for (int layernum = 0 ; layernum < MAX_TILE_LAYERS; layernum++) {
1382
- const TileLayer *layer = &tile.layers [layernum];
1383
- if (layer->texture_id == 0 )
1384
- continue ;
1385
- append (*layer, vertices, numVertices, indices, numIndices,
1386
- layernum, tile.world_aligned );
1387
- }
1388
- }
1389
-
1390
- void MeshCollector::append (const TileLayer &layer,
1391
- const video::S3DVertex *vertices, u32 numVertices,
1392
- const u16 *indices, u32 numIndices, u8 layernum,
1393
- bool use_scale)
1394
- {
1395
- if (numIndices > 65535 ) {
1396
- dstream << " FIXME: MeshCollector::append() called with numIndices="
1397
- << numIndices << " (limit 65535)" << std::endl;
1398
- return ;
1399
- }
1400
- std::vector<PreMeshBuffer> *buffers = &prebuffers[layernum];
1401
-
1402
- PreMeshBuffer *p = NULL ;
1403
- for (PreMeshBuffer &pp : *buffers) {
1404
- if (pp.layer == layer && pp.indices .size () + numIndices <= 65535 ) {
1405
- p = &pp;
1406
- break ;
1407
- }
1408
- }
1409
-
1410
- if (p == NULL ) {
1411
- PreMeshBuffer pp;
1412
- pp.layer = layer;
1413
- buffers->push_back (pp);
1414
- p = &(*buffers)[buffers->size () - 1 ];
1415
- }
1416
-
1417
- f32 scale = 1.0 ;
1418
- if (use_scale)
1419
- scale = 1.0 / layer.scale ;
1420
-
1421
- u32 vertex_count;
1422
- if (m_use_tangent_vertices) {
1423
- vertex_count = p->tangent_vertices .size ();
1424
- for (u32 i = 0 ; i < numVertices; i++) {
1425
-
1426
- video::S3DVertexTangents vert (vertices[i].Pos , vertices[i].Normal ,
1427
- vertices[i].Color , scale * vertices[i].TCoords );
1428
- p->tangent_vertices .push_back (vert);
1429
- }
1430
- } else {
1431
- vertex_count = p->vertices .size ();
1432
- for (u32 i = 0 ; i < numVertices; i++) {
1433
- video::S3DVertex vert (vertices[i].Pos , vertices[i].Normal ,
1434
- vertices[i].Color , scale * vertices[i].TCoords );
1435
-
1436
- p->vertices .push_back (vert);
1437
- }
1438
- }
1439
-
1440
- for (u32 i = 0 ; i < numIndices; i++) {
1441
- u32 j = indices[i] + vertex_count;
1442
- p->indices .push_back (j);
1443
- }
1444
- }
1445
-
1446
- /*
1447
- MeshCollector - for meshnodes and converted drawtypes.
1448
- */
1449
-
1450
- void MeshCollector::append (const TileSpec &tile,
1451
- const video::S3DVertex *vertices, u32 numVertices,
1452
- const u16 *indices, u32 numIndices,
1453
- v3f pos, video::SColor c, u8 light_source)
1454
- {
1455
- for (int layernum = 0 ; layernum < MAX_TILE_LAYERS; layernum++) {
1456
- const TileLayer *layer = &tile.layers [layernum];
1457
- if (layer->texture_id == 0 )
1458
- continue ;
1459
- append (*layer, vertices, numVertices, indices, numIndices, pos,
1460
- c, light_source, layernum, tile.world_aligned );
1461
- }
1462
- }
1463
-
1464
- void MeshCollector::append (const TileLayer &layer,
1465
- const video::S3DVertex *vertices, u32 numVertices,
1466
- const u16 *indices, u32 numIndices,
1467
- v3f pos, video::SColor c, u8 light_source, u8 layernum,
1468
- bool use_scale)
1469
- {
1470
- if (numIndices > 65535 ) {
1471
- dstream << " FIXME: MeshCollector::append() called with numIndices="
1472
- << numIndices << " (limit 65535)" << std::endl;
1473
- return ;
1474
- }
1475
- std::vector<PreMeshBuffer> *buffers = &prebuffers[layernum];
1476
-
1477
- PreMeshBuffer *p = NULL ;
1478
- for (PreMeshBuffer &pp : *buffers) {
1479
- if (pp.layer == layer && pp.indices .size () + numIndices <= 65535 ) {
1480
- p = &pp;
1481
- break ;
1482
- }
1483
- }
1484
-
1485
- if (p == NULL ) {
1486
- PreMeshBuffer pp;
1487
- pp.layer = layer;
1488
- buffers->push_back (pp);
1489
- p = &(*buffers)[buffers->size () - 1 ];
1490
- }
1491
-
1492
- f32 scale = 1.0 ;
1493
- if (use_scale)
1494
- scale = 1.0 / layer.scale ;
1495
-
1496
- video::SColor original_c = c;
1497
- u32 vertex_count;
1498
- if (m_use_tangent_vertices) {
1499
- vertex_count = p->tangent_vertices .size ();
1500
- for (u32 i = 0 ; i < numVertices; i++) {
1501
- if (!light_source) {
1502
- c = original_c;
1503
- applyFacesShading (c, vertices[i].Normal );
1504
- }
1505
- video::S3DVertexTangents vert (vertices[i].Pos + pos,
1506
- vertices[i].Normal , c, scale * vertices[i].TCoords );
1507
- p->tangent_vertices .push_back (vert);
1508
- }
1509
- } else {
1510
- vertex_count = p->vertices .size ();
1511
- for (u32 i = 0 ; i < numVertices; i++) {
1512
- if (!light_source) {
1513
- c = original_c;
1514
- applyFacesShading (c, vertices[i].Normal );
1515
- }
1516
- video::S3DVertex vert (vertices[i].Pos + pos, vertices[i].Normal , c,
1517
- scale * vertices[i].TCoords );
1518
- p->vertices .push_back (vert);
1519
- }
1520
- }
1521
-
1522
- for (u32 i = 0 ; i < numIndices; i++) {
1523
- u32 j = indices[i] + vertex_count;
1524
- p->indices .push_back (j);
1525
- }
1526
- }
1527
-
1528
- void MeshCollector::applyTileColors ()
1529
- {
1530
- if (m_use_tangent_vertices)
1531
- for (auto &prebuffer : prebuffers) {
1532
- for (PreMeshBuffer &pmb : prebuffer) {
1533
- video::SColor tc = pmb.layer .color ;
1534
- if (tc == video::SColor (0xFFFFFFFF ))
1535
- continue ;
1536
- for (video::S3DVertexTangents &tangent_vertex : pmb.tangent_vertices ) {
1537
- video::SColor *c = &tangent_vertex.Color ;
1538
- c->set (c->getAlpha (), c->getRed () * tc.getRed () / 255 ,
1539
- c->getGreen () * tc.getGreen () / 255 ,
1540
- c->getBlue () * tc.getBlue () / 255 );
1541
- }
1542
- }
1543
- }
1544
- else
1545
- for (auto &prebuffer : prebuffers) {
1546
- for (PreMeshBuffer &pmb : prebuffer) {
1547
- video::SColor tc = pmb.layer .color ;
1548
- if (tc == video::SColor (0xFFFFFFFF ))
1549
- continue ;
1550
- for (video::S3DVertex &vertex : pmb.vertices ) {
1551
- video::SColor *c = &vertex.Color ;
1552
- c->set (c->getAlpha (), c->getRed () * tc.getRed () / 255 ,
1553
- c->getGreen () * tc.getGreen () / 255 ,
1554
- c->getBlue () * tc.getBlue () / 255 );
1555
- }
1556
- }
1557
- }
1558
- }
1559
-
1560
1363
video::SColor encode_light (u16 light, u8 emissive_light)
1561
1364
{
1562
1365
// Get components
0 commit comments