/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 "mapblock_mesh.h"
#include "client.h"
#include "mapblock.h"
#include "map.h"
#include "profiler.h"
#include "shader.h"
#include "mesh.h"
#include "minimap.h"
#include "content_mapblock.h"
#include "util/directiontables.h"
#include "client/renderingengine.h"
/*
MeshMakeData
*/
MeshMakeData::MeshMakeData(Client *client, bool use_shaders,
bool use_tangent_vertices):
m_client(client),
m_use_shaders(use_shaders),
m_use_tangent_vertices(use_tangent_vertices)
{}
void MeshMakeData::fillBlockDataBegin(const v3s16 &blockpos)
{
m_blockpos = blockpos;
v3s16 blockpos_nodes = m_blockpos*MAP_BLOCKSIZE;
m_vmanip.clear();
VoxelArea voxel_area(blockpos_nodes - v3s16(1,1,1) * MAP_BLOCKSIZE,
blockpos_nodes + v3s16(1,1,1) * MAP_BLOCKSIZE*2-v3s16(1,1,1));
m_vmanip.addArea(voxel_area);
}
void MeshMakeData::fillBlockData(const v3s16 &block_offset, MapNode *data)
{
v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
v3s16 bp = m_blockpos + block_offset;
v3s16 blockpos_nodes = bp * MAP_BLOCKSIZE;
m_vmanip.copyFrom(data, data_area, v3s16(0,0,0), blockpos_nodes, data_size);
}
void MeshMakeData::fill(MapBlock *block)
{
fillBlockDataBegin(block->getPos());
fillBlockData(v3s16(0,0,0), block->getData());
// Get map for reading neigbhor blocks
Map *map = block->getParent();
for (const v3s16 &dir : g_26dirs) {
v3s16 bp = m_blockpos + dir;
MapBlock *b = map->getBlockNoCreateNoEx(bp);
if(b)
fillBlockData(dir, b->getData());
}
}
void MeshMakeData::fillSingleNode(MapNode *node)
{
m_blockpos = v3s16(0,0,0);
v3s16 blockpos_nodes = v3s16(0,0,0);
VoxelArea area(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE,
blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1));
s32 volume = area.getVolume();
s32 our_node_index = area.index(1,1,1);
// Allocate this block + neighbors
m_vmanip.clear();
m_vmanip.addArea(area);
// Fill in data
MapNode *data = new MapNode[volume];
for(s32 i = 0; i < volume; i++)
{
if (i == our_node_index)
data[i] = *node;
else
data[i] = MapNode(CONTENT_AIR, LIGHT_MAX, 0);
}
m_vmanip.copyFrom(data, area, area.MinEdge, area.MinEdge, area.getExtent());
delete[] data;
}
void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos)
{
if (crack_level >= 0)
m_crack_pos_relative = crack_pos - m_blockpos*MAP_BLOCKSIZE;
}
void MeshMakeData::setSmoothLighting(bool smooth_lighting)
{
m_smooth_lighting = smooth_lighting;
}
/*
Light and vertex color functions
*/
/*
Calculate non-smooth lighting at interior of node.
Single light bank.
*/
static u8 getInteriorLight(enum LightBank bank, MapNode n, s32 increment,
INodeDefManager *ndef)
{
u8 light = n.getLight(bank, ndef);
while(increment > 0)
{
light = undiminish_light(light);
--increment;
}
while(increment < 0)
{
light = diminish_light(light);
++increment;
}
return decode_light(light);
}
/*
Calculate non-smooth lighting at interior of node.
Both light banks.
*/
u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef)
{
u16 day = getInteriorLight(LIGHTBANK_DAY, n, increment, ndef);
u16 night = getInteriorLight(LIGHTBANK_NIGHT, n, increment, ndef);
return day | (night << 8);
}
/*
Calculate non-smooth lighting at face of node.
Single light bank.
*/
static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2,
v3s16 face_dir, INodeDefManager *ndef)
{
u8 light;
u8 l1 = n.getLight(bank, ndef);
u8 l2 = n2.getLight(bank, ndef);
if(l1 > l2)
light = l1;
else
light = l2;
// Boost light level for light sources
u8 light_source = MYMAX(ndef->get(n).light_source,
ndef->get(n2).light_source);
if(light_source > light)
light = light_source;
return decode_light(light);
}
/*
Calculate non-smooth lighting at face of node.
Both light banks.
*/
u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef)
{
u16 day = getFaceLight(LIGHTBANK_DAY, n, n2, face_dir, ndef);
u16 night = getFaceLight(LIGHTBANK_NIGHT, n, n2, face_dir, ndef);
return day | (night << 8);
}
/*
Calculate smooth lighting at the XYZ- corner of p.
Both light banks
*/
static u16 getSmoothLightCombined(const v3s16 &p, MeshMakeData *data)
{
static const v3s16 dirs8[8] = {
v3s16(0,0,0),
v3s16(0,0,1),
v3s16(0,1,0),
v3s16(0,1,1),
v3s16(1,0,0),
v3s16(1,1,0),
v3s16(1,0,1),
v3s16(1,1,1),
};
INodeDefManager *ndef = data->m_client->ndef();
u16 ambient_occlusion = 0;
u16 light_count = 0;
u8 light_source_max = 0;
u16 light_day = 0;
u16 light_night = 0;
for (const v3s16 &dir : dirs8) {
MapNode n = data->m_vmanip.getNodeNoExNoEmerge(p - dir);
// if it's CONTENT_IGNORE we can't do any light calculations
if (n.getContent() == CONTENT_IGNORE)
continue;
const ContentFeatures &f = ndef->get(n);
if (f.light_source > light_source_max)
light_source_max = f.light_source;
// Check f.solidness because fast-style leaves look better this way
if (f.param_type == CPT_LIGHT && f.solidness != 2) {
light_day += decode_light(n.getLightNoChecks(LIGHTBANK_DAY, &f));
light_night += decode_light(
n.getLightNoChecks(LIGHTBANK_NIGHT, &f));
light_count++;
} else {
ambient_occlusion++;
}
}
if(light_count == 0)
return 0xffff;
light_day /= light_count;
|