diff --git a/Model3d.cpp b/Model3d.cpp index 1292440b..38d8dc63 100644 --- a/Model3d.cpp +++ b/Model3d.cpp @@ -685,8 +685,8 @@ std::pair TSubModel::Load( cParser &parser, bool dynamic ) } } Vertices.resize( m_geometry.vertex_count ); // in case we had some degenerate triangles along the way - gfx::calculate_tangents( Vertices, GL_TRIANGLES ); gfx::calculate_indices( Indices, Vertices ); + gfx::calculate_tangents( Vertices, Indices, GL_TRIANGLES ); // update values potentially changed by indexing m_geometry.index_count = Indices.size(); m_geometry.vertex_count = Vertices.size(); diff --git a/geometrybank.cpp b/geometrybank.cpp index ff4fcc29..769e2e6f 100644 --- a/geometrybank.cpp +++ b/geometrybank.cpp @@ -67,7 +67,7 @@ basic_vertex::deserialize_packed( std::istream &s, bool const Tangent ) { // based on // Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. // Terathon Software, 2001. http://terathon.com/code/tangent.html -void calculate_tangents(vertex_array &vertices, int type) +void calculate_tangents(vertex_array &vertices, const index_array &indices, int type) { size_t vertex_count = vertices.size(); @@ -75,12 +75,14 @@ void calculate_tangents(vertex_array &vertices, int type) return; size_t triangle_count; + size_t tri_count_base = indices.empty() ? vertex_count : indices.size(); + if (type == GL_TRIANGLES) - triangle_count = vertex_count / 3; + triangle_count = tri_count_base / 3; else if (type == GL_TRIANGLE_STRIP) - triangle_count = vertex_count - 2; + triangle_count = tri_count_base - 2; else if (type == GL_TRIANGLE_FAN) - triangle_count = vertex_count - 2; + triangle_count = tri_count_base - 2; else return; @@ -116,6 +118,12 @@ void calculate_tangents(vertex_array &vertices, int type) i3 = a + 2; } + if (!indices.empty()) { + i1 = indices[i1]; + i2 = indices[i2]; + i3 = indices[i3]; + } + const glm::vec3 &v1 = vertices[i1].position; const glm::vec3 &v2 = vertices[i2].position; const glm::vec3 &v3 = vertices[i3].position; @@ -198,14 +206,10 @@ void calculate_indices( index_array &Indices, vertex_array &Vertices ) { ++matchiter; if( ( glm::all( glm::epsilonEqual( vertex.position, matchiter->position, matchtolerance ) ) ) && ( glm::all( glm::epsilonEqual( vertex.normal, matchiter->normal, matchtolerance ) ) ) - && ( glm::all( glm::epsilonEqual( vertex.texture, matchiter->texture, matchtolerance ) ) ) - && ( vertex.tangent.w == matchiter->tangent.w ) ) { + && ( glm::all( glm::epsilonEqual( vertex.texture, matchiter->texture, matchtolerance ) ) ) ) { Indices[ matchidx ] = Indices[ idx ]; - // HACK, TODO: tangent math winged/adapted from opengl-tutorial.org 13, check if it makes any sense - vertex.tangent += glm::vec4{ glm::vec3( matchiter->tangent ), 0.f }; } } - vertex.tangent = { glm::normalize( glm::vec3{ vertex.tangent } ), vertex.tangent.w }; indexedvertices.emplace_back( vertex ); } // done indexing, swap the source vertex bank with the processed one diff --git a/geometrybank.h b/geometrybank.h index 5d603477..fce47294 100644 --- a/geometrybank.h +++ b/geometrybank.h @@ -52,7 +52,7 @@ using basic_index = std::uint32_t; using vertex_array = std::vector; using index_array = std::vector; -void calculate_tangents( vertex_array &vertices, int type ); +void calculate_tangents(vertex_array &vertices, const index_array &indices, int type ); void calculate_indices( index_array &Indices, vertex_array &Vertices ); // generic geometry bank class, allows storage, update and drawing of geometry chunks diff --git a/opengl33renderer.cpp b/opengl33renderer.cpp index 2fdeabda..ad0481ef 100644 --- a/opengl33renderer.cpp +++ b/opengl33renderer.cpp @@ -1860,7 +1860,7 @@ gfx::geometry_handle opengl33_renderer::Insert( gfx::index_array &Indices, gfx:: // creates a new geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL gfx::geometry_handle opengl33_renderer::Insert(gfx::vertex_array &Vertices, gfx::geometrybank_handle const &Geometry, int const Type) { - gfx::calculate_tangents(Vertices, Type); + gfx::calculate_tangents(Vertices, gfx::index_array(), Type); return m_geometry.create_chunk(Vertices, Geometry, Type); } @@ -1868,7 +1868,7 @@ gfx::geometry_handle opengl33_renderer::Insert(gfx::vertex_array &Vertices, gfx: // replaces data of specified chunk with the supplied vertex data, starting from specified offset bool opengl33_renderer::Replace(gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type, std::size_t const Offset) { - gfx::calculate_tangents(Vertices, Type); + gfx::calculate_tangents(Vertices, gfx::index_array(), Type); return m_geometry.replace(Vertices, Geometry, Offset); } @@ -1876,7 +1876,7 @@ bool opengl33_renderer::Replace(gfx::vertex_array &Vertices, gfx::geometry_handl // adds supplied vertex data at the end of specified chunk bool opengl33_renderer::Append(gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type) { - gfx::calculate_tangents(Vertices, Type); + gfx::calculate_tangents(Vertices, gfx::index_array(), Type); return m_geometry.append(Vertices, Geometry); } diff --git a/vr/openvr_imp.cpp b/vr/openvr_imp.cpp index d49ef4b9..e772eedf 100644 --- a/vr/openvr_imp.cpp +++ b/vr/openvr_imp.cpp @@ -154,7 +154,7 @@ void vr_openvr::begin_frame() glm::vec2(vertex->rfTextureCoord[0], vertex->rfTextureCoord[1]))); } - gfx::calculate_tangents(vertices, GL_TRIANGLES); + gfx::calculate_tangents(vertices, indices, GL_TRIANGLES); submodel_name = std::string(component == -1 ? rendermodel_name : component_name); sm = controllers[i]->model->AppendChildFromGeometry(submodel_name, "__root", vertices, indices);