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