@@ -33,24 +33,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
33
33
#include " util/directiontables.h"
34
34
#include < IMeshManipulator.h>
35
35
36
- static void applyFacesShading (video::SColor& color, float factor)
36
+ static void applyFacesShading (video::SColor & color, const float factor)
37
37
{
38
- color.setRed (core::clamp (core::round32 (color.getRed ()* factor), 0 , 255 ));
39
- color.setGreen (core::clamp (core::round32 (color.getGreen ()* factor), 0 , 255 ));
38
+ color.setRed (core::clamp (core::round32 (color.getRed () * factor), 0 , 255 ));
39
+ color.setGreen (core::clamp (core::round32 (color.getGreen () * factor), 0 , 255 ));
40
40
}
41
41
42
42
/*
43
43
MeshMakeData
44
44
*/
45
45
46
- MeshMakeData::MeshMakeData (IGameDef *gamedef, bool use_shaders):
46
+ MeshMakeData::MeshMakeData (IGameDef *gamedef, bool use_shaders,
47
+ bool use_tangent_vertices):
47
48
m_vmanip(),
48
49
m_blockpos(-1337 ,-1337 ,-1337 ),
49
50
m_crack_pos_relative(-1337 , -1337 , -1337 ),
50
51
m_smooth_lighting(false ),
51
52
m_show_hud(false ),
52
53
m_gamedef(gamedef),
53
- m_use_shaders(use_shaders)
54
+ m_use_shaders(use_shaders),
55
+ m_use_tangent_vertices(use_tangent_vertices)
54
56
{}
55
57
56
58
void MeshMakeData::fill (MapBlock *block)
@@ -1032,6 +1034,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1032
1034
m_daynight_diffs()
1033
1035
{
1034
1036
m_enable_shaders = data->m_use_shaders ;
1037
+ m_use_tangent_vertices = data->m_use_tangent_vertices ;
1035
1038
1036
1039
if (g_settings->getBool (" enable_minimap" )) {
1037
1040
m_minimap_mapblock = new MinimapMapblock;
@@ -1064,15 +1067,14 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1064
1067
Convert FastFaces to MeshCollector
1065
1068
*/
1066
1069
1067
- MeshCollector collector;
1070
+ MeshCollector collector (m_use_tangent_vertices) ;
1068
1071
1069
1072
{
1070
1073
// avg 0ms (100ms spikes when loading textures the first time)
1071
1074
// (NOTE: probably outdated)
1072
1075
// TimeTaker timer2("MeshCollector building");
1073
1076
1074
- for (u32 i=0 ; i<fastfaces_new.size (); i++)
1075
- {
1077
+ for (u32 i = 0 ; i < fastfaces_new.size (); i++) {
1076
1078
FastFace &f = fastfaces_new[i];
1077
1079
1078
1080
const u16 indices[] = {0 ,1 ,2 ,2 ,3 ,0 };
@@ -1150,35 +1152,43 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1150
1152
p.tile .texture = animation_frame.texture ;
1151
1153
}
1152
1154
1153
- for (u32 j = 0 ; j < p.vertices .size (); j++)
1154
- {
1155
- video::S3DVertex *vertex = &p.vertices [j];
1155
+ u32 vertex_count = m_use_tangent_vertices ?
1156
+ p.tangent_vertices .size () : p.vertices .size ();
1157
+ for (u32 j = 0 ; j < vertex_count; j++) {
1158
+ v3f *Normal;
1159
+ video::SColor *vc;
1160
+ if (m_use_tangent_vertices) {
1161
+ vc = &p.tangent_vertices [j].Color ;
1162
+ Normal = &p.tangent_vertices [j].Normal ;
1163
+ } else {
1164
+ vc = &p.vertices [j].Color ;
1165
+ Normal = &p.vertices [j].Normal ;
1166
+ }
1156
1167
// Note applyFacesShading second parameter is precalculated sqrt
1157
1168
// value for speed improvement
1158
1169
// Skip it for lightsources and top faces.
1159
- video::SColor &vc = vertex->Color ;
1160
- if (!vc.getBlue ()) {
1161
- if (vertex->Normal .Y < -0.5 ) {
1162
- applyFacesShading (vc, 0.447213 );
1163
- } else if (vertex->Normal .X > 0.5 ) {
1164
- applyFacesShading (vc, 0.670820 );
1165
- } else if (vertex->Normal .X < -0.5 ) {
1166
- applyFacesShading (vc, 0.670820 );
1167
- } else if (vertex->Normal .Z > 0.5 ) {
1168
- applyFacesShading (vc, 0.836660 );
1169
- } else if (vertex->Normal .Z < -0.5 ) {
1170
- applyFacesShading (vc, 0.836660 );
1170
+ if (!vc->getBlue ()) {
1171
+ if (Normal->Y < -0.5 ) {
1172
+ applyFacesShading (*vc, 0.447213 );
1173
+ } else if (Normal->X > 0.5 ) {
1174
+ applyFacesShading (*vc, 0.670820 );
1175
+ } else if (Normal->X < -0.5 ) {
1176
+ applyFacesShading (*vc, 0.670820 );
1177
+ } else if (Normal->Z > 0.5 ) {
1178
+ applyFacesShading (*vc, 0.836660 );
1179
+ } else if (Normal->Z < -0.5 ) {
1180
+ applyFacesShading (*vc, 0.836660 );
1171
1181
}
1172
1182
}
1173
- if (!m_enable_shaders)
1174
- {
1183
+ if (!m_enable_shaders) {
1175
1184
// - Classic lighting (shaders handle this by themselves)
1176
1185
// Set initial real color and store for later updates
1177
- u8 day = vc. getRed ();
1178
- u8 night = vc. getGreen ();
1179
- finalColorBlend (vc, day, night, 1000 );
1180
- if (day != night)
1186
+ u8 day = vc-> getRed ();
1187
+ u8 night = vc-> getGreen ();
1188
+ finalColorBlend (* vc, day, night, 1000 );
1189
+ if (day != night) {
1181
1190
m_daynight_diffs[i][j] = std::make_pair (day, night);
1191
+ }
1182
1192
}
1183
1193
}
1184
1194
@@ -1201,34 +1211,46 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
1201
1211
p.tile .applyMaterialOptions (material);
1202
1212
}
1203
1213
1204
- // Create meshbuffer
1205
- scene::SMeshBuffer *buf = new scene::SMeshBuffer ();
1206
- // Set material
1207
- buf->Material = material;
1208
- // Add to mesh
1209
- scene::SMesh *mesh = (scene::SMesh *)m_mesh;
1210
- mesh->addMeshBuffer (buf);
1211
- // Mesh grabbed it
1212
- buf->drop ();
1213
- buf->append (&p.vertices [0 ], p.vertices .size (),
1214
- &p.indices [0 ], p.indices .size ());
1215
- }
1216
- m_camera_offset = camera_offset;
1214
+ scene::SMesh *mesh = (scene::SMesh *)m_mesh;
1215
+
1216
+ // Create meshbuffer, add to mesh
1217
+ if (m_use_tangent_vertices) {
1218
+ scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents ();
1219
+ // Set material
1220
+ buf->Material = material;
1221
+ // Add to mesh
1222
+ mesh->addMeshBuffer (buf);
1223
+ // Mesh grabbed it
1224
+ buf->drop ();
1225
+ buf->append (&p.tangent_vertices [0 ], p.tangent_vertices .size (),
1226
+ &p.indices [0 ], p.indices .size ());
1227
+ } else {
1228
+ scene::SMeshBuffer *buf = new scene::SMeshBuffer ();
1229
+ // Set material
1230
+ buf->Material = material;
1231
+ // Add to mesh
1232
+ mesh->addMeshBuffer (buf);
1233
+ // Mesh grabbed it
1234
+ buf->drop ();
1235
+ buf->append (&p.vertices [0 ], p.vertices .size (),
1236
+ &p.indices [0 ], p.indices .size ());
1237
+ }
1238
+ }
1217
1239
1218
1240
/*
1219
1241
Do some stuff to the mesh
1220
1242
*/
1243
+ m_camera_offset = camera_offset;
1244
+ translateMesh (m_mesh,
1245
+ intToFloat (data->m_blockpos * MAP_BLOCKSIZE - camera_offset, BS));
1221
1246
1222
- translateMesh (m_mesh, intToFloat (data->m_blockpos * MAP_BLOCKSIZE - camera_offset, BS));
1223
-
1224
- if (m_enable_shaders) {
1225
- scene::IMeshManipulator* meshmanip = m_gamedef->getSceneManager ()->getMeshManipulator ();
1226
- scene::IMesh* tangentMesh = meshmanip->createMeshWithTangents (m_mesh);
1227
- m_mesh->drop ();
1228
- m_mesh = tangentMesh;
1247
+ if (m_use_tangent_vertices) {
1248
+ scene::IMeshManipulator* meshmanip =
1249
+ m_gamedef->getSceneManager ()->getMeshManipulator ();
1250
+ meshmanip->recalculateTangents (m_mesh, true , false , false );
1229
1251
}
1230
1252
1231
- if (m_mesh)
1253
+ if (m_mesh)
1232
1254
{
1233
1255
#if 0
1234
1256
// Usually 1-700 faces and 1-7 materials
@@ -1400,17 +1422,30 @@ void MeshCollector::append(const TileSpec &tile,
1400
1422
p = &prebuffers[prebuffers.size () - 1 ];
1401
1423
}
1402
1424
1403
- u32 vertex_count = p->vertices .size ();
1404
- for (u32 i = 0 ; i < numIndices; i++) {
1425
+ u32 vertex_count;
1426
+ if (m_use_tangent_vertices) {
1427
+ vertex_count = p->tangent_vertices .size ();
1428
+ p->tangent_vertices .reserve (vertex_count + numVertices);
1429
+ for (u32 i = 0 ; i < numVertices; i++) {
1430
+ video::S3DVertexTangents vert (vertices[i].Pos , vertices[i].Normal ,
1431
+ vertices[i].Color , vertices[i].TCoords );
1432
+ p->tangent_vertices .push_back (vert);
1433
+ }
1434
+ } else {
1435
+ vertex_count = p->vertices .size ();
1436
+ p->vertices .reserve (vertex_count + numVertices);
1437
+ for (u32 i = 0 ; i < numVertices; i++) {
1438
+ video::S3DVertex vert (vertices[i].Pos , vertices[i].Normal ,
1439
+ vertices[i].Color , vertices[i].TCoords );
1440
+ p->vertices .push_back (vert);
1441
+ }
1442
+ }
1443
+
1444
+ p->indices .reserve (p->indices .size () + numIndices);
1445
+ for (u32 i = 0 ; i < numIndices; i++) {
1405
1446
u32 j = indices[i] + vertex_count;
1406
1447
p->indices .push_back (j);
1407
1448
}
1408
-
1409
- for (u32 i = 0 ; i < numVertices; i++) {
1410
- video::S3DVertex vert (vertices[i].Pos , vertices[i].Normal ,
1411
- vertices[i].Color , vertices[i].TCoords );
1412
- p->vertices .push_back (vert);
1413
- }
1414
1449
}
1415
1450
1416
1451
/*
@@ -1446,15 +1481,28 @@ void MeshCollector::append(const TileSpec &tile,
1446
1481
p = &prebuffers[prebuffers.size () - 1 ];
1447
1482
}
1448
1483
1449
- u32 vertex_count = p->vertices .size ();
1484
+ u32 vertex_count;
1485
+ if (m_use_tangent_vertices) {
1486
+ vertex_count = p->tangent_vertices .size ();
1487
+ p->tangent_vertices .reserve (vertex_count + numVertices);
1488
+ for (u32 i = 0 ; i < numVertices; i++) {
1489
+ video::S3DVertexTangents vert (vertices[i].Pos + pos,
1490
+ vertices[i].Normal , c, vertices[i].TCoords );
1491
+ p->tangent_vertices .push_back (vert);
1492
+ }
1493
+ } else {
1494
+ vertex_count = p->vertices .size ();
1495
+ p->vertices .reserve (vertex_count + numVertices);
1496
+ for (u32 i = 0 ; i < numVertices; i++) {
1497
+ video::S3DVertex vert (vertices[i].Pos + pos,
1498
+ vertices[i].Normal , c, vertices[i].TCoords );
1499
+ p->vertices .push_back (vert);
1500
+ }
1501
+ }
1502
+
1503
+ p->indices .reserve (p->indices .size () + numIndices);
1450
1504
for (u32 i = 0 ; i < numIndices; i++) {
1451
1505
u32 j = indices[i] + vertex_count;
1452
1506
p->indices .push_back (j);
1453
1507
}
1454
-
1455
- for (u32 i = 0 ; i < numVertices; i++) {
1456
- video::S3DVertex vert (vertices[i].Pos + pos, vertices[i].Normal ,
1457
- c, vertices[i].TCoords );
1458
- p->vertices .push_back (vert);
1459
- }
1460
1508
}
0 commit comments