Skip to content

Commit

Permalink
Face and mesh logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Nov 3, 2024
1 parent 994d4af commit ce18cb2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
32 changes: 32 additions & 0 deletions include/face.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include "../include/color.hpp"
#include "../include/texture.hpp"

namespace graphics {
// Represents a two-dimensional texture
class Face {
public:
// Constructor to initialize memory
Face(int v1, int v2, int v3, const Texture2D& t1, const Texture2D& t2,
const Texture2D& t3, const Color& color)
: v1(v1), v2(v2), v3(v3), t1(t1), t2(t2), t3(t3), color(color) {}

// Destructor to free the memory allocated
~Face() = default;

// The first vertex index
int v1;
// The second vertex index
int v2;
// The third vertex index
int v3;
// The first texture
Texture2D t1;
// The second texture
Texture2D t2;
// The third texture
Texture2D t3;
// The color of the face
Color color;
};
} // namespace graphics
32 changes: 26 additions & 6 deletions include/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,49 @@

#include <vector>

#include "../include/face.hpp"
#include "../include/texture.hpp"
#include "../include/vector3d.hpp"

namespace graphics {

class Mesh {
public:
// Constructor
// Constructor to initialize memory
Mesh();

// Destructor
~Mesh();
// Destructor to free the memory allocated
~Mesh() = default;

// Method to load mesh data from a file
bool loadFromFile(const std::string& filename);
// Method to load textures from a Wavefront .obj file
bool loadOBJ(const std::string& filename);

// Method to load textures from a file
bool loadTextures(const std::string& filename);

// Method to get vertices
const std::vector<Vector3D>& getVertices() const;

// Method to add a vertex
void addVertex(const Vector3D& vertex);

// Method to get textures
const std::vector<Texture2D>& getTextures() const;

// Method to add a texture
void addTexture(const Texture2D& texture);

// Method to add a face
void addFace(int v1, int v2, int v3, const Texture2D& t1, const Texture2D& t2,
const Texture2D& t3, const Color& color);

private:
std::vector<Vector3D> vertices; // Store vertices of the mesh
std::vector<Vector3D> vertices;
std::vector<Face> faces;
std::vector<Texture2D> textures;
Vector3D scale;
Vector3D rotation;
Vector3D translation;
};

} // namespace graphics
3 changes: 3 additions & 0 deletions src/face.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "../include/face.hpp"

namespace graphics {} // namespace graphics
56 changes: 39 additions & 17 deletions src/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,56 @@
#include <iostream>
#include <sstream>

#include "../include/face.hpp"

namespace graphics {

Mesh::Mesh() {}

Mesh::~Mesh() {}

bool Mesh::loadFromFile(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
bool Mesh::loadOBJ(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
if (line.find("v ") != std::string::npos) {
// Parse the vertex
Vector3D vertex;
if (sscanf(line.c_str(), "v %lf %lf %lf", &vertex.x, &vertex.y,
&vertex.z) == 3) {
addVertex(vertex);
}
} else if (line.find("vt ") != std::string::npos) {
// Parse the texture coordinate
Texture2D texture;
if (sscanf(line.c_str(), "vt %lf %lf", &texture.u, &texture.v) == 2) {
addTexture(texture);
}
} else if (line.find("f ") != std::string::npos) {
// Parse the face
int v1, v2, v3, t1, t2, t3, n1, n2, n3;
if (sscanf(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &t1, &n1,
&v2, &t2, &n2, &v3, &t3, &n3) == 9) {
addFace(v1, v2, v3, textures[t1 - 1], textures[t2 - 1],
textures[t3 - 1], Color::WHITE);
}
}
}
file.close();
return true;
} else {
std::cerr << "Failed to open mesh file: " << filename << std::endl;
return false;
}

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
double x, y, z;
if (iss >> x >> y >> z) {
addVertex(Vector3D(x, y, z));
}
}

file.close();
return true;
}

const std::vector<Vector3D>& Mesh::getVertices() const { return vertices; }

void Mesh::addVertex(const Vector3D& vertex) { vertices.push_back(vertex); }

void Mesh::addTexture(const Texture2D& texture) { textures.push_back(texture); }
void Mesh::addFace(int v1, int v2, int v3, const Texture2D& t1,
const Texture2D& t2, const Texture2D& t3, const Color& color) {
faces.push_back(Face(v1, v2, v3, t1, t2, t3, color));
}

} // namespace graphics

0 comments on commit ce18cb2

Please sign in to comment.