Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load mesh
- std::vector<vertex_t> objModel;
- bool hasNormals = false;
- {
- // A constant scale to apply to all vertex positions
- float scale = 0.5f;
- std::string warn;
- std::string err;
- tinyobj::attrib_t attrib;
- std::vector<tinyobj::shape_t> shapes;
- tinyobj::LoadObj(&attrib, &shapes, NULL, &warn, &err, "media/teapot.obj", NULL, true);
- if (!err.empty())
- printf("Error loading obj: %s\n", err.c_str());
- if (!warn.empty())
- printf("Warning loading obj: %s\n", warn.c_str());
- hasNormals = !attrib.normals.empty();
- for (const tinyobj::shape_t& shape : shapes)
- {
- for (const tinyobj::index_t& index : shape.mesh.indices)
- {
- vertex_t v;
- v.position = {
- scale * attrib.vertices[index.vertex_index * 3 + 0],
- scale * attrib.vertices[index.vertex_index * 3 + 1],
- scale * attrib.vertices[index.vertex_index * 3 + 2]
- };
- if (hasNormals)
- {
- v.normal = {
- attrib.normals[index.normal_index * 3 + 0],
- attrib.normals[index.normal_index * 3 + 1],
- attrib.normals[index.normal_index * 3 + 2]
- };
- }
- objModel.push_back(v);
- }
- }
- }
- if (!hasNormals)
- {
- for (int i = 0; i < objModel.size(); i += 3)
- {
- vertex_t& v0 = objModel[i+0];
- vertex_t& v1 = objModel[i+1];
- vertex_t& v2 = objModel[i+2];
- vec3_t normal = vec3_cross((v1.position - v0.position), (v2.position - v0.position));
- v0.normal = v1.normal = v2.normal = normal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement