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(IGameDef *gamedef, bool use_shaders,
53 bool use_tangent_vertices = false);
56 Copy central data directly from block, and other data from
59 void fill(MapBlock *block);
62 Set up with only a single node at (1,1,1)
64 void fillSingleNode(MapNode *node);
67 Set the (node) position of a crack
69 void setCrack(int crack_level, v3s16 crack_pos);
72 Enable or disable smooth lighting
74 void setSmoothLighting(bool smooth_lighting);
78 Holds a mesh for a mapblock.
80 Besides the SMesh*, this contains information used for animating
81 the vertex positions, colors and texture coordinates of the mesh.
83 - cracks [implemented]
84 - day/night transitions [implemented]
85 - animated flowing liquids [not implemented]
86 - animating vertex positions for e.g. axles [not implemented]
91 // Builds the mesh given
92 MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
95 // Main animation function, parameters:
96 // faraway: whether the block is far away from the camera (~50 nodes)
97 // time: the global animation time, 0 .. 60 (repeats every minute)
98 // daynight_ratio: 0 .. 1000
99 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
100 // Returns true if anything has been changed.
101 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
103 scene::IMesh *getMesh()
108 MinimapMapblock *moveMinimapMapblock()
110 MinimapMapblock *p = m_minimap_mapblock;
111 m_minimap_mapblock = NULL;
115 bool isAnimationForced() const
117 return m_animation_force_timer == 0;
120 void decreaseAnimationForceTimer()
122 if(m_animation_force_timer > 0)
123 m_animation_force_timer--;
126 void updateCameraOffset(v3s16 camera_offset);
129 scene::IMesh *m_mesh;
130 MinimapMapblock *m_minimap_mapblock;
132 video::IVideoDriver *m_driver;
133 ITextureSource *m_tsrc;
134 IShaderSource *m_shdrsrc;
136 bool m_enable_shaders;
137 bool m_use_tangent_vertices;
140 // Must animate() be called before rendering?
141 bool m_has_animation;
142 int m_animation_force_timer;
144 // Animation info: cracks
145 // Last crack value passed to animate()
147 // Maps mesh buffer (i.e. material) indices to base texture names
148 UNORDERED_MAP<u32, std::string> m_crack_materials;
150 // Animation info: texture animationi
151 // Maps meshbuffers to TileSpecs
152 UNORDERED_MAP<u32, TileSpec> m_animation_tiles;
153 UNORDERED_MAP<u32, int> m_animation_frames; // last animation frame
154 UNORDERED_MAP<u32, int> m_animation_frame_offsets;
156 // Animation info: day/night transitions
157 // Last daynight_ratio value passed to animate()
158 u32 m_last_daynight_ratio;
159 // For each meshbuffer, maps vertex indices to (day,night) pairs
160 std::map<u32, std::map<u32, std::pair<u8, u8> > > m_daynight_diffs;
162 // Camera offset info -> do we have to translate the mesh?
163 v3s16 m_camera_offset;
169 This is used because CMeshBuffer::append() is very slow
174 std::vector<u16> indices;
175 std::vector<video::S3DVertex> vertices;
176 std::vector<video::S3DVertexTangents> tangent_vertices;
181 std::vector<PreMeshBuffer> prebuffers;
182 bool m_use_tangent_vertices;
184 MeshCollector(bool use_tangent_vertices):
185 m_use_tangent_vertices(use_tangent_vertices)
189 void append(const TileSpec &material,
190 const video::S3DVertex *vertices, u32 numVertices,
191 const u16 *indices, u32 numIndices);
192 void append(const TileSpec &material,
193 const video::S3DVertex *vertices, u32 numVertices,
194 const u16 *indices, u32 numIndices,
195 v3f pos, video::SColor c);
199 // alpha in the A channel of the returned SColor
200 // day light (0-255) in the R channel of the returned SColor
201 // night light (0-255) in the G channel of the returned SColor
202 // light source (0-255) in the B channel of the returned SColor
203 inline video::SColor MapBlock_LightColor(u8 alpha, u16 light, u8 light_source=0)
205 return video::SColor(alpha, (light & 0xff), (light >> 8), light_source);
208 // Compute light at node
209 u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
210 u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
211 u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data);
213 // Converts from day + night color values (0..255)
214 // and a given daynight_ratio to the final SColor shown on screen.
215 void finalColorBlend(video::SColor& result,
216 u8 day, u8 night, u32 daynight_ratio);
218 // Retrieves the TileSpec of a face of a node
219 // Adds MATERIAL_FLAG_CRACK if the node is cracked
220 TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data);
221 TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data);