Readadd obj loading
This commit is contained in:
parent
ca6c2337d6
commit
2a72a30aa2
|
@ -171,9 +171,15 @@ int main(int argc, char *argv[]) {
|
||||||
se::gfx::Material material(white_texture, 1.f, 0.f, 0.f, 0.f, 0.f);
|
se::gfx::Material material(white_texture, 1.f, 0.f, 0.f, 0.f, 0.f);
|
||||||
|
|
||||||
// Create the entity and add the model component to it.
|
// Create the entity and add the model component to it.
|
||||||
auto entity = std::make_shared<simpleengine::Entity>();
|
/* auto entity = std::make_shared<simpleengine::Entity>();
|
||||||
entity->add_component<se::ModelComponent>(cube_vertices, cube_indicies, material, true);
|
entity->add_component<se::ModelComponent>(cube_vertices, cube_indicies, material, true);
|
||||||
entity->translate(3.5f, 0.f, 0.f);
|
entity->translate(3.5f, 0.f, 0.f); */
|
||||||
|
|
||||||
|
//auto entity = std::make_shared<se::gfx::M>(game.get_window(), core_shader, white_texture, "examples/dev_testing/resources/dragon.obj");
|
||||||
|
auto entity = std::make_shared<simpleengine::Entity>();
|
||||||
|
se::gfx::Model model(material, "examples/dev_testing/resources/dragon.obj");
|
||||||
|
entity->add_component<se::ModelComponent>(model);
|
||||||
|
entity->translate(5.f, 0.f, 0.f);
|
||||||
|
|
||||||
// Create a renderer and submit the entity into it.
|
// Create a renderer and submit the entity into it.
|
||||||
auto renderer = std::make_shared<se::gfx::Renderer>(game.get_window(), core_shader);
|
auto renderer = std::make_shared<se::gfx::Renderer>(game.get_window(), core_shader);
|
||||||
|
|
|
@ -30,6 +30,8 @@ namespace simpleengine::gfx {
|
||||||
|
|
||||||
Model(std::vector<LitVertex> vertices, std::vector<GLuint> indicies, Material material);
|
Model(std::vector<LitVertex> vertices, std::vector<GLuint> indicies, Material material);
|
||||||
Model(std::vector<LitVertex> vertices, std::vector<GLuint> indicies = std::vector<GLuint>(), std::optional<Material> material = std::nullopt);
|
Model(std::vector<LitVertex> vertices, std::vector<GLuint> indicies = std::vector<GLuint>(), std::optional<Material> material = std::nullopt);
|
||||||
|
Model(Material material, std::string filename);
|
||||||
|
Model(Material material, std::ifstream file_stream);
|
||||||
|
|
||||||
virtual void destroy() override;
|
virtual void destroy() override;
|
||||||
|
|
||||||
|
@ -44,5 +46,10 @@ namespace simpleengine::gfx {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void calculate_normals();
|
void calculate_normals();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void process_vertex(const std::vector<std::string>& vertex_data, const std::vector<glm::vec2>& in_textures,
|
||||||
|
const std::vector<glm::vec3>& in_normals, std::vector<GLuint>& out_indicies,
|
||||||
|
std::vector<glm::vec2>& out_textures, std::vector<glm::vec3>& out_normals);
|
||||||
};
|
};
|
||||||
}
|
}
|
|
@ -16,6 +16,111 @@ namespace simpleengine::gfx {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Model::Model(Material material,std::string filename) :
|
||||||
|
Model(material, std::ifstream(filename, std::ios::in | std::ios::binary)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> split_string(std::string str, const char delim) {
|
||||||
|
std::istringstream ss(str);
|
||||||
|
|
||||||
|
std::vector<std::string> tokens;
|
||||||
|
size_t pos = 0;
|
||||||
|
std::string token;
|
||||||
|
while ((pos = str.find(delim)) != std::string::npos) {
|
||||||
|
token = str.substr(0, pos);
|
||||||
|
tokens.push_back(token);
|
||||||
|
str.erase(0, pos + 1);
|
||||||
|
}
|
||||||
|
tokens.push_back(str);
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
Model::Model(Material material, std::ifstream file_stream) :
|
||||||
|
vbo(gfx::VBO::init(GL_ARRAY_BUFFER, false)), ebo(gfx::VBO::init(GL_ELEMENT_ARRAY_BUFFER, false)),
|
||||||
|
vao(gfx::VAO::init()), material(material) {
|
||||||
|
|
||||||
|
if (!file_stream.is_open()) {
|
||||||
|
std::cerr << "File stream that was given to ObjModel::ObjModel is not open!" << std::endl;
|
||||||
|
throw std::runtime_error("Failed to open ObjModel model file");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The vertices, texture coords, and normals that were read from the obj file
|
||||||
|
// these are not in a particular order.
|
||||||
|
std::vector<glm::vec3> obj_vertices;
|
||||||
|
std::vector<glm::vec2> obj_textures;
|
||||||
|
std::vector<glm::vec3> obj_normals;
|
||||||
|
|
||||||
|
// The texture coords and normals that have been sorted.
|
||||||
|
std::vector<glm::vec2> textures;
|
||||||
|
std::vector<glm::vec3> normals;
|
||||||
|
|
||||||
|
// Read the vertices, texture coords, and normals. Break when run into indices
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(file_stream, line)) {
|
||||||
|
std::vector<std::string> line_tokens = split_string(line, ' ');
|
||||||
|
|
||||||
|
if (line_tokens.front() == "v") {
|
||||||
|
//glm::vec3 vertex(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3]));
|
||||||
|
obj_vertices.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3]));
|
||||||
|
} else if (line_tokens.front() == "vt") {
|
||||||
|
obj_textures.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]));
|
||||||
|
} else if (line_tokens.front() == "vn") {
|
||||||
|
obj_normals.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3]));
|
||||||
|
} else if (line_tokens.front() == "f") {
|
||||||
|
auto size = obj_vertices.size();
|
||||||
|
textures.resize(size);
|
||||||
|
normals.resize(size);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the indicies. This will sort everything for storing inside of the Vertex list.
|
||||||
|
do {
|
||||||
|
if (!line.starts_with("f")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std::vector<std::string> line_tokens = split_string(line, ' ');
|
||||||
|
std::vector<std::string> vertex1 = split_string(line_tokens[1], '/');
|
||||||
|
std::vector<std::string> vertex2 = split_string(line_tokens[2], '/');
|
||||||
|
std::vector<std::string> vertex3 = split_string(line_tokens[3], '/');
|
||||||
|
|
||||||
|
process_vertex(vertex1, obj_textures, obj_normals, indicies, textures, normals);
|
||||||
|
process_vertex(vertex2, obj_textures, obj_normals, indicies, textures, normals);
|
||||||
|
process_vertex(vertex3, obj_textures, obj_normals, indicies, textures, normals);
|
||||||
|
} while (std::getline(file_stream, line));
|
||||||
|
|
||||||
|
file_stream.close();
|
||||||
|
|
||||||
|
const int texture_id = 0;
|
||||||
|
|
||||||
|
std::cout << "Texture ID: " << texture_id << std::endl;
|
||||||
|
|
||||||
|
// Insert everything into lit_vertices.
|
||||||
|
for (int i = 0; i < obj_vertices.size(); i++) {
|
||||||
|
vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i), normals.at(i), texture_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Model::process_vertex(const std::vector<std::string>& vertex_data, const std::vector<glm::vec2>& in_textures,
|
||||||
|
const std::vector<glm::vec3>& in_normals, std::vector<GLuint>& out_indicies,
|
||||||
|
std::vector<glm::vec2>& out_textures, std::vector<glm::vec3>& out_normals) {
|
||||||
|
|
||||||
|
// Get the index the current vertex and put it in indicies
|
||||||
|
int currentVertexIndex = stoi(vertex_data[0]) - 1;
|
||||||
|
out_indicies.push_back(currentVertexIndex);
|
||||||
|
|
||||||
|
// Read texture coords
|
||||||
|
glm::vec2 current_tex = in_textures.at(stoi(vertex_data[1]) - 1);
|
||||||
|
current_tex.y = 1 - current_tex.y;
|
||||||
|
out_textures.at(currentVertexIndex) = current_tex;
|
||||||
|
|
||||||
|
// Read normals
|
||||||
|
glm::vec3 current_norm = in_normals.at(stoi(vertex_data[2]) - 1);
|
||||||
|
out_normals.at(currentVertexIndex) = current_norm;
|
||||||
|
}
|
||||||
|
|
||||||
void Model::destroy() {
|
void Model::destroy() {
|
||||||
this->ebo.destroy();
|
this->ebo.destroy();
|
||||||
this->vbo.destroy();
|
this->vbo.destroy();
|
||||||
|
|
Loading…
Reference in New Issue