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