Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- // face normals
- luminos_hash_map<glm::u32vec3, glm::f32vec3> face_normals;
- luminos_hash_map<uint32_t, luminos_vector<uint32_t>> index_positions;
- for (uint32_t i = 0; i < indices.size(); i += 3)
- {
- const glm::vec3 p1(positions[3 * (indices[i + 0]) + 0], positions[3 * (indices[i + 0]) + 1], positions[3 * (indices[i + 0]) + 2]);
- const glm::vec3 p2(positions[3 * (indices[i + 1]) + 0], positions[3 * (indices[i + 1]) + 1], positions[3 * (indices[i + 1]) + 2]);
- const glm::vec3 p3(positions[3 * (indices[i + 2]) + 0], positions[3 * (indices[i + 2]) + 1], positions[3 * (indices[i + 2]) + 2]);
- const auto v1 = p2 - p1;
- const auto v2 = p3 - p1;
- const auto normal = glm::cross(v1, v2);
- glm::u32vec3 face_indices(indices[i], indices[i + 1], indices[i + 2]);
- glm::f32vec3 face_normal(normal);
- face_normals.insert(luminos_pair<glm::u32vec3, glm::f32vec3>(face_indices, face_normal));
- if (index_positions.find(indices[i]) == index_positions.end())
- {
- luminos_vector<uint32_t> idx_pos = { i };
- index_positions.insert(luminos_pair<uint32_t, luminos_vector<uint32_t>>(indices[i], idx_pos));
- }
- else
- {
- index_positions.find(i)->second.push_back(i);
- }
- if (index_positions.find(indices[i + 1]) == index_positions.end())
- {
- luminos_vector<uint32_t> idx_pos = { i + 1 };
- index_positions.insert(luminos_pair<uint32_t, luminos_vector<uint32_t>>(indices[i + 1], idx_pos));
- }
- else
- {
- index_positions.find(i)->second.push_back(i + 1);
- }
- if (index_positions.find(indices[i + 2]) == index_positions.end())
- {
- luminos_vector<uint32_t> idx_pos = { i + 2 };
- index_positions.insert(luminos_pair<uint32_t, luminos_vector<uint32_t>>(indices[i + 2], idx_pos));
- }
- else
- {
- index_positions.find(i)->second.push_back(i + 2);
- }
- }
- // vertex normals
- for (auto i = 0; i < positions.size() / 3; i++)
- {
- glm::vec3 normal;
- auto indices = index_positions.find(i)->second;
- for (auto ind : indices)
- {
- const auto rem = ind % 3;
- const auto p1 = ind - rem + 0;
- const auto p2 = ind - rem + 1;
- const auto p3 = ind - rem + 2;
- glm::u32vec3 face(p1, p2, p3);
- glm::f32vec3 norm = face_normals.find(face)->second;
- normal += norm;
- }
- const auto norm = glm::normalize(normal);
- normals.push_back(norm.x);
- normals.push_back(norm.y);
- normals.push_back(norm.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement