✨ feat(mesh): add optional vertical flip when loading OBJ files
Add a boolean flag to Mesh3D constructor and loadOBJFile to optionally flip the Y coordinate of vertices. Update engine to use this flag for specific objects (e.g., mimikyu) that require flipping.
This commit is contained in:
parent
cae53dca8b
commit
652577d0e6
@ -12,8 +12,8 @@ namespace djalim {
|
||||
public:
|
||||
|
||||
Mesh3D();
|
||||
Mesh3D(const std::string& filepath);
|
||||
void loadOBJFile(const std::string& filepath);
|
||||
Mesh3D(const std::string& filepath, bool flipObjectVertically = false);
|
||||
void loadOBJFile(const std::string& filepath, bool flipObjectVertically = false);
|
||||
public:
|
||||
Texture2D texture;
|
||||
glm::vec3 position = {0.0f, 0.0f, 0.0f};
|
||||
|
||||
@ -23,11 +23,11 @@ struct objFace {
|
||||
|
||||
djalim::Mesh3D::Mesh3D(){}
|
||||
|
||||
djalim::Mesh3D::Mesh3D(const std::string& filepath) {
|
||||
loadOBJFile(filepath);
|
||||
djalim::Mesh3D::Mesh3D(const std::string& filepath, bool flipObjectVertically) {
|
||||
loadOBJFile(filepath, flipObjectVertically);
|
||||
}
|
||||
|
||||
void djalim::Mesh3D::loadOBJFile(const std::string& filepath) {
|
||||
void djalim::Mesh3D::loadOBJFile(const std::string& filepath, bool flipObjectVertically) {
|
||||
std::ifstream file(filepath);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Failed to open OBJ file: " << filepath << std::endl;
|
||||
@ -48,7 +48,10 @@ void djalim::Mesh3D::loadOBJFile(const std::string& filepath) {
|
||||
if (type == "v") {
|
||||
float x, y, z;
|
||||
ss >> x >> y >> z;
|
||||
vertices.push_back({x, y, z});
|
||||
if(flipObjectVertically)
|
||||
vertices.push_back({x, -y, z});
|
||||
else
|
||||
vertices.push_back({x, y, z});
|
||||
numMeshVertices++;
|
||||
}
|
||||
else if (type == "vn") {
|
||||
|
||||
@ -65,7 +65,8 @@ void djalim::OpenGlEngine::createObject(std::string name, std::string textures,
|
||||
void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::string filepath){
|
||||
|
||||
// Load the mesh from the file
|
||||
objects[name] = std::make_unique<Mesh3D>(Mesh3D(filepath));
|
||||
bool flip = (name == "mimikyu");
|
||||
objects[name] = std::make_unique<Mesh3D>(Mesh3D(filepath, flip));
|
||||
|
||||
bool textureLoaded = objects[name]->texture.loadTexture(textures);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user