lua api: add register_on_item_activate
[waspsaliva.git] / src / client / mapblock_mesh.h
blobec1faea3294a6f25bc9dbb1520328bb9afac7d32
1 /*
2 Minetest
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 #pragma once
22 #include "irrlichttypes_extrabloated.h"
23 #include "client/tile.h"
24 #include "voxel.h"
25 #include <array>
26 #include <map>
28 class Client;
29 class IShaderSource;
32 Mesh making stuff
36 class MapBlock;
37 struct MinimapMapblock;
39 struct MeshMakeData
41 VoxelManipulator m_vmanip;
42 v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
43 v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
44 bool m_smooth_lighting = false;
46 Client *m_client;
47 bool m_use_shaders;
49 MeshMakeData(Client *client, bool use_shaders);
52 Copy block data manually (to allow optimizations by the caller)
54 void fillBlockDataBegin(const v3s16 &blockpos);
55 void fillBlockData(const v3s16 &block_offset, MapNode *data);
58 Copy central data directly from block, and other data from
59 parent of block.
61 void fill(MapBlock *block);
64 Set the (node) position of a crack
66 void setCrack(int crack_level, v3s16 crack_pos);
69 Enable or disable smooth lighting
71 void setSmoothLighting(bool smooth_lighting);
75 Holds a mesh for a mapblock.
77 Besides the SMesh*, this contains information used for animating
78 the vertex positions, colors and texture coordinates of the mesh.
79 For example:
80 - cracks [implemented]
81 - day/night transitions [implemented]
82 - animated flowing liquids [not implemented]
83 - animating vertex positions for e.g. axles [not implemented]
85 class MapBlockMesh
87 public:
88 // Builds the mesh given
89 MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
90 ~MapBlockMesh();
92 // Main animation function, parameters:
93 // faraway: whether the block is far away from the camera (~50 nodes)
94 // time: the global animation time, 0 .. 60 (repeats every minute)
95 // daynight_ratio: 0 .. 1000
96 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
97 // Returns true if anything has been changed.
98 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
100 scene::IMesh *getMesh()
102 return m_mesh[0];
105 scene::IMesh *getMesh(u8 layer)
107 return m_mesh[layer];
110 MinimapMapblock *moveMinimapMapblock()
112 MinimapMapblock *p = m_minimap_mapblock;
113 m_minimap_mapblock = NULL;
114 return p;
117 bool isAnimationForced() const
119 return m_animation_force_timer == 0;
122 void decreaseAnimationForceTimer()
124 if(m_animation_force_timer > 0)
125 m_animation_force_timer--;
128 void updateCameraOffset(v3s16 camera_offset);
130 std::set<v3s16> esp_nodes;
132 private:
133 scene::IMesh *m_mesh[MAX_TILE_LAYERS];
134 MinimapMapblock *m_minimap_mapblock;
135 ITextureSource *m_tsrc;
136 IShaderSource *m_shdrsrc;
138 bool m_enable_shaders;
139 bool m_enable_vbo;
141 // Must animate() be called before rendering?
142 bool m_has_animation;
143 int m_animation_force_timer;
145 // Animation info: cracks
146 // Last crack value passed to animate()
147 int m_last_crack;
148 // Maps mesh and mesh buffer (i.e. material) indices to base texture names
149 std::map<std::pair<u8, u32>, std::string> m_crack_materials;
151 // Animation info: texture animationi
152 // Maps mesh and mesh buffer indices to TileSpecs
153 // Keys are pairs of (mesh index, buffer index in the mesh)
154 std::map<std::pair<u8, u32>, TileLayer> m_animation_tiles;
155 std::map<std::pair<u8, u32>, int> m_animation_frames; // last animation frame
156 std::map<std::pair<u8, u32>, int> m_animation_frame_offsets;
158 // Animation info: day/night transitions
159 // Last daynight_ratio value passed to animate()
160 u32 m_last_daynight_ratio;
161 // For each mesh and mesh buffer, stores pre-baked colors
162 // of sunlit vertices
163 // Keys are pairs of (mesh index, buffer index in the mesh)
164 std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
166 // Camera offset info -> do we have to translate the mesh?
167 v3s16 m_camera_offset;
171 * Encodes light of a node.
172 * The result is not the final color, but a
173 * half-baked vertex color.
174 * You have to multiply the resulting color
175 * with the node's color.
177 * \param light the first 8 bits are day light,
178 * the last 8 bits are night light
179 * \param emissive_light amount of light the surface emits,
180 * from 0 to LIGHT_SUN.
182 video::SColor encode_light(u16 light, u8 emissive_light);
184 // Compute light at node
185 u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
186 u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
187 const NodeDefManager *ndef);
188 u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
189 u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
192 * Returns the sunlight's color from the current
193 * day-night ratio.
195 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
198 * Gives the final SColor shown on screen.
200 * \param result output color
201 * \param light first 8 bits are day light, second 8 bits are
202 * night light
204 void final_color_blend(video::SColor *result,
205 u16 light, u32 daynight_ratio);
208 * Gives the final SColor shown on screen.
210 * \param result output color
211 * \param data the half-baked vertex color
212 * \param dayLight color of the sunlight
214 void final_color_blend(video::SColor *result,
215 const video::SColor &data, const video::SColorf &dayLight);
217 // Retrieves the TileSpec of a face of a node
218 // Adds MATERIAL_FLAG_CRACK if the node is cracked
219 // TileSpec should be passed as reference due to the underlying TileFrame and its vector
220 // TileFrame vector copy cost very much to client
221 void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
222 void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);