3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef MAPBLOCK_MESH_HEADER
21 #define MAPBLOCK_MESH_HEADER
23 #include "irrlichttypes_extrabloated.h"
24 #include "client/tile.h"
26 #include "util/cpp11_container.h"
38 struct MinimapMapblock;
42 VoxelManipulator m_vmanip;
44 v3s16 m_crack_pos_relative;
45 bool m_smooth_lighting;
50 bool m_use_tangent_vertices;
52 MeshMakeData(Client *client, bool use_shaders,
53 bool use_tangent_vertices = false);
56 Copy block data manually (to allow optimizations by the caller)
58 void fillBlockDataBegin(const v3s16 &blockpos);
59 void fillBlockData(const v3s16 &block_offset, MapNode *data);
62 Copy central data directly from block, and other data from
65 void fill(MapBlock *block);
68 Set up with only a single node at (1,1,1)
70 void fillSingleNode(MapNode *node);
73 Set the (node) position of a crack
75 void setCrack(int crack_level, v3s16 crack_pos);
78 Enable or disable smooth lighting
80 void setSmoothLighting(bool smooth_lighting);
84 Holds a mesh for a mapblock.
86 Besides the SMesh*, this contains information used for animating
87 the vertex positions, colors and texture coordinates of the mesh.
89 - cracks [implemented]
90 - day/night transitions [implemented]
91 - animated flowing liquids [not implemented]
92 - animating vertex positions for e.g. axles [not implemented]
97 // Builds the mesh given
98 MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
101 // Main animation function, parameters:
102 // faraway: whether the block is far away from the camera (~50 nodes)
103 // time: the global animation time, 0 .. 60 (repeats every minute)
104 // daynight_ratio: 0 .. 1000
105 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
106 // Returns true if anything has been changed.
107 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
109 scene::IMesh *getMesh()
114 MinimapMapblock *moveMinimapMapblock()
116 MinimapMapblock *p = m_minimap_mapblock;
117 m_minimap_mapblock = NULL;
121 bool isAnimationForced() const
123 return m_animation_force_timer == 0;
126 void decreaseAnimationForceTimer()
128 if(m_animation_force_timer > 0)
129 m_animation_force_timer--;
132 void updateCameraOffset(v3s16 camera_offset);
135 scene::IMesh *m_mesh;
136 MinimapMapblock *m_minimap_mapblock;
138 video::IVideoDriver *m_driver;
139 ITextureSource *m_tsrc;
140 IShaderSource *m_shdrsrc;
142 bool m_enable_shaders;
143 bool m_use_tangent_vertices;
146 // Must animate() be called before rendering?
147 bool m_has_animation;
148 int m_animation_force_timer;
150 // Animation info: cracks
151 // Last crack value passed to animate()
153 // Maps mesh buffer (i.e. material) indices to base texture names
154 UNORDERED_MAP<u32, std::string> m_crack_materials;
156 // Animation info: texture animationi
157 // Maps meshbuffers to TileSpecs
158 UNORDERED_MAP<u32, TileSpec> m_animation_tiles;
159 UNORDERED_MAP<u32, int> m_animation_frames; // last animation frame
160 UNORDERED_MAP<u32, int> m_animation_frame_offsets;
162 // Animation info: day/night transitions
163 // Last daynight_ratio value passed to animate()
164 u32 m_last_daynight_ratio;
165 // For each meshbuffer, stores pre-baked colors of sunlit vertices
166 std::map<u32, std::map<u32, video::SColor > > m_daynight_diffs;
168 // Camera offset info -> do we have to translate the mesh?
169 v3s16 m_camera_offset;
175 This is used because CMeshBuffer::append() is very slow
180 std::vector<u16> indices;
181 std::vector<video::S3DVertex> vertices;
182 std::vector<video::S3DVertexTangents> tangent_vertices;
187 std::vector<PreMeshBuffer> prebuffers;
188 bool m_use_tangent_vertices;
190 MeshCollector(bool use_tangent_vertices):
191 m_use_tangent_vertices(use_tangent_vertices)
195 void append(const TileSpec &material,
196 const video::S3DVertex *vertices, u32 numVertices,
197 const u16 *indices, u32 numIndices);
198 void append(const TileSpec &material,
199 const video::S3DVertex *vertices, u32 numVertices,
200 const u16 *indices, u32 numIndices,
201 v3f pos, video::SColor c, u8 light_source);
205 * Encodes light and color of a node.
206 * The result is not the final color, but a
207 * half-baked vertex color.
209 * \param light the first 8 bits are day light,
210 * the last 8 bits are night light
211 * \param color the node's color
212 * \param emissive_light amount of light the surface emits,
213 * from 0 to LIGHT_SUN.
215 video::SColor encode_light_and_color(u16 light, const video::SColor &color,
218 // Compute light at node
219 u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
220 u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
221 u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data);
224 * Returns the sunlight's color from the current
227 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
230 * Gives the final SColor shown on screen.
232 * \param result output color
233 * \param light first 8 bits are day light, second 8 bits are
236 void final_color_blend(video::SColor *result,
237 u16 light, u32 daynight_ratio);
240 * Gives the final SColor shown on screen.
242 * \param result output color
243 * \param data the half-baked vertex color
244 * \param dayLight color of the sunlight
246 void final_color_blend(video::SColor *result,
247 const video::SColor &data, const video::SColorf &dayLight);
249 // Retrieves the TileSpec of a face of a node
250 // Adds MATERIAL_FLAG_CRACK if the node is cracked
251 TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data);
252 TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data);