feat: added phong and basic shaders, updated mesh format

This commit is contained in:
Daniel Ledda
2023-01-10 03:54:24 +01:00
parent 93dadfbf4b
commit 658b5d693a
10 changed files with 384 additions and 116 deletions

View File

@@ -6,33 +6,58 @@ auto Mesh::init(const char* obj_file) -> void {
auto reader = tinyobj::ObjReader();
auto success = reader.ParseFromFile(obj_file);
std::cout << reader.Error() << std::endl;
auto attrib = reader.GetAttrib();
auto indices_t = reader.GetShapes().at(0).mesh.indices;
auto indices = std::vector<unsigned int>(indices_t.size());
auto vertices = std::vector<float>(3*indices_t.size());
auto normals = std::vector<float>(3*indices_t.size());
auto texcoords = std::vector<float>(2*indices_t.size());
for (int i = 0; i < indices_t.size(); i++) {
indices[i] = indices_t[i].vertex_index;
auto vertex_data = indices_t[i];
vertices[3*i] = attrib.vertices[3*vertex_data.vertex_index];
vertices[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
vertices[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
normals[3*i] = attrib.normals[3*vertex_data.normal_index];
normals[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
normals[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
texcoords[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
texcoords[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
indices[i] = i;
}
num_indices = indices.size();
num_indices = indices_t.size();
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo_xyz);
glGenBuffers(1, &vbo_uv);
glGenBuffers(1, &ebo);
glGenBuffers(1, &vbo_norm);
//glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, attrib.vertices.size() * sizeof(float), attrib.vertices.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_uv);
glBufferData(GL_ARRAY_BUFFER, attrib.texcoords.size() * sizeof(float), attrib.texcoords.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(float), texcoords.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo_norm);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(2);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
}
auto Mesh::init(const LeddaGeometry::Shape* shape) -> void {