Improve glass
[oweals/minetest.git] / src / mapblock_mesh.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
24 #include "mapblock_nodemod.h"
25 #include "voxel.h"
26
27 class IGameDef;
28
29 /*
30         Mesh making stuff
31 */
32
33 /*
34         This is used because CMeshBuffer::append() is very slow
35 */
36 struct PreMeshBuffer
37 {
38         video::SMaterial material;
39         core::array<u16> indices;
40         core::array<video::S3DVertex> vertices;
41 };
42
43 class MeshCollector
44 {
45 public:
46         void append(
47                         video::SMaterial material,
48                         const video::S3DVertex* const vertices,
49                         u32 numVertices,
50                         const u16* const indices,
51                         u32 numIndices
52                 )
53         {
54                 PreMeshBuffer *p = NULL;
55                 for(u32 i=0; i<m_prebuffers.size(); i++)
56                 {
57                         PreMeshBuffer &pp = m_prebuffers[i];
58                         if(pp.material != material)
59                                 continue;
60
61                         p = &pp;
62                         break;
63                 }
64
65                 if(p == NULL)
66                 {
67                         PreMeshBuffer pp;
68                         pp.material = material;
69                         m_prebuffers.push_back(pp);
70                         p = &m_prebuffers[m_prebuffers.size()-1];
71                 }
72
73                 u32 vertex_count = p->vertices.size();
74                 for(u32 i=0; i<numIndices; i++)
75                 {
76                         u32 j = indices[i] + vertex_count;
77                         if(j > 65535)
78                         {
79                                 dstream<<"FIXME: Meshbuffer ran out of indices"<<std::endl;
80                                 // NOTE: Fix is to just add an another MeshBuffer
81                         }
82                         p->indices.push_back(j);
83                 }
84                 for(u32 i=0; i<numVertices; i++)
85                 {
86                         p->vertices.push_back(vertices[i]);
87                 }
88         }
89
90         void fillMesh(scene::SMesh *mesh)
91         {
92                 /*dstream<<"Filling mesh with "<<m_prebuffers.size()
93                                 <<" meshbuffers"<<std::endl;*/
94                 for(u32 i=0; i<m_prebuffers.size(); i++)
95                 {
96                         PreMeshBuffer &p = m_prebuffers[i];
97
98                         /*dstream<<"p.vertices.size()="<<p.vertices.size()
99                                         <<", p.indices.size()="<<p.indices.size()
100                                         <<std::endl;*/
101                         
102                         // Create meshbuffer
103                         
104                         // This is a "Standard MeshBuffer",
105                         // it's a typedeffed CMeshBuffer<video::S3DVertex>
106                         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
107                         // Set material
108                         buf->Material = p.material;
109                         //((scene::SMeshBuffer*)buf)->Material = p.material;
110                         // Use VBO
111                         //buf->setHardwareMappingHint(scene::EHM_STATIC);
112                         // Add to mesh
113                         mesh->addMeshBuffer(buf);
114                         // Mesh grabbed it
115                         buf->drop();
116
117                         buf->append(p.vertices.pointer(), p.vertices.size(),
118                                         p.indices.pointer(), p.indices.size());
119                 }
120         }
121
122 private:
123         core::array<PreMeshBuffer> m_prebuffers;
124 };
125
126 // Helper functions
127 video::SColor MapBlock_LightColor(u8 alpha, u8 light);
128
129 class MapBlock;
130
131 struct MeshMakeData
132 {
133         u32 m_daynight_ratio;
134         NodeModMap m_temp_mods;
135         VoxelManipulator m_vmanip;
136         v3s16 m_blockpos;
137         
138         /*
139                 Copy central data directly from block, and other data from
140                 parent of block.
141         */
142         void fill(u32 daynight_ratio, MapBlock *block);
143 };
144
145 // This is the highest-level function in here
146 scene::SMesh* makeMapBlockMesh(MeshMakeData *data, IGameDef *gamedef);
147
148 #endif
149