18 #ifndef HEADER_SP_MESH_BUFFER_HPP
19 #define HEADER_SP_MESH_BUFFER_HPP
21 #include "graphics/gl_headers.hpp"
22 #include "graphics/sp/sp_base.hpp"
23 #include "graphics/sp/sp_instanced_data.hpp"
24 #include "graphics/sp/sp_per_object_uniform.hpp"
27 #include <IMeshBuffer.h>
28 #include <S3DVertex.h>
34 #include <unordered_map>
38 using namespace scene;
50 std::shared_ptr<SPShader> m_shaders[2];
52 std::vector<std::tuple<
size_t,
53 unsigned,
Material*> > m_stk_material;
55 std::vector<std::array<std::shared_ptr<SPTexture>, 6> > m_textures;
57 std::unordered_map<std::string, unsigned> m_tex_cmp;
59 std::vector<video::S3DVertexSkinnedMesh> m_vertices;
63 GLuint m_vao[DCT_FOR_VAO];
68 std::vector<uint16_t> m_indices;
70 core::aabbox3d<f32> m_bounding_box;
72 std::vector<SPInstancedData> m_ins_dat[DCT_FOR_VAO];
74 void* m_ins_dat_mapped_ptr[DCT_FOR_VAO];
76 unsigned m_gl_instance_size[DCT_FOR_VAO];
78 GLuint m_ins_array[DCT_FOR_VAO];
82 bool m_uploaded_instance;
93 setDebugName(
"SMeshBuffer");
95 m_stk_material.resize(1, std::make_tuple(0u, 0u,
nullptr));
97 for (
unsigned i = 0; i < DCT_FOR_VAO; i++)
99 m_ins_dat_mapped_ptr[i] = NULL;
100 m_gl_instance_size[i] = 0;
108 m_uploaded_gl =
false;
109 m_uploaded_instance =
false;
115 virtual void draw(DrawCallType dct = DCT_NORMAL,
int material_id = -1)
const
118 glBindVertexArray(m_vao[dct]);
119 if (material_id == -1)
122 glDrawElementsInstanced(GL_TRIANGLES, getIndexCount(),
123 GL_UNSIGNED_SHORT, 0, (
unsigned)m_ins_dat[dct].size());
127 unsigned idx_count = std::get<1>(m_stk_material[material_id]);
130 glDrawElementsInstanced(GL_TRIANGLES,
133 (
void*)(std::get<0>(m_stk_material[material_id]) << 1),
134 (
unsigned)m_ins_dat[dct].size());
139 virtual void uploadGLMesh();
141 virtual void uploadInstanceData();
143 bool combineMeshBuffer(
SPMeshBuffer* spmb,
bool different_material =
true)
146 if (spmb->m_vertices.size() + m_vertices.size() > 65536)
150 const uint16_t old_vtx_count = (uint16_t)m_vertices.size();
151 m_vertices.insert(m_vertices.end(), spmb->m_vertices.begin(),
152 spmb->m_vertices.end());
153 for (uint16_t& idx : spmb->m_indices)
155 idx += old_vtx_count;
157 if (different_material)
159 m_stk_material.emplace_back(getIndexCount(), spmb->getIndexCount(),
160 std::get<2>(spmb->m_stk_material[0]));
164 std::get<1>(m_stk_material[0]) += (unsigned)spmb->m_indices.size();
166 m_indices.insert(m_indices.end(), spmb->m_indices.begin(),
167 spmb->m_indices.end());
171 void initDrawMaterial();
173 void enableSkinningData() { m_skinned =
true; }
175 Material* getSTKMaterial(
unsigned first_index = 0)
const
177 for (
unsigned i = 0; i < m_stk_material.size(); i++)
179 if (i ==
unsigned(m_stk_material.size() - 1) ||
180 (first_index >= std::get<0>(m_stk_material[i]) &&
181 first_index < std::get<0>(m_stk_material[i + 1])))
183 return std::get<2>(m_stk_material[i]);
190 void enableTextureMatrix(
unsigned mat_id);
192 std::array<std::shared_ptr<SPTexture>, 6>&
193 getSPTextures(
unsigned first_index = 0)
195 assert(m_stk_material.size() == m_textures.size());
196 for (
unsigned i = 0; i < m_stk_material.size(); i++)
198 if (i ==
unsigned(m_stk_material.size() - 1) ||
199 (first_index >= std::get<0>(m_stk_material[i]) &&
200 first_index < std::get<0>(m_stk_material[i + 1])))
202 return m_textures[i];
206 return m_textures[0];
209 const std::array<std::shared_ptr<SPTexture>, 6>&
210 getSPTexturesByMaterialID(
int material_id)
const
212 assert((
size_t)material_id < m_textures.size());
213 return m_textures[material_id];
216 std::vector<Material*> getAllSTKMaterials()
const
218 std::vector<Material*> ret;
219 for (
unsigned i = 0; i < m_stk_material.size(); i++)
221 ret.push_back(std::get<2>(m_stk_material[i]));
226 const std::unordered_map<std::string, unsigned>& getTextureCompare()
const
227 {
return m_tex_cmp; }
229 int getMaterialID(
const std::string& tex_cmp)
const
231 auto itr = m_tex_cmp.find(tex_cmp);
232 if (itr != m_tex_cmp.end())
234 return (
int)itr->second;
241 if (m_uploaded_instance)
243 for (
unsigned i = 0; i < DCT_FOR_VAO; i++)
245 m_ins_dat[i].clear();
246 m_uploaded_instance =
false;
249 m_ins_dat[dct].push_back(
id);
252 void recreateVAO(
unsigned i);
254 video::S3DVertexSkinnedMesh* getSPMVertex()
256 return m_vertices.data();
259 void addSPMVertex(
const video::S3DVertexSkinnedMesh& v)
261 m_vertices.push_back(v);
264 void addIndex(uint16_t idx)
266 m_indices.push_back(idx);
269 void setSPMVertices(std::vector<video::S3DVertexSkinnedMesh>& vertices)
271 m_vertices = std::move(vertices);
274 void setIndices(std::vector<uint16_t>& indices)
276 m_indices = std::move(indices);
281 void reloadTextureCompare();
285 m_vertices.shrink_to_fit();
286 m_indices.shrink_to_fit();
289 SPShader* getShader(
bool skinned =
false)
const
290 {
return skinned ? m_shaders[1].get() : m_shaders[0].get(); }
292 virtual const video::SMaterial& getMaterial()
const
294 static video::SMaterial unused;
298 virtual video::SMaterial& getMaterial()
300 static video::SMaterial unused;
304 virtual const void* getVertices()
const
306 return m_vertices.data();
309 virtual void* getVertices()
311 return m_vertices.data();
314 std::vector<video::S3DVertexSkinnedMesh>& getVerticesRef()
319 virtual u32 getVertexCount()
const
321 return (
unsigned)m_vertices.size();
324 virtual video::E_INDEX_TYPE getIndexType()
const
326 return video::EIT_16BIT;
329 std::vector<u16>& getIndicesRef()
334 virtual const u16* getIndices()
const
336 return m_indices.data();
339 virtual u16* getIndices()
341 return m_indices.data();
344 virtual u32 getIndexCount()
const
346 return (
unsigned)m_indices.size();
349 virtual const core::aabbox3d<f32>& getBoundingBox()
const
351 return m_bounding_box;
354 virtual void setBoundingBox(
const core::aabbox3df& box)
356 m_bounding_box = box;
359 virtual void recalculateBoundingBox()
361 if (m_vertices.empty())
363 m_bounding_box.reset(0.0f, 0.0f, 0.0f);
367 m_bounding_box.reset(m_vertices[0].m_position);
368 for (u32 i = 1; i < m_vertices.size(); i++)
370 m_bounding_box.addInternalPoint(m_vertices[i].m_position);
375 virtual video::E_VERTEX_TYPE getVertexType()
const
377 return video::EVT_SKINNED_MESH;
380 virtual const core::vector3df& getPosition(u32 i)
const
382 return m_vertices[i].m_position;
385 virtual core::vector3df& getPosition(u32 i)
387 return m_vertices[i].m_position;
390 virtual const core::vector3df& getNormal(u32 i)
const
392 static core::vector3df unused;
396 virtual core::vector3df& getNormal(u32 i)
398 static core::vector3df unused;
402 virtual const core::vector2df& getTCoords(u32 i)
const
404 static core::vector2df unused;
408 virtual core::vector2df& getTCoords(u32 i)
410 static core::vector2df unused;
414 virtual scene::E_PRIMITIVE_TYPE getPrimitiveType()
const
416 return EPT_TRIANGLES;
419 virtual void append(
const void*
const vertices, u32 numm_vertices,
420 const u16*
const indices, u32 numm_indices) {}
422 virtual void append(
const IMeshBuffer*
const other) {}
424 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex()
const
429 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index()
const
434 virtual void setHardwareMappingHint(E_HARDWARE_MAPPING,
435 E_BUFFER_TYPE Buffer) {}
437 virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) {}
439 virtual u32 getChangedID_Vertex()
const {
return 0; }
441 virtual u32 getChangedID_Index()
const {
return 0; }
443 void disableForMaterial(u32 idx) { std::get<1>(m_stk_material[idx]) = 0; }
Definition: material.hpp:48
Definition: sp_instanced_data.hpp:32
Definition: sp_mesh_buffer.hpp:48
Definition: sp_shader.hpp:81
Declares the general types that are used by the network.