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>
34#include <unordered_map>
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 glDrawElementsInstanced(GL_TRIANGLES,
128 std::get<1>(m_stk_material[material_id]),
130 (
void*)(std::get<0>(m_stk_material[material_id]) << 1),
131 (
unsigned)m_ins_dat[dct].size());
136 virtual void uploadGLMesh();
138 virtual void uploadInstanceData();
140 bool combineMeshBuffer(
SPMeshBuffer* spmb,
bool different_material =
true)
143 if (spmb->m_vertices.size() + m_vertices.size() > 65536)
147 const uint16_t old_vtx_count = (uint16_t)m_vertices.size();
148 m_vertices.insert(m_vertices.end(), spmb->m_vertices.begin(),
149 spmb->m_vertices.end());
150 for (uint16_t& idx : spmb->m_indices)
152 idx += old_vtx_count;
154 if (different_material)
156 m_stk_material.emplace_back(getIndexCount(), spmb->getIndexCount(),
157 std::get<2>(spmb->m_stk_material[0]));
161 std::get<1>(m_stk_material[0]) += (unsigned)spmb->m_indices.size();
163 m_indices.insert(m_indices.end(), spmb->m_indices.begin(),
164 spmb->m_indices.end());
168 void initDrawMaterial();
170 void enableSkinningData() { m_skinned =
true; }
172 Material* getSTKMaterial(
unsigned first_index = 0)
const
174 for (
unsigned i = 0; i < m_stk_material.size(); i++)
176 if (i ==
unsigned(m_stk_material.size() - 1) ||
177 (first_index >= std::get<0>(m_stk_material[i]) &&
178 first_index < std::get<0>(m_stk_material[i + 1])))
180 return std::get<2>(m_stk_material[i]);
187 void enableTextureMatrix(
unsigned mat_id);
189 std::array<std::shared_ptr<SPTexture>, 6>&
190 getSPTextures(
unsigned first_index = 0)
192 assert(m_stk_material.size() == m_textures.size());
193 for (
unsigned i = 0; i < m_stk_material.size(); i++)
195 if (i ==
unsigned(m_stk_material.size() - 1) ||
196 (first_index >= std::get<0>(m_stk_material[i]) &&
197 first_index < std::get<0>(m_stk_material[i + 1])))
199 return m_textures[i];
203 return m_textures[0];
206 const std::array<std::shared_ptr<SPTexture>, 6>&
207 getSPTexturesByMaterialID(
int material_id)
const
209 assert((
size_t)material_id < m_textures.size());
210 return m_textures[material_id];
213 std::vector<Material*> getAllSTKMaterials()
const
215 std::vector<Material*> ret;
216 for (
unsigned i = 0; i < m_stk_material.size(); i++)
218 ret.push_back(std::get<2>(m_stk_material[i]));
223 const std::unordered_map<std::string, unsigned>& getTextureCompare()
const
224 {
return m_tex_cmp; }
226 int getMaterialID(
const std::string& tex_cmp)
const
228 auto itr = m_tex_cmp.find(tex_cmp);
229 if (itr != m_tex_cmp.end())
231 return (
int)itr->second;
238 if (m_uploaded_instance)
240 for (
unsigned i = 0; i < DCT_FOR_VAO; i++)
242 m_ins_dat[i].clear();
243 m_uploaded_instance =
false;
246 m_ins_dat[dct].push_back(
id);
249 void recreateVAO(
unsigned i);
251 video::S3DVertexSkinnedMesh* getSPMVertex()
253 return m_vertices.data();
256 void addSPMVertex(
const video::S3DVertexSkinnedMesh& v)
258 m_vertices.push_back(v);
261 void addIndex(uint16_t idx)
263 m_indices.push_back(idx);
266 void setSPMVertices(std::vector<video::S3DVertexSkinnedMesh>& vertices)
268 m_vertices = std::move(vertices);
271 void setIndices(std::vector<uint16_t>& indices)
273 m_indices = std::move(indices);
278 void reloadTextureCompare();
282 m_vertices.shrink_to_fit();
283 m_indices.shrink_to_fit();
286 SPShader* getShader(
bool skinned =
false)
const
287 {
return skinned ? m_shaders[1].get() : m_shaders[0].get(); }
289 virtual const video::SMaterial& getMaterial()
const
291 static video::SMaterial unused;
295 virtual video::SMaterial& getMaterial()
297 static video::SMaterial unused;
301 virtual const void* getVertices()
const
303 return m_vertices.data();
306 virtual void* getVertices()
308 return m_vertices.data();
311 std::vector<video::S3DVertexSkinnedMesh>& getVerticesRef()
316 virtual u32 getVertexCount()
const
318 return (
unsigned)m_vertices.size();
321 virtual video::E_INDEX_TYPE getIndexType()
const
323 return video::EIT_16BIT;
326 std::vector<u16>& getIndicesRef()
331 virtual const u16* getIndices()
const
333 return m_indices.data();
336 virtual u16* getIndices()
338 return m_indices.data();
341 virtual u32 getIndexCount()
const
343 return (
unsigned)m_indices.size();
346 virtual const core::aabbox3d<f32>& getBoundingBox()
const
348 return m_bounding_box;
351 virtual void setBoundingBox(
const core::aabbox3df& box)
353 m_bounding_box = box;
356 virtual void recalculateBoundingBox()
358 if (m_vertices.empty())
360 m_bounding_box.reset(0.0f, 0.0f, 0.0f);
364 m_bounding_box.reset(m_vertices[0].m_position);
365 for (u32 i = 1; i < m_vertices.size(); i++)
367 m_bounding_box.addInternalPoint(m_vertices[i].m_position);
372 virtual video::E_VERTEX_TYPE getVertexType()
const
374 return video::EVT_SKINNED_MESH;
377 virtual const core::vector3df& getPosition(u32 i)
const
379 return m_vertices[i].m_position;
382 virtual core::vector3df& getPosition(u32 i)
384 return m_vertices[i].m_position;
387 virtual const core::vector3df& getNormal(u32 i)
const
389 static core::vector3df unused;
393 virtual core::vector3df& getNormal(u32 i)
395 static core::vector3df unused;
399 virtual const core::vector2df& getTCoords(u32 i)
const
401 static core::vector2df unused;
405 virtual core::vector2df& getTCoords(u32 i)
407 static core::vector2df unused;
411 virtual scene::E_PRIMITIVE_TYPE getPrimitiveType()
const
413 return EPT_TRIANGLES;
416 virtual void append(
const void*
const vertices, u32 numm_vertices,
417 const u16*
const indices, u32 numm_indices) {}
419 virtual void append(
const IMeshBuffer*
const other) {}
421 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex()
const
426 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index()
const
431 virtual void setHardwareMappingHint(E_HARDWARE_MAPPING,
432 E_BUFFER_TYPE Buffer) {}
434 virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) {}
436 virtual u32 getChangedID_Vertex()
const {
return 0; }
438 virtual u32 getChangedID_Index()
const {
return 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.