✨ Add Mesh3D class for loading OBJ files
This commit is contained in:
parent
1041ec48a2
commit
f5430f4dd7
19
include/Mesh3D.h
Normal file
19
include/Mesh3D.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef _MESH3D
|
||||
#define _MESH3D
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace djalim {
|
||||
|
||||
class Mesh3D {
|
||||
public:
|
||||
// Constructor
|
||||
Mesh3D(const std::string& filepath);
|
||||
std::vector<float> meshVertices;
|
||||
void loadOBJFile(const std::string& filepath);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // !_MESH3D
|
||||
|
||||
123
src/core/Mesh3D.cpp
Normal file
123
src/core/Mesh3D.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include "Mesh3D.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
struct objVertex {
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
struct objVertexTexture{
|
||||
float u,v;
|
||||
};
|
||||
|
||||
struct objFaceIndice{
|
||||
int v,vt,vn;
|
||||
};
|
||||
|
||||
struct objFace {
|
||||
objFaceIndice vertices[3];
|
||||
};
|
||||
|
||||
|
||||
|
||||
djalim::Mesh3D::Mesh3D(const std::string& filepath) {
|
||||
loadOBJFile(filepath);
|
||||
}
|
||||
|
||||
|
||||
void djalim::Mesh3D::loadOBJFile(const std::string& filepath){
|
||||
|
||||
std::ifstream file(filepath);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Failed to open OBJ file: " << filepath << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
std::string line;
|
||||
std::vector<objVertex> vertices;
|
||||
std::vector<objVertex> vertexNormal;
|
||||
std::vector<objVertexTexture> vertexTexture;
|
||||
std::vector<objFace> faces;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
std::stringstream ss(line);
|
||||
std::string type;
|
||||
ss >> type;
|
||||
|
||||
if (type == "v"){
|
||||
float x, y, z;
|
||||
ss >> x >> y >> z;
|
||||
//std::cout << "found vertice: (" << x << ", " << y << ", " << z << ")" << std::endl;
|
||||
objVertex v = {x, y, z};
|
||||
vertices.push_back(v);
|
||||
}
|
||||
else if (type == "vn"){
|
||||
float x, y, z;
|
||||
ss >> x >> y >> z;
|
||||
//std::cout << "found normal: (" << x << ", " << y << ", " << z << ")" << std::endl;
|
||||
objVertex v = {x, y, z};
|
||||
vertexNormal.push_back(v);
|
||||
}
|
||||
else if (type == "vt"){
|
||||
float u, v;
|
||||
ss >> u >> v;
|
||||
//std::cout << "found texture: (" << u << ", " << v << ")" << std::endl;
|
||||
objVertexTexture vt = {u, v};
|
||||
vertexTexture.push_back(vt);
|
||||
}
|
||||
else if (type == "f"){
|
||||
std::cout << "found face" << std::endl;
|
||||
std::cout << ss.str() << std::endl;
|
||||
objFace face;
|
||||
// TODO Find a more elegant way to do this
|
||||
for (int i = 0; i < 3; i++){
|
||||
int a, b, c;
|
||||
char slash;
|
||||
ss >> a >> slash >> b >> slash >> c;
|
||||
//std::cout << "vertex " << i << " :(" << a << ", " << b << ", " << c << ")" << std::endl;
|
||||
objFaceIndice f = {a-1, b-1, c-1};
|
||||
face.vertices[i] = f;
|
||||
}
|
||||
faces.push_back(face);
|
||||
}
|
||||
}
|
||||
// Pour chaque face
|
||||
for(int i = 0; i < faces.size() ; i++ ){
|
||||
objFace currentFace = faces[i];
|
||||
// pour chaque vertex de cette face
|
||||
for (int j = 0; j < 3; j++){
|
||||
|
||||
int verticeIndex = currentFace.vertices[j].v;
|
||||
|
||||
if(verticeIndex >= vertices.size()){
|
||||
std::cerr << "Error: Vertex index out of range" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
objVertex currentVertice = vertices[verticeIndex];
|
||||
|
||||
meshVertices.push_back(currentVertice.x);
|
||||
meshVertices.push_back(currentVertice.y);
|
||||
meshVertices.push_back(currentVertice.z);
|
||||
|
||||
//uv
|
||||
|
||||
int verticeTextureIndex = currentFace.vertices[j].vt;
|
||||
|
||||
if(verticeTextureIndex >= vertexTexture.size()){
|
||||
std::cerr << "Error: Texture index out of range" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
objVertexTexture currentTexture = vertexTexture[verticeTextureIndex];
|
||||
|
||||
meshVertices.push_back(currentTexture.u);
|
||||
meshVertices.push_back(currentTexture.v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
#define DEBUG true
|
||||
|
||||
#include "engine.h"
|
||||
#include "Mesh3D.h"
|
||||
|
||||
int main() {
|
||||
djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600);
|
||||
engine.start();
|
||||
//djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600);
|
||||
//engine.start();
|
||||
djalim::Mesh3D mesh = djalim::Mesh3D("../assets/models/UTAH_BLEND.obj");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user