Add some comments to ObjModel.
This commit is contained in:
parent
92f7036bc5
commit
ed08a78fb6
|
@ -19,9 +19,27 @@
|
|||
namespace simpleengine::objects_3d {
|
||||
class ObjModel : public simpleengine::gfx::TexturedModel {
|
||||
private:
|
||||
/**
|
||||
* @brief Split a string multiple times (if possible) with a character delimiter.
|
||||
*
|
||||
* @param str The string to split.
|
||||
* @param delim The character to split by.
|
||||
* @return std::vector<std::string> The tokens that were split out of str.
|
||||
*/
|
||||
std::vector<std::string> split_string(std::string str, const char delim);
|
||||
static void process_vertex(const std::vector<std::string>& vertex_data, std::vector<GLuint>& indicies, const std::vector<glm::vec2>& in_textures,
|
||||
const std::vector<glm::vec3>& in_normals, std::vector<glm::vec2>& out_textures, std::vector<glm::vec3>& out_normals);
|
||||
|
||||
/**
|
||||
* @brief Process a vertex from tokens read from the .obj file.
|
||||
*
|
||||
* @param vertex_data The vertex string tokens to process the vertex from.
|
||||
* @param in_textures The texture coords that are unsorted, just read from the .obj file.
|
||||
* @param in_normals The normals that are unsorted, just read from the .obj file.
|
||||
* @param out_indicies (out) The vector to insert the indicies that were extracted from `vertex_data` into.
|
||||
* @param out_textures (out) The vector to insert the texture coords that were extracted from `vertex_data` into.
|
||||
* @param out_normals (out) The vector to insert the normals that were extracted from `vertex_data` into.
|
||||
*/
|
||||
static 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);
|
||||
private:
|
||||
/**
|
||||
* @brief This is replaced with `lit_vertices`!!!!
|
||||
|
@ -35,22 +53,5 @@ namespace simpleengine::objects_3d {
|
|||
ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream);
|
||||
|
||||
virtual void update(const float& delta_time) override;
|
||||
|
||||
/* virtual void render(GLFWwindow* target) override {
|
||||
shader.use();
|
||||
|
||||
shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false);
|
||||
|
||||
// When binding to the texture, also tell the shader if the texture is set or not.
|
||||
if (texture.has_value()) {
|
||||
shader.set_uniform_int("texture_is_set", true, false);
|
||||
texture.value().bind();
|
||||
} else {
|
||||
shader.set_uniform_int("texture_is_set", false, false);
|
||||
}
|
||||
|
||||
vao.bind();
|
||||
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
|
||||
} */
|
||||
};
|
||||
}
|
|
@ -17,11 +17,12 @@ namespace simpleengine::objects_3d {
|
|||
return tokens;
|
||||
}
|
||||
|
||||
void ObjModel::process_vertex(const std::vector<std::string>& vertex_data, std::vector<GLuint>& indicies, const std::vector<glm::vec2>& in_textures,
|
||||
const std::vector<glm::vec3>& in_normals, std::vector<glm::vec2>& out_textures, std::vector<glm::vec3>& out_normals) {
|
||||
void ObjModel::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;
|
||||
indicies.push_back(currentVertexIndex);
|
||||
out_indicies.push_back(currentVertexIndex);
|
||||
|
||||
// Read texture coords
|
||||
glm::vec2 current_tex = in_textures.at(stoi(vertex_data[1]) - 1);
|
||||
|
@ -46,13 +47,17 @@ namespace simpleengine::objects_3d {
|
|||
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, ' ');
|
||||
|
@ -72,6 +77,7 @@ namespace simpleengine::objects_3d {
|
|||
}
|
||||
}
|
||||
|
||||
// Process the indicies. This will sort everything for storing inside of the Vertex list.
|
||||
do {
|
||||
if (!line.starts_with("f")) {
|
||||
continue;
|
||||
|
@ -81,21 +87,24 @@ namespace simpleengine::objects_3d {
|
|||
std::vector<std::string> vertex2 = split_string(line_tokens[2], '/');
|
||||
std::vector<std::string> vertex3 = split_string(line_tokens[3], '/');
|
||||
|
||||
process_vertex(vertex1, indicies, obj_textures, obj_normals, textures, normals);
|
||||
process_vertex(vertex2, indicies, obj_textures, obj_normals, textures, normals);
|
||||
process_vertex(vertex3, indicies, obj_textures, obj_normals, textures, normals);
|
||||
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();
|
||||
|
||||
// Insert everything into lit_vertices.
|
||||
for (int i = 0; i < obj_vertices.size(); i++) {
|
||||
lit_vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i), normals.at(i));
|
||||
}
|
||||
|
||||
// Create VAO and EBO and assign buffers
|
||||
vao.bind();
|
||||
vbo.buffer(lit_vertices.data(), 0, sizeof(LitVertex) * lit_vertices.size());
|
||||
ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint));
|
||||
|
||||
// Enable VAO attributes
|
||||
vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, position));
|
||||
//vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, color));
|
||||
vao.enable_attrib(vbo, 1, 2, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, tex_coord));
|
||||
|
@ -106,6 +115,6 @@ namespace simpleengine::objects_3d {
|
|||
}
|
||||
|
||||
void ObjModel::update(const float& delta_time) {
|
||||
this->rotate_y(0.5f);
|
||||
this->rotate_y(0.5f); // Slowly rotate (for debugging)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue