11#include " renderer/Mesh.hpp"
22
33#include " core/Project.hpp"
4+ #include < iostream>
5+ #include < map>
46
5- Mesh::Mesh (std::vector<Vertex> vertices, std::vector<unsigned int > indices, Texture const * texture_diffuse, Texture const * texture_opacity, AABB aabb)
6- : m_vertices(vertices)
7- , m_indices(indices)
8- , m_texture_diffuse(texture_diffuse)
9- , m_texture_opacity(texture_opacity)
10- , aabb(aabb)
11- { }
7+ bool IntermediateMesh::merge (IntermediateMesh const & other)
8+ {
9+ std::size_t new_textures_count = 0 ;
10+ for (auto other_texture : other.textures ) {
11+ auto found_it = std::find (textures.begin (), textures.end (), other_texture);
12+ if (found_it == textures.end ()) {
13+ ++new_textures_count;
14+ }
15+ }
16+
17+ if (textures.size () + new_textures_count > 10 ) {
18+ return false ;
19+ }
20+
21+ std::map<std::size_t , std::size_t > texture_mapping;
22+ for (std::size_t i = 0 ; auto other_texture : other.textures ) {
23+ auto found_it = std::find (textures.begin (), textures.end (), other_texture);
24+ if (found_it == textures.end ()) {
25+ texture_mapping[i] = textures.size ();
26+ textures.push_back (other_texture);
27+ } else {
28+ texture_mapping[i] = found_it - textures.begin ();
29+ }
30+ ++i;
31+ }
32+
33+ for (auto vertex : other.vertices ) {
34+ auto diffuse_index = texture_mapping[vertex.texture_indices >> 24 ];
35+ auto opacity_index = texture_mapping[(vertex.texture_indices >> 16 ) & 0xff ];
36+ vertex.texture_indices = (diffuse_index << 24 ) | (opacity_index << 16 );
37+ vertices.push_back (vertex);
38+ }
39+
40+ auto index_offset = indices.size ();
41+ for (auto index : other.indices ) {
42+ indices.push_back (index + index_offset);
43+ }
44+
45+ aabb = aabb.merge (other.aabb );
46+ return true ;
47+ }
48+
49+ Mesh::Mesh (IntermediateMesh mesh)
50+ {
51+ m_indices_size = mesh.indices .size ();
52+ textures = mesh.textures ;
53+
54+ setup_mesh (mesh.vertices , mesh.indices );
55+ }
1256
1357void Mesh::draw () const
1458{
1559 glBindVertexArray (m_vao);
16- glDrawElements (GL_TRIANGLES, m_indices. size () , GL_UNSIGNED_INT, nullptr );
60+ glDrawElements (GL_TRIANGLES, m_indices_size , GL_UNSIGNED_INT, nullptr );
1761}
1862
1963void Mesh::draw (ViewingMode mode) const
2064{
2165 auto const & shader = Shader::get_shader_for_mode (mode);
22- auto diffuse_texture_id = mode == ViewingMode::SOLID ? Project::get_current ()->fallback_texture ()->id : m_texture_diffuse->id ;
2366
24- // set diffuse texture
25- glActiveTexture (GL_TEXTURE0);
26- shader.set_uniform (shader.uniform_locations .texture_diffuse , 0 );
27- glBindTexture (GL_TEXTURE_2D, diffuse_texture_id);
67+ for (std::size_t i = 0 ; auto const & texture : textures) {
68+ auto id = mode == ViewingMode::SOLID ? Project::get_current ()->fallback_texture ()->id : texture->id ;
69+
70+ glActiveTexture (GL_TEXTURE0 + i);
71+ glBindTexture (GL_TEXTURE_2D, id);
72+ shader.set_uniform (shader.uniform_locations .textures [i], static_cast <int >(i));
2873
29- // set opacity texture
30- glActiveTexture (GL_TEXTURE1);
31- shader.set_uniform (shader.uniform_locations .texture_opacity , 1 );
32- glBindTexture (GL_TEXTURE_2D, m_texture_opacity->id );
74+ ++i;
75+ }
3376
3477 // set active
3578 glBindVertexArray (m_vao);
36- glDrawElements (GL_TRIANGLES, static_cast <unsigned int >(m_indices. size () ), GL_UNSIGNED_INT, nullptr );
79+ glDrawElements (GL_TRIANGLES, static_cast <unsigned int >(m_indices_size ), GL_UNSIGNED_INT, nullptr );
3780}
3881
39- void Mesh::setup_mesh ()
82+ void Mesh::setup_mesh (std::vector<Vertex> const & vertices, std::vector< unsigned int > const & indices )
4083{
4184 if (m_vao != 0 ) {
4285 return ;
@@ -48,10 +91,10 @@ void Mesh::setup_mesh()
4891
4992 glBindVertexArray (m_vao);
5093 glBindBuffer (GL_ARRAY_BUFFER, m_vbo);
51- glBufferData (GL_ARRAY_BUFFER, m_vertices .size () * sizeof (Vertex), &m_vertices [0 ], GL_STATIC_DRAW);
94+ glBufferData (GL_ARRAY_BUFFER, vertices .size () * sizeof (Vertex), &vertices [0 ], GL_STATIC_DRAW);
5295
5396 glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, m_ebo);
54- glBufferData (GL_ELEMENT_ARRAY_BUFFER, m_indices .size () * sizeof (unsigned int ), &m_indices [0 ], GL_STATIC_DRAW);
97+ glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices .size () * sizeof (unsigned int ), &indices [0 ], GL_STATIC_DRAW);
5598
5699 glVertexAttribPointer (0 , 3 , GL_FLOAT, GL_FALSE, sizeof (Vertex), (void *)0 );
57100 glEnableVertexAttribArray (0 );
@@ -61,11 +104,19 @@ void Mesh::setup_mesh()
61104 // vertex texture coords
62105 glVertexAttribPointer (2 , 2 , GL_FLOAT, GL_FALSE, sizeof (Vertex), (void *)offsetof (Vertex, m_tex_coords));
63106 glEnableVertexAttribArray (2 );
107+ // texture index
108+ glVertexAttribIPointer (3 , 1 , GL_UNSIGNED_INT, sizeof (Vertex), (void *)offsetof (Vertex, texture_indices));
109+ glEnableVertexAttribArray (3 );
64110}
65111
66112bool Mesh::is_fully_loaded () const
67113{
68- return m_texture_diffuse->is_loaded ;
114+ for (auto const & texture : textures) {
115+ if (!texture->is_loaded ) {
116+ return false ;
117+ }
118+ }
119+ return true ;
69120}
70121
71122AABB AABB::merge (AABB const & other)
0 commit comments