]> git.lizzy.rs Git - minetest.git/blob - src/mapblock_mesh.h
shader.cpp: don't test twice if shader programs are present
[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 "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
50         MeshMakeData(IGameDef *gamedef, bool use_shaders);
51
52         /*
53                 Copy central data directly from block, and other data from
54                 parent of block.
55         */
56         void fill(MapBlock *block);
57
58         /*
59                 Set up with only a single node at (1,1,1)
60         */
61         void fillSingleNode(MapNode *node);
62
63         /*
64                 Set the (node) position of a crack
65         */
66         void setCrack(int crack_level, v3s16 crack_pos);
67
68         /*
69                 Enable or disable smooth lighting
70         */
71         void setSmoothLighting(bool smooth_lighting);
72 };
73
74 /*
75         Holds a mesh for a mapblock.
76
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]
84 */
85 class MapBlockMesh
86 {
87 public:
88         // Builds the mesh given
89         MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
90         ~MapBlockMesh();
91
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);
99
100         scene::IMesh *getMesh()
101         {
102                 return m_mesh;
103         }
104
105         MinimapMapblock *moveMinimapMapblock()
106         {
107                 MinimapMapblock *p = m_minimap_mapblock;
108                 m_minimap_mapblock = NULL;
109                 return p;
110         }
111
112         bool isAnimationForced() const
113         {
114                 return m_animation_force_timer == 0;
115         }
116
117         void decreaseAnimationForceTimer()
118         {
119                 if(m_animation_force_timer > 0)
120                         m_animation_force_timer--;
121         }
122         
123         void updateCameraOffset(v3s16 camera_offset);
124
125 private:
126         scene::IMesh *m_mesh;
127         MinimapMapblock *m_minimap_mapblock;
128         IGameDef *m_gamedef;
129         ITextureSource *m_tsrc;
130         IShaderSource *m_shdrsrc;
131
132         bool m_enable_shaders;
133
134         // Must animate() be called before rendering?
135         bool m_has_animation;
136         int m_animation_force_timer;
137
138         // Animation info: cracks
139         // Last crack value passed to animate()
140         int m_last_crack;
141         // Maps mesh buffer (i.e. material) indices to base texture names
142         std::map<u32, std::string> m_crack_materials;
143
144         // Animation info: texture animationi
145         // Maps meshbuffers to TileSpecs
146         std::map<u32, TileSpec> m_animation_tiles;
147         std::map<u32, int> m_animation_frames; // last animation frame
148         std::map<u32, int> m_animation_frame_offsets;
149         
150         // Animation info: day/night transitions
151         // Last daynight_ratio value passed to animate()
152         u32 m_last_daynight_ratio;
153         // For each meshbuffer, maps vertex indices to (day,night) pairs
154         std::map<u32, std::map<u32, std::pair<u8, u8> > > m_daynight_diffs;
155         
156         // Camera offset info -> do we have to translate the mesh?
157         v3s16 m_camera_offset;
158 };
159
160
161
162 /*
163         This is used because CMeshBuffer::append() is very slow
164 */
165 struct PreMeshBuffer
166 {
167         TileSpec tile;
168         std::vector<u16> indices;
169         std::vector<video::S3DVertex> vertices;
170 };
171
172 struct MeshCollector
173 {
174         std::vector<PreMeshBuffer> prebuffers;
175         void append(const TileSpec &material,
176                         const video::S3DVertex *vertices, u32 numVertices,
177                         const u16 *indices, u32 numIndices);
178         void append(const TileSpec &material,
179                         const video::S3DVertex *vertices, u32 numVertices,
180                         const u16 *indices, u32 numIndices,
181                         v3f pos, video::SColor c);
182 };
183
184 // This encodes
185 //   alpha in the A channel of the returned SColor
186 //   day light (0-255) in the R channel of the returned SColor
187 //   night light (0-255) in the G channel of the returned SColor
188 //   light source (0-255) in the B channel of the returned SColor
189 inline video::SColor MapBlock_LightColor(u8 alpha, u16 light, u8 light_source=0)
190 {
191         return video::SColor(alpha, (light & 0xff), (light >> 8), light_source);
192 }
193
194 // Compute light at node
195 u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
196 u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
197 u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data);
198
199 // Converts from day + night color values (0..255)
200 // and a given daynight_ratio to the final SColor shown on screen.
201 void finalColorBlend(video::SColor& result,
202                 u8 day, u8 night, u32 daynight_ratio);
203
204 // Retrieves the TileSpec of a face of a node
205 // Adds MATERIAL_FLAG_CRACK if the node is cracked
206 TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data);
207 TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data);
208
209 #endif
210