Always calculate tangent space for Models

This commit is contained in:
SeanOMik 2022-10-12 23:35:39 -04:00
parent 098555453a
commit ce8886ade1
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
4 changed files with 5 additions and 59 deletions

View File

@ -78,7 +78,6 @@ int main(int argc, char *argv[]) {
se::ecs::Entity other_e = scene->create_entity(); se::ecs::Entity other_e = scene->create_entity();
other_e.add_component<se::ModelComponent>("examples/dev_testing/resources/transparent_window.fbx", other_e.add_component<se::ModelComponent>("examples/dev_testing/resources/transparent_window.fbx",
se::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE |
se::gfx::ModelProcessingFlags::MdlProcFlag_TRANSPARENT); se::gfx::ModelProcessingFlags::MdlProcFlag_TRANSPARENT);
auto &other_transform = other_e.add_component<se::TransformComponent>(); auto &other_transform = other_e.add_component<se::TransformComponent>();
other_transform.translate(5.f, 0.5f, 1.f); other_transform.translate(5.f, 0.5f, 1.f);
@ -89,15 +88,13 @@ int main(int argc, char *argv[]) {
// entity.add_component<se::ModelComponent>("examples/dev_testing/resources/planks/planks.fbx", simpleengine::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE); // entity.add_component<se::ModelComponent>("examples/dev_testing/resources/planks/planks.fbx", simpleengine::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE);
//entity.add_component<se::ModelComponent>("examples/dev_testing/resources/bricks/bricks.fbx", simpleengine::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE); //entity.add_component<se::ModelComponent>("examples/dev_testing/resources/bricks/bricks.fbx", simpleengine::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE);
entity.add_component<se::ModelComponent>("examples/dev_testing/resources/transparent_window.fbx", entity.add_component<se::ModelComponent>("examples/dev_testing/resources/transparent_window.fbx",
se::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE |
se::gfx::ModelProcessingFlags::MdlProcFlag_TRANSPARENT); se::gfx::ModelProcessingFlags::MdlProcFlag_TRANSPARENT);
auto &transform_comp = entity.add_component<se::TransformComponent>(); auto &transform_comp = entity.add_component<se::TransformComponent>();
transform_comp.translate(4.f, 0.f, 0.f); transform_comp.translate(4.f, 0.f, 0.f);
se::ecs::Entity brick_e = scene->create_entity(); se::ecs::Entity brick_e = scene->create_entity();
brick_e.add_component<se::ModelComponent>("examples/dev_testing/resources/bricks/bricks.fbx", brick_e.add_component<se::ModelComponent>("examples/dev_testing/resources/bricks/bricks.fbx");
se::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE);
auto &brick_transf = brick_e.add_component<se::TransformComponent>(); auto &brick_transf = brick_e.add_component<se::TransformComponent>();
brick_transf.translate(6.f, -0.5f, 1.f); brick_transf.translate(6.f, -0.5f, 1.f);

View File

@ -15,8 +15,7 @@ namespace simpleengine::gfx {
MdlProcFlag_NONE = 0x0, MdlProcFlag_NONE = 0x0,
MdlProcFlag_FLIP_TEX_COORDS_VERTICALLY = 0x1, MdlProcFlag_FLIP_TEX_COORDS_VERTICALLY = 0x1,
MdlProcFlag_FLIP_TEX_COORDS_HORIZONTALLY = 0x2, MdlProcFlag_FLIP_TEX_COORDS_HORIZONTALLY = 0x2,
MdlProcFlag_CALCULATE_TANGENT_SPACE = 0x4, MdlProcFlag_TRANSPARENT = 0x4,
MdlProcFlag_TRANSPARENT = 0x8,
}; };
/** /**

View File

@ -27,7 +27,6 @@ struct Material {
bool has_specular_map; bool has_specular_map;
sampler2D specular_map; sampler2D specular_map;
// TODO: Make Optional
bool has_normal_map; bool has_normal_map;
sampler2D normal_map; sampler2D normal_map;

View File

@ -34,12 +34,8 @@ namespace simpleengine::gfx {
void Model::load_model(std::string path) { void Model::load_model(std::string path) {
Assimp::Importer importer; Assimp::Importer importer;
if (model_processing_flags & ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE) {
additional_assimp_flags |= aiProcess_CalcTangentSpace;
}
// assimp post processing options: http://assimp.sourceforge.net/lib_html/postprocess_8h.html // assimp post processing options: http://assimp.sourceforge.net/lib_html/postprocess_8h.html
const aiScene *scene = importer.ReadFile(path, additional_assimp_flags | aiProcess_Triangulate | aiProcess_FlipUVs); const aiScene *scene = importer.ReadFile(path, aiProcess_GenNormals | aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cout << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl; std::cout << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl;
@ -141,9 +137,6 @@ namespace simpleengine::gfx {
std::vector<std::shared_ptr<Texture>> normal_maps = load_material_texture(processed_textures, material, loading_type); std::vector<std::shared_ptr<Texture>> normal_maps = load_material_texture(processed_textures, material, loading_type);
if (!normal_maps.empty()) { if (!normal_maps.empty()) {
textures.emplace(loading_type, normal_maps); textures.emplace(loading_type, normal_maps);
// Force calculation of tangent space.
model_processing_flags |= MdlProcFlag_CALCULATE_TANGENT_SPACE;
} }
// TODO Handle other types of texture maps // TODO Handle other types of texture maps
@ -180,10 +173,7 @@ namespace simpleengine::gfx {
Mesh se_mesh(vertices, indicies, mat); Mesh se_mesh(vertices, indicies, mat);
if (!mesh->HasNormals()) { // Store the tangents in the mesh
se_mesh.calculate_normals();
}
if (mesh->HasTangentsAndBitangents()) { if (mesh->HasTangentsAndBitangents()) {
se_mesh.tangents = tangents; se_mesh.tangents = tangents;
} }
@ -255,43 +245,4 @@ namespace simpleengine::gfx {
mesh.calculate_tangents(); mesh.calculate_tangents();
} }
} }
/* std::vector<std::pair<simpleengine::Vectorf, simpleengine::Vectorf>> calculate_tangent_space(
std::vector<LitVertex> vertices, std::vector<unsigned int> indices) {
std::vector<std::pair<simpleengine::Vectorf, simpleengine::Vectorf>> tangents;
tangents.resize(vertices.size());
for (int i = 0; i < indices.size(); i += 3) {
int index0 = indices[i];
int index1 = indices[i+1];
int index2 = indices[i+2];
LitVertex& lit_vertex0 = vertices[index0];
LitVertex& lit_vertex1 = vertices[index1];
LitVertex& lit_vertex2 = vertices[index2];
glm::vec3 pos0 = lit_vertex0.position;
glm::vec3 pos1 = lit_vertex1.position;
glm::vec3 pos2 = lit_vertex2.position;
// Edges of the triangle : postion delta
glm::vec3 delta_pos1 = pos1 - pos0;
glm::vec3 delta_pos2 = pos2 - pos0;
// UV delta
glm::vec2 delta_uv1 = lit_vertex1.tex_coord - lit_vertex0.tex_coord;
glm::vec2 delta_uv2 = lit_vertex2.tex_coord - lit_vertex0.tex_coord;
float r = 1.0f / (delta_uv1.x * delta_uv2.y - delta_uv1.y * delta_uv2.x);
glm::vec3 tangent = (delta_pos1 * delta_uv2.y - delta_pos2 * delta_uv1.y)*r;
glm::vec3 bitangent = (delta_pos2 * delta_uv1.x - delta_pos1 * delta_uv2.x)*r;
tangents[0] = {tangent, bitangent};
tangents[1] = {tangent, bitangent};
tangents[2] = {tangent, bitangent};
}
return tangents;
} */
} // namespace simpleengine::gfx } // namespace simpleengine::gfx