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