feat: refactor code + optimizations for obj file parsing

This commit is contained in:
2025-10-01 17:55:29 +02:00
parent 2b0494a23d
commit fc91f6662e
10 changed files with 510 additions and 235 deletions

20
include/IO/parser.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef PARSER_H_
#define PARSER_H_
// Very fast OBJ/MTL line parser
class Parser {
private:
char* m_sv;
public:
Parser(char* sv) : m_sv(sv) {}
public:
void SkipSpaces();
char* TakeWord();
float TakeFloat();
int TakeInt();
bool TakeFaceIndices(int& vi, int& ti, int& ni);
char* TakeUntil(char d);
int TakeIndex(int baseCount);
};
#endif // PARSER_H_

27
include/renderer/mesh.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef MESH_H_
#define MESH_H_
#include <vector>
#include <string>
#include <GL/glew.h>
#include "renderer/basics.h"
class Mesh {
public: // TODO: abstract away
unsigned int m_vao, m_vbo, m_ebo;
std::vector<Vertex> m_vertexBuffer;
std::vector<unsigned int> m_indexBuffer;
public: // TODO: abstract away
void Bind() { glBindVertexArray(m_vao); }
void Unbind() { glBindVertexArray(0); }
void Upload();
public:
std::string materialName;
public:
Mesh();
public:
void Render();
};
#endif // MESH_H_

View File

@ -11,27 +11,11 @@
#include "texture.h"
#include "renderer/material.h"
#include "renderer/basics.h"
#include "renderer/mesh.h"
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
class Mesh {
public: // TODO: abstract away
unsigned int m_vao, m_vbo, m_ebo;
std::vector<Vertex> m_vertexBuffer;
std::vector<unsigned int> m_indexBuffer;
public: // TODO: abstract away
void Bind() { glBindVertexArray(m_vao); }
void Unbind() { glBindVertexArray(0); }
void Upload();
public:
std::string materialName;
public:
Mesh();
public:
void Render();
};
class Object {
private:
std::string m_name;
@ -43,7 +27,7 @@ private:
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
private:
static inline int NormalizeIndex(const std::string &s, int baseCount);
static inline int NormalizeIndex(int idx, int baseCount);
private:
Object();