Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::unordered_map<Vertex,u32> individVerts{};
- u32 verts_count {0};
- for(const auto& shape : shapes){
- for(const auto& ind : shape.mesh.indices){
- Vertex vert{};
- vert.pos = {
- attr.vertices[3 * ind.vertex_index + 0],
- attr.vertices[3 * ind.vertex_index + 1],
- attr.vertices[3 * ind.vertex_index + 2],
- };
- vert.uvco = {
- attr.texcoords[2 * ind.texcoord_index + 0],
- 1.f - attr.texcoords[2 * ind.texcoord_index + 1]
- };
- vert.norm = {
- attr.normals[3 * ind.normal_index + 0],
- attr.normals[3 * ind.normal_index + 1],
- attr.normals[3 * ind.normal_index + 2],
- };
- if(individVerts.count(vert) == 0){
- individVerts[vert] = static_cast<u32>(obju->verts.size());
- obju->verts . push_back(vert);
- }
- obju->inds.push_back(individVerts[vert]);
- verts_count++;
- size_t size = shape.mesh.indices.size();
- if(verts_count % 3 == 0){
- u64 ind_1 = obju->inds[verts_count - 3];
- u64 ind_2 = obju->inds[verts_count - 2];
- u64 ind_3 = obju->inds[verts_count - 1];
- Vertex vert_1 = obju->verts[ind_1];
- Vertex vert_2 = obju->verts[ind_2];
- Vertex vert_3 = obju->verts[ind_3];
- glm::vec3 E_1 = vert_2.pos - vert_1.pos;
- glm::vec3 E_2 = vert_3.pos - vert_1.pos;
- glm::vec2 dUV_1 = vert_2.uvco - vert_1.uvco;
- glm::vec2 dUV_2 = vert_3.uvco - vert_1.uvco;
- f32 f = 1.0f / (dUV_1.x * dUV_2.y - dUV_2.x * dUV_1.y);
- glm::vec3 tan;
- tan.x = f * (dUV_2.y * E_1.x - dUV_1.y * E_2.x);
- tan.y = f * (dUV_2.y * E_1.y - dUV_1.y * E_2.y);
- tan.z = f * (dUV_2.y * E_1.z - dUV_1.y * E_2.z);
- glm::vec3 btan;
- btan.x = f * (-dUV_2.x * E_1.x + dUV_1.x * E_2.x);
- btan.y = f * (-dUV_2.x * E_1.y + dUV_1.x * E_2.y);
- btan.z = f * (-dUV_2.x * E_1.z + dUV_1.x * E_2.z);
- obju->verts[ind_1].tan = tan;
- obju->verts[ind_2].tan = tan;
- obju->verts[ind_3].tan = tan;
- obju->verts[ind_1].btan = btan;
- obju->verts[ind_2].btan = btan;
- obju->verts[ind_3].btan = btan;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment