Skip to content

Commit 7ea40e4

Browse files
RealBadAngelparamat
authored andcommittedFeb 15, 2016
Use vertices with tangents only when its needed.
1 parent 3a74b84 commit 7ea40e4

File tree

4 files changed

+131
-67
lines changed

4 files changed

+131
-67
lines changed
 

‎src/client.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ Client::Client(
263263

264264
m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
265265
m_cache_enable_shaders = g_settings->getBool("enable_shaders");
266+
m_cache_use_tangent_vertices = m_cache_enable_shaders && (
267+
g_settings->getBool("enable_bumpmapping") ||
268+
g_settings->getBool("enable_parallax_occlusion"));
266269
}
267270

268271
void Client::Stop()
@@ -1582,7 +1585,8 @@ void Client::addUpdateMeshTask(v3s16 p, bool ack_to_server, bool urgent)
15821585
Create a task to update the mesh of the block
15831586
*/
15841587

1585-
MeshMakeData *data = new MeshMakeData(this, m_cache_enable_shaders);
1588+
MeshMakeData *data = new MeshMakeData(this, m_cache_enable_shaders,
1589+
m_cache_use_tangent_vertices);
15861590

15871591
{
15881592
//TimeTaker timer("data fill");

‎src/client.h

+1
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
677677
// TODO: Add callback to update these when g_settings changes
678678
bool m_cache_smooth_lighting;
679679
bool m_cache_enable_shaders;
680+
bool m_cache_use_tangent_vertices;
680681

681682
DISABLE_CLASS_COPY(Client);
682683
};

‎src/mapblock_mesh.cpp

+113-65
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3333
#include "util/directiontables.h"
3434
#include <IMeshManipulator.h>
3535

36-
static void applyFacesShading(video::SColor& color, float factor)
36+
static void applyFacesShading(video::SColor &color, const float factor)
3737
{
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));
4040
}
4141

4242
/*
4343
MeshMakeData
4444
*/
4545

46-
MeshMakeData::MeshMakeData(IGameDef *gamedef, bool use_shaders):
46+
MeshMakeData::MeshMakeData(IGameDef *gamedef, bool use_shaders,
47+
bool use_tangent_vertices):
4748
m_vmanip(),
4849
m_blockpos(-1337,-1337,-1337),
4950
m_crack_pos_relative(-1337, -1337, -1337),
5051
m_smooth_lighting(false),
5152
m_show_hud(false),
5253
m_gamedef(gamedef),
53-
m_use_shaders(use_shaders)
54+
m_use_shaders(use_shaders),
55+
m_use_tangent_vertices(use_tangent_vertices)
5456
{}
5557

5658
void MeshMakeData::fill(MapBlock *block)
@@ -1032,6 +1034,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
10321034
m_daynight_diffs()
10331035
{
10341036
m_enable_shaders = data->m_use_shaders;
1037+
m_use_tangent_vertices = data->m_use_tangent_vertices;
10351038

10361039
if (g_settings->getBool("enable_minimap")) {
10371040
m_minimap_mapblock = new MinimapMapblock;
@@ -1064,15 +1067,14 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
10641067
Convert FastFaces to MeshCollector
10651068
*/
10661069

1067-
MeshCollector collector;
1070+
MeshCollector collector(m_use_tangent_vertices);
10681071

10691072
{
10701073
// avg 0ms (100ms spikes when loading textures the first time)
10711074
// (NOTE: probably outdated)
10721075
//TimeTaker timer2("MeshCollector building");
10731076

1074-
for(u32 i=0; i<fastfaces_new.size(); i++)
1075-
{
1077+
for (u32 i = 0; i < fastfaces_new.size(); i++) {
10761078
FastFace &f = fastfaces_new[i];
10771079

10781080
const u16 indices[] = {0,1,2,2,3,0};
@@ -1150,35 +1152,43 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
11501152
p.tile.texture = animation_frame.texture;
11511153
}
11521154

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+
}
11561167
// Note applyFacesShading second parameter is precalculated sqrt
11571168
// value for speed improvement
11581169
// 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);
11711181
}
11721182
}
1173-
if(!m_enable_shaders)
1174-
{
1183+
if (!m_enable_shaders) {
11751184
// - Classic lighting (shaders handle this by themselves)
11761185
// 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) {
11811190
m_daynight_diffs[i][j] = std::make_pair(day, night);
1191+
}
11821192
}
11831193
}
11841194

@@ -1201,34 +1211,46 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
12011211
p.tile.applyMaterialOptions(material);
12021212
}
12031213

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+
}
12171239

12181240
/*
12191241
Do some stuff to the mesh
12201242
*/
1243+
m_camera_offset = camera_offset;
1244+
translateMesh(m_mesh,
1245+
intToFloat(data->m_blockpos * MAP_BLOCKSIZE - camera_offset, BS));
12211246

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);
12291251
}
12301252

1231-
if(m_mesh)
1253+
if (m_mesh)
12321254
{
12331255
#if 0
12341256
// Usually 1-700 faces and 1-7 materials
@@ -1400,17 +1422,30 @@ void MeshCollector::append(const TileSpec &tile,
14001422
p = &prebuffers[prebuffers.size() - 1];
14011423
}
14021424

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++) {
14051446
u32 j = indices[i] + vertex_count;
14061447
p->indices.push_back(j);
14071448
}
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-
}
14141449
}
14151450

14161451
/*
@@ -1446,15 +1481,28 @@ void MeshCollector::append(const TileSpec &tile,
14461481
p = &prebuffers[prebuffers.size() - 1];
14471482
}
14481483

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);
14501504
for (u32 i = 0; i < numIndices; i++) {
14511505
u32 j = indices[i] + vertex_count;
14521506
p->indices.push_back(j);
14531507
}
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-
}
14601508
}

‎src/mapblock_mesh.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ struct MeshMakeData
4646

4747
IGameDef *m_gamedef;
4848
bool m_use_shaders;
49+
bool m_use_tangent_vertices;
4950

50-
MeshMakeData(IGameDef *gamedef, bool use_shaders);
51+
MeshMakeData(IGameDef *gamedef, bool use_shaders,
52+
bool use_tangent_vertices = false);
5153

5254
/*
5355
Copy central data directly from block, and other data from
@@ -130,6 +132,7 @@ class MapBlockMesh
130132
IShaderSource *m_shdrsrc;
131133

132134
bool m_enable_shaders;
135+
bool m_use_tangent_vertices;
133136

134137
// Must animate() be called before rendering?
135138
bool m_has_animation;
@@ -167,11 +170,19 @@ struct PreMeshBuffer
167170
TileSpec tile;
168171
std::vector<u16> indices;
169172
std::vector<video::S3DVertex> vertices;
173+
std::vector<video::S3DVertexTangents> tangent_vertices;
170174
};
171175

172176
struct MeshCollector
173177
{
174178
std::vector<PreMeshBuffer> prebuffers;
179+
bool m_use_tangent_vertices;
180+
181+
MeshCollector(bool use_tangent_vertices):
182+
m_use_tangent_vertices(use_tangent_vertices)
183+
{
184+
}
185+
175186
void append(const TileSpec &material,
176187
const video::S3DVertex *vertices, u32 numVertices,
177188
const u16 *indices, u32 numIndices);

0 commit comments

Comments
 (0)
Please sign in to comment.