--- /dev/null
+/*
+Minetest
+Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "collector.h"
+#include <stdexcept>
+#include "log.h"
+#include "mesh.h"
+
+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)
+{
+ PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
+
+ f32 scale = 1.0f;
+ if (use_scale)
+ scale = 1.0f / layer.scale;
+
+ u32 vertex_count = p.vertices.size();
+ for (u32 i = 0; i < numVertices; i++)
+ p.vertices.emplace_back(vertices[i].Pos, vertices[i].Normal,
+ vertices[i].Color, scale * vertices[i].TCoords);
+
+ for (u32 i = 0; i < numIndices; i++)
+ p.indices.push_back(indices[i] + vertex_count);
+}
+
+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)
+{
+ PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
+
+ f32 scale = 1.0f;
+ if (use_scale)
+ scale = 1.0f / layer.scale;
+
+ u32 vertex_count = p.vertices.size();
+ for (u32 i = 0; i < numVertices; i++) {
+ video::SColor color = c;
+ if (!light_source)
+ applyFacesShading(color, vertices[i].Normal);
+ p.vertices.emplace_back(vertices[i].Pos + pos, vertices[i].Normal, color,
+ scale * vertices[i].TCoords);
+ }
+
+ for (u32 i = 0; i < numIndices; i++)
+ p.indices.push_back(indices[i] + vertex_count);
+}
+
+PreMeshBuffer &MeshCollector::findBuffer(
+ const TileLayer &layer, u8 layernum, u32 numVertices)
+{
+ if (numVertices > U16_MAX)
+ throw std::invalid_argument(
+ "Mesh can't contain more than 65536 vertices");
+ std::vector<PreMeshBuffer> &buffers = prebuffers[layernum];
+ for (PreMeshBuffer &p : buffers)
+ if (p.layer == layer && p.vertices.size() + numVertices <= U16_MAX)
+ return p;
+ buffers.emplace_back(layer);
+ return buffers.back();
+}
--- /dev/null
+/*
+Minetest
+Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+#include <array>
+#include <vector>
+#include "irrlichttypes.h"
+#include <S3DVertex.h>
+#include "client/tile.h"
+
+struct PreMeshBuffer
+{
+ TileLayer layer;
+ std::vector<u16> indices;
+ std::vector<video::S3DVertex> vertices;
+
+ PreMeshBuffer() = default;
+ explicit PreMeshBuffer(const TileLayer &layer) : layer(layer) {}
+};
+
+struct MeshCollector
+{
+ std::array<std::vector<PreMeshBuffer>, MAX_TILE_LAYERS> prebuffers;
+
+ // clang-format off
+ void append(const TileSpec &material,
+ const video::S3DVertex *vertices, u32 numVertices,
+ const u16 *indices, u32 numIndices);
+ void append(const TileSpec &material,
+ const video::S3DVertex *vertices, u32 numVertices,
+ const u16 *indices, u32 numIndices,
+ v3f pos, video::SColor c, u8 light_source);
+ // clang-format on
+
+private:
+ // clang-format off
+ void append(const TileLayer &material,
+ const video::S3DVertex *vertices, u32 numVertices,
+ const u16 *indices, u32 numIndices,
+ u8 layernum, bool use_scale = false);
+ void append(const TileLayer &material,
+ const video::S3DVertex *vertices, u32 numVertices,
+ const u16 *indices, u32 numIndices,
+ v3f pos, video::SColor c, u8 light_source,
+ u8 layernum, bool use_scale = false);
+ // clang-format on
+
+ PreMeshBuffer &findBuffer(const TileLayer &layer, u8 layernum, u32 numVertices);
+};
#include "minimap.h"
#include "content_mapblock.h"
#include "util/directiontables.h"
+#include "client/meshgen/collector.h"
#include "client/renderingengine.h"
#include <array>
struct FastFace
{
- TileLayer layer;
+ TileSpec tile;
video::S3DVertex vertices[4]; // Precalculated vertices
/*!
* The face is divided into two triangles. If this is true,
* 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,
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;
}
/*
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
*/
Convert FastFaces to MeshCollector
*/
- MeshCollector collector(m_use_tangent_vertices);
+ MeshCollector collector;
{
// avg 0ms (100ms spikes when loading textures the first time)
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);
}
}
generator.generate();
}
- collector.applyTileColors();
-
/*
Convert MeshCollector to SMesh
*/
{
PreMeshBuffer &p = collector.prebuffers[layer][i];
+ applyTileColor(p);
+
// Generate animation data
// - Cracks
if (p.layer.material_flags & MATERIAL_FLAG_CRACK) {
// 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
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();
}
}
}
}
-/*
- 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