added new cmake setup, graphics, vendors, obj importer, etc.
This commit is contained in:
60
src/gfx/Mesh.cpp
Normal file
60
src/gfx/Mesh.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <iostream>
|
||||
#include "Mesh.h"
|
||||
#include "loaders/tinyobj.h"
|
||||
|
||||
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());
|
||||
for (int i = 0; i < indices_t.size(); i++) {
|
||||
indices[i] = indices_t[i].vertex_index;
|
||||
}
|
||||
|
||||
num_indices = indices.size();
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo_xyz);
|
||||
glGenBuffers(1, &vbo_uv);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
auto Mesh::init(const LeddaGeometry::Shape* shape) -> void {
|
||||
num_indices = shape->indices_size;
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo_xyz);
|
||||
glGenBuffers(1, &vbo_uv);
|
||||
glGenBuffers(1, &ebo);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
|
||||
glBufferData(GL_ARRAY_BUFFER, shape->xyz_size * sizeof(float), shape->xyz, 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, shape->uv_size * sizeof(float), shape->uv, 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, shape->indices_size * sizeof(unsigned int), shape->indices, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user