indice-aware calculate_tangents

This commit is contained in:
milek7
2020-10-19 15:04:03 +02:00
parent 5b50a39610
commit 6a48293a40
5 changed files with 19 additions and 15 deletions

View File

@@ -685,8 +685,8 @@ std::pair<int, int> TSubModel::Load( cParser &parser, bool dynamic )
} }
} }
Vertices.resize( m_geometry.vertex_count ); // in case we had some degenerate triangles along the way 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_indices( Indices, Vertices );
gfx::calculate_tangents( Vertices, Indices, GL_TRIANGLES );
// update values potentially changed by indexing // update values potentially changed by indexing
m_geometry.index_count = Indices.size(); m_geometry.index_count = Indices.size();
m_geometry.vertex_count = Vertices.size(); m_geometry.vertex_count = Vertices.size();

View File

@@ -67,7 +67,7 @@ basic_vertex::deserialize_packed( std::istream &s, bool const Tangent ) {
// based on // based on
// Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. // Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”.
// Terathon Software, 2001. http://terathon.com/code/tangent.html // 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(); size_t vertex_count = vertices.size();
@@ -75,12 +75,14 @@ void calculate_tangents(vertex_array &vertices, int type)
return; return;
size_t triangle_count; size_t triangle_count;
size_t tri_count_base = indices.empty() ? vertex_count : indices.size();
if (type == GL_TRIANGLES) if (type == GL_TRIANGLES)
triangle_count = vertex_count / 3; triangle_count = tri_count_base / 3;
else if (type == GL_TRIANGLE_STRIP) else if (type == GL_TRIANGLE_STRIP)
triangle_count = vertex_count - 2; triangle_count = tri_count_base - 2;
else if (type == GL_TRIANGLE_FAN) else if (type == GL_TRIANGLE_FAN)
triangle_count = vertex_count - 2; triangle_count = tri_count_base - 2;
else else
return; return;
@@ -116,6 +118,12 @@ void calculate_tangents(vertex_array &vertices, int type)
i3 = a + 2; 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 &v1 = vertices[i1].position;
const glm::vec3 &v2 = vertices[i2].position; const glm::vec3 &v2 = vertices[i2].position;
const glm::vec3 &v3 = vertices[i3].position; const glm::vec3 &v3 = vertices[i3].position;
@@ -198,14 +206,10 @@ void calculate_indices( index_array &Indices, vertex_array &Vertices ) {
++matchiter; ++matchiter;
if( ( glm::all( glm::epsilonEqual( vertex.position, matchiter->position, matchtolerance ) ) ) if( ( glm::all( glm::epsilonEqual( vertex.position, matchiter->position, matchtolerance ) ) )
&& ( glm::all( glm::epsilonEqual( vertex.normal, matchiter->normal, matchtolerance ) ) ) && ( glm::all( glm::epsilonEqual( vertex.normal, matchiter->normal, matchtolerance ) ) )
&& ( glm::all( glm::epsilonEqual( vertex.texture, matchiter->texture, matchtolerance ) ) ) && ( glm::all( glm::epsilonEqual( vertex.texture, matchiter->texture, matchtolerance ) ) ) ) {
&& ( vertex.tangent.w == matchiter->tangent.w ) ) {
Indices[ matchidx ] = Indices[ idx ]; 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 ); indexedvertices.emplace_back( vertex );
} }
// done indexing, swap the source vertex bank with the processed one // done indexing, swap the source vertex bank with the processed one

View File

@@ -52,7 +52,7 @@ using basic_index = std::uint32_t;
using vertex_array = std::vector<basic_vertex>; using vertex_array = std::vector<basic_vertex>;
using index_array = std::vector<basic_index>; using index_array = std::vector<basic_index>;
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 ); void calculate_indices( index_array &Indices, vertex_array &Vertices );
// generic geometry bank class, allows storage, update and drawing of geometry chunks // generic geometry bank class, allows storage, update and drawing of geometry chunks

View File

@@ -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 // 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::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); 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 // 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) 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); 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 // 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) 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); return m_geometry.append(Vertices, Geometry);
} }

View File

@@ -154,7 +154,7 @@ void vr_openvr::begin_frame()
glm::vec2(vertex->rfTextureCoord[0], vertex->rfTextureCoord[1]))); 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); submodel_name = std::string(component == -1 ? rendermodel_name : component_name);
sm = controllers[i]->model->AppendChildFromGeometry(submodel_name, "__root", vertices, indices); sm = controllers[i]->model->AppendChildFromGeometry(submodel_name, "__root", vertices, indices);