]> git.lizzy.rs Git - minetest.git/blob - src/mapblock_mesh.h
Implement WieldMeshSceneNode which improves wield mesh rendering
[minetest.git] / src / mapblock_mesh.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef MAPBLOCK_MESH_HEADER
21 #define MAPBLOCK_MESH_HEADER
22
23 #include "irrlichttypes_extrabloated.h"
24 #include "tile.h"
25 #include "voxel.h"
26 #include <map>
27
28 class IGameDef;
29
30 /*
31         Mesh making stuff
32 */
33
34
35 class MapBlock;
36
37 struct MeshMakeData
38 {
39         VoxelManipulator m_vmanip;
40         v3s16 m_blockpos;
41         v3s16 m_crack_pos_relative;
42         v3s16 m_highlighted_pos_relative;
43         bool m_smooth_lighting;
44         bool m_show_hud;
45         video::SColor m_highlight_mesh_color;
46
47         IGameDef *m_gamedef;
48
49         MeshMakeData(IGameDef *gamedef);
50
51         /*
52                 Copy central data directly from block, and other data from
53                 parent of block.
54         */
55         void fill(MapBlock *block);
56
57         /*
58                 Set up with only a single node at (1,1,1)
59         */
60         void fillSingleNode(MapNode *node);
61
62         /*
63                 Set the (node) position of a crack
64         */
65         void setCrack(int crack_level, v3s16 crack_pos);
66
67         /*
68                 Set the highlighted node position
69         */
70
71         void setHighlighted(v3s16 highlighted_pos, bool show_hud);
72         /*
73                 Enable or disable smooth lighting
74         */
75         void setSmoothLighting(bool smooth_lighting);
76 };
77
78 /*
79         Holds a mesh for a mapblock.
80
81         Besides the SMesh*, this contains information used for animating
82         the vertex positions, colors and texture coordinates of the mesh.
83         For example:
84         - cracks [implemented]
85         - day/night transitions [implemented]
86         - animated flowing liquids [not implemented]
87         - animating vertex positions for e.g. axles [not implemented]
88 */
89 class MapBlockMesh
90 {
91 public:
92         // Builds the mesh given
93         MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
94         ~MapBlockMesh();
95
96         // Main animation function, parameters:
97         //   faraway: whether the block is far away from the camera (~50 nodes)
98         //   time: the global animation time, 0 .. 60 (repeats every minute)
99         //   daynight_ratio: 0 .. 1000
100         //   crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
101         // Returns true if anything has been changed.
102         bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
103
104         scene::SMesh* getMesh()
105         {
106                 return m_mesh;
107         }
108
109         bool isAnimationForced() const
110         {
111                 return m_animation_force_timer == 0;
112         }
113
114         void decreaseAnimationForceTimer()
115         {
116                 if(m_animation_force_timer > 0)
117                         m_animation_force_timer--;
118         }
119         
120         void updateCameraOffset(v3s16 camera_offset);
121
122 private:
123         scene::SMesh *m_mesh;
124         IGameDef *m_gamedef;
125
126         bool m_enable_shaders;
127         bool m_enable_highlighting;
128
129         video::SColor m_highlight_mesh_color;
130         
131         // Must animate() be called before rendering?
132         bool m_has_animation;
133         int m_animation_force_timer;
134
135         // Animation info: cracks
136         // Last crack value passed to animate()
137         int m_last_crack;
138         // Maps mesh buffer (i.e. material) indices to base texture names
139         std::map<u32, std::string> m_crack_materials;
140         std::list<u32> m_highlighted_materials;
141
142         // Animation info: texture animationi
143         // Maps meshbuffers to TileSpecs
144         std::map<u32, TileSpec> m_animation_tiles;
145         std::map<u32, int> m_animation_frames; // last animation frame
146         std::map<u32, int> m_animation_frame_offsets;
147         
148         // Animation info: day/night transitions
149         // Last daynight_ratio value passed to animate()
150         u32 m_last_daynight_ratio;
151         // For each meshbuffer, maps vertex indices to (day,night) pairs
152         std::map<u32, std::map<u32, std::pair<u8, u8> > > m_daynight_diffs;
153         
154         // Camera offset info -> do we have to translate the mesh?
155         v3s16 m_camera_offset;
156 };
157
158
159
160 /*
161         This is used because CMeshBuffer::append() is very slow
162 */
163 struct PreMeshBuffer
164 {
165         TileSpec tile;
166         std::vector<u16> indices;
167         std::vector<video::S3DVertex> vertices;
168 };
169
170 struct MeshCollector
171 {
172         std::vector<PreMeshBuffer> prebuffers;
173
174         void append(const TileSpec &material,
175                         const video::S3DVertex *vertices, u32 numVertices,
176                         const u16 *indices, u32 numIndices);
177         void append(const TileSpec &material,
178                         const video::S3DVertex *vertices, u32 numVertices,
179                         const u16 *indices, u32 numIndices,
180                         v3f pos, video::SColor c);
181 };
182
183 // This encodes
184 //   alpha in the A channel of the returned SColor
185 //   day light (0-255) in the R channel of the returned SColor
186 //   night light (0-255) in the G channel of the returned SColor
187 //   light source (0-255) in the B channel of the returned SColor
188 inline video::SColor MapBlock_LightColor(u8 alpha, u16 light, u8 light_source=0)
189 {
190         return video::SColor(alpha, (light & 0xff), (light >> 8), light_source);
191 }
192
193 // Compute light at node
194 u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
195 u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
196 u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data);
197
198 // Converts from day + night color values (0..255)
199 // and a given daynight_ratio to the final SColor shown on screen.
200 void finalColorBlend(video::SColor& result,
201                 u8 day, u8 night, u32 daynight_ratio);
202
203 // Retrieves the TileSpec of a face of a node
204 // Adds MATERIAL_FLAG_CRACK if the node is cracked
205 TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data);
206 TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data);
207
208 #endif
209