aboutsummaryrefslogtreecommitdiff
path: root/src/mapblock_mesh.cpp
diff options
context:
space:
mode:
authorVitaliy <silverunicorn2011@yandex.ru>2018-03-22 22:59:49 +0300
committerLoïc Blot <nerzhul@users.noreply.github.com>2018-03-22 20:59:49 +0100
commit9fcc0c1217fcd3acabaa11239631de10032fa126 (patch)
tree7596bc17bd51f5b9d22ad918743d90fcfa0e2278 /src/mapblock_mesh.cpp
parent8986a9e6053bed0002726bc000ee394a928b7f51 (diff)
downloadhax-minetest-server-9fcc0c1217fcd3acabaa11239631de10032fa126.tar.gz
hax-minetest-server-9fcc0c1217fcd3acabaa11239631de10032fa126.zip
Update mesh collector and move it to a separate file (#6904)
* Update MeshCollector * Simplify MeshCollector
Diffstat (limited to 'src/mapblock_mesh.cpp')
-rw-r--r--src/mapblock_mesh.cpp293
1 files changed, 48 insertions, 245 deletions
diff --git a/src/mapblock_mesh.cpp b/src/mapblock_mesh.cpp
index 6a94e78df..a616a98b7 100644
--- a/src/mapblock_mesh.cpp
+++ b/src/mapblock_mesh.cpp
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "minimap.h"
#include "content_mapblock.h"
#include "util/directiontables.h"
+#include "client/meshgen/collector.h"
#include "client/renderingengine.h"
#include <array>
@@ -450,7 +451,7 @@ static void getNodeTextureCoords(v3f base, const v3f &scale, v3s16 dir, float *u
struct FastFace
{
- TileLayer layer;
+ TileSpec tile;
video::S3DVertex vertices[4]; // Precalculated vertices
/*!
* The face is divided into two triangles. If this is true,
@@ -458,8 +459,6 @@ struct FastFace
* are connected.
*/
bool vertex_0_2_connected;
- u8 layernum;
- bool world_aligned;
};
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
core::vector2d<f32>(x0, y0),
core::vector2d<f32>(x0 + w * abs_scale, y0) };
- for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
- const TileLayer *layer = &tile.layers[layernum];
- if (layer->texture_id == 0)
- continue;
-
- // equivalent to dest.push_back(FastFace()) but faster
- dest.emplace_back();
- FastFace& face = *dest.rbegin();
-
- for (u8 i = 0; i < 4; i++) {
- video::SColor c = encode_light(li[i], tile.emissive_light);
- if (!tile.emissive_light)
- applyFacesShading(c, normal);
-
- face.vertices[i] = video::S3DVertex(vertex_pos[i], normal, c, f[i]);
- }
-
- /*
- Revert triangles for nicer looking gradient if the
- brightness of vertices 1 and 3 differ less than
- the brightness of vertices 0 and 2.
- */
- face.vertex_0_2_connected = vertex_0_2_connected;
+ // equivalent to dest.push_back(FastFace()) but faster
+ dest.emplace_back();
+ FastFace& face = *dest.rbegin();
- face.layer = *layer;
- face.layernum = layernum;
+ for (u8 i = 0; i < 4; i++) {
+ video::SColor c = encode_light(li[i], tile.emissive_light);
+ if (!tile.emissive_light)
+ applyFacesShading(c, normal);
- face.world_aligned = tile.world_aligned;
+ face.vertices[i] = video::S3DVertex(vertex_pos[i], normal, c, f[i]);
}
+
+ /*
+ Revert triangles for nicer looking gradient if the
+ brightness of vertices 1 and 3 differ less than
+ the brightness of vertices 0 and 2.
+ */
+ face.vertex_0_2_connected = vertex_0_2_connected;
+ face.tile = tile;
}
/*
@@ -1012,6 +1001,20 @@ static void updateAllFastFaceRows(MeshMakeData *data,
dest);
}
+static void applyTileColor(PreMeshBuffer &pmb)
+{
+ video::SColor tc = pmb.layer.color;
+ if (tc == video::SColor(0xFFFFFFFF))
+ return;
+ for (video::S3DVertex &vertex : pmb.vertices) {
+ video::SColor *c = &vertex.Color;
+ c->set(c->getAlpha(),
+ c->getRed() * tc.getRed() / 255,
+ c->getGreen() * tc.getGreen() / 255,
+ c->getBlue() * tc.getBlue() / 255);
+ }
+}
+
/*
MapBlockMesh
*/
@@ -1061,7 +1064,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
Convert FastFaces to MeshCollector
*/
- MeshCollector collector(m_use_tangent_vertices);
+ MeshCollector collector;
{
// avg 0ms (100ms spikes when loading textures the first time)
@@ -1071,15 +1074,9 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
for (const FastFace &f : fastfaces_new) {
static const u16 indices[] = {0, 1, 2, 2, 3, 0};
static const u16 indices_alternate[] = {0, 1, 3, 2, 3, 1};
-
- if (!f.layer.texture)
- continue;
-
const u16 *indices_p =
f.vertex_0_2_connected ? indices : indices_alternate;
-
- collector.append(f.layer, f.vertices, 4, indices_p, 6,
- f.layernum, f.world_aligned);
+ collector.append(f.tile, f.vertices, 4, indices_p, 6);
}
}
@@ -1096,8 +1093,6 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
generator.generate();
}
- collector.applyTileColors();
-
/*
Convert MeshCollector to SMesh
*/
@@ -1107,6 +1102,8 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
{
PreMeshBuffer &p = collector.prebuffers[layer][i];
+ applyTileColor(p);
+
// Generate animation data
// - Cracks
if (p.layer.material_flags & MATERIAL_FLAG_CRACK) {
@@ -1151,16 +1148,10 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
// Dummy sunlight to handle non-sunlit areas
video::SColorf sunlight;
get_sunlight_color(&sunlight, 0);
- u32 vertex_count = m_use_tangent_vertices ?
- p.tangent_vertices.size() : p.vertices.size();
+ u32 vertex_count = p.vertices.size();
for (u32 j = 0; j < vertex_count; j++) {
- video::SColor *vc;
- if (m_use_tangent_vertices) {
- vc = &p.tangent_vertices[j].Color;
- } else {
- vc = &p.vertices[j].Color;
- }
- video::SColor copy(*vc);
+ video::SColor *vc = &p.vertices[j].Color;
+ video::SColor copy = *vc;
if (vc->getAlpha() == 0) // No sunlight - no need to animate
final_color_blend(vc, copy, sunlight); // Finalize color
else // Record color to animate
@@ -1197,24 +1188,23 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
if (m_use_tangent_vertices) {
scene::SMeshBufferTangents *buf =
new scene::SMeshBufferTangents();
- // Set material
buf->Material = material;
- // Add to mesh
+ buf->Vertices.reallocate(p.vertices.size());
+ buf->Indices.reallocate(p.indices.size());
+ for (const video::S3DVertex &v: p.vertices)
+ buf->Vertices.push_back(video::S3DVertexTangents(v.Pos, v.Color, v.TCoords));
+ for (u16 i: p.indices)
+ buf->Indices.push_back(i);
+ buf->recalculateBoundingBox();
mesh->addMeshBuffer(buf);
- // Mesh grabbed it
buf->drop();
- buf->append(&p.tangent_vertices[0], p.tangent_vertices.size(),
- &p.indices[0], p.indices.size());
} else {
scene::SMeshBuffer *buf = new scene::SMeshBuffer();
- // Set material
buf->Material = material;
- // Add to mesh
- mesh->addMeshBuffer(buf);
- // Mesh grabbed it
- buf->drop();
buf->append(&p.vertices[0], p.vertices.size(),
&p.indices[0], p.indices.size());
+ mesh->addMeshBuffer(buf);
+ buf->drop();
}
}
@@ -1370,193 +1360,6 @@ void MapBlockMesh::updateCameraOffset(v3s16 camera_offset)
}
}
-/*
- MeshCollector
-*/
-
-void MeshCollector::append(const TileSpec &tile,
- const video::S3DVertex *vertices, u32 numVertices,
- const u16 *indices, u32 numIndices)
-{
- for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
- const TileLayer *layer = &tile.layers[layernum];
- if (layer->texture_id == 0)
- continue;
- append(*layer, vertices, numVertices, indices, numIndices,
- layernum, tile.world_aligned);
- }
-}
-
-void MeshCollector::append(const TileLayer &layer,
- const video::S3DVertex *vertices, u32 numVertices,
- const u16 *indices, u32 numIndices, u8 layernum,
- bool use_scale)
-{
- if (numIndices > 65535) {
- dstream << "FIXME: MeshCollector::append() called with numIndices="
- << numIndices << " (limit 65535)" << std::endl;
- return;
- }
- std::vector<PreMeshBuffer> *buffers = &prebuffers[layernum];
-
- PreMeshBuffer *p = NULL;
- for (PreMeshBuffer &pp : *buffers) {
- if (pp.layer == layer && pp.indices.size() + numIndices <= 65535) {
- p = &pp;
- break;
- }
- }
-
- if (p == NULL) {
- PreMeshBuffer pp;
- pp.layer = layer;
- buffers->push_back(pp);
- p = &(*buffers)[buffers->size() - 1];
- }
-
- f32 scale = 1.0;
- if (use_scale)
- scale = 1.0 / layer.scale;
-
- u32 vertex_count;
- if (m_use_tangent_vertices) {
- vertex_count = p->tangent_vertices.size();
- for (u32 i = 0; i < numVertices; i++) {
-
- video::S3DVertexTangents vert(vertices[i].Pos, vertices[i].Normal,
- vertices[i].Color, scale * vertices[i].TCoords);
- p->tangent_vertices.push_back(vert);
- }
- } else {
- vertex_count = p->vertices.size();
- for (u32 i = 0; i < numVertices; i++) {
- video::S3DVertex vert(vertices[i].Pos, vertices[i].Normal,
- vertices[i].Color, scale * vertices[i].TCoords);
-
- p->vertices.push_back(vert);
- }
- }
-
- for (u32 i = 0; i < numIndices; i++) {
- u32 j = indices[i] + vertex_count;
- p->indices.push_back(j);
- }
-}
-
-/*
- MeshCollector - for meshnodes and converted drawtypes.
-*/
-
-void MeshCollector::append(const TileSpec &tile,
- const video::S3DVertex *vertices, u32 numVertices,
- const u16 *indices, u32 numIndices,
- v3f pos, video::SColor c, u8 light_source)
-{
- for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
- const TileLayer *layer = &tile.layers[layernum];
- if (layer->texture_id == 0)
- continue;
- append(*layer, vertices, numVertices, indices, numIndices, pos,
- c, light_source, layernum, tile.world_aligned);
- }
-}
-
-void MeshCollector::append(const TileLayer &layer,
- const video::S3DVertex *vertices, u32 numVertices,
- const u16 *indices, u32 numIndices,
- v3f pos, video::SColor c, u8 light_source, u8 layernum,
- bool use_scale)
-{
- if (numIndices > 65535) {
- dstream << "FIXME: MeshCollector::append() called with numIndices="
- << numIndices << " (limit 65535)" << std::endl;
- return;
- }
- std::vector<PreMeshBuffer> *buffers = &prebuffers[layernum];
-
- PreMeshBuffer *p = NULL;
- for (PreMeshBuffer &pp : *buffers) {
- if (pp.layer == layer && pp.indices.size() + numIndices <= 65535) {
- p = &pp;
- break;
- }
- }
-
- if (p == NULL) {
- PreMeshBuffer pp;
- pp.layer = layer;
- buffers->push_back(pp);
- p = &(*buffers)[buffers->size() - 1];
- }
-
- f32 scale = 1.0;
- if (use_scale)
- scale = 1.0 / layer.scale;
-
- video::SColor original_c = c;
- u32 vertex_count;
- if (m_use_tangent_vertices) {
- vertex_count = p->tangent_vertices.size();
- for (u32 i = 0; i < numVertices; i++) {
- if (!light_source) {
- c = original_c;
- applyFacesShading(c, vertices[i].Normal);
- }
- video::S3DVertexTangents vert(vertices[i].Pos + pos,
- vertices[i].Normal, c, scale * vertices[i].TCoords);
- p->tangent_vertices.push_back(vert);
- }
- } else {
- vertex_count = p->vertices.size();
- for (u32 i = 0; i < numVertices; i++) {
- if (!light_source) {
- c = original_c;
- applyFacesShading(c, vertices[i].Normal);
- }
- video::S3DVertex vert(vertices[i].Pos + pos, vertices[i].Normal, c,
- scale * vertices[i].TCoords);
- p->vertices.push_back(vert);
- }
- }
-
- for (u32 i = 0; i < numIndices; i++) {
- u32 j = indices[i] + vertex_count;
- p->indices.push_back(j);
- }
-}
-
-void MeshCollector::applyTileColors()
-{
- if (m_use_tangent_vertices)
- for (auto &prebuffer : prebuffers) {
- for (PreMeshBuffer &pmb : prebuffer) {
- video::SColor tc = pmb.layer.color;
- if (tc == video::SColor(0xFFFFFFFF))
- continue;
- for (video::S3DVertexTangents &tangent_vertex : pmb.tangent_vertices) {
- video::SColor *c = &tangent_vertex.Color;
- c->set(c->getAlpha(), c->getRed() * tc.getRed() / 255,
- c->getGreen() * tc.getGreen() / 255,
- c->getBlue() * tc.getBlue() / 255);
- }
- }
- }
- else
- for (auto &prebuffer : prebuffers) {
- for (PreMeshBuffer &pmb : prebuffer) {
- video::SColor tc = pmb.layer.color;
- if (tc == video::SColor(0xFFFFFFFF))
- continue;
- for (video::S3DVertex &vertex : pmb.vertices) {
- video::SColor *c = &vertex.Color;
- c->set(c->getAlpha(), c->getRed() * tc.getRed() / 255,
- c->getGreen() * tc.getGreen() / 255,
- c->getBlue() * tc.getBlue() / 255);
- }
- }
- }
-}
-
video::SColor encode_light(u16 light, u8 emissive_light)
{
// Get components