Skip to content

Commit

Permalink
triangles and meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 22, 2024
1 parent b3b7fcc commit bcb29c8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>

#include "../include/frame_buffer.hpp"
#include "../include/mesh.hpp" // Add this line
#include "../include/vector3d.hpp"

namespace graphics {
Expand Down Expand Up @@ -36,6 +37,8 @@ class Display {
Vector3D rotation;
Vector3D rotationSpeed;

Mesh mesh; // Add a mesh member

public:
#ifndef BENCHMARK_MODE
// Constructor to initialize memory
Expand Down
30 changes: 30 additions & 0 deletions include/mesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <vector>

#include "../include/vector3d.hpp"

namespace graphics {

class Mesh {
public:
// Constructor
Mesh();

// Destructor
~Mesh();

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

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

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

private:
std::vector<Vector3D> vertices; // Store vertices of the mesh
};

} // namespace graphics
7 changes: 7 additions & 0 deletions include/traingle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

class Triangle {
public:
Triangle(int v1, int v2, int v3) : vertexIndices{v1, v2, v3} {}
int vertexIndices[3]; // Indices of the vertices that form the triangle
};
12 changes: 12 additions & 0 deletions include/triangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

class Triangle {
public:
Triangle(int v1, int v2, int v3) : vertexIndices{v1, v2, v3} {}

// Method to get vertex indices
const int* getVertexIndices() const { return vertexIndices; }

private:
int vertexIndices[3]; // Indices of the vertices that form the triangle
};
37 changes: 37 additions & 0 deletions src/mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "../include/mesh.hpp"

#include <fstream>
#include <iostream>
#include <sstream>

namespace graphics {

Mesh::Mesh() {}

Mesh::~Mesh() {}

bool Mesh::loadFromFile(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
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); }

} // namespace graphics
Empty file added src/trangle.cpp
Empty file.

0 comments on commit bcb29c8

Please sign in to comment.